12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import config from '../config.js';
- const getDomain = (uri, method, type) => {
- let serverUrl = config.serverUrl;
- let fileUrl = config.serverFile;
- if (type && type == 'file') return fileUrl + `/files` + uri;
- else return serverUrl + `/cxyy/api` + uri;
- };
- // 获取token
- const getToken = () => {
- try {
- const value = uni.getStorageSync('token');
- if (value) return value;
- } catch {}
- };
- export const requestBase = async (uri, method = 'GET', data, type) => {
- let baseUrl = getDomain(uri, method, type);
- let header = {};
- const token = uni.getStorageSync('token');
- if (token) header.token = token;
- return new Promise((resolve, reject) => {
- uni.request({
- url: baseUrl,
- method: method || 'GET',
- data: data || {},
- header,
- success: (res) => {
- resolve(res.data);
- },
- fail: (err) => {
- uni.showToast({
- title: '请求接口失败',
- icon: 'fail',
- });
- reject(err.data);
- },
- });
- });
- };
- export const requestApp = async (uri, method = 'GET', data, type) => {
- let baseUrl = getDomain(uri, method, type);
- let header = {};
- const token = uni.getStorageSync('token');
- if (token) header.token = token;
- return new Promise((resolve, reject) => {
- uni.request({
- url: baseUrl,
- method: method || 'GET',
- data: data || {},
- header,
- success: (res) => {
- resolve(res.data);
- },
- fail: (err) => {
- uni.showToast({
- title: '请求接口失败',
- icon: 'fail',
- });
- reject(err.data);
- },
- });
- });
- };
- export const requestFile = (uri, method, data, type) => {
- let baseUrl = getDomain(uri, method, type);
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: baseUrl,
- filePath: data,
- name: method || 'file',
- formData: {},
- success: (res) => {
- let data = JSON.parse(res.data);
- if (data.errcode != '0') {
- return uni.showToast({
- title: data.errmsg,
- icon: 'error',
- });
- }
- resolve(data);
- },
- fail: (err) => {
- let data = JSON.parse(err.data);
- uni.showToast({
- title: '请求接口失败',
- icon: 'fail',
- });
- reject(data);
- },
- });
- });
- };
|