login.js 4.0 KB

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