app.js 5.2 KB

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