|
@@ -1,6 +1,7 @@
|
|
|
-// pages/login/login.js
|
|
|
import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
|
|
|
+import util from '../../utils/util'
|
|
|
const app = getApp()
|
|
|
+
|
|
|
Page({
|
|
|
|
|
|
/**
|
|
@@ -9,76 +10,52 @@ Page({
|
|
|
data: {
|
|
|
username: '',
|
|
|
userPwd: '',
|
|
|
- loginCode: '',
|
|
|
showAccountPwd: false , // 是否使用账号密码登录
|
|
|
},
|
|
|
|
|
|
- /**
|
|
|
- * 生命周期函数--监听页面显示
|
|
|
- */
|
|
|
onShow() {
|
|
|
- this.handleWXLogin()
|
|
|
- // this.loginLogic()
|
|
|
+ this.setTokenExpireTime()
|
|
|
},
|
|
|
-
|
|
|
- // 处理登录条件, 如果本地有accwssToken 则直接进行登录
|
|
|
- loginLogic () {
|
|
|
+ setTokenExpireTime () {
|
|
|
+ const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
|
|
|
+ if (util.getDate() !== tokenExpireTime) {
|
|
|
+ wx.clearStorageSync()
|
|
|
+ }
|
|
|
+ this.handleLogin()
|
|
|
+ },
|
|
|
+ handleLogin () {
|
|
|
const accessToken = wx.getStorageSync('accessToken')
|
|
|
if (accessToken) {
|
|
|
- console.log('本地有token', wx.getStorageSync('device'));
|
|
|
- this.localTokenLogin()
|
|
|
+ this.handleTokenLogin()
|
|
|
} 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)
|
|
|
- }
|
|
|
+ handleTokenLogin () {
|
|
|
+ this.getStorageData()
|
|
|
+ this.handleDeviceFn(wx.getStorageSync('device'))
|
|
|
},
|
|
|
-
|
|
|
- // 获取wx.login => code
|
|
|
handleWXLogin () {
|
|
|
- var that = this
|
|
|
- wx.login({
|
|
|
- success (res) {
|
|
|
- if (res.code) {
|
|
|
- //发起网络请求
|
|
|
- that.setData({
|
|
|
- loginCode: res.code
|
|
|
- })
|
|
|
- that.workerLoginFn(res.code)
|
|
|
- } else {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showModal({
|
|
|
- content: '登录失败!' + res.errMsg,
|
|
|
- confirmColor: '#333',
|
|
|
- showCancel: false
|
|
|
- })
|
|
|
+ const workerLoginCode = wx.getStorageSync('workerLoginCode')
|
|
|
+ if (workerLoginCode == '403') {
|
|
|
+ this.handleCode403()
|
|
|
+ } else {
|
|
|
+ var that = this
|
|
|
+ wx.login({
|
|
|
+ success (res) {
|
|
|
+ if (res.code) {
|
|
|
+ that.workerLoginFn(res.code)
|
|
|
+ } else {
|
|
|
+ wx.showModal({
|
|
|
+ content: '登录失败!' + res.errMsg,
|
|
|
+ confirmColor: '#333',
|
|
|
+ showCancel: false
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- })
|
|
|
+ })
|
|
|
+ }
|
|
|
},
|
|
|
-
|
|
|
- // 使用wx.login > code 登录
|
|
|
workerLoginFn (code) {
|
|
|
const that = this
|
|
|
wx.showLoading({
|
|
@@ -86,22 +63,16 @@ Page({
|
|
|
mask: true
|
|
|
})
|
|
|
workerLogin({code: code}).then(res => {
|
|
|
- // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
|
|
|
wx.hideLoading()
|
|
|
wx.vibrateShort()
|
|
|
- that.setAppGlobalData(res.data)
|
|
|
+ that.setStorageData(res.data)
|
|
|
that.handleDeviceFn(res.data.device)
|
|
|
- wx.setStorageSync('isNeedAccountPwd', false)
|
|
|
+ wx.setStorageSync('workerLoginCode', '000')
|
|
|
}).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)
|
|
|
+ if (e.code == '403') {
|
|
|
+ wx.setStorageSync('workerLoginCode', '403')
|
|
|
+ that.handleCode403()
|
|
|
} else {
|
|
|
wx.showModal({
|
|
|
content: e.msg,
|
|
@@ -111,60 +82,22 @@ Page({
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
-
|
|
|
- // 判断是否绑定了微信
|
|
|
- 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
|
|
|
- })
|
|
|
- },
|
|
|
-
|
|
|
- bindUserPwd(e) {
|
|
|
+ handleCode403 () {
|
|
|
this.setData({
|
|
|
- userPwd: e.detail.value
|
|
|
+ showAccountPwd: true
|
|
|
})
|
|
|
},
|
|
|
- /**
|
|
|
- * 账户密码登录
|
|
|
- */
|
|
|
- handleLogin() {
|
|
|
- var that = this
|
|
|
+ pwdLoginFn () {
|
|
|
+ const that = this
|
|
|
if (this.data.username == '') {
|
|
|
- wx.showModal({
|
|
|
- content: '请输入账号',
|
|
|
- confirmColor: '#333',
|
|
|
- showCancel: false
|
|
|
+ wx.showToast({
|
|
|
+ title: '请输入账号',
|
|
|
+ icon: 'error'
|
|
|
})
|
|
|
} else if (this.data.userPwd == '') {
|
|
|
- wx.showModal({
|
|
|
- content: '请输入密码',
|
|
|
- confirmColor: '#333',
|
|
|
- showCancel: false
|
|
|
+ wx.showToast({
|
|
|
+ title: '请输入密码',
|
|
|
+ icon: 'error'
|
|
|
})
|
|
|
} else {
|
|
|
var data = {
|
|
@@ -180,8 +113,8 @@ Page({
|
|
|
const response = res.data
|
|
|
const isBindWechat = response.isBindWechat
|
|
|
const device = response.device
|
|
|
- that.setAppGlobalData(response)
|
|
|
- if (!isBindWechat) { // 界面加一个微信绑定的按钮
|
|
|
+ that.setStorageData(response)
|
|
|
+ if (!isBindWechat) {
|
|
|
wx.showModal({
|
|
|
content: '请绑定微信',
|
|
|
confirmColor: '#333',
|
|
@@ -192,11 +125,12 @@ Page({
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
- } else { // 已经绑定过微信
|
|
|
+ } else {
|
|
|
wx.vibrateShort()
|
|
|
that.handleDeviceFn(device)
|
|
|
}
|
|
|
}).catch(e => {
|
|
|
+ console.log(e,'eeeee');
|
|
|
wx.hideLoading()
|
|
|
wx.showModal({
|
|
|
content: e.msg,
|
|
@@ -206,29 +140,28 @@ Page({
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
- // 绑定微信
|
|
|
bindWecahtFn (device) {
|
|
|
+ const that = this
|
|
|
wx.showLoading({
|
|
|
title: '绑定中...',
|
|
|
mask: true
|
|
|
})
|
|
|
wx.login({
|
|
|
- success (res) {
|
|
|
- if (res.code) {
|
|
|
- bindWecaht({code: res.code}).then(res => {
|
|
|
+ success (codeRes) {
|
|
|
+ if (codeRes.code) {
|
|
|
+ bindWecaht({code: codeRes.code}).then(res => {
|
|
|
wx.hideLoading()
|
|
|
wx.vibrateShort()
|
|
|
+ wx.setStorageSync('workerLoginCode', '000')
|
|
|
wx.showToast({
|
|
|
title: '绑定成功',
|
|
|
icon: 'success'
|
|
|
})
|
|
|
- app.globalData.isBindWechat = true
|
|
|
- wx.setStorageSync('isBindWechat', 'true')
|
|
|
that.handleDeviceFn(device)
|
|
|
}).catch(e => {
|
|
|
wx.hideLoading()
|
|
|
wx.showModal({
|
|
|
- content: '绑定失败',
|
|
|
+ content: e.msg,
|
|
|
confirmColor: '#333',
|
|
|
showCancel: false
|
|
|
})
|
|
@@ -237,8 +170,18 @@ Page({
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
- // 设置小程序app数据
|
|
|
- setAppGlobalData (data) {
|
|
|
+ handleDeviceFn (device) {
|
|
|
+ if (device !== null) {
|
|
|
+ wx.redirectTo({
|
|
|
+ url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ wx.redirectTo({
|
|
|
+ url: '/pages/permissions/permissions',
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
+ setStorageData (data) {
|
|
|
app.globalData.accessToken = data.accessToken
|
|
|
app.globalData.userName = data.userName
|
|
|
app.globalData.headImg = data.headImg
|
|
@@ -246,10 +189,25 @@ Page({
|
|
|
app.globalData.workerId = data.workerId
|
|
|
app.globalData.device = data.device
|
|
|
wx.setStorageSync('accessToken', data.accessToken)
|
|
|
+ wx.setStorageSync('tokenExpireTime', util.getDate())
|
|
|
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)
|
|
|
+ },
|
|
|
+ getStorageData () {
|
|
|
+ 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')
|
|
|
+ app.globalData.accessToken = accessToken
|
|
|
+ app.globalData.userName = userName
|
|
|
+ app.globalData.headImg = headImg
|
|
|
+ app.globalData.isBindWechat = isBindWechat
|
|
|
+ app.globalData.workerId = workerId
|
|
|
+ app.globalData.device = device
|
|
|
}
|
|
|
})
|