http.js 5.3 KB

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