EmmmuaCode EmmmuaCode
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)

wufan

海内存知己,天涯若比邻。
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)
  • HTML

    • HTML
  • CSS

    • CSS
    • CSS-02
    • CSS-03
    • CSS-浮动(float)
    • CSS-定位(position)
    • CSS3 新特性
    • CSS3 2D和3D的使用
  • JavaScript

    • JavaScript
    • JavaScript 变量
    • JavaScript 数据类型
    • JavaScript 运算符与语句
    • JavaScript For循环与数组
    • JavaScript 函数
    • JavaScript 对象
    • ES6
  • Vue

    • Vue 快速上手
  • uni-app

    • 从基础概念到uni-app项目搭建
    • 页面优化与数据处理的深入实践
    • Vuex 的应用与购物车功能实现
    • 购物车与结算区域的功能实现与优化
    • 购物车与结算区域的深入优化与功能完善
    • 微信支付功能的实现与流程
      • 3 微信支付
        • 3.1 请求头中添加 Token
        • 3.2 微信支付的流程
        • 3.3 创建订单
        • 4.4 订单预支付
        • 4.5 发起微信支付
  • studynotes
  • web
  • uni-app
wufan
2024-11-21
目录

微信支付功能的实现与流程

# 3 微信支付

# 3.1 请求头中添加 Token

  1. 原因说明:只有在登录之后才允许调用支付相关的接口,所以必须为有权限的接口添加身份认证的请求头字段







    fly.interceptors.request.use((config) => {
    	if (uni.getStorageSync('token')) {
    		config.headers.Authorization = uni.getStorageSync('token')
    	}
    })
    
    1
    2
    3
    4
    5

# 3.2 微信支付的流程

  1. 创建订单

    • 请求创建订单的 API 接口:把(订单金额、收货地址、订单中包含的商品信息)发送到服务器

    • 服务器响应的结果:订单编号

  2. 订单预支付

    • 请求订单预支付的 API 接口:把(订单编号)发送到服务器

    • 服务器响应的结果:订单预支付的参数对象,里面包含了订单支付相关的必要参数

  3. 发起微信支付

    • 调用 uni.requestPayment() 这个 API,发起微信支付;把步骤 2 得到的 “订单预支付对象” 作为参数传递给 uni.requestPayment() 方法

    • 监听 uni.requestPayment() 这个 API 的 success,fail,complete 回调函数

# 3.3 创建订单

  1. 改造 my-settle 组件中的 settlement 方法,当前三个判断条件通过之后,调用实现微信支付的方法:













     
     















    settlement() {
    	  // 1. 先判断是否勾选了要结算的商品
    	  if (!this.checkedCount) return uni.showToast({
    		  title: '请选择要结算的商品!',
    		  icon: 'none'
    	  })
    	
    	  // 2. 再判断用户是否选择了收货地址
    	  if (!this.addstr) return uni.showToast({
    		  title: '请选择收货地址!',
    		  icon: 'none'
    	  })
    	
    	  // 3. 最后判断用户是否登录了
    	  if (!this.token) return uni.showModal({
    		  title: '黑马优购',
    		  content: '还未登录,是否去登录页?',
    		  success(e) {
    			  if (e.confirm) {
    				  uni.switchTab({
    				  	url: "/pages/my/my"
    				  })
    			  }
    		  }
    	  })
    	  
    	  this.payOrder()
    	},
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
  2. 在 my-settle 组件中定义 payOrder 方法如下,先实现创建订单的功能:

    import fly from '../utils/flyio.js'
    
    export function login(data) {
    	return fly.post('/users/wxlogin', data)
    }
    
    export function createOrder(data) {
    	return fly.post('/my/orders/create', data)
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 微信支付
    async payOrder() {
      // 1. 创建订单
      // 1.1 组织订单的信息对象
      const orderInfo = {
        // 开发期间,注释掉真实的订单价格,
        // order_price: this.checkedGoodsAmount,
        // 写死订单总价为 1 分钱
        order_price: 0.01,
        consignee_addr: this.addstr,
        goods: this.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
      }
      // 1.2 发起请求创建订单
      const { data: res } = await createOrder(orderInfo)
      if (res.meta.status !== 200) return console.log('创建失败')
      // 1.3 得到服务器响应的“订单编号”
      const orderNumber = res.message.order_number
      console.log(orderNumber)
    
      // 2. 订单预支付
    
      // 3. 发起微信支付
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

# 4.4 订单预支付

  1. 封装api

    export function getPayParams(data) {
    	return fly.post('/my/orders/req_unifiedorder', data)
    }
    
    1
    2
    3
  2. 改造 my-settle 组件的 payOrder 方法,实现订单预支付功能:

    // 2. 订单预支付
    const payParams = await getPayParams({
      order_number: orderNumber
    })
    console.log(payParams)
    
    1
    2
    3
    4
    5

# 4.5 发起微信支付

  1. 改造 my-settle 组件的 payOrder 方法,实现微信支付的功能:

    // 3. 发起微信支付
    // 3. 发起微信支付
    // 3.1 调用 uni.requestPayment() 发起微信支付
    const [err, succ] = await uni.requestPayment(payParams.data.message.pay)
    // 3.2 未完成支付
    if (err) return uni.showToast({
      title: '订单未支付!',
      icon: 'none'
    })
    // 3.3 完成了支付,进一步查询支付的结果
    const { data: res3 } = await isPay({ order_number: orderNumber })
    // 3.4 检测到订单未支付
    if (res3.meta.status !== 200) return console.log('未支付')
    // 3.5 检测到订单支付完成
    uni.showToast({
      title: '支付完成!',
      icon: 'success'
    })
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
  2. isPay接口api封装

    export function isPay(data) {
    	return fly.post('/my/orders/chkOrder', data)
    }
    
    1
    2
    3
#uni-app#小程序
上次更新: 2024/11/21, 09:18:32
购物车与结算区域的深入优化与功能完善

← 购物车与结算区域的深入优化与功能完善

最近更新
01
购物车与结算区域的深入优化与功能完善
11-21
02
购物车与结算区域的功能实现与优化
11-21
03
Vuex 的应用与购物车功能实现
11-21
更多文章>
Theme by Vdoing | Copyright © 2023-2024 EmmmuaCode | 黔ICP备2022009864号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式