login.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // pages/login/login.js
  2. import { loginByWeChat, updateUserInfo } from '../../api/user'
  3. import { navigateTo, showModal, showLoading, hideLoading, setStorageSync,getStorageSync, navigateBack, reLaunch, showToast } from '../../utils/util'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. canIUse: wx.canIUse('button.open-type.getPhoneNumber'),
  10. showAuth: false
  11. },
  12. pageData: {
  13. wxCode: ''
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function(options) {
  19. this.getWXCode()
  20. },
  21. // 获取登录code
  22. getWXCode: function () {
  23. wx.login({
  24. success: (res) => {
  25. this.pageData.wxCode = res.code
  26. }
  27. })
  28. },
  29. // 手机号登录
  30. tapPhoneLoginAction: function() {
  31. navigateTo('/pages/loginMobeil/loginMobeil')
  32. },
  33. // 授权手机号
  34. getPhoneNumber: function(e) {
  35. const { encryptedData, iv } = e.detail
  36. if (!encryptedData || !iv) {
  37. this.tapPhoneLoginAction()
  38. return false
  39. }
  40. showLoading({
  41. title: '登录中...'
  42. })
  43. const { wxCode } = this.pageData
  44. this.wxLogin(wxCode,encryptedData, iv)
  45. },
  46. // 授权用户信息 --> 登录
  47. onGotUserInfo: function(e) {
  48. let { encryptedData, iv, rawData, signature } = e.detail
  49. if (iv && iv.length && signature && signature.length && encryptedData && encryptedData.length) {
  50. try {
  51. rawData = JSON.parse(rawData)
  52. } catch (error) {
  53. rawData = rawData
  54. }
  55. const params = {
  56. signature,
  57. encryptedData,
  58. iv,
  59. userName:rawData.nickName,
  60. avatar:rawData.avatarUrl
  61. }
  62. updateUserInfo(params).then((result)=>{
  63. if (result && result.code == 0) {
  64. let userInfo = getStorageSync('USERINFO')
  65. userInfo = Object.assign({},userInfo,result.data)
  66. setStorageSync('USERINFO', userInfo)
  67. // 获取用户信心成功返回
  68. this.redirect()
  69. } else {
  70. this.tapPhoneLoginAction()
  71. }
  72. })
  73. } else {
  74. this.tapPhoneLoginAction()
  75. }
  76. this.setData({
  77. showAuth: false
  78. })
  79. },
  80. // 登录
  81. wxLogin: function(code, encryptedData, iv) {
  82. const params = {
  83. code,
  84. encryptedData,
  85. iv
  86. }
  87. loginByWeChat(params).then((result) => {
  88. hideLoading()
  89. const data = result.data
  90. if (data && data.userInfo && data.userInfo.token) {
  91. setStorageSync('USERINFO', data.userInfo)
  92. if(parseInt(data.newUser) == 1) { // 1为新用户、0为旧用户,去拿用户信息
  93. this.setData({
  94. showAuth: true
  95. })
  96. } else {
  97. // 登录成功返回
  98. this.redirect()
  99. }
  100. } else {
  101. this.tapPhoneLoginAction()
  102. }
  103. }).catch((err) => {
  104. hideLoading()
  105. showModal({
  106. title: '提示',
  107. content: err.msg,
  108. showCancel: false
  109. })
  110. this.tapPhoneLoginAction()
  111. })
  112. },
  113. // 登录成功返回
  114. redirect: function() {
  115. // 在内存中的所有页面栈对象
  116. const pages = getCurrentPages()
  117. if (pages.length > 1) {
  118. // 上个页面
  119. const prePage = pages[pages.length - 2]
  120. // 页面的登录回调
  121. if (prePage && prePage.loginCallback) {
  122. prePage.loginCallback()
  123. }
  124. navigateBack()
  125. } else {
  126. reLaunch('/pages/index/index')
  127. }
  128. },
  129. // 取消登录
  130. handleNotLoginClick: function() {
  131. const pages = getCurrentPages() /*在内存中的所有页面栈对象*/
  132. if (pages.length > 2) {
  133. navigateBack()
  134. } else {
  135. reLaunch('/pages/index/index')
  136. }
  137. }
  138. })