
【主 题】:测试支付接口 【描 述】: [原因]: [过程]:成功从 从后端拿去到 支付签名 并且测试调用 微信支付接口成功 [影响]: 【结 束】 # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动
261 lines
6.5 KiB
JavaScript
261 lines
6.5 KiB
JavaScript
import mqtt from './mqtt.js'
|
||
/**
|
||
* Parse the time to string
|
||
* @param {(Object|string|number)} time
|
||
* @param {string} cFormat
|
||
* @returns {string | null}
|
||
*/
|
||
export function parseTime(time, cFormat) {
|
||
if (arguments.length === 0 || !time) {
|
||
return null
|
||
}
|
||
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
||
let date
|
||
if (typeof time === 'object') {
|
||
date = time
|
||
} else {
|
||
if ((typeof time === 'string')) {
|
||
if ((/^[0-9]+$/.test(time))) {
|
||
time = parseInt(time)
|
||
} else {
|
||
time = time.replace(new RegExp(/-/gm), '/')
|
||
}
|
||
}
|
||
|
||
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
||
time = time * 1000
|
||
}
|
||
date = new Date(time)
|
||
}
|
||
const formatObj = {
|
||
y: date.getFullYear(),
|
||
m: date.getMonth() + 1,
|
||
d: date.getDate(),
|
||
h: date.getHours(),
|
||
i: date.getMinutes(),
|
||
s: date.getSeconds(),
|
||
a: date.getDay()
|
||
}
|
||
// eslint-disable-next-line camelcase
|
||
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
||
const value = formatObj[key]
|
||
// Note: getDay() returns 0 on Sunday
|
||
if (key === 'a') {
|
||
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
||
}
|
||
return value.toString().padStart(2, '0')
|
||
})
|
||
// eslint-disable-next-line camelcase
|
||
return time_str
|
||
}
|
||
|
||
/**
|
||
* 过滤时间 格式化当天时间 比如把"20:00:00" 格式化成(晚上)8:00
|
||
* @param {string} value 时间格式位"20:00:00"
|
||
*/
|
||
export function formatTime(value) {
|
||
if (!value) return '' // 如果时间为空,则返回空字符串
|
||
|
||
// 将时间字符串转换为 Date 对象
|
||
let time = new Date("2000/01/01 " + value)
|
||
|
||
// 获取小时和分钟
|
||
let hour = time.getHours()
|
||
let minute = time.getMinutes()
|
||
|
||
// 根据小时判断是上午还是下午
|
||
let period = hour >= 12 ? '下午' : '上午'
|
||
|
||
// 格式化小时,如果是下午,则减去12小时
|
||
hour = hour % 12 === 0 ? 12 : hour % 12
|
||
|
||
// 格式化分钟,补零操作
|
||
minute = minute.toString().padStart(2, '0')
|
||
|
||
return `(${period})${hour}:${minute}`
|
||
}
|
||
/**
|
||
* 过滤价格数值 超过小数点两位得 保留两位
|
||
* @param {string} value
|
||
*/
|
||
export function formatPrice(value) {
|
||
if (isNaN(value)) return '' // 如果不是数字,则返回空字符串
|
||
|
||
// 格式化价格为保留两位小数
|
||
let formattedPrice = parseFloat(value).toFixed(2)
|
||
|
||
// 如果价格有两位小数以上,则保留两位小数,否则显示整数或者一位小数
|
||
let decimalCount = (formattedPrice.split('.')[1] || []).length
|
||
if (decimalCount > 2) {
|
||
formattedPrice = parseFloat(formattedPrice).toFixed(2)
|
||
}
|
||
|
||
return formattedPrice
|
||
}
|
||
/**
|
||
* 计算单spu 的单价 ps: spu可能有单sku组成 也可能
|
||
* @param {string} value
|
||
*/
|
||
export function calculateTotal(order) {
|
||
// 检查参数是否有效
|
||
if (!order || typeof order !== 'object' || !order.spu_id || !order.skuG || !order.countG || !order
|
||
.priceG) {
|
||
return ''
|
||
}
|
||
// 计算总价
|
||
let total = 0
|
||
for (let i = 0; i < order.skuG.length; i++) {
|
||
let price = parseFloat(order.priceG[i])
|
||
if (!isNaN(price)) {
|
||
total += price
|
||
}
|
||
}
|
||
// 保留两位小数并返回
|
||
return total.toFixed(2)
|
||
}
|
||
|
||
/**
|
||
* 计算单购物车列表商品的价格总和
|
||
* @param {string} cartList 购物车列表
|
||
* @returns {string | null} 价格总和
|
||
*/
|
||
export function totalPrice(cartList) {
|
||
let totalPrice = 0
|
||
if (cartList.length !== 0) {
|
||
cartList.forEach(item => {
|
||
for (let i = 0; i < item.priceG.length; i++) {
|
||
totalPrice += item.priceG[i] * item.countG[i]
|
||
}
|
||
})
|
||
}
|
||
return Number(totalPrice) // 将 totalPrice 转换为小数点后两位的格式
|
||
}
|
||
/**
|
||
* 检查用户信息登录 信息
|
||
* @param {obj} store vex $store对象
|
||
*/
|
||
export function checkUserInfo(store) {
|
||
//检查是否已经标记登录了
|
||
if (!store.state.loginState) { //没有标记
|
||
//先从storage里调取token
|
||
uni.getStorage({
|
||
key: 'userInfo',
|
||
success: (res) => {
|
||
store.commit('setUserInfo', res.data) //token拿到内存当中
|
||
},
|
||
fail: (err) => { //storage没有token进行登录
|
||
wxLogin(store)
|
||
}
|
||
})
|
||
//再进行检测
|
||
isTokenValid(store).then((isValid) => { //请求判断token是否有效
|
||
if (isValid) { //如果token有效
|
||
store.commit('setLoginState', true) //标记用户已成功登录
|
||
initMqtt(store) //mqtt初始化 连接 并 订阅
|
||
console.log('hi')
|
||
} else { //仍然无效 则最终跳转到 登陆页面
|
||
store.commit('clearUserInfo') //清除内存中的token
|
||
wxLogin(store)
|
||
uni.redirectTo({
|
||
url: '/pages/main/login'
|
||
})
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 微信登录 ps;请求接口 判断token是否有效
|
||
* @param {obj} store vex $store对象
|
||
* @return true有效 false无效
|
||
*/
|
||
async function isTokenValid(store) {
|
||
const res = await uni.$u.http.post('/Api/Check/index', {}, {
|
||
header: {
|
||
'Token': store.state.userInfo.token,
|
||
'Content-Type': 'application/x-www-form-urlencoded'
|
||
}
|
||
})
|
||
// 根据响应状态判断 token 是否有效
|
||
if (res.status === -1) {
|
||
return false
|
||
} else {
|
||
return true
|
||
}
|
||
}
|
||
/**
|
||
* 微信登录 ps;无感登录
|
||
* @param {obj} store vex $store对象
|
||
*/
|
||
export function wxLogin(store) {
|
||
uni.login({
|
||
provider: 'weixin',
|
||
success: res => {
|
||
if (res.code) {
|
||
uni.$u.http.post('/Api/Login/authLogin', {
|
||
code: res.code
|
||
}, {
|
||
header: {
|
||
'Content-Type': 'application/x-www-form-urlencoded'
|
||
}
|
||
}).then(res => {
|
||
// 登录接口访问成功后
|
||
if (res.data.status === 0) {
|
||
uni.showToast({
|
||
title: res.data.msg,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||
url: '/pages/main/login'
|
||
})
|
||
} else if (res.data.status === 1) {
|
||
//写入 用户信息缓存
|
||
uni.setStorage({
|
||
key: 'userInfo',
|
||
data: res.data.userInfo,
|
||
success: () => {
|
||
//登录成功后 把用户信息提取到内存
|
||
store.commit('setUserInfo', res.data.userInfo)
|
||
}
|
||
})
|
||
}
|
||
})
|
||
} else {
|
||
uni.showToast({
|
||
title: '登录态失败',
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||
url: '/pages/main/login'
|
||
})
|
||
}
|
||
},
|
||
fail: err => {
|
||
uni.showToast({
|
||
title: '登录态失败',
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||
url: '/pages/main/login'
|
||
})
|
||
}
|
||
})
|
||
}
|
||
/**
|
||
* 微信登录 ps;订阅主题
|
||
* @param {obj} store vex $store对象
|
||
*/
|
||
function initMqtt(store) {
|
||
/* mqtt */
|
||
mqtt.mqttConf() // 连接mqtt
|
||
// 订阅游客下单频道
|
||
const topic = `refreshQuestList/${store.state.userInfo.topic_prefix}`
|
||
mqtt.doSubscribe(topic, (res) => {
|
||
if (res.topic.indexOf(topic) > -1) {
|
||
console.log('新订单')
|
||
}
|
||
})
|
||
} |