app.js 3.7 KB

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