login.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // pages/login/login.js
  2. import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. username: '',
  10. userPwd: '',
  11. loginCode: '',
  12. showAccountPwd: false , // 是否使用账号密码登录
  13. },
  14. /**
  15. * 生命周期函数--监听页面显示
  16. */
  17. onShow() {
  18. this.handleWXLogin()
  19. // this.loginLogic()
  20. },
  21. // 处理登录条件, 如果本地有accwssToken 则直接进行登录
  22. loginLogic () {
  23. const accessToken = wx.getStorageSync('accessToken')
  24. if (accessToken) {
  25. console.log('本地有token', wx.getStorageSync('device'));
  26. this.localTokenLogin()
  27. } else {
  28. this.handleWXLogin()
  29. }
  30. },
  31. localTokenLogin () {
  32. const accessToken = wx.getStorageSync('accessToken')
  33. const headImg = wx.getStorageSync('headImg')
  34. const userName = wx.getStorageSync('userName')
  35. const isBindWechat = wx.getStorageSync('isBindWechat')
  36. const workerId = wx.getStorageSync('workerId')
  37. const device = wx.getStorageSync('device')
  38. const isNeedAccountPwd = wx.getStorageSync('isNeedAccountPwd')
  39. app.globalData.accessToken = accessToken
  40. app.globalData.userName = userName
  41. app.globalData.headImg = headImg
  42. app.globalData.isBindWechat = isBindWechat
  43. app.globalData.workerId = workerId
  44. app.globalData.device = device
  45. if (isNeedAccountPwd) { // 需要账号密码登录
  46. this.setData({
  47. showAccountPwd: true
  48. })
  49. } else {
  50. this.handleDeviceFn(device)
  51. }
  52. },
  53. // 获取wx.login => code
  54. handleWXLogin () {
  55. var that = this
  56. wx.login({
  57. success (res) {
  58. if (res.code) {
  59. //发起网络请求
  60. that.setData({
  61. loginCode: res.code
  62. })
  63. that.workerLoginFn(res.code)
  64. } else {
  65. wx.hideLoading()
  66. wx.showModal({
  67. content: '登录失败!' + res.errMsg,
  68. confirmColor: '#333',
  69. showCancel: false
  70. })
  71. }
  72. }
  73. })
  74. },
  75. // 使用wx.login > code 登录
  76. workerLoginFn (code) {
  77. const that = this
  78. wx.showLoading({
  79. title: '加载中...',
  80. mask: true
  81. })
  82. workerLogin({code: code}).then(res => {
  83. // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
  84. wx.hideLoading()
  85. wx.vibrateShort()
  86. that.setAppGlobalData(res.data)
  87. that.handleDeviceFn(res.data.device)
  88. wx.setStorageSync('isNeedAccountPwd', false)
  89. }).catch(e => {
  90. wx.hideLoading()
  91. if (e.code == '403') { // 需要用户输入账号密码登录
  92. this.setData({
  93. showAccountPwd: true
  94. })
  95. app.globalData.isBindWechat = false
  96. app.globalData.isNeedAccountPwd = true
  97. wx.setStorageSync('isBindWechat', false)
  98. wx.setStorageSync('isNeedAccountPwd', true)
  99. } else {
  100. wx.showModal({
  101. content: e.msg,
  102. confirmColor: '#333',
  103. showCancel: false
  104. })
  105. }
  106. })
  107. },
  108. // 判断是否绑定了微信
  109. handleBindWxFn (flag) {
  110. if (!flag) {
  111. this.setData({
  112. showAccountPwd: true
  113. })
  114. app.globalData.isBindWechat = false
  115. wx.setStorageSync('isBindWechat', false)
  116. } else {
  117. that.handleDeviceFn(res.data.device)
  118. }
  119. },
  120. // 判断是否绑定了设备
  121. handleDeviceFn (device) {
  122. if (device !== null) {
  123. wx.redirectTo({
  124. url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
  125. })
  126. } else {
  127. wx.redirectTo({
  128. url: '/pages/permissions/permissions',
  129. })
  130. }
  131. },
  132. bindusername(e) {
  133. this.setData({
  134. username: e.detail.value
  135. })
  136. },
  137. bindUserPwd(e) {
  138. this.setData({
  139. userPwd: e.detail.value
  140. })
  141. },
  142. /**
  143. * 账户密码登录
  144. */
  145. handleLogin() {
  146. var that = this
  147. if (this.data.username == '') {
  148. wx.showModal({
  149. content: '请输入账号',
  150. confirmColor: '#333',
  151. showCancel: false
  152. })
  153. } else if (this.data.userPwd == '') {
  154. wx.showModal({
  155. content: '请输入密码',
  156. confirmColor: '#333',
  157. showCancel: false
  158. })
  159. } else {
  160. var data = {
  161. username: this.data.username,
  162. userPwd: this.data.userPwd
  163. }
  164. wx.showLoading({
  165. title: '登录中...',
  166. mask: true
  167. })
  168. pwdLogin(data).then(res => {
  169. wx.hideLoading()
  170. const response = res.data
  171. const isBindWechat = response.isBindWechat
  172. const device = response.device
  173. that.setAppGlobalData(response)
  174. if (!isBindWechat) { // 界面加一个微信绑定的按钮
  175. wx.showModal({
  176. content: '请绑定微信',
  177. confirmColor: '#333',
  178. showCancel: false,
  179. success (res) {
  180. if (res.confirm) {
  181. that.bindWecahtFn(device)
  182. }
  183. }
  184. })
  185. } else { // 已经绑定过微信
  186. wx.vibrateShort()
  187. that.handleDeviceFn(device)
  188. }
  189. }).catch(e => {
  190. wx.hideLoading()
  191. wx.showModal({
  192. content: e.msg,
  193. confirmColor: '#333',
  194. showCancel: false
  195. })
  196. })
  197. }
  198. },
  199. // 绑定微信
  200. bindWecahtFn (device) {
  201. wx.showLoading({
  202. title: '绑定中...',
  203. mask: true
  204. })
  205. wx.login({
  206. success (res) {
  207. if (res.code) {
  208. bindWecaht({code: res.code}).then(res => {
  209. wx.hideLoading()
  210. wx.vibrateShort()
  211. wx.showToast({
  212. title: '绑定成功',
  213. icon: 'success'
  214. })
  215. app.globalData.isBindWechat = true
  216. wx.setStorageSync('isBindWechat', 'true')
  217. that.handleDeviceFn(device)
  218. }).catch(e => {
  219. wx.hideLoading()
  220. wx.showModal({
  221. content: '绑定失败',
  222. confirmColor: '#333',
  223. showCancel: false
  224. })
  225. })
  226. }
  227. }
  228. })
  229. },
  230. // 设置小程序app数据
  231. setAppGlobalData (data) {
  232. app.globalData.accessToken = data.accessToken
  233. app.globalData.userName = data.userName
  234. app.globalData.headImg = data.headImg
  235. app.globalData.isBindWechat = data.isBindWechat
  236. app.globalData.workerId = data.workerId
  237. app.globalData.device = data.device
  238. wx.setStorageSync('accessToken', data.accessToken)
  239. wx.setStorageSync('headImg', data.headImg)
  240. wx.setStorageSync('userName', data.userName)
  241. wx.setStorageSync('isBindWechat', data.isBindWechat)
  242. wx.setStorageSync('workerId', data.workerId)
  243. wx.setStorageSync('device', data.device)
  244. }
  245. })