request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(!res.data.code){
  52. throw Error(res.data);
  53. }
  54. if (_checkTokenValid(res)) {
  55. return res
  56. }
  57. throw Error(res.data.msg);
  58. }
  59. async function postRequest(url, data ) {
  60. const datetime = Math.floor(Date.now() / 1000)
  61. data.time = datetime.toString()
  62. let jsonStr = JSON.stringify(selfSort(data))
  63. var encrypted = crypto.SHA256(jsonStr)
  64. var sign = encrypted.toString()
  65. var token = storage.getUserToken()
  66. // 申请拖车服务接口加密签名校验
  67. // if (url === config.API_DAYHIRE_DRAG_CAR_SERVICE) {
  68. data.sign = sign
  69. // }
  70. let res = await uni.request({
  71. url: url,
  72. data: data,
  73. header: {
  74. 'content-type': 'application/json',
  75. 'X-Token': token
  76. },
  77. method: 'POST',
  78. });
  79. res = res[1]
  80. wx.hideLoading();
  81. if(!res.data.code){
  82. throw Error(res.data);
  83. }
  84. if (res.data.code == 200) {
  85. res.succeed = true
  86. res.body = res.data
  87. return res
  88. }
  89. if (_checkTokenValid(res)) {
  90. return res
  91. }
  92. throw Error(res.data.msg);
  93. }
  94. function selfSort(data) {
  95. // console.log(data,"签名数据")
  96. let sortedKeys = Object.keys(data).sort((a, b) => {
  97. //对key 进行翻转
  98. let reversedA = a.split("").reverse().join("");
  99. let reversedB = b.split("").reverse().join("");
  100. let len = reversedA.length > reversedB.length ? reversedA.length : reversedB.length
  101. for (let i = 0; i < len; i++) {
  102. if (typeof(reversedA[i]) == 'undefined') {
  103. return 1;
  104. }
  105. if (typeof(reversedB[i]) == 'undefined') {
  106. return -1;
  107. }
  108. if (reversedA[i] > reversedB[i]) {
  109. return -1;
  110. } else if (reversedB[i] > reversedA[i]) {
  111. return 1;
  112. }
  113. }
  114. return 1
  115. }) // 对属性数组进行排序
  116. let sortedJson = {}; // 重构排序后的JSON对象
  117. sortedKeys.forEach(key => {
  118. sortedJson[key] = data[key];
  119. })
  120. return sortedJson
  121. }
  122. async function getApi(url, data) {
  123. var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
  124. data.token = token;
  125. data.v = config.APP_VERSION;
  126. data.appid = appid
  127. if(!data.from){
  128. data.from = 'app'
  129. //#ifdef MP-ALIPAY
  130. data.from = 'ali'
  131. //#endif
  132. //#ifdef MP-WEIXIN
  133. data.from = 'wx'
  134. //#endif
  135. }
  136. data.language = language
  137. return await getRequest(url, data);
  138. }
  139. async function postApi(url, data) {
  140. var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
  141. console.log(data, 'token');
  142. data.token = token;
  143. data.v = config.APP_VERSION;
  144. data.appid = appid
  145. if(!data.from){
  146. data.from = 'app'
  147. //#ifdef MP-ALIPAY
  148. data.from = 'ali'
  149. //#endif
  150. //#ifdef MP-WEIXIN
  151. data.from = 'wx'
  152. //#endif
  153. }
  154. data.language = language
  155. return await postRequest(url, data);
  156. }
  157. /**
  158. * 上报formid
  159. */
  160. async function reportFormId(formId) {
  161. const accountInfo = uni.getAccountInfoSync();
  162. var postData = {
  163. formId: formId,
  164. appid
  165. };
  166. await postApi(config.API_FORMID_REPORT, postData);
  167. }
  168. async function getAppConfig() {
  169. const pData = {
  170. appid,
  171. terminal: 'wx_app'
  172. };
  173. let resp = await getApi(config.API_INDEX_APP_CONFIG, pData);
  174. if (resp.data.code === 200) {
  175. const appConfig = resp.data.data.appConfig;
  176. storage.setAppConfig(appConfig);
  177. //#ifdef MP-ALIPAY
  178. uni.setNavigationBarTitle({
  179. title: ''
  180. });
  181. //#endif
  182. //#ifdef MP-WEIXIN
  183. wx.setNavigationBarTitle({
  184. title: appConfig.app_name
  185. });
  186. //#endif
  187. return appConfig
  188. } else {}
  189. }
  190. module.exports = {
  191. getRequest: getRequest,
  192. postRequest: postRequest,
  193. getApi: getApi,
  194. postApi: postApi,
  195. reportFormId: reportFormId,
  196. getAppConfig: getAppConfig
  197. };