login.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { miniLogin } = require('../../api/common')
  2. const { login, sessionStorageKey } = require('../../api/request')
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. booLock: false
  10. },
  11. wxCode: '',
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad(options) {
  16. this.getWxCode()
  17. },
  18. /**
  19. * 生命周期函数--监听页面初次渲染完成
  20. */
  21. onReady() {
  22. },
  23. /**
  24. * 生命周期函数--监听页面显示
  25. */
  26. onShow() {
  27. },
  28. /**
  29. * 生命周期函数--监听页面隐藏
  30. */
  31. onHide() {
  32. },
  33. /**
  34. * 生命周期函数--监听页面卸载
  35. */
  36. onUnload() {
  37. },
  38. async getPhoneNumber(e) {
  39. const isTab = app.globalData.tabBarList.map(item => item.pagePath)
  40. const { errMsg, encryptedData, iv } = e.detail
  41. if (errMsg === 'getPhoneNumber:ok') {
  42. this.setData({
  43. booLock: true
  44. })
  45. try {
  46. const { status, data } = await miniLogin({
  47. wxCode: this.wxCode,
  48. encryptedData,
  49. iv
  50. })
  51. if (status) {
  52. try {
  53. wx.setStorageSync(sessionStorageKey, data.token)
  54. } catch (err) {}
  55. await app.fetchUserData()
  56. const pages = getCurrentPages()
  57. if (pages.length > 1) {
  58. const route = pages[pages.length - 2]
  59. route.bindCallBack && route.bindCallBack()
  60. if (isTab.findIndex(item => item === route.route) > -1) {
  61. wx.reLaunch({
  62. url: '/' + route.route
  63. })
  64. } else {
  65. wx.navigateBack({
  66. delta: 1
  67. })
  68. }
  69. }
  70. } else {
  71. wx.showToast({
  72. title: '登录出错了,请重试',
  73. icon: 'none'
  74. })
  75. this.getWxCode()
  76. }
  77. } catch (e) {
  78. this.getWxCode()
  79. }
  80. this.setData({
  81. booLock: false
  82. })
  83. }
  84. },
  85. async getWxCode() {
  86. try {
  87. this.wxCode = await login()
  88. } catch (e) {}
  89. }
  90. })