login.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
  2. import util from '../../utils/util'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. username: '',
  10. userPwd: '',
  11. showAccountPwd: false , // 是否使用账号密码登录
  12. },
  13. onShow() {
  14. this.setTokenExpireTime()
  15. },
  16. setTokenExpireTime () {
  17. const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
  18. if (tokenExpireTime !== '' && util.getDate() !== tokenExpireTime) {
  19. wx.clearStorageSync()
  20. }
  21. this.handleLogin()
  22. },
  23. handleLogin () {
  24. const accessToken = wx.getStorageSync('accessToken')
  25. if (accessToken) {
  26. const workerLoginCode = wx.getStorageSync('workerLoginCode')
  27. if (workerLoginCode == '403') {
  28. wx.clearStorageSync()
  29. this.handleWXLogin()
  30. } else {
  31. this.handleTokenLogin()
  32. }
  33. } else {
  34. this.handleWXLogin()
  35. }
  36. },
  37. handleTokenLogin () {
  38. this.getStorageData()
  39. this.handleDeviceFn(wx.getStorageSync('device'))
  40. },
  41. handleWXLogin () {
  42. const workerLoginCode = wx.getStorageSync('workerLoginCode')
  43. console.log(workerLoginCode, 'workerLoginCode', wx.getStorageSync('workerLoginCode'));
  44. if (workerLoginCode == '403') {
  45. this.handleCode403()
  46. } else {
  47. var that = this
  48. wx.login({
  49. success (res) {
  50. if (res.code) {
  51. that.workerLoginFn(res.code)
  52. } else {
  53. wx.showModal({
  54. content: '登录失败!' + res.errMsg,
  55. confirmColor: '#333',
  56. showCancel: false
  57. })
  58. }
  59. }
  60. })
  61. }
  62. },
  63. workerLoginFn (code) {
  64. const that = this
  65. wx.showLoading({
  66. title: '加载中...',
  67. mask: true
  68. })
  69. workerLogin({code: code}).then(res => {
  70. wx.hideLoading()
  71. wx.vibrateShort()
  72. that.setStorageData(res.data)
  73. that.handleDeviceFn(res.data.device)
  74. wx.setStorageSync('workerLoginCode', '000')
  75. }).catch(e => {
  76. wx.hideLoading()
  77. if (e.code == '403') {
  78. wx.setStorageSync('workerLoginCode', '403')
  79. that.handleCode403()
  80. } else {
  81. wx.showModal({
  82. content: e.msg,
  83. confirmColor: '#333',
  84. showCancel: false
  85. })
  86. }
  87. })
  88. },
  89. handleCode403 () {
  90. this.setData({
  91. showAccountPwd: true
  92. })
  93. },
  94. pwdLoginFn () {
  95. const that = this
  96. if (this.data.username == '') {
  97. wx.showToast({
  98. title: '请输入账号',
  99. icon: 'error'
  100. })
  101. } else if (this.data.userPwd == '') {
  102. wx.showToast({
  103. title: '请输入密码',
  104. icon: 'error'
  105. })
  106. } else {
  107. var data = {
  108. username: this.data.username,
  109. userPwd: this.data.userPwd
  110. }
  111. wx.showLoading({
  112. title: '登录中...',
  113. mask: true
  114. })
  115. pwdLogin(data).then(res => {
  116. wx.hideLoading()
  117. const response = res.data
  118. const isBindWechat = response.isBindWechat
  119. const device = response.device
  120. that.setStorageData(response)
  121. if (!isBindWechat) {
  122. wx.showModal({
  123. content: '请绑定微信',
  124. confirmColor: '#333',
  125. showCancel: false,
  126. success (res) {
  127. if (res.confirm) {
  128. that.bindWecahtFn(device)
  129. }
  130. }
  131. })
  132. } else {
  133. wx.vibrateShort()
  134. that.handleDeviceFn(device)
  135. }
  136. }).catch(e => {
  137. console.log(e,'eeeee');
  138. wx.hideLoading()
  139. wx.showModal({
  140. content: e.msg,
  141. confirmColor: '#333',
  142. showCancel: false
  143. })
  144. })
  145. }
  146. },
  147. bindWecahtFn (device) {
  148. const that = this
  149. wx.showLoading({
  150. title: '绑定中...',
  151. mask: true
  152. })
  153. wx.login({
  154. success (codeRes) {
  155. if (codeRes.code) {
  156. bindWecaht({code: codeRes.code}).then(res => {
  157. wx.hideLoading()
  158. wx.vibrateShort()
  159. wx.setStorageSync('workerLoginCode', '000')
  160. wx.showToast({
  161. title: '绑定成功',
  162. icon: 'success'
  163. })
  164. that.handleDeviceFn(device)
  165. }).catch(e => {
  166. wx.hideLoading()
  167. wx.showModal({
  168. content: e.msg,
  169. confirmColor: '#333',
  170. showCancel: false
  171. })
  172. })
  173. }
  174. }
  175. })
  176. },
  177. handleDeviceFn (device) {
  178. if (device !== null) {
  179. wx.redirectTo({
  180. url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
  181. })
  182. } else {
  183. wx.redirectTo({
  184. url: '/pages/permissions/permissions',
  185. })
  186. }
  187. },
  188. setStorageData (data) {
  189. app.globalData.accessToken = data.accessToken
  190. app.globalData.userName = data.userName
  191. app.globalData.headImg = data.headImg
  192. app.globalData.isBindWechat = data.isBindWechat
  193. app.globalData.workerId = data.workerId
  194. app.globalData.device = data.device
  195. wx.setStorageSync('accessToken', data.accessToken)
  196. wx.setStorageSync('tokenExpireTime', util.getDate())
  197. wx.setStorageSync('headImg', data.headImg)
  198. wx.setStorageSync('userName', data.userName)
  199. wx.setStorageSync('isBindWechat', data.isBindWechat)
  200. wx.setStorageSync('workerId', data.workerId)
  201. wx.setStorageSync('device', data.device)
  202. },
  203. getStorageData () {
  204. const accessToken = wx.getStorageSync('accessToken')
  205. const headImg = wx.getStorageSync('headImg')
  206. const userName = wx.getStorageSync('userName')
  207. const isBindWechat = wx.getStorageSync('isBindWechat')
  208. const workerId = wx.getStorageSync('workerId')
  209. const device = wx.getStorageSync('device')
  210. app.globalData.accessToken = accessToken
  211. app.globalData.userName = userName
  212. app.globalData.headImg = headImg
  213. app.globalData.isBindWechat = isBindWechat
  214. app.globalData.workerId = workerId
  215. app.globalData.device = device
  216. }
  217. })