app.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if( this.globalData.userInfo && this.globalData.userInfo.userId ){
  54. cb&&cb( this.globalData.userInfo );
  55. }else{
  56. let token = wx.getStorageSync('@token');
  57. console.log("get token", token)
  58. if( !token ) {
  59. this.gotoLogin()
  60. return;
  61. }
  62. this.formPost('weixin/info', {}).then( res =>{
  63. if (res.code == 200) {
  64. this.setUserInfo(res.data);
  65. cb && cb( this.globalData.userInfo )
  66. }else{
  67. this.gotoLogin()
  68. }
  69. })
  70. }
  71. },
  72. message: function (content, type) {
  73. $Message({
  74. content: content,
  75. type: type
  76. });
  77. },
  78. formPost: function (action, data, showErr= true) {
  79. let uid = wx.getStorageSync('@userId')||0;
  80. let token = wx.getStorageSync('@token')||'';
  81. let timestamp = Date.now()
  82. let signstr = `weixin_${uid}_${token}_${timestamp}_${secret}`
  83. let signsure = md5.md5(signstr).toLowerCase()
  84. let headers = {
  85. 'Content-Type': 'application/json',
  86. 'x-token': token,
  87. 'x-signsure': signsure,
  88. 'x-timestamp': timestamp,
  89. 'x-user-id': uid
  90. }
  91. return new Promise(function (resolve, reject) {
  92. wx.showNavigationBarLoading();
  93. wx.request({
  94. url: `${baseUrl}api/${action}`,
  95. header: headers,
  96. method: 'POST',
  97. data,
  98. success(res) {
  99. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  100. showMsg( "请求异常" )
  101. }
  102. let newToken = res.header["new-token"];
  103. if( newToken ) {
  104. wx.setStorageSync( "@token", newToken )
  105. }
  106. if( res.data.data && res.data.data.reload ){
  107. wx.clearStorageSync("@token")
  108. wx.navigateTo({
  109. url: '/pages/user/identify/index',
  110. })
  111. }else if( res.data.code != 200 && showErr ){
  112. util.showMsg(res.data.msg)
  113. }
  114. resolve(res.data);
  115. },
  116. fail(res) {
  117. showMsg( "请求错误" )
  118. resolve({});
  119. },
  120. complete(res) {
  121. wx.hideNavigationBarLoading();
  122. }
  123. })
  124. })
  125. },
  126. getSystemInfo( cb ){
  127. wx.getSystemInfo({
  128. success: function(res) {
  129. cb &&cb( res.system )
  130. }
  131. })
  132. },
  133. checkNavigateTo: function(url){
  134. let openid = wx.getStorageSync('@openid');
  135. if( !openid ) return this.gotoLogin()
  136. wx.navigateTo({url})
  137. },
  138. getDepartments: function( cb ){
  139. if( this.globalData.departments.length > 0 ){
  140. cb&&cb( this.globalData.departments )
  141. }else{
  142. this.loadDepartments( cb )
  143. }
  144. },
  145. loadDepartments: function( cb ){
  146. this.formPost( 'base/loadDepartmentList', {}).then( res=>
  147. {
  148. if( res.code == 200){
  149. this.globalData.departments = res.data;
  150. cb && cb( res.data );
  151. }
  152. }
  153. )
  154. },
  155. setUserInfo: function (info) {
  156. let {token, user} = info
  157. console.log("setUserInfo", token, user)
  158. this.globalData.userInfo = user;
  159. wx.setStorageSync('@token', token)
  160. wx.setStorageSync('@userId', user.userId )
  161. this.loadDepartments()
  162. }
  163. })