app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. const { $Message} = require('/component/iView/base/index');
  2. const md5 = require('./utils/md5.js');
  3. const secret = "117c0743819088468e590246464cc75e"
  4. const {baseUrl} = require("./utils/util.js");
  5. const util = require('./utils/util.js');
  6. App({
  7. globalData: {
  8. pageSize: 10,
  9. userInfo: {},
  10. studyInfo: {},
  11. },
  12. onLaunch: function () {
  13. this.autoUpgrade()
  14. },
  15. autoUpgrade: function () {
  16. if (wx.canIUse('getUpdateManager')) {
  17. const updateManager = wx.getUpdateManager()
  18. updateManager.onCheckForUpdate(function (res) {
  19. // 请求完新版本信息的回调
  20. if (res.hasUpdate) {
  21. console.log('res.hasUpdate====')
  22. updateManager.onUpdateReady(function () {
  23. wx.showModal({
  24. title: '更新提示',
  25. content: '新版本已经准备好,是否重启应用?',
  26. success: function (res) {
  27. console.log('success====', res)
  28. if (res.confirm) {
  29. updateManager.applyUpdate()
  30. }
  31. }
  32. })
  33. })
  34. updateManager.onUpdateFailed(function () {
  35. // 新的版本下载失败
  36. wx.showModal({
  37. title: '已经有新版本了哟~',
  38. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  39. })
  40. })
  41. }
  42. })
  43. }
  44. },
  45. gotoLogin: function(){
  46. wx.navigateTo({
  47. url: `/pages/user/identify/index`,
  48. })
  49. },
  50. checkLogin: function (cb) {
  51. let userInfo = this.globalData.userInfo
  52. if( userInfo && userInfo.uid ){
  53. cb && cb( userInfo)
  54. return;
  55. }
  56. let _this = this
  57. let openid = wx.getStorageSync('@openid');
  58. if( !openid ) {
  59. return
  60. }
  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. }
  67. })
  68. },
  69. //
  70. message: function (content, type="info") {
  71. $Message({
  72. content: content,
  73. type: type
  74. });
  75. },
  76. formPost: function (action, data) {
  77. let _this = this
  78. let userInfo = this.globalData.userInfo||{}
  79. let user_id = userInfo.uid||0;
  80. let token = userInfo.token||'';
  81. let timestamp = Date.now()
  82. action = action.toLowerCase()
  83. let signstr = `weixin_${token}_${action}_${timestamp}_${secret}`
  84. let signsure = md5.md5(signstr).toLowerCase()
  85. let headers = {
  86. 'Content-Type': 'application/json',
  87. 'x-signsure': signsure,
  88. 'x-timestamp': timestamp,
  89. 'x-userId': user_id
  90. }
  91. return new Promise(function (resolve, reject) {
  92. wx.showNavigationBarLoading();
  93. wx.request({
  94. url: `${baseUrl}weixin/${action}`,
  95. header: headers,
  96. method: 'POST',
  97. data,
  98. success(res) {
  99. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  100. reject('网络出错')
  101. return false;
  102. }
  103. if(res.data.code != 200 && res.data.code != 401){
  104. util.showMsg(res.data.msg )
  105. }
  106. if( res.data.code == 401 && action.indexOf( "auth.") == -1 ){
  107. _this.gotoLogin()
  108. return
  109. }
  110. resolve(res.data);
  111. return true;
  112. },
  113. fail(res) {
  114. reject(res.msg)
  115. return false;
  116. },
  117. complete(res) {
  118. wx.hideNavigationBarLoading();
  119. }
  120. })
  121. })
  122. },
  123. studyPost: function (action, data) {
  124. let {token, uid } = this.globalData.userInfo.studyUser;
  125. let version = "1.0"
  126. let _this = this
  127. let mtime = parseInt(Date.now() / 1000)
  128. action = action.toLowerCase()
  129. let body = JSON.stringify(data);
  130. let signstr = `hall_${version}${token}${body}${action}${mtime}${secret}`
  131. let sign = md5.md5(signstr).toLowerCase()
  132. let headers = {
  133. 'Content-Type': 'application/json'
  134. }
  135. let studyUrl = `${baseUrl}rental/${method}?t=${mtime}&u=${uid}&v=${version}&s=${sign}`
  136. return new Promise(function (resolve, reject) {
  137. wx.showNavigationBarLoading();
  138. wx.request({
  139. url: studyUrl,
  140. header: headers,
  141. method: 'POST',
  142. data,
  143. success(res) {
  144. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  145. reject('网络出错')
  146. return false;
  147. }
  148. resolve(res.data);
  149. return true;
  150. },
  151. fail(res) {
  152. reject(res.errMsg)
  153. return false;
  154. },
  155. complete(res) {
  156. wx.hideNavigationBarLoading();
  157. }
  158. })
  159. })
  160. },
  161. getSystemInfo( cb ){
  162. wx.getSystemInfo({
  163. success: function(res) {
  164. cb &&cb( res.system )
  165. }
  166. })
  167. },
  168. checkNavigateTo: function(url){
  169. let openid = wx.getStorageSync('@openid');
  170. if( !openid ) return this.gotoLogin()
  171. wx.navigateTo({url})
  172. },
  173. setUserInfo: function (userInfo) {
  174. this.globalData.userInfo = userInfo;
  175. if( userInfo.openid ){
  176. console.log("@openid", userInfo.openid)
  177. wx.setStorageSync('@openid', userInfo.openid )
  178. }
  179. wx.setStorageSync('@user', userInfo )
  180. }
  181. })