encryption.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //全局网络链接
  2. export const GetNetworkUrl = () => {
  3. let networkUrl = 'https://app.taxbk.cn:9443';
  4. return networkUrl;
  5. };
  6. //POST请求数据
  7. //参数:url:链接地址
  8. // token: Token值
  9. // jason:请求参数
  10. // category:是否加密,true:加密,false:不加密
  11. // types:请求类型,1:post请求,2:get请求
  12. export const GetDataPost = (url, token, jason, category, types) => {
  13. url = GetNetworkUrl() + url;
  14. const fetchPostOption = {
  15. method: 'POST',
  16. headers: {
  17. Accept: 'application/json',
  18. 'Content-Type': 'application/json',
  19. token: token,
  20. },
  21. body: JSON.stringify(jason),
  22. };
  23. const fetchGetOption = {
  24. method: 'GET',
  25. headers: {
  26. Accept: 'application/json',
  27. 'Content-Type': 'application/json',
  28. token: token,
  29. },
  30. };
  31. if (types == 1) {
  32. if (category) {
  33. return fetch(url, fetchPostOption)
  34. .then(response => response.json())
  35. .then(responseJson => {
  36. return responseJson;
  37. })
  38. .catch(error => {
  39. return {code: -1, msg: '服务器故障!'};
  40. });
  41. } else {
  42. return fetch(url, fetchPostOption)
  43. .then(response => response.json())
  44. .then(responseJson => {
  45. return responseJson;
  46. })
  47. .catch(error => {
  48. return {code: -1, msg: '服务器故障!'};
  49. });
  50. }
  51. } else {
  52. //GET请求数据
  53. if (jason) {
  54. let paramsArray = [];
  55. //拼接参数
  56. Object.keys(jason).forEach(key =>
  57. paramsArray.push(key + '=' + jason[key]),
  58. );
  59. if (url.search(/\?/) === -1) {
  60. url += '?' + paramsArray.join('&');
  61. } else {
  62. url += '&' + paramsArray.join('&');
  63. }
  64. }
  65. try {
  66. return fetch(url, fetchGetOption)
  67. .then(response => response.json())
  68. .then(responseJson => {
  69. return responseJson;
  70. });
  71. } catch (e) {
  72. return {code: -1, msg: '服务器故障!'};
  73. }
  74. }
  75. };
  76. //Jason序列化
  77. //参数:jason:jason的字符串
  78. export const JasonSerialize = jason => {
  79. let times = '201909091222123';
  80. jason = jason + times;
  81. return jason;
  82. };
  83. //POST图片上传
  84. //参数:url:链接地址
  85. // token: Token值
  86. // params:请求参数
  87. // image:图片信息
  88. // types:请求类型,1:post请求,2:get请求
  89. export const UploadImage = (url, token, params, image) => {
  90. url = GetNetworkUrl() + url;
  91. return new Promise(function(resolve, reject) {
  92. let formData = new FormData();
  93. for (var key in params) {
  94. formData.append(key, params[key]);
  95. }
  96. let file = {
  97. uri: image.path,
  98. type: 'application/octet-stream',
  99. name: 'image.jpg',
  100. };
  101. formData.append('file', file);
  102. fetch(url, {
  103. method: 'POST',
  104. headers: {
  105. 'Content-Type': 'multipart/form-data;charset=utf-8',
  106. Authorization: token,
  107. },
  108. body: formData,
  109. })
  110. .then(response => response.json())
  111. .then(responseData => {
  112. resolve(responseData);
  113. })
  114. .catch(err => {
  115. reject(err);
  116. });
  117. });
  118. };
  119. //通过url获取服务器jason数据
  120. //参数:url:地址链接
  121. //返回值:string
  122. // export const GetData = (url, jason) => {
  123. // const fetchGetOption = {
  124. // method: 'POST',
  125. // headers: {
  126. // Accept: 'application/json',
  127. // 'Content-Type': 'application/json',
  128. // token: GetToken(),
  129. // },
  130. // body: JSON.stringify(jason),
  131. // };
  132. // return fetch(url)
  133. // .then(response => response.json())
  134. // .then(responseJson => {
  135. // return responseJson.movies;
  136. // })
  137. // .catch(error => {
  138. // return error;
  139. // });
  140. // };