app.js 4.3 KB

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