app.js 4.6 KB

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