login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. },
  20. // 触达微信登录
  21. handleWXLogin () {
  22. var that = this
  23. wx.login({
  24. success (res) {
  25. if (res.code) {
  26. //发起网络请求
  27. wx.showLoading({
  28. title: '加载中...',
  29. mask: true
  30. })
  31. that.setData({
  32. loginCode: res.code
  33. })
  34. workerLogin({code: res.code}).then(res => {
  35. // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
  36. wx.hideLoading()
  37. wx.vibrateShort()
  38. that.setAppGlobalData(res.data)
  39. const device = res.data.device
  40. if (device !== null) {
  41. wx.redirectTo({
  42. url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
  43. })
  44. } else {
  45. wx.redirectTo({
  46. url: '/pages/permissions/permissions',
  47. })
  48. }
  49. }).catch(e => {
  50. // e.code == 403 未绑定微信 进入账户密码登录页面
  51. wx.hideLoading()
  52. console.log(e,'errot');
  53. if (e.code == '403') {
  54. that.setData({
  55. showAccountPwd: true
  56. })
  57. } else {
  58. wx.showModal({
  59. content: e.msg,
  60. confirmColor: '#333',
  61. showCancel: false
  62. })
  63. }
  64. })
  65. } else {
  66. wx.hideLoading()
  67. wx.showModal({
  68. content: '登录失败!' + res.errMsg,
  69. confirmColor: '#333',
  70. showCancel: false
  71. })
  72. }
  73. }
  74. })
  75. },
  76. bindusername(e) {
  77. this.setData({
  78. username: e.detail.value
  79. })
  80. },
  81. bindUserPwd(e) {
  82. this.setData({
  83. userPwd: e.detail.value
  84. })
  85. },
  86. // 设置小程序app数据
  87. setAppGlobalData (data) {
  88. app.globalData.accessToken = data.accessToken
  89. app.globalData.userName = data.userName
  90. app.globalData.headImg = data.headImg
  91. app.globalData.isBindWechat = data.isBindWechat
  92. app.globalData.workerId = data.workerId
  93. },
  94. /**
  95. * 账户密码登录
  96. */
  97. handleLogin() {
  98. var that = this
  99. if (this.data.username == '') {
  100. wx.showModal({
  101. content: '请输入账号',
  102. confirmColor: '#333',
  103. showCancel: false
  104. })
  105. } else if (this.data.userPwd == '') {
  106. wx.showModal({
  107. content: '请输入密码',
  108. confirmColor: '#333',
  109. showCancel: false
  110. })
  111. } else {
  112. var data = {
  113. username: this.data.username,
  114. userPwd: this.data.userPwd
  115. }
  116. wx.showLoading({
  117. title: '登录中...',
  118. mask: true
  119. })
  120. pwdLogin(data).then(res => {
  121. wx.hideLoading()
  122. const response = res.data
  123. const isBindWechat = response.isBindWechat
  124. const device = response.device
  125. that.setAppGlobalData(response)
  126. if (!isBindWechat) { // 界面加一个微信绑定的按钮
  127. wx.showModal({
  128. content: '请绑定微信',
  129. confirmColor: '#333',
  130. showCancel: false,
  131. success (res) {
  132. if (res.confirm) {
  133. that.bindWecahtFn(device)
  134. }
  135. }
  136. })
  137. } else { // 已经绑定过微信
  138. wx.vibrateShort()
  139. if (device !== null) {
  140. wx.redirectTo({
  141. url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
  142. })
  143. } else {
  144. wx.redirectTo({
  145. url: '/pages/permissions/permissions',
  146. })
  147. }
  148. }
  149. }).catch(e => {
  150. wx.hideLoading()
  151. wx.showModal({
  152. content: e.msg,
  153. confirmColor: '#333',
  154. showCancel: false
  155. })
  156. })
  157. }
  158. },
  159. // 绑定微信
  160. bindWecahtFn (device) {
  161. wx.login({
  162. success (res) {
  163. if (res.code) {
  164. wx.showLoading({
  165. title: '绑定中...',
  166. mask: true
  167. })
  168. bindWecaht({code: res.code}).then(res => {
  169. wx.hideLoading()
  170. wx.vibrateShort()
  171. wx.showToast({
  172. title: '绑定成功',
  173. icon: 'success'
  174. })
  175. app.globalData.isBindWechat = true
  176. // 进入首页 todo
  177. if (device !== null) {
  178. wx.redirectTo({
  179. url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
  180. })
  181. } else {
  182. wx.redirectTo({
  183. url: '/pages/permissions/permissions',
  184. })
  185. }
  186. }).catch(e => {
  187. wx.hideLoading()
  188. wx.showModal({
  189. content: e.msg,
  190. confirmColor: '#333',
  191. showCancel: false
  192. })
  193. })
  194. }
  195. }
  196. })
  197. }
  198. })