秦佳浩 2 months ago
commit b831ab7028

@ -1,10 +1,23 @@
/** app.wxss **/
/*
*
* 100%
* 使 border-box 使 padding border
* #333
*/
.container {
height: 100%;
box-sizing: border-box;
color: #333;
font-family: helvetica, 'Heiti SC', PingFangSC-Light;
}
/*
*
* Arial (#eb2444)
* 使
*/
.price {
font-family: Arial;
display: inline-block;
@ -13,44 +26,78 @@
padding-left: 10rpx;
}
/* 价格数字显示不同大小 */
/*
* ¥$
* 24rpx
*/
.symbol {
font-size: 24rpx;
}
/*
*
* 32rpx
*/
.big-num {
font-size: 32rpx;
}
/*
*
* 24rpx
*/
.small-num {
font-size: 24rpx;
}
/*
* checkbox
*
*
*/
/*
* checkbox
* checkbox 100%
*/
/* reg */
uni-checkbox-group {
width: 100% !important;
}
/*
* checkbox
* checkbox 33%使使
*
*/
uni-checkbox-group uni-label {
width: 33% !important;
display: inline-flex;
margin-bottom: 20rpx;
}
/*checkbox 选项框大小 */
/*
* checkbox
* checkbox 38rpxborder-radius: 50%
*/
uni-checkbox .uni-checkbox-input {
width: 38rpx !important;
height: 38rpx !important;
border-radius: 50% !important;
}
/*checkbox选中后样式 */
/*
* checkbox
* checkbox (#e43130)
*/
uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked {
background: #e43130;
border: 1px solid transparent !important;
}
/*checkbox选中后图标样式 */
/*
* checkbox
* checkbox
* 使 transform
*/
uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked::before {
display: inline-block;
width: 20rpx;
@ -66,9 +113,13 @@ uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked::before{
/*
* radio
*
*
*/
/*
* radio
* radio 36rpx
*/
/* 未选中的 背景样式 */
uni-radio .uni-radio-input {
height: 36rpx;
width: 36rpx;
@ -76,12 +127,21 @@ uni-radio .uni-radio-input{
background: transparent;
box-sizing: border-box;
}
/* 选中后的 背景样式 */
/*
* radio
* radio (#e43130)
*/
uni-radio .uni-radio-input.uni-radio-input-checked {
border: none !important;
background: #e43130 !important;
}
/* 选中后的 对勾样式 */
/*
* radio
* radio
* 使 transform
*/
uni-radio .uni-radio-input.uni-radio-input-checked::before {
border-radius: 50%;
width: 32rpx;
@ -96,12 +156,20 @@ uni-radio .uni-radio-input.uni-radio-input-checked::before{
-webkit-transform: translate(-50%, -50%) scale(1);
}
/* 底部按钮兼容 iPhone X以上 */
/*
* iPhone X
* iPhone X
*/
@media screen and (width: 375px) and (height: 812px) {
.container {
padding-bottom: 70px;
}
}
/*
* iPhone 8 Plus
* iPhone 8 Plus
*/
@media screen and (width: 414px) and (height: 736px) {
.container {
padding-bottom: 70px;

@ -1,9 +1,18 @@
import {
createSSRApp
createSSRApp // 从 Vue 库中导入 createSSRApp 函数,用于创建一个适用于服务器端渲染 (SSR) 的 Vue 应用实例
} from 'vue'
import App from './App.vue'
import App from './App.vue' // 导入应用的根组件
/**
* 创建并返回一个 Vue 应用实例
*
* @returns {Object} - 包含应用实例的对象
*/
export function createApp() {
// 使用 createSSRApp 函数创建一个新的 Vue 应用实例,并传入根组件 App
const app = createSSRApp(App)
// 返回一个对象,包含创建的应用实例
return {
app
}

@ -1,99 +1,106 @@
{
"name" : "",
"appid" : "__UNI__2CF44C6",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
"transformPx" : false,
"name": "", //
"appid": "__UNI__2CF44C6", // AppID
"description": "", //
"versionName": "1.0.0", //
"versionCode": "100", //
"transformPx": false, // px rpxfalse
/* 5+App */
"app-plus": {
"compatible": {
"ignoreVersion": true // true HBuilderX 1.9.0
},
"usingComponents" : true,
"nvueStyleCompiler" : "uni-app",
"compilerVersion" : 3,
"usingComponents": true, // true
"nvueStyleCompiler": "uni-app", // nvue 使 uni-app
"compilerVersion": 3, //
"splashscreen": {
"alwaysShowBeforeRender" : true,
"waiting" : true,
"autoclose" : true,
"delay" : 0
"alwaysShowBeforeRender": true, // true
"waiting": true, // true
"autoclose": true, // true
"delay": 0 // 0
},
/* */
"modules": {
"Camera" : {},
"LivePusher" : {}
"Camera": {}, // Camera
"LivePusher": {} // LivePusher
},
/* */
"distribute": {
/* android */
"android": {
"permissions": [
"<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
"<uses-feature android:name=\"android.hardware.camera\"/>", // 使
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>", // 使
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>", // 访
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>", // 访 Wi-Fi
"<uses-permission android:name=\"android.permission.CAMERA\"/>", // 使
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>", //
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>", // Wi-Fi
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>", // 使
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>", //
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>", //
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>", //
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>", //
"<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>", //
"<uses-permission android:name=\"android.permission.VIBRATE\"/>", // 使
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>", //
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>" //
],
"abiFilters" : [ "armeabi-v7a", "arm64-v8a", "x86" ]
"abiFilters": [ "armeabi-v7a", "arm64-v8a", "x86" ] // ABI
}
}
},
/* */
"mp-weixin": {
"appid" : "wx6fa71e69231a4fa4",
"appid": "wx6fa71e69231a4fa4", // AppID
"setting": {
"urlCheck" : false
"urlCheck": false // URL false
},
"usingComponents" : true,
"usingComponents": true, // true
"permission": {
"scope.userLocation": {
"desc" : "你的位置信息将用于地址信息新增、修改以及获取附近门店"
"desc": "你的位置信息将用于地址信息新增、修改以及获取附近门店" //
}
},
"requiredPrivateInfos" : [ "getLocation", "chooseLocation" ]
"requiredPrivateInfos": [ "getLocation", "chooseLocation" ] //
},
"h5": {
"title" : "",
"domain" : "https://mini-h5.mall4j.com",
"title": "", // H5
"domain": "https://mini-h5.mall4j.com", // H5
"router": {
"mode" : "history"
"mode": "history" // 使 history
},
"uniStatistics": {
"enable" : false
"enable": false // Uni-App false
},
"optimization": {
"treeShaking": {
"enable" : false
"enable": false // Tree Shaking false
}
},
"template" : "index.html",
"template": "index.html", // H5
"devServer": {
"disableHostCheck" : true,
"port" : 80
"disableHostCheck": true, // true
"port": 80 //
},
"sdkConfigs": {
"maps": {
"qqmap": {
// h5ip
"key" : ""
"key": "" // QQ API Key H5 IP
}
}
}
},
"uniStatistics": {
"enable" : false
"enable": false // Uni-App false
},
"vueVersion" : "3"
"vueVersion": "3" // 使 Vue Vue 3
}

@ -1,196 +1,200 @@
{
"pages": [
{
"path": "pages/index/index",
"path": "pages/index/index", //
"style": {
"backgroundTextStyle": "dark",
"navigationBarBackgroundColor": "#fff",
"navigationBarTextStyle": "black",
"enablePullDownRefresh": true,
"navigationBarTitleText": "mall4j"
"backgroundTextStyle": "dark", // "dark"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTextStyle": "black", //
"enablePullDownRefresh": true, // true
"navigationBarTitleText": "mall4j" //
}
},
{
"path": "pages/user/user",
"path": "pages/user/user", //
"style": {
"navigationBarTitleText": "个人中心"
"navigationBarTitleText": "个人中心" //
}
},
{
"path": "pages/basket/basket",
"path": "pages/basket/basket", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "购物车",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "购物车", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/category/category",
"path": "pages/category/category", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "分类商品",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "分类商品", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/sub-category/sub-category"
"path": "pages/sub-category/sub-category" //
},
{
"path": "pages/search-page/search-page",
"path": "pages/search-page/search-page", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "搜索",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "搜索", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/delivery-address/delivery-address",
"path": "pages/delivery-address/delivery-address", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "收货地址",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "收货地址", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/editAddress/editAddress",
"path": "pages/editAddress/editAddress", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "编辑收货地址",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "编辑收货地址", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/orderList/orderList",
"path": "pages/orderList/orderList", //
"style": {
"backgroundTextStyle": "light",
"navigationBarTitleText": "订单列表",
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#fafafa"
"backgroundTextStyle": "light", // "light"
"navigationBarTitleText": "订单列表", //
"navigationBarTextStyle": "black", //
"navigationBarBackgroundColor": "#fafafa" //
}
},
{
"path": "pages/order-detail/order-detail",
"path": "pages/order-detail/order-detail", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "订单详情",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "订单详情", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/submit-order/submit-order",
"path": "pages/submit-order/submit-order", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "提交订单",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "提交订单", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/express-delivery/express-delivery",
"path": "pages/express-delivery/express-delivery", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "物流查询",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "物流查询", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/pay-result/pay-result",
"path": "pages/pay-result/pay-result", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "支付结果",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "支付结果", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/search-prod-show/search-prod-show",
"path": "pages/search-prod-show/search-prod-show", //
"style": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "搜索结果",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "搜索结果", //
"navigationBarTextStyle": "black" //
}
},
{
"path": "pages/prod/prod",
"path": "pages/prod/prod", //
"style": {
"navigationBarTitleText": "商品详情"
"navigationBarTitleText": "商品详情" //
}
},
{
"path": "pages/prod-classify/prod-classify",
"path": "pages/prod-classify/prod-classify", //
"style": {
"onReachBottomDistance": 0
"onReachBottomDistance": 0 // 0
}
},
{
"path": "pages/recent-news/recent-news",
"path": "pages/recent-news/recent-news", //
"style": {
"backgroundTextStyle": "light",
"navigationBarTitleText": "最新公告",
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#fafafa"
"backgroundTextStyle": "light", // "light"
"navigationBarTitleText": "最新公告", //
"navigationBarTextStyle": "black", //
"navigationBarBackgroundColor": "#fafafa" //
}
},
{
"path": "pages/news-detail/news-detail",
"path": "pages/news-detail/news-detail", //
"style": {
"backgroundTextStyle": "light",
"navigationBarTitleText": "最新公告",
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#fafafa"
"backgroundTextStyle": "light", // "light"
"navigationBarTitleText": "最新公告", //
"navigationBarTextStyle": "black", //
"navigationBarBackgroundColor": "#fafafa" //
}
},
{
"path": "pages/accountLogin/accountLogin"
"path": "pages/accountLogin/accountLogin" //
},
{
"path": "pages/register/register"
"path": "pages/register/register" //
}
],
"tabBar": {
"selectedColor": "#3a86b9",
"color": "#b8b8b8",
"selectedColor": "#3a86b9", //
"color": "#b8b8b8", //
"list": [
{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "/static/images/tabbar/homepage.png",
"selectedIconPath": "/static/images/tabbar/homepage-sel.png"
"pagePath": "pages/index/index", //
"text": "首页", //
"iconPath": "/static/images/tabbar/homepage.png", //
"selectedIconPath": "/static/images/tabbar/homepage-sel.png" //
},
{
"pagePath": "pages/category/category",
"text": "分类",
"iconPath": "/static/images/tabbar/category.png",
"selectedIconPath": "/static/images/tabbar/category-sel.png"
"pagePath": "pages/category/category", //
"text": "分类", //
"iconPath": "/static/images/tabbar/category.png", //
"selectedIconPath": "/static/images/tabbar/category-sel.png" //
},
{
"pagePath": "pages/basket/basket",
"text": "购物车",
"iconPath": "/static/images/tabbar/basket.png",
"selectedIconPath": "/static/images/tabbar/basket-sel.png"
"pagePath": "pages/basket/basket", //
"text": "购物车", //
"iconPath": "/static/images/tabbar/basket.png", //
"selectedIconPath": "/static/images/tabbar/basket-sel.png" //
},
{
"pagePath": "pages/user/user",
"text": "我的",
"iconPath": "/static/images/tabbar/user.png",
"selectedIconPath": "/static/images/tabbar/user-sel.png"
"pagePath": "pages/user/user", //
"text": "我的", //
"iconPath": "/static/images/tabbar/user.png", //
"selectedIconPath": "/static/images/tabbar/user-sel.png" //
}
]
},
"sitemapLocation": "sitemap.json",
"sitemapLocation": "sitemap.json", //
"globalStyle": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
"backgroundTextStyle": "light", // "light"
"navigationBarBackgroundColor": "#fff", //
"navigationBarTitleText": "WeChat", //
"navigationBarTextStyle": "black" //
},
"subPackages": []
"subPackages": [] //
}

@ -3,49 +3,57 @@
import loginMethods from './login'
const http = {
/**
* 发起HTTP请求
* @param {Object} params - 请求参数对象包含urldatamethod等
* @returns {Promise} - 返回一个Promise对象解析或拒绝根据请求结果
*/
request: async function (params) {
// 请求参数处理
// 处理请求数据类型,确保数组和数字被正确序列化为字符串
if (Object.prototype.toString.call(params.data) === '[object Array]') {
params.data = JSON.stringify(params.data)
} else if (Object.prototype.toString.call(params.data) === '[object Number]') {
params.data = params.data + ''
}
// 刷新token
// 如果不是登录请求且应用未完全启动即未完成首次加载并且不是刷新token的请求则尝试刷新token
if (!params.login && !getApp()?.globalData.isLanding && !params.isRefreshing) {
await loginMethods.refreshToken()
}
// 发起请求
// 发起请求并返回Promise对象处理异步操作
return new Promise((resolve, reject) => {
uni.request({
dataType: 'json',
responseType: params.responseType === undefined ? 'text' : params.responseType,
header: {
Authorization: uni.getStorageSync('Token')
Authorization: uni.getStorageSync('Token') // 设置请求头中的Authorization字段
},
url: (params.domain ? params.domain : import.meta.env.VITE_APP_BASE_API) + params.url,
data: params.data,
method: params.method === undefined ? 'POST' : params.method,
url: (params.domain ? params.domain : import.meta.env.VITE_APP_BASE_API) + params.url, // 拼接完整的请求URL
data: params.data, // 请求体数据
method: params.method === undefined ? 'POST' : params.method, // 默认请求方法为POST
success: (res) => {
const responseData = res.data
// 请求小程序码
// 特殊处理小程序码请求
if (params.responseType === 'arraybuffer' && res.statusCode === 200) {
return resolve(responseData)
}
// 00000 请求成功
// A00002 用于直接显示提示系统的成功,内容由输入决定
if (responseData.code === '00000' || responseData.code === 'A00002') {
// 根据响应码处理不同情况
switch (responseData.code) {
case '00000': // 请求成功
case 'A00002': // 系统成功的提示信息
resolve(responseData)
}
// A00004 未授权
if (responseData.code === 'A00004') {
// 重设登录后跳转地址
break;
case 'A00004': // 未授权
// 重设登录后跳转地址,并清除相关存储
loginMethods.setRouteUrlAfterLogin()
uni.removeStorageSync('expiresTimeStamp')
uni.removeStorageSync('loginResult')
uni.removeStorageSync('Token')
// 如果允许转向登录页面,则显示模态框提示用户重新登录
if (!params.dontTrunLogin) {
uni.showModal({
title: '提示',
@ -69,36 +77,34 @@ const http = {
})
}
resolve(responseData)
}
// A00005 服务器出了点小差
if (responseData.code === 'A00005') {
break;
case 'A00005': // 服务器错误
this.onRequestFail(params, responseData)
uni.showToast({
title: '服务器出了点小差~',
icon: 'none'
})
}
// 其他异常码
// A00001 用于直接显示提示用户的错误,内容由输入内容决定
// A04001 社交账号未绑定
// A00012 tempUid错误
// A00006 验证码错误
if (responseData.code === 'A04001' || responseData.code === 'A00001' || responseData.code === 'A00012' || responseData.code === 'A00006') {
break;
case 'A04001': // 社交账号未绑定
case 'A00001': // 直接显示提示用户的错误
case 'A00012': // tempUid错误
case 'A00006': // 验证码错误
if (!params.hasCatch) {
uni.showToast({
title: responseData.msg || responseData.data || 'Error',
icon: 'none'
})
}
}
reject(responseData)
break;
default:
if (responseData.code !== '00000') {
reject(responseData)
}
}
},
fail: (err) => {
// 请求失败时显示提示并拒绝Promise
uni.showToast({
title: '请求失败'
})
@ -107,56 +113,70 @@ const http = {
})
})
},
/**
* 获取购物车商品数量并更新tabBar徽章
*/
getCartCount: () => {
if (!uni.getStorageSync('Token')) {
util.removeTabBadge()
util.removeTabBadge() // 如果没有token移除购物车徽章
return
}
http.request({
url: '/p/shopCart/prodCount',
method: 'GET',
dontTrunLogin: true,
dontTrunLogin: true, // 不需要转向登录
data: {}
})
.then(({ data }) => {
if (data > 0) {
uni.setTabBarBadge({
index: 2,
text: data + ''
index: 2, // 假设购物车位于第三个tab
text: data + '' // 显示商品数量
})
getApp().globalData.totalCartCount = data
getApp().globalData.totalCartCount = data // 更新全局购物车数量
} else {
uni.removeTabBarBadge({
index: 2
index: 2 // 移除购物车徽章
})
getApp().globalData.totalCartCount = 0
}
})
},
/**
* 请求失败时的日志记录
* @param {Object} params - 请求参数
* @param {Object} responseData - 响应数据
*/
onRequestFail: (params, responseData) => {
console.error('============== 请求异常 ==============')
console.log('接口地址: ', params.url)
console.log('异常信息: ', responseData)
console.error('============== 请求异常 end ==========')
},
/**
* 登录成功后执行
* @param {Object} result 登录成功返回的数据
* @param {Object} fn 登录成功后的回调
* 登录成功后的处理逻辑
* @param {Object} result - 登录成功返回的数据
* @param {Function} fn - 登录成功后的回调函数
*/
loginSuccess: (result, fn) => {
// 保存登陆信息
// 保存登录信息到本地存储
wx.setStorageSync('loginResult', result)
// 保存成功登录标识,token过期判断
// 保存成功登录标识用于判断token是否过期
wx.setStorageSync('hadLogin', true)
const expiresTimeStamp = result.expiresIn * 1000 / 2 + new Date().getTime()
const expiresTimeStamp = result.expiresIn * 1000 / 2 + new Date().getTime() // 计算token过期时间
// 缓存token的过期时间
uni.setStorageSync('expiresTimeStamp', expiresTimeStamp)
wx.setStorageSync('Token', result.accessToken) // 把token存入缓存请求接口数据时要用
// 将accessToken保存到本地存储以便后续请求使用
wx.setStorageSync('Token', result.accessToken)
// 执行登录成功后的回调函数
if (fn) {
fn()
}
}
}
export default http

@ -1,37 +1,58 @@
const loginMethods = {
/**
* 刷新token
* 当用户的Token即将过期时自动发起请求以获取新的Token
* 这个过程会检查当前Token是否有效以及是否正在进行刷新操作以避免重复请求
*/
refreshToken: () => {
// 从本地存储中获取Token的过期时间戳
const expiresTimeStamp = uni.getStorageSync('expiresTimeStamp')
// 如果没有有效的Token或过期时间戳则直接返回不进行刷新
if (!uni.getStorageSync('Token') || !expiresTimeStamp) return
// 检查Token是否已经过期
const isExpires = expiresTimeStamp < new Date().getTime()
// 检查是否已经有刷新Token的操作正在进行
const isRefreshing = uni.getStorageSync('isRefreshingToken')
// 如果Token未过期或已有刷新操作正在进行则直接返回
if (!isExpires || isRefreshing) {
return
}
// 设置标志位表示正在刷新Token防止并发刷新
uni.setStorageSync('isRefreshingToken', true)
// 发起刷新Token的请求
http.request({
url: '/token/refresh',
method: 'POST',
login: true,
isRefreshing: true,
dontTrunLogin: true
url: '/token/refresh', // 刷新Token的API接口
method: 'POST', // 使用POST方法发送请求
login: true, // 标记这是一个与登录相关的请求
isRefreshing: true, // 标记这是刷新Token的请求
dontTrunLogin: true // 即使刷新失败也不跳转到登录页面
}).then(res => {
// 刷新成功后更新本地存储中的Token过期时间
uni.setStorageSync('isRefreshingToken', false)
uni.setStorageSync('expiresTimeStamp', res.data * 1000 / 2 + new Date().getTime())
}).catch(() => {
// 刷新失败后,重置标志位
uni.setStorageSync('isRefreshingToken', false)
})
},
/**
* 设置登录后的跳转地址
* 在用户登录之前记录当前页面的路径以便在登录成功后能够返回到该页面
*/
setRouteUrlAfterLogin: () => {
// 获取当前所有页面的栈
const pages = getCurrentPages()
// 登录后的回跳地址
// 确保当前页面不是登录页面(避免循环跳转)
if (pages[pages.length - 1].route.indexOf('user-login') === -1) {
// 记录当前页面的完整路径,用于登录成功后的跳转
uni.setStorageSync('routeUrlAfterLogin', pages[pages.length - 1].$page.fullPath)
}
}

@ -1,34 +1,71 @@
const util = {
/**
* 格式化日期时间
* 将传入的Date对象格式化为"YYYY/MM/DD HH:MM:SS"的字符串形式
* @param {Date} date - 要格式化的日期对象
* @returns {String} - 格式化后的日期时间字符串
*/
formatTime: date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
const year = date.getFullYear() // 获取年份
const month = date.getMonth() + 1 // 获取月份注意getMonth()返回的是0-11所以需要加1
const day = date.getDate() // 获取日期
const hour = date.getHours() // 获取小时
const minute = date.getMinutes() // 获取分钟
const second = date.getSeconds() // 获取秒
// 使用formatNumber方法确保所有时间部分都是两位数并用'/'连接日期部分,':'连接时间部分
return [year, month, day].map(util.formatNumber).join('/') + ' ' + [hour, minute, second].map(util.formatNumber).join(':')
},
/**
* 格式化数字
* 确保传入的数字是两位数如果不是则在前面补零
* @param {Number|String} n - 需要格式化的数字
* @returns {String} - 格式化后的两位数字符串
*/
formatNumber: n => {
n = n.toString()
return n[1] ? n : '0' + n
n = n.toString() // 将数字转换为字符串
return n[1] ? n : '0' + n // 如果长度大于1则直接返回否则在前面补零
},
/**
* 格式化HTML内容
* 对传入的HTML字符串进行样式调整以适应移动端显示特别是处理图片表格等内容的宽度和布局
* @param {String} content - 需要格式化的HTML字符串
* @returns {String} - 格式化后的HTML字符串
*/
formatHtml: content => {
if (!content) {
return
return // 如果没有内容,直接返回
}
// 替换<p>标签,设置最大宽度、单词换行等样式
content = content.replace(/<p/gi, '<p style="max-width:100% !important;word-wrap:break-word;word-break:break-word;" ')
// 替换<img>标签,设置最大宽度、高度自适应、边距等样式
content = content.replace(/<img/gi, '<img style="max-width:100% !important;height:auto !important;margin:0;display:flex;" ')
// 替换<style>标签中的样式,确保内部样式不会覆盖外部样式
content = content.replace(/style="/gi, 'style="max-width:100% !important;table-layout:fixed;word-wrap:break-word;word-break:break-word;')
// 替换<table>标签,设置表格布局和单词换行等样式
content = content.replace(/<table/gi, '<table style="table-layout:fixed;word-wrap:break-word;word-break:break-word;" ')
// 替换<td>标签,设置单元格间距、填充、边框等样式
content = content.replace(/<td/gi, '<td cellspacing="0" cellpadding="0" style="border-width:1px; border-style:solid; border-color:#666; margin: 0px; padding: 0px;"')
// 移除width和height属性防止图片和表格固定宽度或高度
content = content.replace(/width=/gi, 'sss=')
content = content.replace(/height=/gi, 'sss=')
// 确保自闭合标签如<img>有正确的样式
content = content.replace(/\/>/gi, ' style="max-width:100% !important;height:auto !important;margin:0;display:block;" />')
return content
},
return content // 返回格式化后的HTML字符串
}
}
export default util
/**
* 移除购物车Tabbar的数字
*/

Loading…
Cancel
Save