微信小程序 后臺(tái)登錄(非微信賬號(hào))實(shí)例詳解
微信小程序 后臺(tái)登錄
實(shí)現(xiàn)效果圖:

最近寫了一個(gè)工具類的小程序,按需求要求不要微信提供的微信賬號(hào)登錄,需要調(diào)取后臺(tái)登錄接口來登錄。由于小程序大部分都是調(diào)取微信信息登錄,很少有調(diào)用自己后臺(tái)來登錄的,所以寫的時(shí)候各種坑,現(xiàn)在把趟好坑的代碼共享給大家吧?。≒S:如有不妥之處,共勉之。)

廢話不說,直接上代碼
找到app.js在里面寫如下代碼
App({
onLaunch: function () {
//調(diào)用API從本地緩存中獲取數(shù)據(jù)
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"
},
敲黑板劃重點(diǎn):上圖中的代碼片段重要的地方就是:“globalData中的 adminUserViewId: "",token: "" ”
這兩個(gè)參數(shù)是前端需要存儲(chǔ)的后臺(tái)參數(shù),用來標(biāo)記用戶的登錄狀態(tài)的。
然后建一個(gè)login文件夾,在login.wxml中寫如下代碼
<import src="../../components/toast.wxml" />
<!-- is="toast" 匹配組件中的toast提示 如果用dialog的話這就是dialog -->
<template is="toast" data="{{ ...$wux.toast }}" />
<view class="login_container">
<view class="login_view">
<text class="login_lable">賬號(hào):</text>
<input class="login_text" placeholder="請(qǐng)輸入登錄賬號(hào)" bindinput="listenerUsernameInput"/>
</view>
<view class="login_view">
<text class="login_lable">密碼:</text>
<input class="login_text" placeholder="請(qǐng)輸入密碼" password="true" bindinput="listenerPasswordInput"/>
</view>
<view>
<button class="login_button" bindtap="loginAction">登錄</button>
</view>
</view>
然后建一個(gè)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
//獲取應(yīng)用實(shí)例
var app = getApp()
var util = require('../../utils/util.js');
Page({
data: {
motto: 'Hello World',
username: "",
password: ""
},
onLoad(options) {
// 初始化提示框
this.$wuxToast = app.wux(this).$wuxToast
},
/** 監(jiān)聽?zhēng)ぬ?hào)輸入 */
listenerUsernameInput: function (e) {
this.data.username = e.detail.value;
},
/** 監(jiān)聽密碼輸入 */
listenerPasswordInput: function (e) {
this.data.password = e.detail.value;
},
// 登錄按鈕點(diǎn)擊事件
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, //僅為示例,并非真實(shí)的接口地址
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) {
// 后臺(tái)傳遞過來的值
var adminUserViewId = res.data.data.adminUserViewId;
var token = res.data.data.token;
// 設(shè)置全局變量的值
app.globalData.adminUserViewId = res.data.data.adminUserViewId;
app.globalData.token = res.data.data.token;
// 將token存儲(chǔ)到本地
wx.setStorageSync('adminUserViewId', adminUserViewId);
wx.setStorageSync('token', token);
console.log("登錄成功的adminUserViewId:" + adminUserViewId);
console.log("登錄成功的token:" + token);
// 切換到首頁(yè)
wx.switchTab({
url: '/pages/index/index'
})
} else {
that.$wuxToast.show({
type: 'text',
timer: 1000,
color: '#fff',
text: res.data.msg,
success: () => console.log('登錄失敗,請(qǐng)稍后重試。' + res.data.msg)
})
}
},
fail: function () {
util.hideToast();
console.log("登錄失敗");
that.$wuxToast.show({
type: 'text',
timer: 1000,
color: '#fff',
text: '服務(wù)器君好累😫,請(qǐng)稍后重試',
success: () => console.log('登錄失敗,請(qǐng)稍后重試。')
})
}
})
}
})
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Performance 內(nèi)存監(jiān)控使用技巧詳解
這篇文章主要為大家介紹了Performance 內(nèi)存監(jiān)控使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
網(wǎng)頁(yè)里控制圖片大小的相關(guān)代碼
網(wǎng)頁(yè)里控制圖片大小的相關(guān)代碼...2006-06-06
微信小程序中使用javascript 回調(diào)函數(shù)
這篇文章主要介紹了微信小程序中使用javascript 回調(diào)函數(shù)的相關(guān)資料,需要的朋友可以參考下2017-05-05
javascript實(shí)現(xiàn)超炫的向上滑行菜單實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)超炫的向上滑行菜單實(shí)現(xiàn)方法,以一個(gè)完整實(shí)例形式分析了javascript針對(duì)頁(yè)面元素的遍歷與樣式操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
精確到按鈕級(jí)別前端權(quán)限管理實(shí)現(xiàn)方案
這篇文章主要為大家介紹了精確到按鈕級(jí)別前端權(quán)限管理實(shí)現(xiàn)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

