Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize background login in Wechat development

2025-04-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

小编给大家分享一下微信开发中如何实现后台登录,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

效果图

找到app.js在里面写如下代码

App({ onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) }, globalData: { adminUserViewId: "", token: "", userInfo: null, BaseURL:"http://airb.cakeboss.com.cn" // BaseURL:"http://192.168.0.107:8080" },

敲黑板划重点:上图中的代码片段重要的地方就是:"globalData中的 adminUserViewId: "",token: "" "

这两个参数是前端需要存储的后台参数,用来标记用户的登录状态的。

然后建一个login文件夹,在login.wxml中写如下代码

账号: 密码: 登录

然后建一个login文件夹,在login.wxss中写如下代码

.login_container { margin-top: 30px;}.login_view { width: calc(100% - 40px); padding: 0 20px; line-height: 45px; height: 45px; margin-bottom: 20px;}.login_text { float: left; height: 45px; line-height: 45px; font-size: 12px; border: 1px solid rgb(241, 242, 243); padding: 0 12px; width: calc(100% - 70px); border-radius: 4px;}.login_lable { float: left; font-size: 12px; width: 40px;}.login_button { width: 150px; background: green; color: #fff;}

在login.js中写如下代码

//login.js//获取应用实例var app = getApp()var util = require('../../utils/util.js');Page({ data: { motto: 'Hello World', username: "", password: "" }, onLoad(options) { // 初始化提示框 this.$wuxToast = app.wux(this).$wuxToast }, /** 监听帐号输入 */ listenerUsernameInput: function (e) { this.data.username = e.detail.value; }, /** 监听密码输入 */ listenerPasswordInput: function (e) { this.data.password = e.detail.value; }, // 登录按钮点击事件 loginAction: function () { var userName = this.data.username; var passwords = this.data.password; var that = this; if (userName === "") { that.$wuxToast.show({ type: 'text', timer: 1000, color: '#fff', text: "用户名不能为空!", success: () => console.log('用户名不能为空!') }) return; } if (passwords === "") { that.$wuxToast.show({ type: 'text', timer: 1000, color: '#fff', text: "密码不能为空!", success: () => console.log('密码不能为空!') }) return; } //加载提示框 util.showLoading("登录中..."); var urlStr = app.globalData.BaseURL + '/api/adminUser/login'; wx.request({ method: "POST", url: urlStr, //仅为示例,并非真实的接口地址 data: util.json2Form({ username: userName, password: passwords }), header: { "Content-Type": "application/x-www-form-urlencoded" }, success: function (res) { util.hideToast(); console.log(res.data); var code = res.data.code; if (code === 200) { // 后台传递过来的值 var adminUserViewId = res.data.data.adminUserViewId; var token = res.data.data.token; // 设置全局变量的值 app.globalData.adminUserViewId = res.data.data.adminUserViewId; app.globalData.token = res.data.data.token; // 将token存储到本地 wx.setStorageSync('adminUserViewId', adminUserViewId); wx.setStorageSync('token', token); console.log("登录成功的adminUserViewId:" + adminUserViewId); console.log("登录成功的token:" + token); // 切换到首页 wx.switchTab({ url: '/pages/index/index' }) } else { that.$wuxToast.show({ type: 'text', timer: 1000, color: '#fff', text: res.data.msg, success: () => console.log('登录失败,请稍后重试。' + res.data.msg) }) } }, fail: function () { util.hideToast(); console.log("登录失败"); that.$wuxToast.show({ type: 'text', timer: 1000, color: '#fff', text: '服务器君好累以上是"微信开发中如何实现后台登录"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report