|
@@ -0,0 +1,152 @@
|
|
|
|
+package com.management.platform.controller;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.management.platform.entity.WeiXinTicketEntity;
|
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
|
+import com.management.platform.util.HttpUtil;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 生成带参二维码的ticket值
|
|
|
|
+ * @author wyx
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("weixinTicket")
|
|
|
|
+public class WeiXinTicketController {
|
|
|
|
+
|
|
|
|
+ @Value("${weixin.ticket.action_name.QR_SCENE}")
|
|
|
|
+ private String QRSCENE;
|
|
|
|
+ @Value("${weixin.ticket.action_name.QR_STR_SCENE}")
|
|
|
|
+ private String QR_STR_SCENE;
|
|
|
|
+
|
|
|
|
+ private static final String APP_ID = "wx1c1d8fc81bc073a8";
|
|
|
|
+ private static final String APP_SECRET = "17ad07f90ee845f99f4c1605647ef755";
|
|
|
|
+ private static final String GRANT_TYPE = "client_credential";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "sendInterviewInvitation",method=RequestMethod.POST)
|
|
|
|
+ public HttpRespMsg sendInterviewInvitation(@RequestBody WeiXinTicketEntity reqDto) throws Exception{
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ try {
|
|
|
|
+ String accessToken = getWeiXinAccessToken();
|
|
|
|
+ String sceneStr = reqDto.getSceneStr();
|
|
|
|
+ String result = new WeiXinTicketController().createForeverStrTicket(accessToken, sceneStr);
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
|
+ String ticket = null ;
|
|
|
|
+ if(null!=jsonObject){
|
|
|
|
+ ticket = jsonObject.getString("ticket");
|
|
|
|
+ System.out.println("创建永久带参二维码成功,ticket="+ticket);
|
|
|
|
+ }
|
|
|
|
+ msg.data=ticket;
|
|
|
|
+ return msg;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new Exception(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建临时带参数二维码
|
|
|
|
+ * 获取灵石带参二维码ticket
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
|
|
|
|
+ * @param sceneId 场景Id
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String createTempTicket(String accessToken, String expireSeconds, int sceneId) throws Exception {
|
|
|
|
+ Map<String, Integer> intMap = new HashMap<>();
|
|
|
|
+ intMap.put("scene_id", sceneId);
|
|
|
|
+ Map<String, Map<String, Integer>> mapMap = new HashMap<>();
|
|
|
|
+ mapMap.put("scene", intMap);
|
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
|
+ paramsMap.put("expire_seconds", expireSeconds);
|
|
|
|
+ paramsMap.put("action_name", QRSCENE);
|
|
|
|
+ paramsMap.put("action_info", mapMap);
|
|
|
|
+ String jsonObject = JSONObject.toJSONString(paramsMap);
|
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;
|
|
|
|
+ String result = HttpUtil.post(requestUrl,accessToken, jsonObject);
|
|
|
|
+ return result;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建永久二维码(数字)
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param sceneId
|
|
|
|
+ * 场景Id
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String createForeverTicket(String accessToken, int sceneId) throws Exception {
|
|
|
|
+ Map<String, Integer> intMap = new HashMap<>();
|
|
|
|
+ intMap.put("scene_id", sceneId);
|
|
|
|
+ Map<String, Map<String, Integer>> mapMap = new HashMap<>();
|
|
|
|
+ mapMap.put("scene", intMap);
|
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
|
+ paramsMap.put("action_name", QR_STR_SCENE);
|
|
|
|
+ paramsMap.put("action_info", mapMap);
|
|
|
|
+ String jsonObject = JSONObject.toJSONString(paramsMap);
|
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
|
|
|
|
+ String result = HttpUtil.post(requestUrl,accessToken, jsonObject);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建永久二维码(字符串)
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param sceneStr
|
|
|
|
+ * 场景str
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String createForeverStrTicket(String accessToken, String sceneStr) throws Exception {
|
|
|
|
+ Map<String, String> intMap = new HashMap<>();
|
|
|
|
+ intMap.put("scene_str", sceneStr);
|
|
|
|
+ Map<String, Map<String, String>> mapMap = new HashMap<>();
|
|
|
|
+ mapMap.put("scene", intMap);
|
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
|
+ paramsMap.put("action_name", QR_STR_SCENE);
|
|
|
|
+ paramsMap.put("action_info", mapMap);
|
|
|
|
+ String jsonObject = JSONObject.toJSONString(paramsMap);
|
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;
|
|
|
|
+ String result = HttpUtil.post(requestUrl,accessToken, jsonObject);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //获取第三方应用临时凭证
|
|
|
|
+ private String getWeiXinAccessToken() {
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ String response = restTemplate.getForObject(
|
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/token",
|
|
|
|
+ String.class,
|
|
|
|
+ GRANT_TYPE,
|
|
|
|
+ APP_ID,
|
|
|
|
+ APP_SECRET);
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(response);
|
|
|
|
+ if (jsonObject.containsKey("access_token")) {
|
|
|
|
+ return jsonObject.getString("access_token");
|
|
|
|
+ } else {
|
|
|
|
+ System.err.println("获取access_token失败: " + response);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|