request.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // import common = require("./common");
  2. // const appid = 'wxefa2810018787a41'
  3. const appid = '487987498'
  4. const language = 'zh'
  5. var common = require('./common.js');
  6. var storage = require('./storage.js');
  7. var config = require('./config.js'); // 检查token是否失效 如失效 清除缓存 提示重新登录
  8. // 402: 解析token出错 403: token格式错误 404: 公共声明中缺少必要字段 405: token验证失败
  9. import crypto from '@/node_modules/crypto-js';
  10. function _checkTokenValid(res) {
  11. if (
  12. res.data.code === 402 ||
  13. res.data.code === 403 ||
  14. res.data.code === 404 ||
  15. res.data.code === 405 ||
  16. res.data.code === 300 ||
  17. res.data.code === 302 ||
  18. res.data.code === 303 ||
  19. res.data.code === 304 ||
  20. res.data.code === 305 ||
  21. res.data.code === 306
  22. ) {
  23. //wx.clearStorageSync();
  24. //TODO
  25. // var shareCode = storage.getShareCode();
  26. // storage.clearStorage();
  27. // storage.setShareCode(shareCode);
  28. uni.reLaunch({
  29. url: '/pages/index/index'
  30. });
  31. return false;
  32. }
  33. return true;
  34. }
  35. async function getRequest(url, data) {
  36. // function getRequest(url1, data, successCallBack, failCallBack) {
  37. // if (!failCallBack) failCallBack = function() {};
  38. // var url=url1?url1:''
  39. if (url.indexOf('?') > 0) {
  40. url = url + '&' + common.obj2UrlQuery(data);
  41. } else {
  42. url = '?' + common.obj2UrlQuery(data);
  43. // return
  44. }
  45. let res = await uni.request({
  46. url: url,
  47. method: 'GET',
  48. });
  49. uni.hideLoading();
  50. res = res[1]
  51. if (_checkTokenValid(res)) {
  52. return res
  53. }
  54. throw Error(res.data.msg);
  55. }
  56. async function postRequest(url, data ) {
  57. const datetime = Math.floor(Date.now() / 1000)
  58. data.time = datetime.toString()
  59. let jsonStr = JSON.stringify(selfSort(data))
  60. var encrypted = crypto.SHA256(jsonStr)
  61. var sign = encrypted.toString()
  62. var token = storage.getUserToken()
  63. // 申请拖车服务接口加密签名校验
  64. // if (url === config.API_DAYHIRE_DRAG_CAR_SERVICE) {
  65. data.sign = sign
  66. // }
  67. let res = await uni.request({
  68. url: url,
  69. data: data,
  70. header: {
  71. 'content-type': 'application/json',
  72. 'X-Token': token
  73. },
  74. method: 'POST',
  75. });
  76. res = res[1]
  77. wx.hideLoading();
  78. if (res.data.code == 200) {
  79. res.succeed = true
  80. res.body = res.data
  81. return res
  82. }
  83. if (_checkTokenValid(res)) {
  84. return res
  85. }
  86. throw Error(res.data.msg);
  87. }
  88. function selfSort(data) {
  89. // console.log(data,"签名数据")
  90. let sortedKeys = Object.keys(data).sort((a, b) => {
  91. //对key 进行翻转
  92. let reversedA = a.split("").reverse().join("");
  93. let reversedB = b.split("").reverse().join("");
  94. let len = reversedA.length > reversedB.length ? reversedA.length : reversedB.length
  95. for (let i = 0; i < len; i++) {
  96. if (typeof(reversedA[i]) == 'undefined') {
  97. return 1;
  98. }
  99. if (typeof(reversedB[i]) == 'undefined') {
  100. return -1;
  101. }
  102. if (reversedA[i] > reversedB[i]) {
  103. return -1;
  104. } else if (reversedB[i] > reversedA[i]) {
  105. return 1;
  106. }
  107. }
  108. return 1
  109. }) // 对属性数组进行排序
  110. let sortedJson = {}; // 重构排序后的JSON对象
  111. sortedKeys.forEach(key => {
  112. sortedJson[key] = data[key];
  113. })
  114. return sortedJson
  115. }
  116. async function getApi(url, data) {
  117. var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
  118. data.token = token;
  119. data.v = config.APP_VERSION;
  120. data.appid = appid
  121. if(!data.from){
  122. data.from = 'app'
  123. //#ifdef MP-ALIPAY
  124. data.from = 'ali'
  125. //#endif
  126. //#ifdef MP-WEIXIN
  127. data.from = 'wx'
  128. //#endif
  129. }
  130. data.language = language
  131. return await getRequest(url, data);
  132. }
  133. async function postApi(url, data) {
  134. var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
  135. console.log(data, 'token');
  136. data.token = token;
  137. data.v = config.APP_VERSION;
  138. data.appid = appid
  139. if(!data.from){
  140. data.from = 'app'
  141. //#ifdef MP-ALIPAY
  142. data.from = 'ali'
  143. //#endif
  144. //#ifdef MP-WEIXIN
  145. data.from = 'wx'
  146. //#endif
  147. }
  148. data.language = language
  149. return await postRequest(url, data);
  150. }
  151. /**
  152. * 上报formid
  153. */
  154. async function reportFormId(formId) {
  155. const accountInfo = uni.getAccountInfoSync();
  156. var postData = {
  157. formId: formId,
  158. appid
  159. };
  160. await postApi(config.API_FORMID_REPORT, postData);
  161. }
  162. async function getAppConfig() {
  163. const pData = {
  164. appid,
  165. terminal: 'wx_app'
  166. };
  167. let resp = await getApi(config.API_INDEX_APP_CONFIG, pData);
  168. if (resp.data.code === 200) {
  169. const appConfig = resp.data.data.appConfig;
  170. storage.setAppConfig(appConfig);
  171. //#ifdef MP-ALIPAY
  172. uni.setNavigationBarTitle({
  173. title: ''
  174. });
  175. //#endif
  176. //#ifdef MP-WEIXIN
  177. wx.setNavigationBarTitle({
  178. title: appConfig.app_name
  179. });
  180. //#endif
  181. return appConfig
  182. } else {}
  183. }
  184. module.exports = {
  185. getRequest: getRequest,
  186. postRequest: postRequest,
  187. getApi: getApi,
  188. postApi: postApi,
  189. reportFormId: reportFormId,
  190. getAppConfig: getAppConfig
  191. };