app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const { $Message} = require('/component/iView/base/index');
  2. const md5 = require('./utils/md5.js');
  3. const secret = "117c0743819088468e590246464cc75e"
  4. const {baseUrl, showMsg} = require("./utils/util.js");
  5. const util = require('./utils/util.js');
  6. App({
  7. globalData: {
  8. pageSize: 10,
  9. userInfo: {},
  10. departments: [],
  11. studyInfo: {},
  12. },
  13. onLaunch: function () {
  14. this.autoUpgrade()
  15. },
  16. autoUpgrade: function () {
  17. if (wx.canIUse('getUpdateManager')) {
  18. const updateManager = wx.getUpdateManager()
  19. updateManager.onCheckForUpdate(function (res) {
  20. // 请求完新版本信息的回调
  21. if (res.hasUpdate) {
  22. console.log('res.hasUpdate====')
  23. updateManager.onUpdateReady(function () {
  24. wx.showModal({
  25. title: '更新提示',
  26. content: '新版本已经准备好,是否重启应用?',
  27. success: function (res) {
  28. console.log('success====', res)
  29. if (res.confirm) {
  30. updateManager.applyUpdate()
  31. }
  32. }
  33. })
  34. })
  35. updateManager.onUpdateFailed(function () {
  36. // 新的版本下载失败
  37. wx.showModal({
  38. title: '已经有新版本了哟~',
  39. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  40. })
  41. })
  42. }
  43. })
  44. }
  45. },
  46. gotoLogin: function(){
  47. console.log("gologin")
  48. wx.navigateTo({
  49. url: `/pages/user/identify/index`,
  50. })
  51. },
  52. checkLogin: function (cb) {
  53. let userInfo = this.globalData.userInfo
  54. if( userInfo && userInfo.userId ){
  55. cb && cb( userInfo)
  56. return;
  57. }
  58. let _this = this
  59. let token = wx.getStorageSync('@token');
  60. if(!token) return cb && cb({});
  61. wx.login({
  62. success(wxres) {
  63. if( !wxres.code){
  64. util.showMsg("登入失败")
  65. }else{
  66. _this.doLoginByCode( wxres.code, cb )
  67. }
  68. }
  69. })
  70. },
  71. doLoginByCode( code, cb ){
  72. let param = {code};
  73. let _this = this;
  74. _this.formPost('Auth.wxLogin', param).then( res =>{
  75. if (res.code == 200) {
  76. _this.setUserInfo(res.data);
  77. cb && cb(res.data);
  78. }else{
  79. return cb && cb({code:401});
  80. }
  81. })
  82. },
  83. message: function (content, type) {
  84. $Message({
  85. content: content,
  86. type: type
  87. });
  88. },
  89. formPost: function (action, data) {
  90. let _this = this
  91. let userInfo = this.globalData.userInfo||{}
  92. let user_id = userInfo.userId||0;
  93. let token = userInfo.token||'';
  94. let timestamp = Date.now()
  95. action = action.toLowerCase()
  96. let signstr = `weixin_${token}_${action}_${timestamp}_${secret}`
  97. let signsure = md5.md5(signstr).toLowerCase()
  98. let headers = {
  99. 'Content-Type': 'application/json',
  100. 'x-signsure': signsure,
  101. 'x-timestamp': timestamp,
  102. 'x-userId': user_id
  103. }
  104. return new Promise(function (resolve, reject) {
  105. wx.showNavigationBarLoading();
  106. wx.request({
  107. url: `${baseUrl}weixin/${action}`,
  108. header: headers,
  109. method: 'POST',
  110. data,
  111. success(res) {
  112. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  113. reject('网络出错')
  114. return false;
  115. }
  116. if(res.data.code != 200 && res.data.code != 401){
  117. util.showMsg(res.data.msg )
  118. }
  119. if( res.data.code == 401 && action.indexOf( "auth.") == -1 ){
  120. _this.gotoLogin()
  121. return
  122. }
  123. resolve( res.data );
  124. return true;
  125. },
  126. fail(res) {
  127. reject(res.msg)
  128. return false;
  129. },
  130. complete(res) {
  131. wx.hideNavigationBarLoading();
  132. }
  133. })
  134. })
  135. },
  136. getSystemInfo( cb ){
  137. wx.getSystemInfo({
  138. success: function(res) {
  139. cb &&cb( res.system )
  140. }
  141. })
  142. },
  143. checkNavigateTo: function(url){
  144. let token = wx.getStorageSync('@token');
  145. if( !token ) return this.gotoLogin()
  146. wx.navigateTo({url})
  147. },
  148. setUserInfo: function (user) {
  149. this.globalData.userInfo = user;
  150. wx.setStorageSync('@token', user.token)
  151. }
  152. })