login.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // pages/login/login.js
  2. const app = getApp()
  3. const util = require("../../utils/util")
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) {
  14. wx.showLoading({
  15. title: '加载中',
  16. mask: true
  17. })
  18. this.getOpenId()
  19. },
  20. // 登录
  21. getOpenId: function () {
  22. wx.login({
  23. success: res => {
  24. util.doPost(
  25. 'getOpenId', {
  26. appId: app.globalData.appId,
  27. code: res.code
  28. },
  29. ).then(res => {
  30. if (res.success == 1) {
  31. wx.setStorageSync('openId', res.data.openId)
  32. wx.hideLoading({
  33. })
  34. }
  35. })
  36. },
  37. fail: res => {
  38. wx.hideLoading({
  39. success: (res) => {
  40. wx.showToast({
  41. title: '提示',
  42. icon: 'none',
  43. content: '连接失败,请重试'
  44. })
  45. },
  46. })
  47. }
  48. })
  49. },
  50. //授权手机号
  51. getPhoneNumber: function (e) {
  52. const that = this;
  53. if (e.detail.errMsg == "getPhoneNumber:ok") {
  54. wx.showLoading({
  55. title: '正在登陆',
  56. mask: true
  57. })
  58. util.doPost(
  59. 'getPhoneNumber', {
  60. encryptedData: e.detail.encryptedData,
  61. iv: e.detail.iv,
  62. openId: wx.getStorageSync('openId'),
  63. }
  64. ).then(rs => {
  65. if (rs.success > 0) {
  66. wx.setStorageSync('phone', rs.data.phoneNumber);
  67. this.autoLogin();
  68. } else {
  69. wx.showToast({
  70. title: '手机号验证失败',
  71. })
  72. }
  73. })
  74. }
  75. that.setData({
  76. model: false,
  77. localtion: true
  78. })
  79. },
  80. autoLogin: function () {
  81. util.doPost(
  82. 'autoLogin', {
  83. openId: wx.getStorageSync('openId'),
  84. }
  85. ).then(rs => {
  86. if (rs.success > 0) {
  87. wx.setStorageSync('sso-token', rs.data.token);
  88. wx.showToast({
  89. title: '登录成功',
  90. })
  91. wx.switchTab({
  92. url: '/pages/index/index'
  93. })
  94. } else {
  95. wx.showToast({
  96. title: rs.errMsg
  97. })
  98. }
  99. wx.hideLoading({ })
  100. })
  101. },
  102. /**
  103. * 生命周期函数--监听页面初次渲染完成
  104. */
  105. onReady: function () {
  106. },
  107. /**
  108. * 生命周期函数--监听页面显示
  109. */
  110. onShow: function () {
  111. },
  112. /**
  113. * 生命周期函数--监听页面隐藏
  114. */
  115. onHide: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面卸载
  119. */
  120. onUnload: function () {
  121. },
  122. /**
  123. * 页面相关事件处理函数--监听用户下拉动作
  124. */
  125. onPullDownRefresh: function () {
  126. },
  127. /**
  128. * 页面上拉触底事件的处理函数
  129. */
  130. onReachBottom: function () {
  131. },
  132. /**
  133. * 用户点击右上角分享
  134. */
  135. onShareAppMessage: function () {
  136. }
  137. })