wiki-files/wg-basic/wecaht.md

184 lines
7.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 5.微信模块
description: 微信模块功能
published: true
date: 2021-09-01T09:54:01.973Z
tags: wg-basic, wecaht
editor: markdown
dateCreated: 2021-08-26T08:21:24.080Z
---
# 模块加载
微信模块目前一共有两个,分别是 `login-wechat``module-wechat` 。`login-wechat` 模块不能单独使用,`module-wechat` 模块可以单独使用。
两个模块依赖分别为
```xml
<dependency>
<groupId>ink.wgink</groupId>
<artifactId>login-wechat</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ink.wgink</groupId>
<artifactId>module-wechat</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
# 配置说明
为了能够使用微信功能模块,需要在配置文件中添加如下配置
```yml
open-platform:
wechat:
# 支付
pay:
# 激活状态
active: true
# 生产环境
produce: true
# 商家ID
mchid: 123123123123
# 证书路径 和 名称
certificate-path: /Users/xxxxxxx_cert
certificate-name: apiclient_cert.pem
# 公钥文件路径
key-file-path: /Users/xxxxxxx/apiclient_key.pem
# api v3 秘钥
api-v3-secretkey: 111111111111111111
# 小程序
mini-app:
# 激活状态
active: true
# 认证地址,固定
authorizeUrl: https://api.weixin.qq.com/sns/jscode2session
# 认证方式,固定
grantType: authorization_code
# 小程序的appKey
app-key: 123123123123123
# 小程序秘钥
app-secret: 232323232323232323
# 公众号
official-account:
# 激活状态
activate: false
# 公众号认证
authorize:
authorize-url: https://open.weixin.qq.com/connect/oauth2/authorize
access-token-url: https://api.weixin.qq.com/sns/oauth2/access_token
access-token-refresh-url: https://api.weixin.qq.com/sns/oauth2/refresh_token
userinfo-url: https://api.weixin.qq.com/sns/userinfo
response-type: code
scope: snsapi_userinfo
state: wechatRedirectUrl
grant-type: authorization_code
# token地址
access-token-url: https://api.weixin.qq.com/cgi-bin/token
# 公众号appid
app-id: 123123123123123
# 公众号secret
app-secret: 123123123123123123
grant-type: client_credential
config-token: WenG
```
# 模块内功能
## 1 小程序
### 1.1 小程序登录
小程序的登录流程如下:
1. 用户在打开小程序时,小程序需要调用后台的 `/wechat/sign/default` 接口,完成用户默认登录。
2. 接口会返回该用户的 token 和初始化状态,初始化状态为数字,通过下划线 `_` 与token拼接
3. 前端将返回值分割 token 和 初始化状态
4. 判断初始化状态,如果为 **1** 表示账户是初始化状态,**0** 表示账户是非初始化状态
5. 如果用户不需要绑定手机号则第4步可以省略
6. 如果需要绑定用户手机号,则需要在获得用户授权之后,调用 `/app/miniapp/update-phone` 接口完成用户绑定,在拿到结果之后,重新获取 **token**
> 这里需要说明的是:小程序默认登录时,会在系统创建随机账户,用户名密码都是随机的。当用户绑定过手机之后,会将随机的用户名修改为手机号。如果手机已经绑定,则提示失败,需要用户更换手机号或微信号。
> {.is-info}
### 1.2 更新个人信息
有时候,我们可能需要展示微信的头像和昵称,获取这些信息也需要用户对小程序进行授权后通过调用接口 `/wechat/app/miniapp/user/update-info` 接口来完成修改。
> 这里的修改只是修改了小程序用户信息,并没有对系统用户进行修改,系统中已经体提供了对应的接口,开发人员需要对其封装处理
> {.is-info}
### 1.3 用户新增自定义处理
在用户完成自动创建后,如果需要执行一些自定义操作,这时,开发人员可以实现 `ink.wgink.module.wechat.service.miniapp.IMiniappUserCreateHandleService` 接口来实现响应的业务逻辑。
## 2 公众号
暂无
## 3 支付
### 3.1 发起预支付
用户在发起支付之前,需要调用后台系统接口来生成预支付订单,这个订单需要后台调用微信的预支付订单接口来完成。
目前,模块中只对微信支付调用实现了封装,并提供了 `ink.wgink.module.wechat.service.pay.v3.jsapi.IJsapiService` 接口来方便开发人员调用。开发人员只需要引入这个接口,调用 `placeOrder` 下单方法即可完成预支付订单的创建。
### 3.2 监听支付回调
模块已经默认实现了支付的被动通知,但是,并没有做具体的业务,只是将通知结果进行了输出。开发人员如果想根据业务需要处理支付结果,可以实现 `ink.wgink.module.wechat.service.pay.v3.IPayNoticeService` 接口,并将实现业务类用 `@Primary` 注解标注。
### 3.3 查询账单情况
模块提供查询账单的业务功能,开发人员可以引入 `ink.wgink.module.wechat.pojo.pay.v3.order.OrderSearch.IOrderSearchService` 接口,调用其中的方法完成订单查询。
### 3.4 关闭订单
模块提供关闭订单业务功能,开发人员可以引入 `ink.wgink.module.wechat.service.pay.v3.order.IOrderCloseService` 接口,调用其中方法完成订单关闭
# 提供的接口与方法
## API接口
详细接口内容请见系统接口文档
| 接口名称 | 说明 |
| :-: | :-: |
| /wechat/sign/default | 默认登录接口 |
| /app/miniapp/update-phone | 绑定手机接口 |
| /wechat/app/miniapp/user/update-info | 更新微信头像、昵称接口 |
## 业务接口与方法
> ink.wgink.module.wechat.service.miniapp.IMiniappUserCreateHandleService
| 方法 | 参数 | 说明 | 返回结果 |
| :-: | :-: | :-: | :-: |
| handle | UserPO, openid | 1.用户信息2.小程序用户openid | 无返回结果,有问题抛出异常 |
> ink.wgink.module.wechat.service.pay.v3.jsapi.IJsapiService
| 方法 | 参数 | 说明 | 返回结果 |
| :-: | :-: | :-: | :-: |
| placeOrder | PayPlaceOrder | 预支订单内容,参数见微信支付文档,预支付接口 | 预支付交易会话标识预支付交易会话标识。用于后续接口调用中使用该值有效期在没有指定时2小时。还包括了小程序前端支付需要用到的参数。 |
> ink.wgink.module.wechat.service.pay.v3.IPayNoticeService
| 方法 | 参数 | 说明 | 返回结果 |
| :-: | :-: | :-: | :-: |
| handle | PayNoticeCiphertext | 解密后的密文,详情见微信支付文档回调接口 | 无 |
> ink.wgink.module.wechat.pojo.pay.v3.order.OrderSearch.IOrderSearchService
| 方法 | 参数 | 说明 | 返回结果 |
| :-: | :-: | :-: | :-: |
| getOrder | transactionId | 微信订单ID | 订单支付详情,详情见微信支付文档支付查询接口 |
| getBusinessOrder | businessOrderId | 自定义订单ID | 订单支付详情,详情见微信支付文档支付查询接口 |
> ink.wgink.module.wechat.service.pay.v3.order.IOrderCloseService
| 方法 | 参数 | 说明 | 返回结果 |
| :-: | :-: | :-: | :-: |
| closeOrder | businessOrderId | 自定义订单ID | 无返回值,出错会抛出异常 |