|
@@ -0,0 +1,59 @@
|
|
|
+package com.usky.dxtop.controller.web;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.usky.dxtop.common.exception.CustomException;
|
|
|
+import com.usky.dxtop.common.utils.DateUtils;
|
|
|
+import com.usky.dxtop.common.utils.http.HttpUtils;
|
|
|
+import com.usky.dxtop.common.utils.sign.Md5Utils;
|
|
|
+import com.usky.dxtop.common.utils.uuid.UUID;
|
|
|
+import com.usky.dxtop.service.api.TopApi;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.TreeMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yq
|
|
|
+ * @date 2021/9/22 10:04
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+public class WxController {
|
|
|
+
|
|
|
+ private static final String WX_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+
|
|
|
+ @GetMapping("/wxLogin")
|
|
|
+ public String wxLogin(@RequestParam String jsCode){
|
|
|
+ TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
+ treeMap.put("appid", TopApi.WX_APP_ID);
|
|
|
+ treeMap.put("secret", TopApi.WX_APP_SECRET);
|
|
|
+ treeMap.put("js_code", jsCode);
|
|
|
+ treeMap.put("grant_type", "authorization_code");
|
|
|
+ String result = HttpUtils.sendGet(WX_LOGIN_URL, TopApi.joinParam(treeMap));
|
|
|
+ JSONObject obj= JSON.parseObject(result);
|
|
|
+ log.info("获取到的相应"+obj);
|
|
|
+ String openId = "";
|
|
|
+ if (null == obj.get("errcode")){
|
|
|
+ openId = obj.get("openid").toString();
|
|
|
+ }else {
|
|
|
+ throw new CustomException(String.format("登录异常:code:%s,message:%s",obj.get("errcode"),obj.get("errmsg")));
|
|
|
+ }
|
|
|
+ return openId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/wxPayParam")
|
|
|
+ public String wxPayParam(@RequestParam String payInfo){
|
|
|
+ TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
+ treeMap.put("appid", TopApi.WX_APP_ID);
|
|
|
+ treeMap.put("timeStamp", DateUtils.dateTimeNow());
|
|
|
+ treeMap.put("nonceStr", UUID.randomUUID().toString(true));
|
|
|
+ treeMap.put("package", String.format("prepay_id=%s",payInfo));
|
|
|
+ treeMap.put("signType", "MD5");
|
|
|
+ String format = String.format("%s&key=%s", TopApi.joinParam(treeMap), TopApi.SECRET);
|
|
|
+ treeMap.put("paySign", Md5Utils.hash(format).toUpperCase());
|
|
|
+ return JSONObject.toJSONString(treeMap);
|
|
|
+ }
|
|
|
+}
|