first commit

This commit is contained in:
编码猿
2024-09-27 01:32:49 +08:00
commit 28ebe05a64
7399 changed files with 1174529 additions and 0 deletions

View File

@@ -0,0 +1 @@
5bd25fd9-e19a-4bec-bb49-13565a47b248

View File

@@ -0,0 +1,131 @@
import {Utils} from '../utils';
import axios, {AxiosInstance} from 'axios';
/**
* 小程序端不可用
*/
export class Axios {
private instance: AxiosInstance;
// @ts-ignore
public async constructor() {
this.instance = axios.create({
baseURL: Utils.CheckAjaxUrl(),
timeout: 3000
});
this.ResponseInterceptor();
this.RequestInterceptor();
}
/**
* Get 请求
* 请求参数请参考接口:RequestParams
* @param params
*/
public async get(params: { url: string, data: Object }): Promise<Object> {
// let token:string = localStorage.getItem('token') || ''
// if(!!token) token = JSON.parse(token);
try {
return await this.instance.get(params.url, {
params: params.data
})
} catch (e) {
return e.message
}
}
/**
* Post 请求
* 请求参数请参考接口:RequestParams
* @param params
*/
public async post(params: { url: string, data: Object, headers?: Object }): Promise<Object> {
console.log(Utils.CheckAjaxUrl()+params.url);
try {
return await this.instance.post(params.url, params.data, {})
} catch (e) {
return e.message
}
}
/**
* Put 请求
* 请求参数请参考接口:RequestParams
* @param params
*/
public async put(params: { url: string, data: Object }): Promise<Object> {
try {
return await this.instance.put(params.url, params.data)
} catch (e) {
return e.message
}
}
/**
* delete 请求
* 请求参数请参考接口:RequestParams
* @param params
*/
public async delete(params: { url: string, data: Object }): Promise<Object> {
try {
return await this.instance.delete(params.url, {params: params.data})
} catch (e) {
return e.message
}
}
/**
* 添加响应拦截器
* @constructor
*/
public async ResponseInterceptor(): Promise<any> {
this.instance.interceptors.response.use(response => {
this.CloseLoadingData();
switch (response.data.status) {
case 1:
return response.data.data;
break;
case 500:
console.log(response.data.msg);
break;
default:
console.log(response.data.msg);
break;
}
}, function (error) {
return Promise.reject(error)
})
}
/**
* 添加请求拦截器
* @constructor
*/
public async RequestInterceptor(): Promise<any> {
this.instance.interceptors.request.use(config => {
this.OpenLoadingData();
return config
}, function (error) {
return Promise.reject(error)
})
}
/**
* 显示加载进度
* @constructor
*/
private OpenLoadingData(): void {
uni.showLoading({
title: '加载中...',
mask: true
});
}
/**
* 清除加载进度条
*/
private CloseLoadingData(): void {
uni.hideLoading();
}
}

View File

@@ -0,0 +1,237 @@
import { Utils } from '../utils';
interface AxiosConfig {
baseUrl: string
header: any
data: any
method: string
dataType: string
responseType: string,
success: Function
fail: Function
complete: Function
}
interface Interceptor {
request: Function
response: Function
}
export class Axios {
private config: AxiosConfig
private interceptor: Interceptor
private Utils: Utils = new Utils()
constructor() {
this.config = {
baseUrl: Utils.CheckAjaxUrl(),
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {},
method: "GET",
dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
responseType: "text",
success() { },
fail() { },
complete() { }
}
this.interceptor = {
request: null || Function,
response: null || Function
}
}
/**
* 封装 请求函数
* @param options
*/
private async request(options: any): Promise<any> {
let Original: any = { ...options }
if (!options) {
options = {}
}
options.baseUrl = options.baseUrl || this.config.baseUrl
options.dataType = options.dataType || this.config.dataType
options.url = options.baseUrl + options.url
options.data = options.data || {}
options.method = options.method || this.config.method
//TODO 加密数据
let token: string = await this.Utils.getStorage('token')
if (!!token) {
options.header = { token: token }
}
//TODO 数据签名
/*
_token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
_sign = {'sign': sign(JSON.stringify(options.data))}
options.header = Object.assign({}, options.header, _token,_sign)
*/
return new Promise((resolve, reject) => {
let _config: any = null
options.complete = async (response: any) => {
let statusCode = response.data.status
response.config = _config
if (process.env.NODE_ENV === 'development') {
if (statusCode === 200) {
// console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
}
}
// if (this.interceptor.response) {
// let newResponse = this.interceptor.response(response)
// if (newResponse) {
// response = newResponse
// }
// }
// 统一的响应日志记录
_reslog(response)
if (statusCode === 1) { //成功
resolve(response.data.data);
} else if (statusCode === 401) {
// token过期自动刷新
if (!!token) {
let that = this
// #ifdef MP-WEIXIN
uni.login({
async success(res) {
if (res.code) {
let rel = await that.post({ url: '/storeapi/login', data: { code: res.code } })
that.Utils.setStorage('token', rel.token, () => {
that.request(Original)
.then(toRes => {
resolve(toRes);
})
})
}
}
})
// #endif
// #ifdef H5
// 获取加密字符串重新调用登录接口
let channelKey: string = await this.Utils.getStorage('channelKey')
if(!!channelKey){
let rel = await that.post({ url: '/storeapi/huiLogin', data: {channelKey: channelKey} })
that.Utils.setStorage('token', rel.token, () => {
that.request(Original)
.then(toRes => {
resolve(toRes);
})
})
}
// #endif
}
} else {
uni.showToast({
title: response.data.msg,
icon: 'none'
})
reject(response)
}
}
_config = Object.assign({}, this.config, options)
_config.requestId = new Date().getTime()
// if (this.interceptor.request) {
// this.interceptor.request(_config)
// }
// 统一的请求日志记录
_reqlog(_config)
if (process.env.NODE_ENV === 'development') {
// console.log("【" + _config.requestId + "】 地址:" + _config.url)
if (_config.data) {
// console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
}
}
uni.request(_config);
});
}
public get(options: any): Promise<any> {
if (!options) {
options = {}
}
options.url = options.url
options.data = options.data
options.method = 'GET'
return this.request(options)
}
public post(options: any): Promise<any> {
if (!options) {
options = {}
}
options.url = options.url
options.data = options.data
options.method = 'POST'
return this.request(options)
}
public put(options: any): Promise<any> {
if (!options) {
options = {}
}
options.url = options.url
options.data = options.data
options.method = 'PUT'
return this.request(options)
}
public delete(options: any): Promise<any> {
if (!options) {
options = {}
}
options.url = options.url
options.data = options.data
options.method = 'DELETE'
return this.request(options)
}
}
/**
* 请求接口日志记录
*/
function _reqlog(req: any) {
// uni.showLoading({
// title: '加载中...',
// mask: true
// });
// if (process.env.NODE_ENV === 'development') {
// console.log("【" + req.requestId + "】 地址:" + req.url)
// if (req.data) {
// console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
// }
// }
//TODO 调接口异步写入日志数据库
}
/**
* 响应接口日志记录
*/
function _reslog(res: any) {
let _statusCode = res.statusCode;
// uni.hideLoading();
// if (process.env.NODE_ENV === 'development') {
// console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
// if (res.config.data) {
// console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
// }
// console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
// }
//TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
switch (_statusCode) {
case 200:
break;
case 401:
break;
case 404:
break;
default:
break;
}
}