login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. imageUrl: app.globalData.imageUrl,
  12. },
  13. update: function () {
  14. const updateManager = wx.getUpdateManager()
  15. const that = this
  16. updateManager.onCheckForUpdate(function (res) {
  17. // 请求完新版本信息的回调
  18. if(res.hasUpdate){//发现新版本,提示更新
  19. updateManager.onUpdateReady(function () {
  20. wx.showModal({
  21. title: '更新提示',
  22. content: '发现新版本,请重新启动小程序',
  23. showCancel: false,
  24. success(res) {
  25. if (res.confirm) {
  26. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  27. updateManager.applyUpdate()
  28. }
  29. }
  30. })
  31. })
  32. } else {//没有新版本,直接进行下一步获取相关参数
  33. that.getVersion()
  34. }
  35. })
  36. updateManager.onUpdateReady(function () {
  37. wx.showModal({
  38. title: '更新提示',
  39. content: '新版本已经准备好,是否重启应用?',
  40. success(res) {
  41. if (res.confirm) {
  42. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  43. updateManager.applyUpdate()
  44. }
  45. }
  46. })
  47. })
  48. updateManager.onUpdateFailed(function () {
  49. // 新版本下载失败
  50. wx.showModal({
  51. title: '提示',
  52. content: '更新失败,请检查网络或者重新启动小程序',
  53. showCancel: false,
  54. success(res){
  55. if(res.confirm){
  56. updateManager.applyUpdate()
  57. }
  58. }
  59. })
  60. })
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad: function (options) {
  66. wx.showLoading({
  67. title: '加载中',
  68. mask: true
  69. })
  70. if (options.isPage) {
  71. this.setData({
  72. isPage: options.isPage,
  73. isId: options.isId
  74. })
  75. }
  76. this.update()
  77. },
  78. //判断版本号
  79. getVersion: function () {
  80. util.doPost(
  81. 'getVersion', {
  82. appId: app.globalData.appId
  83. }
  84. ).then(res => {
  85. if (res.version != app.globalData.ver) {
  86. wx.setStorageSync('isShow', false)
  87. } else {
  88. wx.setStorageSync('isShow', true)
  89. this.getOpenId()
  90. }
  91. })
  92. },
  93. // 登录
  94. getOpenId: function () {
  95. wx.login({
  96. success: res => {
  97. util.doPost(
  98. 'getOpenId', {
  99. appId: app.globalData.appId,
  100. code: res.code
  101. },
  102. ).then(res => {
  103. if (res.success == 1) {
  104. wx.setStorageSync('openId', res.data.openId)
  105. }
  106. })
  107. },
  108. fail: res => {
  109. wx.hideLoading({
  110. success: (res) => {
  111. wx.showToast({
  112. title: '提示',
  113. icon: 'none',
  114. content: '连接失败,请重试'
  115. })
  116. },
  117. })
  118. }
  119. })
  120. },
  121. //授权手机号
  122. getPhoneNumber: function (e) {
  123. const that = this;
  124. if (e.detail.errMsg == "getPhoneNumber:ok") {
  125. wx.showLoading({
  126. title: '正在登陆',
  127. mask: true
  128. })
  129. util.doPost(
  130. 'getPhoneNumber', {
  131. encryptedData: e.detail.encryptedData,
  132. iv: e.detail.iv,
  133. openId: wx.getStorageSync('openId'),
  134. }
  135. ).then(rs => {
  136. if (rs.success > 0) {
  137. wx.setStorageSync('phone', rs.data.phoneNumber);
  138. // wx.setStorageSync('phone', '13871019618')
  139. this.autoLogin();
  140. } else {
  141. wx.showToast({
  142. title: '手机号验证失败',
  143. })
  144. }
  145. })
  146. }
  147. that.setData({
  148. model: false,
  149. localtion: true
  150. })
  151. },
  152. autoLogin: function () {
  153. util.doPost(
  154. 'autoLogin', {
  155. openId: wx.getStorageSync('openId'),
  156. }
  157. ).then(rs => {
  158. wx.hideLoading()
  159. if (rs.success > 0) {
  160. wx.setStorageSync('sso-token', rs.data.token);
  161. wx.showToast({
  162. title: '登录成功',
  163. })
  164. if (this.data.isPage) {
  165. wx.redirectTo({
  166. url: '/pages/' + this.data.isPage + "/" + this.data.isPage + "?proId=" + this.data.isId,
  167. })
  168. } else {
  169. wx.switchTab({
  170. url: '/pages/index/index'
  171. })
  172. }
  173. } else {
  174. wx.showToast({
  175. title: rs.errMsg,
  176. icon: 'none',
  177. duration: 3000
  178. })
  179. }
  180. })
  181. },
  182. /**
  183. * 生命周期函数--监听页面初次渲染完成
  184. */
  185. onReady: function () {
  186. },
  187. /**
  188. * 生命周期函数--监听页面显示
  189. */
  190. onShow: function () {
  191. },
  192. /**
  193. * 生命周期函数--监听页面隐藏
  194. */
  195. onHide: function () {
  196. },
  197. /**
  198. * 生命周期函数--监听页面卸载
  199. */
  200. onUnload: function () {
  201. },
  202. /**
  203. * 页面相关事件处理函数--监听用户下拉动作
  204. */
  205. onPullDownRefresh: function () {
  206. },
  207. /**
  208. * 页面上拉触底事件的处理函数
  209. */
  210. onReachBottom: function () {
  211. },
  212. /**
  213. * 用户点击右上角分享
  214. */
  215. onShareAppMessage: function () {
  216. }
  217. })