feat: 优化代码
This commit is contained in:
+44
-44
@@ -1,15 +1,15 @@
|
||||
// 检查是否登录,未登录跳转登录页,抛出异常中断后续操作
|
||||
export function isLogin() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let token = uni.getStorageSync('token');
|
||||
if (!token) {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
return reject('未登录');
|
||||
}
|
||||
return resolve('已登录');
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
let token = uni.getStorageSync('token');
|
||||
if (!token) {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login_package/login/login'
|
||||
})
|
||||
return reject('未登录');
|
||||
}
|
||||
return resolve('已登录');
|
||||
});
|
||||
}
|
||||
|
||||
export const TOKEN_NAME = 'fcc-token';// 用户对象
|
||||
@@ -22,45 +22,45 @@ export const SYS_LANGUAGE_TIME = 'fcc-sys-language';// 当前系统语言
|
||||
* 设置token
|
||||
* @param {Object} token
|
||||
*/
|
||||
export function setStorageFun(key, value, sync = true){
|
||||
try {
|
||||
if (sync) {
|
||||
return uni.setStorageSync(key, value);
|
||||
} else {
|
||||
uni.setStorage({
|
||||
key: key,
|
||||
data: value
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
export function setStorageFun(key, value, sync = true) {
|
||||
try {
|
||||
if (sync) {
|
||||
return uni.setStorageSync(key, value);
|
||||
} else {
|
||||
uni.setStorage({
|
||||
key: key,
|
||||
data: value
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*/
|
||||
export function getStorageFun(key,sync = true){
|
||||
try {
|
||||
if(sync){
|
||||
return uni.getStorageSync(key);
|
||||
}else{
|
||||
let data = '';
|
||||
uni.getStorage({
|
||||
key:key,
|
||||
success: function (res) {
|
||||
data = res.data;
|
||||
}
|
||||
});
|
||||
return data;
|
||||
}
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
export function getStorageFun(key, sync = true) {
|
||||
try {
|
||||
if (sync) {
|
||||
return uni.getStorageSync(key);
|
||||
} else {
|
||||
let data = '';
|
||||
uni.getStorage({
|
||||
key: key,
|
||||
success: function (res) {
|
||||
data = res.data;
|
||||
}
|
||||
});
|
||||
return data;
|
||||
}
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//移除
|
||||
export function del(key, sync = true){
|
||||
export function del(key, sync = true) {
|
||||
try {
|
||||
if (sync) {
|
||||
return uni.removeStorageSync(key);
|
||||
@@ -75,16 +75,16 @@ export function del(key, sync = true){
|
||||
}
|
||||
|
||||
//清空
|
||||
export function clear(sync = true){
|
||||
export function clear(sync = true) {
|
||||
try {
|
||||
if (sync) {
|
||||
return uni.clearStorageSync();
|
||||
} else {
|
||||
uni.clearStorage();
|
||||
}
|
||||
uni.removeStorage({
|
||||
key: 'isForcedLogin',
|
||||
});
|
||||
uni.removeStorage({
|
||||
key: 'isForcedLogin',
|
||||
});
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
+30
-30
@@ -2,15 +2,15 @@ import { feedback } from '@/api/payment.js';
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* 微信支付
|
||||
* @param {Object} alipay 微信支付的参数
|
||||
* @param {Object} orderId 支付的订单ID
|
||||
* @param {Object} orderNum 支付的订单编号
|
||||
*/
|
||||
export function appWxpayFun(alipay,orderId,orderNum){
|
||||
export function appWxpayFun(alipay, orderId, orderNum) {
|
||||
const type = alipay.type;
|
||||
const orderStr = alipay.orderStr;
|
||||
if(type === "app"){// app
|
||||
if (type === "app") {// app
|
||||
const orderInfo = {
|
||||
"appid": alipay.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
|
||||
"noncestr": alipay.nonceStr, // 随机字符串
|
||||
@@ -21,21 +21,21 @@ export function appWxpayFun(alipay,orderId,orderNum){
|
||||
"sign": alipay.sign // 签名,这里用的 MD5/RSA 签名
|
||||
};
|
||||
uni.requestPayment({
|
||||
provider:"wxpay",
|
||||
provider: "wxpay",
|
||||
orderInfo: orderInfo,
|
||||
success: function(res) {
|
||||
success: function (res) {
|
||||
console.log('success:' + res);
|
||||
payFeedbackFun(orderId,orderNum,1);
|
||||
payFeedbackFun(orderId, orderNum, 1);
|
||||
jumpWayOk();
|
||||
},
|
||||
fail: function(err) {
|
||||
console.log('fail:',err);
|
||||
fail: function (err) {
|
||||
console.log('fail:', err);
|
||||
jumpWayError();
|
||||
}
|
||||
});
|
||||
}else if(type === "h5"){// h5
|
||||
} else if (type === "h5") {// h5
|
||||
uni.navigateTo({
|
||||
url: `/pages/payment_processing/payment_processing?link=${encodeURIComponent(orderStr)}&orderNum=`+orderNum // 假设有一个专门的支付页面来处理支付链接
|
||||
url: `/pages/other_package/payment_processing/payment_processing?link=${encodeURIComponent(orderStr)}&orderNum=` + orderNum // 假设有一个专门的支付页面来处理支付链接
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -46,25 +46,25 @@ export function appWxpayFun(alipay,orderId,orderNum){
|
||||
* @param {Object} orderId 支付的订单ID
|
||||
* @param {Object} orderNum 支付的订单编号
|
||||
*/
|
||||
export function zfbPayFun(alipay,orderId,orderNum){
|
||||
console.log("alipay",alipay);
|
||||
export function zfbPayFun(alipay, orderId, orderNum) {
|
||||
console.log("alipay", alipay);
|
||||
const type = alipay.type;
|
||||
const orderStr = alipay.orderStr;
|
||||
if(type === "h5"){// h5
|
||||
if (type === "h5") {// h5
|
||||
uni.navigateTo({
|
||||
url: `/pages/payment_processing/payment_processing?link=${encodeURIComponent(orderStr)}&orderNum=`+orderNum // 假设有一个专门的支付页面来处理支付链接
|
||||
url: `/pages/other_package/payment_processing/payment_processing?link=${encodeURIComponent(orderStr)}&orderNum=` + orderNum // 假设有一个专门的支付页面来处理支付链接
|
||||
});
|
||||
}else if(type === "app"){ // app
|
||||
} else if (type === "app") { // app
|
||||
uni.requestPayment({
|
||||
provider: 'alipay',
|
||||
orderInfo: orderStr,
|
||||
success: function(res) {
|
||||
console.log("zfbPayFun res",res);
|
||||
payFeedbackFun(orderId,orderNum,1);
|
||||
success: function (res) {
|
||||
console.log("zfbPayFun res", res);
|
||||
payFeedbackFun(orderId, orderNum, 1);
|
||||
jumpWayOk();
|
||||
},
|
||||
fail: function(err) {
|
||||
console.error("支付宝支付error",err);
|
||||
fail: function (err) {
|
||||
console.error("支付宝支付error", err);
|
||||
jumpWayError();
|
||||
}
|
||||
});
|
||||
@@ -76,33 +76,33 @@ export function zfbPayFun(alipay,orderId,orderNum){
|
||||
};
|
||||
|
||||
// 跳转到支付成功页面
|
||||
export function jumpWayOk(){
|
||||
export function jumpWayOk() {
|
||||
uni.navigateTo({
|
||||
url:"/pages/order_submit_ok/order_submit_ok",
|
||||
url: "/pages/order_package/order_submit_ok/order_submit_ok",
|
||||
})
|
||||
}
|
||||
// 跳转到支付失败页面
|
||||
export function jumpWayError(){
|
||||
export function jumpWayError() {
|
||||
uni.navigateTo({
|
||||
url:"/pages/order_submit_error/order_submit_error",
|
||||
url: "/pages/order_package/order_submit_error/order_submit_error",
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 支付结果回调
|
||||
* 支付结果回调
|
||||
* @param {Object} orderId 支付订单ID
|
||||
* @param {Object} orderNum 支付订单编号
|
||||
* @param {Object} status 反馈结果【0:失败,1:成功】
|
||||
*/
|
||||
async function payFeedbackFun(orderId,orderNum,status){
|
||||
async function payFeedbackFun(orderId, orderNum, status) {
|
||||
const params = {
|
||||
orderId,
|
||||
orderNum,
|
||||
status,
|
||||
}
|
||||
console.log("params",params);
|
||||
console.log("params", params);
|
||||
const response = await feedback(params);
|
||||
console.log("payFeedbackFun response",response);
|
||||
if(response && response.bizcode !==100){
|
||||
console.error("反馈结果",response);
|
||||
console.log("payFeedbackFun response", response);
|
||||
if (response && response.bizcode !== 100) {
|
||||
console.error("反馈结果", response);
|
||||
}
|
||||
}
|
||||
+16
-16
@@ -1,25 +1,25 @@
|
||||
import {getStorageFun,TOKEN_NAME} from './auth.js';
|
||||
import { getStorageFun, TOKEN_NAME } from './auth.js';
|
||||
import qs from "qs";
|
||||
import { BASE_URL,Authorization } from '@/utils/config.js'
|
||||
import { BASE_URL, Authorization } from '@/utils/config.js'
|
||||
|
||||
// utils/http.js (修改后的版本)
|
||||
// const baseUrl = 'http://192.168.0.108:8087';
|
||||
|
||||
function request(options) {
|
||||
const { url, method = 'GET', data = {}, headers = {},isShowLoading=true } = options;
|
||||
const { url, method = 'GET', data = {}, headers = {}, isShowLoading = true } = options;
|
||||
|
||||
// const token = uni.getStorageSync(tokenKey); // 获取token, 根据实际情况调整获取方式
|
||||
const token = getStorageFun(TOKEN_NAME); // 获取token, 根据实际情况调整获取方式
|
||||
const defaultHeaders = {
|
||||
"Authorization":Authorization,
|
||||
"Authorization": Authorization,
|
||||
'HSM-AUTH': token ? token : '', // 如果存在token, 则添加到请求头中
|
||||
'content-type': ['post','POST'].includes(method) ? 'application/x-www-form-urlencoded' : 'application/json',
|
||||
'content-type': ['post', 'POST'].includes(method) ? 'application/x-www-form-urlencoded' : 'application/json',
|
||||
};
|
||||
if(token){
|
||||
if (token) {
|
||||
defaultHeaders['HSM-AUTH'] = token;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
if(isShowLoading){
|
||||
if (isShowLoading) {
|
||||
// 请求之前做一些事
|
||||
uni.showLoading({
|
||||
title: "加载中...",
|
||||
@@ -34,16 +34,16 @@ function request(options) {
|
||||
...headers, // 使用展开运算符合并默认头和自定义头
|
||||
},
|
||||
success: (res) => {
|
||||
if(isShowLoading){
|
||||
if (isShowLoading) {
|
||||
// 请求之前做一些事
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
|
||||
let data = res.data;
|
||||
if(data && typeof(data) === "string"){
|
||||
if (data && typeof (data) === "string") {
|
||||
data = JSON.parse(data);
|
||||
}
|
||||
if(data.status === 500){
|
||||
if (data.status === 500) {
|
||||
uni.showToast({
|
||||
title: "系统异常,请稍后再试",
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
@@ -55,7 +55,7 @@ function request(options) {
|
||||
if (data.bizcode === 100) {
|
||||
resolve(data); // 成功时返回数据
|
||||
} else if (data.bizcode === 201) {
|
||||
if([201,200].includes(data.errcode)){
|
||||
if ([201, 200].includes(data.errcode)) {
|
||||
uni.showToast({
|
||||
title: '登录失效,请重新登录',
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
@@ -63,7 +63,7 @@ function request(options) {
|
||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||
position: 'middle' // 位置,可选 top/middle/bottom
|
||||
});
|
||||
}else{
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: data.msg,
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
@@ -76,10 +76,10 @@ function request(options) {
|
||||
* 205:已经有账号,返回登录页面
|
||||
* 200 or 201:token失效,返回登录页面
|
||||
*/
|
||||
if([201,200,205].includes(data.errcode )){
|
||||
if ([201, 200, 205].includes(data.errcode)) {
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url:'/pages/login/login'
|
||||
url: '/pages/login_package/login/login'
|
||||
})
|
||||
}, 2000);
|
||||
}
|
||||
@@ -93,7 +93,7 @@ function request(options) {
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
if(isShowLoading){
|
||||
if (isShowLoading) {
|
||||
// 请求之前做一些事
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user