This commit is contained in:
qiaojiale 2025-04-14 18:07:21 +08:00
commit 8ce32c01c6
3 changed files with 212 additions and 3 deletions

View File

@ -1,18 +1,36 @@
<template>
<view>
123
123456
</view>
</template>
<script>
import { request } from '../../utils/index.js';
export default {
data() {
return {
}
},
onLoad () {
this.init();
},
methods: {
init: async () => {
console.log('123');
const res = await request('/wechat/shop/shop_list', 'POST', {
page_num: 1,
page_size: 10,
user_id: 68
}, {
'ctoken': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZWFsZXJfaWQiOjAsImV4cCI6MTc0NDI2NDk1NiwiaW1nIjoiL3N0YXRpYy9pbWFnZXMvMjAyNS0wNC0wOC9pREVEV1NDR2tnYlc4YzhhYjZkODc0MjYzMzZmODM0MzMzNTE0NjAzNjdmOC5wbmciLCJtc2hvcF9pZCI6NDIsIm9wZW5faWQiOiJvVWlWajQ5NDNDODZjTXViZU1WQnIyRktzQV8wIiwicm9sZV9pZCI6MSwidXNlcl9pZCI6NjcsInVzZXJfbmFtZSI6IuW-ruS_oeeUqOaItyIsIndzaG9wX2lkIjo0Mn0.pHtbo4GS92iq8BNHFqn7mu6-Ah07XgM_KED-Y1BzmMI',
silent: false // true loading
})
console.log('1231312312331', res);
},
}
}
</script>

View File

@ -1 +1 @@
<view>123</view>
<view>123456</view>

191
utils/index.js Normal file
View File

@ -0,0 +1,191 @@
const RequsetUrl = 'https://huishang.magicany.cc/api/v1'; // 请求地址前缀
export const picUrl = 'https://huishang.magicany.cc'; // 图片地址前缀
/**
* @description 小程序跳转方法二次封装
* @method NavgateTo
* @param {String} path - 跳转的目标页面路径
* @param {Object} options - 配置选项对象
* @param {Boolean} options.isLogin - 是否需要校验登录态默认为 true
*/
export const NavgateTo = (path, options = {}) => {
const { isLogin = true, } = options;
const userId = uni.getStorageSync('userId'); // 假设这个方法存在并返回用户信息
if (isLogin) {
if (!userId) {
uni.navigateTo({ url: '/pages/login/login' })
return
} else {
uni.navigateTo({
url: path
});
return;
}
}
uni.navigateTo({ url: path })
}
/**
* 封装请求方法
* @param {string} url 请求地址
* @param {string} method 请求方法
* @param {Object} data 请求参数
* @param {Object} options 配置选项对象
* @returns {Promise} 返回一个Promise对象
*/
export const request = (url, method = 'POST', data = {}, options = {}) => {
const { silent = true } = options;
let ctoken;
if (options.token) {
ctoken = options.token;
} else {
ctoken = uni.getStorageSync('ctoken'); // 后续接口强依赖强校验该字段
}
if (silent) {
uni.showLoading({
title: '加载中',
mask: true
})
};
let params = {
user_id: uni.getStorageSync('userId'),
...data,
}
return new Promise((resolve, reject) => {
uni.request({
url: RequsetUrl + url,
method: method,
data: params,
header: {
'Content-Type': 'application/json',
'Authorization': ctoken,
// ...header,
},
success: (res) => {
console.log('请求成功,接口返参', res);
if (res.statusCode >= 200 && res.statusCode < 300) {
if (silent) {
uni.hideLoading();
}
resolve(res.data); // 请求成功
} else {
if (silent) {
uni.hideLoading();
}
reject({
code: res.statusCode,
message: res.data.message || '请求失败',
data: res.data,
});
}
},
fail: (err) => {
if (silent) {
uni.hideLoading();
}
reject({
code: -1,
message: '接口异常,请稍后重试',
error: err,
});
},
});
});
}
/**
* 校验手机号是否合法
* @param {string} option 请求方法
* @returns {Boolean} 手机号是否正确
*/
export const isPhone = (option) => {
if(option.length != 11){
return false
}
if (!(/^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(option))) {
return false
} else {
return true
}
}
/**
* 精确的浮点数运算
* @param {number} num1 - 第一个数字
* @param {number} num2 - 第二个数字
* @param {string} operator - 运算符支持 '+', '-', '*', '/', '+='
* @returns {number} - 运算结果
*/
export const floatCalculate = (num1, num2, operator) => {
// 获取小数位数
function getPrecision(num) {
const str = num.toString();
const decimalIndex = str.indexOf('.');
return decimalIndex === -1 ? 0 : str.length - decimalIndex - 1;
}
// 计算放大倍数
const precision1 = getPrecision(num1);
const precision2 = getPrecision(num2);
const maxPrecision = Math.max(precision1, precision2);
const factor = Math.pow(10, maxPrecision);
// 将数字转换为整数
const intNum1 = Math.round(num1 * factor);
const intNum2 = Math.round(num2 * factor);
// 根据运算符进行计算
let result;
switch (operator) {
case '+':
result = (intNum1 + intNum2) / factor;
break;
case '-':
result = (intNum1 - intNum2) / factor;
break;
case '*':
result = (intNum1 * intNum2) / (factor * factor);
break;
case '/':
result = intNum1 / intNum2;
break;
case '+=':
result = (intNum1 + intNum2) / factor;
break;
default:
throw new Error('不支持的运算符');
}
return result;
}
/**
* 图片上传
* @param {string} filename - 图片上传地址
* @param {Function} fn - 接口回调函数
*/
export const upload = (filename, fn) => {
uni.uploadFile({
url: RequsetUrl + '/public/upload-image',
filePath: filename,
name: 'image',
formData: {
'uid': uni.getStorageSync('uid'),
},
success: (f) => {
fn(f.data);
},
fail: (res) => {
console.log(res);
uni.showToast({
title: '上传文件失败',
icon: 'none'
})
},
complete: () => {}
});
}