调用微信小程序发券插件 发表于 2021-03-15 | 分类于 ---对接其他平台服务 | 1、开发流程 2、小程序发券插件API(注意,最下面的参与签名字段说明) 3、签名算法规范 4、在线生成 sign 签名 5、自己写生成 sign 接口(下面就不多注释了) 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253package 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; }}