123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- // pages/login/login.js
- import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- username: '',
- userPwd: '',
- loginCode: '',
- showAccountPwd: false , // 是否使用账号密码登录
- },
- /**
- * 生命周期函数--监听页面显示
- */
- 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) {
- //发起网络请求
- that.setData({
- loginCode: res.code
- })
- that.workerLoginFn(res.code)
- } else {
- wx.hideLoading()
- wx.showModal({
- content: '登录失败!' + res.errMsg,
- confirmColor: '#333',
- showCancel: false
- })
- }
- }
- })
- },
- // 使用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
- })
- },
- bindUserPwd(e) {
- this.setData({
- userPwd: e.detail.value
- })
- },
- /**
- * 账户密码登录
- */
- handleLogin() {
- var that = this
- if (this.data.username == '') {
- wx.showModal({
- content: '请输入账号',
- confirmColor: '#333',
- showCancel: false
- })
- } else if (this.data.userPwd == '') {
- wx.showModal({
- content: '请输入密码',
- confirmColor: '#333',
- showCancel: false
- })
- } else {
- var data = {
- username: this.data.username,
- userPwd: this.data.userPwd
- }
- wx.showLoading({
- title: '登录中...',
- mask: true
- })
- pwdLogin(data).then(res => {
- wx.hideLoading()
- const response = res.data
- const isBindWechat = response.isBindWechat
- const device = response.device
- that.setAppGlobalData(response)
- if (!isBindWechat) { // 界面加一个微信绑定的按钮
- wx.showModal({
- content: '请绑定微信',
- confirmColor: '#333',
- showCancel: false,
- success (res) {
- if (res.confirm) {
- that.bindWecahtFn(device)
- }
- }
- })
- } else { // 已经绑定过微信
- wx.vibrateShort()
- that.handleDeviceFn(device)
- }
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: e.msg,
- confirmColor: '#333',
- showCancel: false
- })
- })
- }
- },
- // 绑定微信
- bindWecahtFn (device) {
- wx.showLoading({
- title: '绑定中...',
- mask: true
- })
- wx.login({
- success (res) {
- if (res.code) {
- bindWecaht({code: res.code}).then(res => {
- wx.hideLoading()
- wx.vibrateShort()
- wx.showToast({
- title: '绑定成功',
- icon: 'success'
- })
- app.globalData.isBindWechat = true
- wx.setStorageSync('isBindWechat', 'true')
- that.handleDeviceFn(device)
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: '绑定失败',
- confirmColor: '#333',
- showCancel: false
- })
- })
- }
- }
- })
- },
- // 设置小程序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)
- }
- })
|