encryption.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //全局网络链接
  2. import {IndividualStorageData, RetrieveData} from './storage';
  3. export const GetNetworkUrl = () => {
  4. let networkUrl = 'https://www.chtax.cn/api';
  5. // let networkUrl = 'http://192.168.1.173:50009';
  6. return networkUrl;
  7. };
  8. // 网络请求
  9. export const RequestNetwork = async (url, token, jason, category, types) => {
  10. if (token !== null) {
  11. token = await RetrieveData('token');
  12. token = await UpdateToken(token);
  13. return await GetDataPost(url, token, jason, category, types);
  14. } else {
  15. return await GetDataPost(url, token, jason, category, types);
  16. }
  17. };
  18. // 网络请求更新token
  19. export const UpdateToken = async (token) => {
  20. let account = await RetrieveData('account');
  21. let url = '/auth/comm/user/refreshToken';
  22. let res = await GetDataPost(
  23. url,
  24. token,
  25. {
  26. account: account,
  27. reqChannel: 3,
  28. },
  29. false,
  30. 1,
  31. );
  32. if (res) {
  33. if (res.code === 0) {
  34. let token = res.data.token;
  35. await IndividualStorageData('token', token);
  36. return token;
  37. }
  38. }
  39. };
  40. //POST请求数据
  41. //参数:url:链接地址
  42. // token: Token值
  43. // jason:请求参数
  44. // category:是否加密,true:加密,false:不加密
  45. // types:请求类型,1:post请求,2:get请求
  46. export const GetDataPost = async (url, token, jason, category, types) => {
  47. url = GetNetworkUrl() + url;
  48. const fetchPostOption = {
  49. method: 'POST',
  50. headers: {
  51. Accept: 'application/json',
  52. 'Content-Type': 'application/json',
  53. token: token,
  54. },
  55. body: JSON.stringify(jason),
  56. };
  57. const fetchGetOption = {
  58. method: 'GET',
  59. headers: {
  60. Accept: 'application/json',
  61. 'Content-Type': 'application/json',
  62. token: token,
  63. },
  64. };
  65. if (types === 1) {
  66. if (category) {
  67. return fetch(url, fetchPostOption)
  68. .then((response) => {
  69. response.json();
  70. })
  71. .then((responseJson) => {
  72. return RequestStatus(responseJson);
  73. })
  74. .catch((error) => {
  75. return {code: -1, msg: '网络异常,请稍后再试!'};
  76. });
  77. // let response = fetch(url, fetchPostOption);
  78. } else {
  79. return fetch(url, fetchPostOption)
  80. .then((response) => {
  81. return response.json();
  82. })
  83. .then((responseJson) => {
  84. return RequestStatus(responseJson);
  85. })
  86. .catch((error) => {
  87. return {code: -1, msg: '网络异常,请稍后再试!'};
  88. });
  89. }
  90. } else {
  91. //GET请求数据
  92. if (jason) {
  93. let paramsArray = [];
  94. //拼接参数
  95. Object.keys(jason).forEach((key) =>
  96. paramsArray.push(key + '=' + jason[key]),
  97. );
  98. if (url.search(/\?/) === -1) {
  99. url += '?' + paramsArray.join('&');
  100. } else {
  101. url += '&' + paramsArray.join('&');
  102. }
  103. }
  104. try {
  105. return fetch(url, fetchGetOption)
  106. .then((response) => {
  107. return response.json();
  108. })
  109. .then((responseJson) => {
  110. return RequestStatus(responseJson);
  111. });
  112. } catch (e) {
  113. return {code: -1, msg: '网络异常,请稍后再试!'};
  114. }
  115. }
  116. };
  117. // 处理请求异常返回信息
  118. // 参数:jason: 传入参数
  119. export const RequestStatus = (jason) => {
  120. if (jason.ok) {
  121. return jason;
  122. } else {
  123. if (jason.status === 401) {
  124. return {code: 401, msg: '登录失效,请重新登录!'};
  125. }
  126. if (jason.status === 403) {
  127. return {code: 403, msg: '暂无权限操作此模块!'};
  128. }
  129. return jason;
  130. }
  131. };
  132. //Jason序列化
  133. //参数:jason:jason的字符串
  134. export const JasonSerialize = (jason) => {
  135. let times = '201909091222123';
  136. jason = jason + times;
  137. return jason;
  138. };
  139. //POST图片上传
  140. //参数:url:链接地址
  141. // token: Token值
  142. // params:请求参数
  143. // image:图片信息
  144. // types:请求类型,1:post请求,2:get请求
  145. export const UploadImage = (url, token, params, image) => {
  146. url = GetNetworkUrl() + url;
  147. return new Promise(function (resolve, reject) {
  148. let formData = new FormData();
  149. for (var key in params) {
  150. formData.append(key, params[key]);
  151. }
  152. let file = {
  153. uri: image.path,
  154. type: 'application/octet-stream',
  155. name: 'image.jpg',
  156. };
  157. formData.append('file', file);
  158. fetch(url, {
  159. method: 'POST',
  160. headers: {
  161. 'Content-Type': 'multipart/form-data;charset=utf-8',
  162. Authorization: token,
  163. },
  164. body: formData,
  165. })
  166. .then((response) => response.json())
  167. .then((responseData) => {
  168. resolve(responseData);
  169. })
  170. .catch((err) => {
  171. reject(err);
  172. });
  173. });
  174. };
  175. //通过url获取服务器jason数据
  176. //参数:url:地址链接
  177. //返回值:string
  178. // export const GetData = (url, jason) => {
  179. // const fetchGetOption = {
  180. // method: 'POST',
  181. // headers: {
  182. // Accept: 'application/json',
  183. // 'Content-Type': 'application/json',
  184. // token: GetToken(),
  185. // },
  186. // body: JSON.stringify(jason),
  187. // };
  188. // return fetch(url)
  189. // .then(response => response.json())
  190. // .then(responseJson => {
  191. // return responseJson.movies;
  192. // })
  193. // .catch(error => {
  194. // return error;
  195. // });
  196. // };