request.js 5.2 KB

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