login.js 5.8 KB

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