调用微信小程序发券插件

1、开发流程

2、小程序发券插件API(注意,最下面的参与签名字段说明
在这里插入图片描述

3、签名算法规范

4、在线生成 sign 签名
在这里插入图片描述

5、自己写生成 sign 接口(下面就不多注释了)

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.decathlon.easypromotion.Controller;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.http.HttpUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* 公共接口
*
* @author 陶攀峰
* @version 1.0
* @date 2021/3/15 下午5:54
*/
@RestController
@RequestMapping("common")
public class CommonController {

@PostMapping("sign")
public Map<String, String> sign(@RequestBody TreeMap<String, Object> requestBody) {
// 0、先导入 hutool 依赖
// <dependency>
// <groupId>cn.hutool</groupId>
// <artifactId>hutool-all</artifactId>
// <version>5.5.7</version>
// </dependency>

// 1、获取 key
String key = (String) requestBody.remove("key");

// 2、拼接除了 key 以外的 k-v(按照 k 顺序拼接)
String url = HttpUtil.toParams(requestBody);

// 3、再拼接 key:(k-v)
url = url + "&key=" + key;

// 4、使用 HmacSHA256 转大写,得到 sign
String sign = new HMac(HmacAlgorithm.HmacSHA256, key.getBytes()).digestHex(url).toUpperCase();

Map<String, String> map = new HashMap<>(16);
map.put("datetime", LocalDateTime.now().toString().replace("T", " "));
map.put("sign", sign);
return map;
}
}

在这里插入图片描述