|
@@ -0,0 +1,159 @@
|
|
|
+package com.rf.psychological.mp.rest;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.rf.psychological.base.rest.BaseController;
|
|
|
+import com.rf.psychological.mp.model.JsapiSignature;
|
|
|
+import com.rf.psychological.mp.utils.MpUtils;
|
|
|
+import com.rf.psychological.opLog.annotation.OperationLogAnnotation;
|
|
|
+import com.rf.psychological.security.SafetyProcess;
|
|
|
+import com.rf.psychological.user.service.UserService;
|
|
|
+import com.rf.psychological.utils.Result;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author:zzf
|
|
|
+ * @Date:2024/7/17:19:05
|
|
|
+ * @Description:
|
|
|
+ */
|
|
|
+@CrossOrigin //跨域
|
|
|
+@RestController
|
|
|
+@Slf4j
|
|
|
+@RequestMapping("/mp/api")
|
|
|
+@Api(tags = "微信公众号")
|
|
|
+public class MpController extends BaseController {
|
|
|
+
|
|
|
+ @Value("${weixin.mptoken}")
|
|
|
+ private String mptoken;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MpUtils mpUtils;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String,String> redisTemplate;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 服务器验证接口
|
|
|
+ * @param signature 微信加密啊签名
|
|
|
+ * @param timestamp 时间戳
|
|
|
+ * @param nonce 随机数
|
|
|
+ * @param echostr 随机字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/message")
|
|
|
+ public String check(String signature,String timestamp,String nonce,String echostr){
|
|
|
+
|
|
|
+ log.info("signature:{} == timestamp:{} == nonce:{} == echostr:{}",signature,timestamp,nonce,echostr);
|
|
|
+ if (signature.equals(mpUtils.getSignature(mptoken,timestamp,nonce))){
|
|
|
+ return echostr;
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/code2accesstoken/{code}")
|
|
|
+ @ApiOperation("code2accesstoken")
|
|
|
+ @SafetyProcess
|
|
|
+ public Result code2accesstoken(@PathVariable String code){
|
|
|
+ JSONObject jsonObject = mpUtils.code2accesstoken(code);
|
|
|
+ return success(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/signature2")
|
|
|
+ //@OperationLogAnnotation("获取JS-SDK签名")
|
|
|
+ @ApiOperation(value = "获取JS-SDK签名")
|
|
|
+ public Result signature2(String url) {
|
|
|
+ String jsapiTicket = redisTemplate.opsForValue().get("jsapi_ticket");
|
|
|
+ Map<String, String> sign = sign(jsapiTicket, url);
|
|
|
+ JsapiSignature signature = new JsapiSignature();
|
|
|
+
|
|
|
+ signature.setUrl(sign.put("url", url));
|
|
|
+ signature.setNonceStr(sign.put("nonceStr", sign.get("nonceStr")));
|
|
|
+ signature.setTimestamp(Long.parseLong(Objects.requireNonNull(sign.put("timestamp", sign.get("timestamp")))));
|
|
|
+ signature.setSignature(sign.put("signature", sign.get("signature")));
|
|
|
+ return success(sign);
|
|
|
+ }
|
|
|
+ public static Map<String, String> sign(String jsapi_ticket, String url) {
|
|
|
+ Map<String, String> ret = new HashMap<String, String>();
|
|
|
+// String nonce_str = RandomUtils.getRandomStr();
|
|
|
+ String nonce_str = create_nonce_str();
|
|
|
+// double random = Math.random();
|
|
|
+// String nonce_str = (random + "").split("\\.")[1];
|
|
|
+ System.out.println(nonce_str);
|
|
|
+ String timestamp = create_timestamp();
|
|
|
+ String string1;
|
|
|
+ String signature = "";
|
|
|
+
|
|
|
+ //注意这里参数名必须全部小写,且必须有序
|
|
|
+ string1 = "jsapi_ticket=" + jsapi_ticket +
|
|
|
+ "&noncestr=" + nonce_str +
|
|
|
+ "×tamp=" + timestamp +
|
|
|
+ "&url=" + url;
|
|
|
+ System.out.println(string1);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
|
|
+ crypt.reset();
|
|
|
+ crypt.update(string1.getBytes("UTF-8"));
|
|
|
+ signature = byteToHex(crypt.digest());
|
|
|
+ }
|
|
|
+ catch (NoSuchAlgorithmException e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ catch (UnsupportedEncodingException e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ ret.put("url", url);
|
|
|
+ ret.put("jsapi_ticket", jsapi_ticket);
|
|
|
+ ret.put("nonceStr", nonce_str);
|
|
|
+ ret.put("timestamp", timestamp);
|
|
|
+ ret.put("signature", signature);
|
|
|
+ log.info(ret.toString());
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String byteToHex(final byte[] hash) {
|
|
|
+ Formatter formatter = new Formatter();
|
|
|
+ for (byte b : hash)
|
|
|
+ {
|
|
|
+ formatter.format("%02x", b);
|
|
|
+ }
|
|
|
+ String result = formatter.toString();
|
|
|
+ formatter.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String create_nonce_str() {
|
|
|
+ return UUID.randomUUID().toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String create_timestamp() {
|
|
|
+ return Long.toString(System.currentTimeMillis() / 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|