app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.uid ){
  55. cb && cb( userInfo)
  56. return;
  57. }
  58. let _this = this
  59. let openid = wx.getStorageSync('@openid');
  60. if( !openid ) return cb && cb({code:401});
  61. let param = {"openid": openid};
  62. _this.formPost('Auth.wxLogin', param).then( res =>{
  63. if (res.code == 200) {
  64. _this.setUserInfo(res.data);
  65. cb && cb(res.data);
  66. }else{
  67. return cb && cb({code:401});
  68. }
  69. })
  70. },
  71. message: function (content, type) {
  72. $Message({
  73. content: content,
  74. type: type
  75. });
  76. },
  77. formPost: function (action, data) {
  78. let _this = this
  79. let userInfo = this.globalData.userInfo||{}
  80. let user_id = userInfo.uid||0;
  81. let token = userInfo.token||'';
  82. let timestamp = Date.now()
  83. action = action.toLowerCase()
  84. let signstr = `weixin_${token}_${action}_${timestamp}_${secret}`
  85. let signsure = md5.md5(signstr).toLowerCase()
  86. let headers = {
  87. 'Content-Type': 'application/json',
  88. 'x-signsure': signsure,
  89. 'x-timestamp': timestamp,
  90. 'x-userId': user_id
  91. }
  92. return new Promise(function (resolve, reject) {
  93. wx.showNavigationBarLoading();
  94. wx.request({
  95. url: `${baseUrl}weixin/${action}`,
  96. header: headers,
  97. method: 'POST',
  98. data,
  99. success(res) {
  100. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  101. reject('网络出错')
  102. return false;
  103. }
  104. if(res.data.code != 200 && res.data.code != 401){
  105. util.showMsg(res.data.msg )
  106. }
  107. if( res.data.code == 401 && action.indexOf( "auth.") == -1 ){
  108. _this.gotoLogin()
  109. return
  110. }
  111. resolve(res.data);
  112. return true;
  113. },
  114. fail(res) {
  115. reject(res.msg)
  116. return false;
  117. },
  118. complete(res) {
  119. wx.hideNavigationBarLoading();
  120. }
  121. })
  122. })
  123. },
  124. getSystemInfo( cb ){
  125. wx.getSystemInfo({
  126. success: function(res) {
  127. cb &&cb( res.system )
  128. }
  129. })
  130. },
  131. checkNavigateTo: function(url){
  132. let openid = wx.getStorageSync('@openid');
  133. if( !openid ) return this.gotoLogin()
  134. wx.navigateTo({url})
  135. },
  136. setUserInfo: function (info) {
  137. let {token, user} = info
  138. console.log("setUserInfo", token, user)
  139. this.globalData.userInfo = user;
  140. wx.setStorageSync('@token', token)
  141. wx.setStorageSync('@userId', user.userId )
  142. this.loadDepartments()
  143. }
  144. })