login.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. wx.setStorageSync('indexQuanTip', true)
  63. if (options.isPage) {
  64. this.setData({
  65. isPage: options.isPage,
  66. isId: options.isId
  67. })
  68. }
  69. this.update()
  70. },
  71. toAgreement:function(){
  72. wx.navigateTo({
  73. url: '/pages/agreement/agreement',
  74. })
  75. },
  76. //判断版本号
  77. getVersion: function () {
  78. util.doPost(
  79. 'getVersion', {
  80. appId: app.globalData.appId
  81. }
  82. ).then(res => {
  83. if (res.version != app.globalData.ver) {
  84. wx.setStorageSync('isShow', false)
  85. } else {
  86. wx.setStorageSync('isShow', true)
  87. }
  88. this.getOpenId()
  89. this.setData({
  90. isShow: res.version == app.globalData.ver
  91. })
  92. })
  93. },
  94. // 登录
  95. getOpenId: function () {
  96. wx.login({
  97. success: res => {
  98. util.doPost(
  99. 'getOpenId', {
  100. appId: app.globalData.appId,
  101. code: res.code
  102. },
  103. ).then(res => {
  104. if (res.success == 1) {
  105. wx.setStorageSync('openId', res.data.openId)
  106. this.setData({
  107. isOpenId :res.data.openId
  108. })
  109. }
  110. })
  111. },
  112. fail: res => {
  113. wx.showToast({
  114. title: '提示',
  115. icon: 'none',
  116. content: '连接失败,请重试'
  117. })
  118. }
  119. })
  120. },
  121. loginBtn:function(){
  122. wx.showLoading({
  123. title: '正在登陆',
  124. mask: true
  125. })
  126. },
  127. //授权手机号
  128. getPhoneNumber: function (e) {
  129. const that = this;
  130. if (e.detail.errMsg == "getPhoneNumber:ok") {
  131. wx.showLoading({
  132. title: '正在登陆',
  133. mask: true
  134. })
  135. util.doPost(
  136. 'getPhoneNumber', {
  137. encryptedData: e.detail.encryptedData,
  138. iv: e.detail.iv,
  139. openId: wx.getStorageSync('openId'),
  140. }
  141. ).then(rs => {
  142. if (rs.success > 0) {
  143. if(that.data.isShow){
  144. wx.setStorageSync('phone', rs.data.phoneNumber);
  145. } else {
  146. wx.setStorageSync('phone', '13293333334');
  147. }
  148. this.autoLogin();
  149. } else {
  150. wx.showToast({
  151. title: '手机号验证失败',
  152. })
  153. }
  154. })
  155. }
  156. that.setData({
  157. model: false,
  158. localtion: true
  159. })
  160. },
  161. getName:function(e){
  162. this.setData({
  163. testName : e.detail.value
  164. })
  165. },
  166. getPwd:function(e){
  167. this.setData({
  168. testPwd : e.detail.value
  169. })
  170. },
  171. testLogin:function(){
  172. if(this.data.testName != '13293333334'){
  173. wx.showToast({
  174. title: '账号错误',
  175. icon: 'none'
  176. })
  177. return
  178. } else if(this.data.testPwd != '333334') {
  179. wx.showToast({
  180. title: '密码错误',
  181. icon: 'none'
  182. })
  183. return
  184. } else {
  185. wx.setStorageSync('phone', '13293333334')
  186. this.autoLogin()
  187. }
  188. },
  189. autoLogin: function () {
  190. var openId = ''
  191. if(this.data.isShow){
  192. openId = wx.getStorageSync('openId')
  193. } else {
  194. openId ='o9b5O5J24HLeDEJtZ2BfGXd8z6J4'
  195. }
  196. util.doPost(
  197. 'autoLogin', {
  198. openId ,
  199. }
  200. ).then(rs => {
  201. if (rs.success > 0) {
  202. wx.setStorageSync('sso-token', rs.data.token);
  203. wx.showToast({
  204. title: '登录成功',
  205. })
  206. if (this.data.isPage) {
  207. wx.redirectTo({
  208. url: '/pages/' + this.data.isPage + "/" + this.data.isPage + "?proId=" + this.data.isId,
  209. })
  210. } else {
  211. wx.redirectTo({
  212. url: '/pages/index/index'
  213. })
  214. }
  215. } else {
  216. wx.showToast({
  217. title: rs.errMsg,
  218. icon: 'none',
  219. duration: 3000
  220. })
  221. }
  222. })
  223. },
  224. /**
  225. * 生命周期函数--监听页面初次渲染完成
  226. */
  227. onReady: function () {
  228. },
  229. /**
  230. * 生命周期函数--监听页面显示
  231. */
  232. onShow: function () {
  233. },
  234. /**
  235. * 生命周期函数--监听页面隐藏
  236. */
  237. onHide: function () {
  238. },
  239. /**
  240. * 生命周期函数--监听页面卸载
  241. */
  242. onUnload: function () {
  243. },
  244. /**
  245. * 页面相关事件处理函数--监听用户下拉动作
  246. */
  247. onPullDownRefresh: function () {
  248. },
  249. /**
  250. * 页面上拉触底事件的处理函数
  251. */
  252. onReachBottom: function () {
  253. },
  254. /**
  255. * 用户点击右上角分享
  256. */
  257. onShareAppMessage: function () {
  258. }
  259. })