login.js 4.1 KB

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