123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import config from '../config.js';
- const getDomain = (uri, method, type) => {
- let wx_env = config.wx_miniProgram;
- let system = config.system;
- let url = config.serverUrl;
- let Fileurl = config.serverFile;
- if (type && type == 'file') return Fileurl + `/files` + uri;
- else return url + `/material/v1/api` + uri;
- };
- // 获取token
- export const getToken = async (uri, method = 'GET', data, type) => {
- let baseUrl = getDomain(uri, method, type);
- let header = {
- token: data.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 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 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);
- },
- });
- });
- };
|