From 7e90cc1757774ea97f3167e15f471c2c23655355 Mon Sep 17 00:00:00 2001 From: Xubx <1827135378@qq.com> Date: Wed, 4 Sep 2024 23:56:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 2 +- cloud/pay/config.json | 6 + cloud/pay/index.js | 22 ++++ cloud/pay/package.json | 14 +++ cloud/pay_success/config.json | 6 + cloud/pay_success/index.js | 25 +++++ cloud/pay_success/package.json | 14 +++ page/component/details/details.js | 4 +- page/component/index.js | 49 +++++---- page/component/index.wxml | 2 +- page/component/myOrders/myOrder.wxml | 2 +- page/component/orders/orders.js | 158 +++++++++++++++------------ project.config.json | 4 +- 13 files changed, 209 insertions(+), 99 deletions(-) create mode 100644 cloud/pay/config.json create mode 100644 cloud/pay/index.js create mode 100644 cloud/pay/package.json create mode 100644 cloud/pay_success/config.json create mode 100644 cloud/pay_success/index.js create mode 100644 cloud/pay_success/package.json diff --git a/app.js b/app.js index 693ded8..4e29f73 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,4 @@ -const baseUrl = "http://localhost:8080" +const baseUrl = "http://localhost:8082" App({ diff --git a/cloud/pay/config.json b/cloud/pay/config.json new file mode 100644 index 0000000..5ecc33e --- /dev/null +++ b/cloud/pay/config.json @@ -0,0 +1,6 @@ +{ + "permissions": { + "openapi": [ + ] + } +} \ No newline at end of file diff --git a/cloud/pay/index.js b/cloud/pay/index.js new file mode 100644 index 0000000..b204369 --- /dev/null +++ b/cloud/pay/index.js @@ -0,0 +1,22 @@ +// 云函数入口文件 +const cloud = require('wx-server-sdk') + +cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境 + +// 云函数入口函数 +exports.main = async (event, context) => { + console.log("有进来") + const res = await cloud.cloudPay.unifiedOrder({ + "functionName": "pay_success", // 支付结果通知回调云函数名 + "envId": "zdt2024-9g7pr48u64f887f0", // 结果通知回调云函数环境 + "subMchId": "1684540409", // 商户号 + "nonceStr": event.nonceStr,//随机字符串,主要保证签名不可预测 + "body": "解锁悄悄话", // 商品描述 + "outTradeNo": event.outTradeNo, // 商户订单号 + "totalFee": event.totalFee, // 总金额 + "spbillCreateIp": "127.0.0.1", // 终端 IP,社区说可以随便填,不知道为什么,可能会出bug + "tradeType": "JSAPI",//交易类型 + }) + console.log("这里" + res) + return res +} \ No newline at end of file diff --git a/cloud/pay/package.json b/cloud/pay/package.json new file mode 100644 index 0000000..ddde2ea --- /dev/null +++ b/cloud/pay/package.json @@ -0,0 +1,14 @@ +{ + "name": "pay", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "wx-server-sdk": "latest" + } +} \ No newline at end of file diff --git a/cloud/pay_success/config.json b/cloud/pay_success/config.json new file mode 100644 index 0000000..5ecc33e --- /dev/null +++ b/cloud/pay_success/config.json @@ -0,0 +1,6 @@ +{ + "permissions": { + "openapi": [ + ] + } +} \ No newline at end of file diff --git a/cloud/pay_success/index.js b/cloud/pay_success/index.js new file mode 100644 index 0000000..54ed3e6 --- /dev/null +++ b/cloud/pay_success/index.js @@ -0,0 +1,25 @@ +// 云函数入口文件 +const cloud = require('wx-server-sdk') + +cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境 + +// 云函数入口函数 +exports.main = async (event, context) => { + const returnCode = event.returnCode + const openid = event.userInfo.openid + const orderid = event.outTradeNo + const db = cloud.database(); + if (returnCode == 'SUCCESS') { + //支付成功的处理逻辑 + // await db.collection("orders").where({ + // _id: orderid, + // _openid: openid, + // }).update({ + // data: { + // pay_status: true + // } + // }) + const res = { errCode: 0, errmag: "6666666办款完毕!" } + return res + } +} \ No newline at end of file diff --git a/cloud/pay_success/package.json b/cloud/pay_success/package.json new file mode 100644 index 0000000..de37b07 --- /dev/null +++ b/cloud/pay_success/package.json @@ -0,0 +1,14 @@ +{ + "name": "pay_success", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "wx-server-sdk": "latest" + } +} \ No newline at end of file diff --git a/page/component/details/details.js b/page/component/details/details.js index 153ee81..b20a7a0 100644 --- a/page/component/details/details.js +++ b/page/component/details/details.js @@ -11,7 +11,7 @@ Page({ onLoad(options) { const goods = { - id: options.id, + id: options.goodsId, goodName: options.goodName, goodPrice: options.goodPrice, goodDetail: options.goodDetail, @@ -109,7 +109,7 @@ Page({ const { id, goodName, goodImage, goodPrice } = this.data.goods; const num = this.data.buyNum; - const url = `../orders/orders?id=${id}&title=${goodName}&image=${goodImage[0]}&price=${goodPrice}&num=${num}&specs=${this.data.selectedSpecs.join(',')}`; + const url = `../orders/orders?goodsId=${id}&title=${goodName}&image=${goodImage[0]}&price=${goodPrice}&num=${num}&specs=${this.data.selectedSpecs.join(',')}`; wx.navigateTo({ url }); // }, diff --git a/page/component/index.js b/page/component/index.js index deb7121..f314f91 100644 --- a/page/component/index.js +++ b/page/component/index.js @@ -45,32 +45,31 @@ Page({ 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', - }); + console.log("查到的用户信息" + res.data.data) + if (res.data.data != null) { + // 将用户信息存到 StorageSync + const userInfo = { + avatarUrl: res.data.data.avatarUrl, + nickName: res.data.data.username + }; + wx.setStorageSync('userInfo', userInfo); // 存储到本地 + } else { + wx.showModal({ + title: '温馨提示', + content: '请先登录!', + showCancel: false, + success: (res) => { + if (res.confirm) { + console.log(res) + wx.navigateTo({ + url: '/page/component/login/login', + }); + } } - } - }); + }) + } } }) - } }, //获取openId @@ -80,12 +79,14 @@ Page({ success: (res) => { const wxConfig = { appid: "wx865aefa5a7115ae0", - secret: "3f9849429894435abc935eea88178dfd", + secret: "df0817d59696a6160de2770222d8ec53", code: res.code } + console.log("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) => { + console.log("openid:" + res.data.openId) wx.setStorageSync('openid', res.data.openid); }, fail: (err) => { diff --git a/page/component/index.wxml b/page/component/index.wxml index 6bb4604..cad3bc2 100644 --- a/page/component/index.wxml +++ b/page/component/index.wxml @@ -39,7 +39,7 @@ - + {{item.goodName}} diff --git a/page/component/myOrders/myOrder.wxml b/page/component/myOrders/myOrder.wxml index 230cb75..98be691 100644 --- a/page/component/myOrders/myOrder.wxml +++ b/page/component/myOrders/myOrder.wxml @@ -1,6 +1,6 @@ - + diff --git a/page/component/orders/orders.js b/page/component/orders/orders.js index fa24f47..1dbf9b1 100644 --- a/page/component/orders/orders.js +++ b/page/component/orders/orders.js @@ -6,7 +6,7 @@ const md5 = require('js-md5'); Page({ onLoad: function (options) { const orders = { - id: options.id, + goodsId: options.goodsId, title: options.title, price: options.price, num: options.num, @@ -70,27 +70,14 @@ Page({ } 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() { const self = this; const orderData = this.data.orders; // 假设用户ID暂时写死 - orderData.userId = 1; + orderData.openId = wx.getStorageSync('openid'); orderData.status = 1; - + console.log("orderData", orderData) // 生成订单并请求支付 wx.request({ url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址 @@ -103,63 +90,96 @@ Page({ }, success(res) { if (res.statusCode === 200) { - wx.showToast({ - title: "订单已提交", - icon: "success", - duration: 2000, + console.log(res) + wx.cloud.init({ + env: 'zdt2024-9g7pr48u64f887f0', //填上你的云开发环境id + traceUser: true, }); - - // 假设服务器返回的支付信息中包含 prepay_id - 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", + const outTradeNo = res.data.data.orderCode + const orderId = res.data.data.id + wx.cloud.callFunction({ + name: "pay", + data: { + nonceStr: randomString, //随机字符串,String(32) + outTradeNo: outTradeNo, //商户订单号,String(32) + totalFee: self.data.total, //Int + }, + success: res => { + console.log('下单结果: ', res); + // 获取到预付单信息 + const payment = res.result.payment + wx.hideLoading(); + // 唤起微信支付组件,完成支付 + wx.requestPayment({ + ...payment, //把payment展开 + success(res) { + // 支付成功回调,实现自定义的业务逻辑 + console.log('唤起支付组件成功:', res); + // 将订单的status改为已支付 + orderData.status = 2; + orderData.id = orderId; + wx.request({ + url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址 + method: "POST", + data: { + ...orderData, + }, + header: { + "Content-Type": "application/json", // 设置请求头 + }, + success(res) { + wx.showToast({ + title: "订单支付成功", + icon: "success", + duration: 2000, + }); + wx.switchTab({ + url: '/page/component/index', // 替换为你的首页路径 + }); + } + }) + }, + fail(err) { + // 支付失败回调 + console.error('唤起支付组件失败:', err); + // 将订单的status改为已关单 + orderData.status = 0; + orderData.id = orderId; + wx.request({ + url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址 + method: "POST", + data: { + ...orderData, + }, + header: { + "Content-Type": "application/json", // 设置请求头 + }, + success(res) { + wx.showToast({ + title: "订单支付成功", + icon: "success", + duration: 2000, + }); + wx.switchTab({ + url: '/page/component/index', // 替换为你的首页路径 + }); + } + }) + wx.showToast({ + title: "支付失败", + icon: "error", + duration: 2000, + }); + } }); }, - fail(ress) { - console.log('支付失败', ress); - wx.showToast({ - title: "支付失败,请重试", - icon: "none", - duration: 2000, - }); - }, - complete(ress) { - console.log('支付流程结束', ress); + fail: err => { + console.log(err) } - }); - - } else { - wx.showToast({ - title: "提交失败", - icon: "none", - duration: 2000, - }); + }) } - }, - fail() { - wx.showToast({ - title: "请求失败", - icon: "none", - duration: 2000, - }); - }, - }); - }, - + } + }) + } }) diff --git a/project.config.json b/project.config.json index 8291bcf..5111fb3 100644 --- a/project.config.json +++ b/project.config.json @@ -1,5 +1,6 @@ { "description": "项目配置文件。", + "cloudfunctionRoot": "cloud/", "packOptions": { "ignore": [], "include": [] @@ -33,5 +34,6 @@ "editorSetting": { "tabIndent": "insertSpaces", "tabSize": 2 - } + }, + "cloudfunctionTemplateRoot": "cloudfunctionTemplate/" } \ No newline at end of file