支付、地址优化

This commit is contained in:
Xubx 2024-09-03 23:14:06 +08:00
parent 6de50cf8b9
commit 663be8f39f
6 changed files with 165 additions and 35 deletions

View File

@ -14,6 +14,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@vant/weapp": "^1.11.6", "@vant/weapp": "^1.11.6",
"js-md5": "^0.8.3",
"tdesign-miniprogram": "^1.6.0" "tdesign-miniprogram": "^1.6.0"
} }
} }

View File

@ -16,7 +16,7 @@ Page({
url: baseUrl + "/user/getById", url: baseUrl + "/user/getById",
method: "GET", method: "GET",
data: { data: {
id: 3, openId: wx.getStorageSync('openid'),
}, },
success(res) { success(res) {
self.setData({ self.setData({
@ -36,14 +36,14 @@ Page({
formSubmit(e) { formSubmit(e) {
console.log(e, "e") console.log(e, "e")
const value = e.detail.value const value = e.detail.value
value.openId = wx.getStorageSync('openid');
if (value.username && value.phone && value.address) { if (value.username && value.phone && value.address) {
//保存地址 //保存地址
wx.request({ wx.request({
url: baseUrl + "/user/addOrUpdate", url: baseUrl + "/user/addOrUpdate",
method: "POST", method: "POST",
data: { data: {
...value, ...value
openId: wx.getStorageSync('openid'),
}, },
success(res) { success(res) {
console.log(res) console.log(res)

View File

@ -10,6 +10,8 @@ Page({
}, },
onLoad() { onLoad() {
var self = this var self = this
self.checkUserProfile()
self.getUserProfile()// //获取openId
/** /**
* 发起请求获取商品列表信息 * 发起请求获取商品列表信息
*/ */
@ -23,4 +25,74 @@ Page({
}, },
}) })
}, },
/**
* 检查本地缓存中是否有用户信息
*/
checkUserProfile() {
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
// 如果有用户信息,直接设置数据
this.setData({
thumb: userInfo.avatarUrl,
nickname: userInfo.nickName,
});
} else {
//如果本地没有存储用户信息则通过openId查数据库中是否已经注册
wx.request({
url: app.globalData.baseUrl + "/user/getById",
method: "GET",
data: {
openId: wx.getStorageSync('openid'),
},
success(res) {
console.log(res.data.data)
// 将用户信息存到 StorageSync
const userInfo = {
avatarUrl: res.data.data.avatarUrl,
nickName: res.data.data.username
};
wx.setStorageSync('userInfo', userInfo); // 存储到本地
},
fail: (err) => {
// 没有用户信息,提示用户登录授权
wx.showModal({
title: '温馨提示',
content: '请先登录!',
showCancel: false,
success: (res) => {
if (res.confirm) {
console.log(res)
wx.navigateTo({
url: '/page/component/login/login',
});
}
}
});
}
})
}
},
//获取openId
getUserProfile() {
//通过wx.login()获取登录凭证code
wx.login({
success: (res) => {
const wxConfig = {
appid: "wx865aefa5a7115ae0",
secret: "3f9849429894435abc935eea88178dfd",
code: res.code
}
wx.request({
url: "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxConfig.appid + "&secret=" + wxConfig.secret + "&code=" + wxConfig.code + "&js_code=" + wxConfig.code + '&grant_type=authorization_code',
success: (res) => {
wx.setStorageSync('openid', res.data.openid);
},
fail: (err) => {
console.error('wx.login 失败', err);
}
})
},
})
},
}) })

View File

@ -46,12 +46,12 @@ Page({
return; return;
} }
//将用户信息存到StorageSync //将用户信息存到StorageSync
// 将用户信息存到 StorageSync // 将用户信息存到 StorageSync
const userInfo = { const userInfo = {
avatarUrl: avatarUrl, avatarUrl: avatarUrl,
nickName: nickname nickName: nickname
}; };
wx.setStorageSync('userInfo', userInfo); // 存储到本地 wx.setStorageSync('userInfo', userInfo); // 存储到本地
// 将数据发送到后端 // 将数据发送到后端
wx.request({ wx.request({
url: app.globalData.baseUrl + "/user/addOrUpdate", // 替换为你的后端接口地址 url: app.globalData.baseUrl + "/user/addOrUpdate", // 替换为你的后端接口地址

View File

@ -1,5 +1,7 @@
const app = getApp() const app = getApp()
const baseUrl = app.globalData.baseUrl const baseUrl = app.globalData.baseUrl
const md5 = require('js-md5');
// page/component/orders/orders.js // page/component/orders/orders.js
Page({ Page({
onLoad: function (options) { onLoad: function (options) {
@ -19,6 +21,10 @@ Page({
hasAddress: false, hasAddress: false,
total: 0, total: 0,
orders: {}, orders: {},
config: {
appid: "wx865aefa5a7115ae0",
key: "d5a58d44588b42cbbe01daa5cfa4e792"
}
// {id:1,title:'新鲜芹菜 半斤',image:'/image/s5.png',num:4,price:0.01}, // {id:1,title:'新鲜芹菜 半斤',image:'/image/s5.png',num:4,price:0.01},
// {id:2,title:'素米 500g',image:'/image/s6.png',num:1,price:0.03} // {id:2,title:'素米 500g',image:'/image/s6.png',num:1,price:0.03}
}, },
@ -51,17 +57,42 @@ Page({
total: total, total: total,
}) })
}, },
timeStamp: function () {
return parseInt(new Date().getTime() / 1000) + ''
},
/* 随机数 */
randomString: function () {
var chars = 'A2345678';
var maxPos = chars.length;
var pwd = '';
for (var i = 0; i < 32; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
},
// 调起支付签名 这里我不太明白,虽然前面加载签名和后面验证,但里面加了随机数为什么验证还能通过我没还转过 弯来,希望大家能搞明白吧,到时候可不要吝啬留言讲解一下下
MixedencryMD5: function (data, randomString, timeStamp) {
const payString = "appId=" + this.data.config.appid +
"&nonceStr=" + randomString +
"&package=prepay_id=" + "data.prepay_id" +
"&signType=MD5" +
"&timeStamp=" + timeStamp +
"&key=" + this.data.config.key;
const hash = md5(payString); // 使用 md5 进行签名
console.log(hash);
return hash;
},
toPay() { toPay() {
const self = this const self = this;
// 假设订单信息在 this.data.orders 中 const orderData = this.data.orders;
const orderData = this.data.orders
// TODO 用户id暂时写死 // 假设用户ID暂时写死
orderData.userId = 1 orderData.userId = 1;
orderData.status = 1 orderData.status = 1;
console.log(orderData)
//先生成订单 // 生成订单并请求支付
wx.request({ wx.request({
// TODO 未测试
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址 url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
method: "POST", method: "POST",
data: { data: {
@ -76,17 +107,49 @@ Page({
title: "订单已提交", title: "订单已提交",
icon: "success", icon: "success",
duration: 2000, duration: 2000,
}) });
// 跳转到用户页面
wx.switchTab({ // 假设服务器返回的支付信息中包含 prepay_id
url: "/page/component/user/user", const paymentInfo = res.data.paymentInfo; // 服务器返回的支付信息,包含 prepay_id 等
})
// 生成支付签名等所需的参数(最好在服务器生成)
const time = self.timeStamp()
const randomString = self.randomString();
const parSigns = self.MixedencryMD5(paymentInfo, randomString, time);
// 发起支付请求
wx.requestPayment({
timeStamp: time,
nonceStr: randomString,
package: "prepay_id=" + "paymentInfo.prepay_id",
signType: "MD5",
paySign: parSigns,
success(ress) {
console.log('支付成功', ress);
// 支付成功后的处理,跳转到用户页面或其他页面
wx.switchTab({
url: "/page/component/user/user",
});
},
fail(ress) {
console.log('支付失败', ress);
wx.showToast({
title: "支付失败,请重试",
icon: "none",
duration: 2000,
});
},
complete(ress) {
console.log('支付流程结束', ress);
}
});
} else { } else {
wx.showToast({ wx.showToast({
title: "提交失败", title: "提交失败",
icon: "none", icon: "none",
duration: 2000, duration: 2000,
}) });
} }
}, },
fail() { fail() {
@ -94,16 +157,9 @@ Page({
title: "请求失败", title: "请求失败",
icon: "none", icon: "none",
duration: 2000, duration: 2000,
}) });
}, },
}) });
wx.showModal({
title: "提示",
content: "本系统只做演示,支付系统已屏蔽",
text: "center",
complete() {
// 发送订单数据到后端
},
})
}, },
}) })

View File

@ -11,7 +11,7 @@ Page({
onLoad() { onLoad() {
this.checkUserProfile(); // 检查并获取用户信息 this.checkUserProfile(); // 检查并获取用户信息
this.getUserProfile(); // this.getUserProfile();
}, },
/** /**
@ -33,7 +33,8 @@ Page({
showCancel: false, showCancel: false,
success: (res) => { success: (res) => {
if (res.confirm) { if (res.confirm) {
wx.switchTab({ console.log(res)
wx.navigateTo({
url: '/page/component/login/login', url: '/page/component/login/login',
}); });
} }