This commit is contained in:
xingyy 2023-12-07 11:08:43 +08:00
parent 4a73f5f51d
commit 99dc5eac39
7 changed files with 196 additions and 8 deletions

8
package-lock.json generated
View File

@ -23,9 +23,12 @@
"@dcloudio/uni-mp-weixin": "3.0.0-3090820231124001",
"@dcloudio/uni-mp-xhs": "3.0.0-3090820231124001",
"@dcloudio/uni-quickapp-webview": "3.0.0-3090820231124001",
"@xingyy/uni-fetch": "^1.0.2",
"echarts": "5.4.2",
"node-sass": "^9.0.0",
"pinia": " 2.0.33",
"sass": "^1.69.5",
"sass-loader": "^13.3.2",
"vue": "3.2.47",
"vue-i18n": "9.8.0"
},
@ -4562,6 +4565,11 @@
"@xtuc/long": "4.2.2"
}
},
"node_modules/@xingyy/uni-fetch": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@xingyy/uni-fetch/-/uni-fetch-1.0.2.tgz",
"integrity": "sha512-NCJq9IeyxpNl1G3VsWq6jgwkZSyBNeCQn5S8R5pmqn4+Gg+gy4y05RoowW0QtnW/DCvRYE7cMKwPh9JevCQu1g=="
},
"node_modules/@xtuc/ieee754": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz",

View File

@ -48,6 +48,7 @@
"@dcloudio/uni-mp-weixin": "3.0.0-3090820231124001",
"@dcloudio/uni-mp-xhs": "3.0.0-3090820231124001",
"@dcloudio/uni-quickapp-webview": "3.0.0-3090820231124001",
"@xingyy/uni-fetch": "^1.0.2",
"echarts": "5.4.2",
"node-sass": "^9.0.0",
"pinia": " 2.0.33",

23
src/http/init.ts Normal file
View File

@ -0,0 +1,23 @@
import {uniFetch} from "@/http/main";
const fetch=new uniFetch({
baseUrl:'https://warehouse.szjixun.cn',
interceptors:{
//请求拦截器
request(config ){
},
//响应拦截器
response(response){
if (response.data?.status === 401) {
let curPage = getCurrentPages();
let route = curPage[curPage.length - 1].route; //获取当前页面的路由
if (route !== "pages/login/login") {
uni.navigateTo({
url: "/pages/login/login",
});
}
}
}
}
})
export default fetch

View File

@ -1,8 +1,8 @@
import http from "./interface";
import fetch from "@/http/init";
// openId
export const login = (data) => {
return http.request({
return fetch.request({
url: "/api/wxuser/openid",
method: "POST",
data,
@ -10,7 +10,7 @@ export const login = (data) => {
};
// 获取手机号
export const getTel = (data) => {
return http.request({
return fetch.request({
url: "/api/wxuser/get/telnum",
method: "POST",
data,
@ -18,7 +18,7 @@ export const getTel = (data) => {
};
// 注册
export const register = (data) => {
return http.request({
return fetch.request({
url: "/api/wxuser/register",
method: "POST",
data,
@ -26,7 +26,7 @@ export const register = (data) => {
};
// 身份验证
export const chenckId = (data) => {
return http.request({
return fetch.request({
url: "/api/wxuser/ocr",
method: "POST",
data,
@ -34,7 +34,7 @@ export const chenckId = (data) => {
};
// 法大大
export const fddRealName = (data) => {
return http.request({
return fetch.request({
url: "/api/wxuser/bind/fdd",
method: "POST",
data,
@ -42,7 +42,7 @@ export const fddRealName = (data) => {
};
//法大大是否验证
export const checkFdd = () => {
return http.request({
return fetch.request({
url: "/api/wxuser/fdd/check",
method: "POST",
});

147
src/http/main.ts Normal file
View File

@ -0,0 +1,147 @@
type HttpMethod =
| "GET"
| 'get'
| "POST"
| 'post'
| "PUT"
| 'put'
| "DELETE"
| 'delete'
| "PATCH"
| 'patch'
| "OPTIONS"
| 'options'
| "HEAD"
| 'head'
| "TRACE"
| 'trace'
| "CONNECT"
| 'connect';
interface RequestOptions {
baseUrl?: string;
dataType?: string;
url: string;
data?: Record<string, any>;
method?: HttpMethod;
header?: Record<string, string>;
params?: Record<string, any>; // 新增的参数字段
}
type RequestInterceptor = (config: RequestOptions) => RequestOptions;
type ResponseInterceptor = (response: any) => any;
interface Interceptors{
request:(config: RequestOptions)=>{},
response:(response:any)=>{}
}
class uniFetch {
baseUrl: string;
defaultHeader: Record<string, string>;
interceptors: { request: RequestInterceptor | null; response: ResponseInterceptor | null };
constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: {
baseUrl?: string;
defaultHeader?: Record<string, string>;
interceptors?: Interceptors;
}) {
this.baseUrl = baseUrl;
this.defaultHeader = {
"Content-Type": "application/json;charset=UTF-8",
...defaultHeader,
};
this.interceptors = interceptors;
}
setBaseUrl(baseUrl: string): void {
this.baseUrl = baseUrl;
}
setDefaultHeader(header: Record<string, string>): void {
this.defaultHeader = header;
}
request(options: RequestOptions): Promise<any> {
options = options || {};
options.baseUrl = options.baseUrl || this.baseUrl;
options.dataType = options.dataType || "json";
options.url = options.baseUrl + options.url;
options.data = options.data || {};
options.method = options.method || "GET";
options.header = options.header || this.defaultHeader;
if (this.interceptors.request) {
options = this.interceptors.request(options);
}
return new Promise((resolve, reject) => {
uni.request({
...options,
success: (res) => {
const response = this.handleResponse(res);
resolve(response);
},
fail: (err) => {
const error = this.handleError(err);
reject(error);
},
});
});
}
private handleResponse(response: any): any {
// 执行响应拦截器(如果有)
if (this.interceptors.response) {
response = this.interceptors.response(response);
}
const statusCode = response.statusCode;
if (statusCode === 200) {
return response.data;
} else {
throw response;
}
}
private handleError(error: any): any {
// 在这里处理错误
throw error;
}
setRequestInterceptor(interceptor: RequestInterceptor): void {
this.interceptors.request = interceptor;
}
setResponseInterceptor(interceptor: ResponseInterceptor): void {
this.interceptors.response = interceptor;
}
get(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "GET";
return this.request(this.buildRequestOptions(options));
}
private buildRequestOptions(options: RequestOptions): RequestOptions {
if (options.params) {
const queryString = Object.keys(options.params)
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params[key])}`)
.join('&');
options.url += `?${queryString}`;
}
delete options.params;
return options;
}
post(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "POST";
return this.request(this.buildRequestOptions(options));
}
put(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "PUT";
return this.request(this.buildRequestOptions(options));
}
delete(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "DELETE";
return this.request(this.buildRequestOptions(options));
}
}
export { uniFetch}

View File

@ -15,6 +15,16 @@
}
}
},
{
"path": "pages/login/index",
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false,
"app-plus": {
"titleNView": false //
}
}
},
{
"path": "pages/setup/index",
"style": {

View File

@ -288,7 +288,6 @@ background-image: url('https://cdns.fontree.cn/fonchain-main/prod/image/1833/ava
height: 100rpx;
}
}
}
}
}