encryption.js 4.2 KB

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