|
@@ -18,52 +18,54 @@ Page({
|
|
|
*/
|
|
|
onShow() {
|
|
|
this.handleWXLogin()
|
|
|
+ // this.loginLogic()
|
|
|
},
|
|
|
- // 触达微信登录
|
|
|
+
|
|
|
+ // 处理登录条件, 如果本地有accwssToken 则直接进行登录
|
|
|
+ loginLogic () {
|
|
|
+ const accessToken = wx.getStorageSync('accessToken')
|
|
|
+ if (accessToken) {
|
|
|
+ console.log('本地有token', wx.getStorageSync('device'));
|
|
|
+ this.localTokenLogin()
|
|
|
+ } else {
|
|
|
+ this.handleWXLogin()
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ localTokenLogin () {
|
|
|
+ const accessToken = wx.getStorageSync('accessToken')
|
|
|
+ const headImg = wx.getStorageSync('headImg')
|
|
|
+ const userName = wx.getStorageSync('userName')
|
|
|
+ const isBindWechat = wx.getStorageSync('isBindWechat')
|
|
|
+ const workerId = wx.getStorageSync('workerId')
|
|
|
+ const device = wx.getStorageSync('device')
|
|
|
+ const isNeedAccountPwd = wx.getStorageSync('isNeedAccountPwd')
|
|
|
+ app.globalData.accessToken = accessToken
|
|
|
+ app.globalData.userName = userName
|
|
|
+ app.globalData.headImg = headImg
|
|
|
+ app.globalData.isBindWechat = isBindWechat
|
|
|
+ app.globalData.workerId = workerId
|
|
|
+ app.globalData.device = device
|
|
|
+ if (isNeedAccountPwd) { // 需要账号密码登录
|
|
|
+ this.setData({
|
|
|
+ showAccountPwd: true
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ this.handleDeviceFn(device)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 获取wx.login => code
|
|
|
handleWXLogin () {
|
|
|
var that = this
|
|
|
wx.login({
|
|
|
success (res) {
|
|
|
if (res.code) {
|
|
|
//发起网络请求
|
|
|
- wx.showLoading({
|
|
|
- title: '加载中...',
|
|
|
- mask: true
|
|
|
- })
|
|
|
that.setData({
|
|
|
loginCode: res.code
|
|
|
})
|
|
|
- workerLogin({code: res.code}).then(res => {
|
|
|
- // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
|
|
|
- wx.hideLoading()
|
|
|
- wx.vibrateShort()
|
|
|
- that.setAppGlobalData(res.data)
|
|
|
- const device = res.data.device
|
|
|
- if (device !== null) {
|
|
|
- wx.redirectTo({
|
|
|
- url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
|
|
|
- })
|
|
|
- } else {
|
|
|
- wx.redirectTo({
|
|
|
- url: '/pages/permissions/permissions',
|
|
|
- })
|
|
|
- }
|
|
|
- }).catch(e => {
|
|
|
- // e.code == 403 未绑定微信 进入账户密码登录页面
|
|
|
- wx.hideLoading()
|
|
|
- console.log(e,'errot');
|
|
|
- if (e.code == '403') {
|
|
|
- that.setData({
|
|
|
- showAccountPwd: true
|
|
|
- })
|
|
|
- } else {
|
|
|
- wx.showModal({
|
|
|
- content: e.msg,
|
|
|
- confirmColor: '#333',
|
|
|
- showCancel: false
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
+ that.workerLoginFn(res.code)
|
|
|
} else {
|
|
|
wx.hideLoading()
|
|
|
wx.showModal({
|
|
@@ -75,6 +77,67 @@ Page({
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
+
|
|
|
+ // 使用wx.login > code 登录
|
|
|
+ workerLoginFn (code) {
|
|
|
+ const that = this
|
|
|
+ wx.showLoading({
|
|
|
+ title: '加载中...',
|
|
|
+ mask: true
|
|
|
+ })
|
|
|
+ workerLogin({code: code}).then(res => {
|
|
|
+ // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
|
|
|
+ wx.hideLoading()
|
|
|
+ wx.vibrateShort()
|
|
|
+ that.setAppGlobalData(res.data)
|
|
|
+ that.handleDeviceFn(res.data.device)
|
|
|
+ wx.setStorageSync('isNeedAccountPwd', false)
|
|
|
+ }).catch(e => {
|
|
|
+ wx.hideLoading()
|
|
|
+ if (e.code == '403') { // 需要用户输入账号密码登录
|
|
|
+ this.setData({
|
|
|
+ showAccountPwd: true
|
|
|
+ })
|
|
|
+ app.globalData.isBindWechat = false
|
|
|
+ app.globalData.isNeedAccountPwd = true
|
|
|
+ wx.setStorageSync('isBindWechat', false)
|
|
|
+ wx.setStorageSync('isNeedAccountPwd', true)
|
|
|
+ } else {
|
|
|
+ wx.showModal({
|
|
|
+ content: e.msg,
|
|
|
+ confirmColor: '#333',
|
|
|
+ showCancel: false
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ // 判断是否绑定了微信
|
|
|
+ handleBindWxFn (flag) {
|
|
|
+ if (!flag) {
|
|
|
+ this.setData({
|
|
|
+ showAccountPwd: true
|
|
|
+ })
|
|
|
+ app.globalData.isBindWechat = false
|
|
|
+ wx.setStorageSync('isBindWechat', false)
|
|
|
+ } else {
|
|
|
+ that.handleDeviceFn(res.data.device)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 判断是否绑定了设备
|
|
|
+ handleDeviceFn (device) {
|
|
|
+ if (device !== null) {
|
|
|
+ wx.redirectTo({
|
|
|
+ url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ wx.redirectTo({
|
|
|
+ url: '/pages/permissions/permissions',
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
bindusername(e) {
|
|
|
this.setData({
|
|
|
username: e.detail.value
|
|
@@ -86,15 +149,6 @@ Page({
|
|
|
userPwd: e.detail.value
|
|
|
})
|
|
|
},
|
|
|
-
|
|
|
- // 设置小程序app数据
|
|
|
- setAppGlobalData (data) {
|
|
|
- app.globalData.accessToken = data.accessToken
|
|
|
- app.globalData.userName = data.userName
|
|
|
- app.globalData.headImg = data.headImg
|
|
|
- app.globalData.isBindWechat = data.isBindWechat
|
|
|
- app.globalData.workerId = data.workerId
|
|
|
- },
|
|
|
/**
|
|
|
* 账户密码登录
|
|
|
*/
|
|
@@ -140,15 +194,7 @@ Page({
|
|
|
})
|
|
|
} else { // 已经绑定过微信
|
|
|
wx.vibrateShort()
|
|
|
- if (device !== null) {
|
|
|
- wx.redirectTo({
|
|
|
- url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
|
|
|
- })
|
|
|
- } else {
|
|
|
- wx.redirectTo({
|
|
|
- url: '/pages/permissions/permissions',
|
|
|
- })
|
|
|
- }
|
|
|
+ that.handleDeviceFn(device)
|
|
|
}
|
|
|
}).catch(e => {
|
|
|
wx.hideLoading()
|
|
@@ -162,13 +208,13 @@ Page({
|
|
|
},
|
|
|
// 绑定微信
|
|
|
bindWecahtFn (device) {
|
|
|
+ wx.showLoading({
|
|
|
+ title: '绑定中...',
|
|
|
+ mask: true
|
|
|
+ })
|
|
|
wx.login({
|
|
|
success (res) {
|
|
|
if (res.code) {
|
|
|
- wx.showLoading({
|
|
|
- title: '绑定中...',
|
|
|
- mask: true
|
|
|
- })
|
|
|
bindWecaht({code: res.code}).then(res => {
|
|
|
wx.hideLoading()
|
|
|
wx.vibrateShort()
|
|
@@ -177,20 +223,12 @@ Page({
|
|
|
icon: 'success'
|
|
|
})
|
|
|
app.globalData.isBindWechat = true
|
|
|
- // 进入首页 todo
|
|
|
- if (device !== null) {
|
|
|
- wx.redirectTo({
|
|
|
- url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
|
|
|
- })
|
|
|
- } else {
|
|
|
- wx.redirectTo({
|
|
|
- url: '/pages/permissions/permissions',
|
|
|
- })
|
|
|
- }
|
|
|
+ wx.setStorageSync('isBindWechat', 'true')
|
|
|
+ that.handleDeviceFn(device)
|
|
|
}).catch(e => {
|
|
|
wx.hideLoading()
|
|
|
wx.showModal({
|
|
|
- content: e.msg,
|
|
|
+ content: '绑定失败',
|
|
|
confirmColor: '#333',
|
|
|
showCancel: false
|
|
|
})
|
|
@@ -198,5 +236,20 @@ Page({
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
+ },
|
|
|
+ // 设置小程序app数据
|
|
|
+ setAppGlobalData (data) {
|
|
|
+ app.globalData.accessToken = data.accessToken
|
|
|
+ app.globalData.userName = data.userName
|
|
|
+ app.globalData.headImg = data.headImg
|
|
|
+ app.globalData.isBindWechat = data.isBindWechat
|
|
|
+ app.globalData.workerId = data.workerId
|
|
|
+ app.globalData.device = data.device
|
|
|
+ wx.setStorageSync('accessToken', data.accessToken)
|
|
|
+ wx.setStorageSync('headImg', data.headImg)
|
|
|
+ wx.setStorageSync('userName', data.userName)
|
|
|
+ wx.setStorageSync('isBindWechat', data.isBindWechat)
|
|
|
+ wx.setStorageSync('workerId', data.workerId)
|
|
|
+ wx.setStorageSync('device', data.device)
|
|
|
}
|
|
|
})
|