123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //全局网络链接
- import {IndividualStorageData, RetrieveData} from './storage';
- export const GetNetworkUrl = () => {
- let networkUrl = 'https://www.chtax.cn/api';
- // let networkUrl = 'http://192.168.1.173:50009';
- return networkUrl;
- };
- // 网络请求
- export const RequestNetwork = async (url, token, jason, category, types) => {
- if (token !== null) {
- token = await RetrieveData('token');
- token = await UpdateToken(token);
- return await GetDataPost(url, token, jason, category, types);
- } else {
- return await GetDataPost(url, token, jason, category, types);
- }
- };
- // 网络请求更新token
- export const UpdateToken = async (token) => {
- let account = await RetrieveData('account');
- let url = '/auth/comm/user/refreshToken';
- let res = await GetDataPost(
- url,
- token,
- {
- account: account,
- reqChannel: 3,
- },
- false,
- 1,
- );
- if (res) {
- if (res.code === 0) {
- let token = res.data.token;
- await IndividualStorageData('token', token);
- return token;
- }
- }
- };
- //POST请求数据
- //参数:url:链接地址
- // token: Token值
- // jason:请求参数
- // category:是否加密,true:加密,false:不加密
- // types:请求类型,1:post请求,2:get请求
- export const GetDataPost = async (url, token, jason, category, types) => {
- url = GetNetworkUrl() + url;
- 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 RequestStatus(responseJson);
- })
- .catch((error) => {
- return {code: -1, msg: '网络异常,请稍后再试!'};
- });
- // let response = fetch(url, fetchPostOption);
- } else {
- return fetch(url, fetchPostOption)
- .then((response) => {
- return response.json();
- })
- .then((responseJson) => {
- return RequestStatus(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) => {
- return response.json();
- })
- .then((responseJson) => {
- return RequestStatus(responseJson);
- });
- } catch (e) {
- return {code: -1, msg: '网络异常,请稍后再试!'};
- }
- }
- };
- // 处理请求异常返回信息
- // 参数:jason: 传入参数
- export const RequestStatus = (jason) => {
- if (jason.ok) {
- return jason;
- } else {
- if (jason.status === 401) {
- return {code: 401, msg: '登录失效,请重新登录!'};
- }
- if (jason.status === 403) {
- return {code: 403, msg: '暂无权限操作此模块!'};
- }
- return jason;
- }
- };
- //Jason序列化
- //参数:jason:jason的字符串
- export const JasonSerialize = (jason) => {
- let times = '201909091222123';
- jason = jason + times;
- return jason;
- };
- //POST图片上传
- //参数:url:链接地址
- // token: Token值
- // params:请求参数
- // image:图片信息
- // types:请求类型,1:post请求,2:get请求
- export const UploadImage = (url, token, params, image) => {
- url = GetNetworkUrl() + url;
- 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;
- // });
- // };
|