login.js 5.1 KB

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