login.js 3.5 KB

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