app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. wx.navigateTo({
  60. url: `/pages/user/identify/index`,
  61. })
  62. return
  63. }
  64. let param = {"openid": openid};
  65. _this.formPost('Auth.wxLogin', param).then( res =>{
  66. if (res.code == 200) {
  67. _this.setUserInfo(res.data);
  68. cb && cb(res.data);
  69. }
  70. })
  71. },
  72. //
  73. message: function (content, type="info") {
  74. $Message({
  75. content: content,
  76. type: type
  77. });
  78. },
  79. formPost: function (action, data) {
  80. let _this = this
  81. let userInfo = this.globalData.userInfo||{}
  82. let user_id = userInfo.uid||0;
  83. let token = userInfo.token||'';
  84. let timestamp = Date.now()
  85. action = action.toLowerCase()
  86. let signstr = `weixin_${token}_${action}_${timestamp}_${secret}`
  87. let signsure = md5.md5(signstr).toLowerCase()
  88. let headers = {
  89. 'Content-Type': 'application/json',
  90. 'x-signsure': signsure,
  91. 'x-timestamp': timestamp,
  92. 'x-userId': user_id
  93. }
  94. return new Promise(function (resolve, reject) {
  95. wx.showNavigationBarLoading();
  96. wx.request({
  97. url: `${baseUrl}weixin/${action}`,
  98. header: headers,
  99. method: 'POST',
  100. data,
  101. success(res) {
  102. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  103. reject('网络出错')
  104. return false;
  105. }
  106. if(res.data.code != 200 && res.data.code != 401){
  107. util.showMsg(res.data.msg )
  108. }
  109. if( res.data.code == 401 && action.indexOf( "auth.") == -1 ){
  110. _this.gotoLogin()
  111. return
  112. }
  113. resolve(res.data);
  114. return true;
  115. },
  116. fail(res) {
  117. reject(res.msg)
  118. return false;
  119. },
  120. complete(res) {
  121. wx.hideNavigationBarLoading();
  122. }
  123. })
  124. })
  125. },
  126. studyPost: function (action, data) {
  127. let {token, uid } = this.globalData.userInfo.studyUser;
  128. let version = "1.0"
  129. let _this = this
  130. let mtime = parseInt(Date.now() / 1000)
  131. action = action.toLowerCase()
  132. let body = JSON.stringify(data);
  133. let signstr = `hall_${version}${token}${body}${action}${mtime}${secret}`
  134. let sign = md5.md5(signstr).toLowerCase()
  135. let headers = {
  136. 'Content-Type': 'application/json'
  137. }
  138. let studyUrl = `${baseUrl}rental/${method}?t=${mtime}&u=${uid}&v=${version}&s=${sign}`
  139. return new Promise(function (resolve, reject) {
  140. wx.showNavigationBarLoading();
  141. wx.request({
  142. url: studyUrl,
  143. header: headers,
  144. method: 'POST',
  145. data,
  146. success(res) {
  147. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  148. reject('网络出错')
  149. return false;
  150. }
  151. resolve(res.data);
  152. return true;
  153. },
  154. fail(res) {
  155. reject(res.errMsg)
  156. return false;
  157. },
  158. complete(res) {
  159. wx.hideNavigationBarLoading();
  160. }
  161. })
  162. })
  163. },
  164. getSystemInfo( cb ){
  165. wx.getSystemInfo({
  166. success: function(res) {
  167. cb &&cb( res.system )
  168. }
  169. })
  170. },
  171. checkNavigateTo: function(url){
  172. let openid = wx.getStorageSync('@openid');
  173. if( !openid ) return this.gotoLogin()
  174. wx.navigateTo({url})
  175. },
  176. setUserInfo: function (userInfo) {
  177. this.globalData.userInfo = userInfo;
  178. if( userInfo.openid ){
  179. console.log("@openid", userInfo.openid)
  180. wx.setStorageSync('@openid', userInfo.openid )
  181. wx.setStorageSync('@user', userInfo )
  182. }else{
  183. wx.clearStorageSync("@openid")
  184. wx.clearStorageSync("@user")
  185. }
  186. }
  187. })