app.js 5.1 KB

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