123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- // pages/login/login.js
- import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- username: '',
- userPwd: '',
- loginCode: '',
- showAccountPwd: false , // 是否使用账号密码登录
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- this.handleWXLogin()
- },
- // 触达微信登录
- 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
- })
- }
- })
- } else {
- wx.hideLoading()
- wx.showModal({
- content: '登录失败!' + res.errMsg,
- confirmColor: '#333',
- showCancel: false
- })
- }
- }
- })
- },
- bindusername(e) {
- this.setData({
- username: e.detail.value
- })
- },
- bindUserPwd(e) {
- this.setData({
- 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
- },
- /**
- * 账户密码登录
- */
- 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()
- if (device !== null) {
- wx.redirectTo({
- url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
- })
- } else {
- wx.redirectTo({
- url: '/pages/permissions/permissions',
- })
- }
- }
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: e.msg,
- confirmColor: '#333',
- showCancel: false
- })
- })
- }
- },
- // 绑定微信
- bindWecahtFn (device) {
- wx.login({
- success (res) {
- if (res.code) {
- wx.showLoading({
- title: '绑定中...',
- mask: true
- })
- bindWecaht({code: res.code}).then(res => {
- wx.hideLoading()
- wx.vibrateShort()
- wx.showToast({
- title: '绑定成功',
- 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',
- })
- }
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: e.msg,
- confirmColor: '#333',
- showCancel: false
- })
- })
- }
- }
- })
- }
- })
|