123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import {RetrieveData} from './storage';
- import {user_token} from './configure_information';
- import {log} from 'react-native-reanimated';
- //POST请求数据
- //参数:url:链接地址
- // token: Token值
- // jason:请求参数
- // category:是否加密,true:加密,false:不加密
- // types:请求类型,1:post请求,2:get请求
- export const GetDataPost = (url, token, jason, category, types) => {
- const fetchPostOption = {
- method: 'POST',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- token: token,
- },
- body: JSON.stringify(jason),
- };
- const fetchGetOption = {
- method: 'GET',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- token: token,
- },
- };
- if (types == 1) {
- if (category) {
- return fetch(url, fetchPostOption)
- .then((response) => response.json())
- .then((responseJson) => {
- return responseJson;
- })
- .catch((error) => {
- return {code: -1, msg: '服务器故障!'};
- });
- } else {
- return fetch(url, fetchPostOption)
- .then((response) => response.json())
- .then((responseJson) => {
- return responseJson;
- })
- .catch((error) => {
- return {code: -1, msg: '服务器故障!'};
- });
- }
- } else {
- //GET请求数据
- if (jason) {
- let paramsArray = [];
- //拼接参数
- Object.keys(jason).forEach((key) =>
- paramsArray.push(key + '=' + jason[key]),
- );
- if (url.search(/\?/) === -1) {
- url += '?' + paramsArray.join('&');
- } else {
- url += '&' + paramsArray.join('&');
- }
- }
- try {
- return fetch(url, fetchGetOption)
- .then((response) => response.json())
- .then((responseJson) => {
- return responseJson;
- });
- } catch (e) {
- return {code: -1, msg: '服务器故障!'};
- }
- }
- };
- //Jason序列化
- //参数:jason:jason的字符串
- export const JasonSerialize = (jason) => {
- let times = '201909091222123';
- jason = jason + times;
- return jason;
- };
- //POST请求数据
- //参数:url:链接地址
- // token: Token值
- // jason:请求参数
- // category:是否加密,true:加密,false:不加密
- // types:请求类型,1:post请求,2:get请求
- export const HeadImagePost = (url, token, jason) => {
- const fetchPostOption = {
- method: 'POST',
- headers: {
- Accept: 'application/json',
- token: token,
- },
- body: JSON.stringify(jason),
- };
- return fetch(url, fetchPostOption)
- .then((response) => response.json())
- .then((responseJson) => {
- return responseJson;
- })
- .catch((error) => {
- return {code: -1, msg: '服务器故障!'};
- });
- };
- //POST图片上传
- //参数:url:链接地址
- // token: Token值
- // params:请求参数
- // image:图片信息
- // types:请求类型,1:post请求,2:get请求
- export const UploadImage = (url, token, params, image) => {
- return new Promise(function (resolve, reject) {
- let formData = new FormData();
- for (var key in params) {
- formData.append(key, params[key]);
- }
- let file = {
- uri: image.path,
- type: 'application/octet-stream',
- name: 'image.jpg',
- };
- formData.append('file', file);
- fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'multipart/form-data;charset=utf-8',
- Authorization: token,
- },
- body: formData,
- })
- .then((response) => response.json())
- .then((responseData) => {
- resolve(responseData);
- })
- .catch((err) => {
- reject(err);
- });
- });
- };
- //通过url获取服务器jason数据
- //参数:url:地址链接
- //返回值:string
- // export const GetData = (url, jason) => {
- // const fetchGetOption = {
- // method: 'POST',
- // headers: {
- // Accept: 'application/json',
- // 'Content-Type': 'application/json',
- // token: GetToken(),
- // },
- // body: JSON.stringify(jason),
- // };
- // return fetch(url)
- // .then(response => response.json())
- // .then(responseJson => {
- // return responseJson.movies;
- // })
- // .catch(error => {
- // return error;
- // });
- // };
|