login.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. wx.hideLoading()
  87. if (rs.success > 0) {
  88. wx.setStorageSync('sso-token', rs.data.token);
  89. wx.showToast({
  90. title: '登录成功',
  91. })
  92. wx.switchTab({
  93. url: '/pages/index/index'
  94. })
  95. } else {
  96. wx.showToast({
  97. title: rs.errMsg,
  98. icon: 'none'
  99. })
  100. }
  101. })
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: function () {
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow: function () {
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide: function () {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload: function () {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh: function () {
  127. },
  128. /**
  129. * 页面上拉触底事件的处理函数
  130. */
  131. onReachBottom: function () {
  132. },
  133. /**
  134. * 用户点击右上角分享
  135. */
  136. onShareAppMessage: function () {
  137. }
  138. })