request.js 5.2 KB

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