http.js 5.4 KB

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