request.js 4.9 KB

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