123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- const app = getApp()
- Page({
- onShareAppMessage() {
- return {
- title: '发起支付',
- path: 'packageAPI/pages/api/request-payment/request-payment'
- }
- },
- onLoad() {
- this.setData({
- theme: wx.getSystemInfoSync().theme || 'light'
- })
- if (wx.onThemeChange) {
- wx.onThemeChange(({theme}) => {
- this.setData({theme})
- })
- }
- },
- requestPayment() {
- const self = this
- self.setData({
- loading: true
- })
- // 此处需要先调用wx.login方法获取code,然后在服务端调用微信接口使用code换取下单用户的openId
- // 具体文档参考https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html?t=20161230#wxloginobject
- app.getUserOpenId(function (err, openid) {
- if (!err) {
- wx.cloud.callFunction({
- name: 'pay',
- data: {
- theme: 'light',
- action: 'unifiedorder',
- userInfo: {
- openId: openid
- },
- price: 0.01
- },
- success: res => {
- console.warn('[云函数] [openapi] templateMessage.send 调用成功:', res)
- const data = res.result.data
- wx.requestPayment({
- timeStamp: data.time_stamp,
- nonceStr: data.nonce_str,
- package: `prepay_id=${data.prepay_id}`,
- signType: 'MD5',
- paySign: data.sign,
- success: () => {
- wx.showToast({title: '支付成功'})
- },
- fail(err) {
- self.setData({
- loading: false
- })
- console.error('支付失败:', err)
- }
- })
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '支付失败',
- })
- console.error('[云函数] [openapi] templateMessage.send 调用失败:', err)
- }
- })
- } else {
- console.log('err:', err)
- self.setData({
- loading: false
- })
- }
- })
- }
- })
|