Browse Source

添加颜选美学服务配置

kaick 1 year ago
parent
commit
4dc60f1978

+ 5 - 3
src/main/java/com/caimei365/wechat/controller/WechatServerApi.java

@@ -4,9 +4,11 @@ import com.caimei365.wechat.entity.WeChatConstant;
 import com.caimei365.wechat.service.WechatServerService;
 import com.caimei365.wechat.utils.MessageUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.annotations.Param;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
@@ -40,7 +42,7 @@ public class WechatServerApi {
      * 处理微信服务器的消息转发
      */
     @PostMapping("")
-    public String handleMessage(HttpServletRequest request) throws UnsupportedEncodingException {
+    public String handleMessage(@RequestParam(required = false) String type, HttpServletRequest request) throws UnsupportedEncodingException {
         request.setCharacterEncoding("UTF-8");
         long startProcessTime = System.currentTimeMillis();
         // xml格式的消息数据
@@ -58,7 +60,7 @@ public class WechatServerApi {
                     // subscribe(订阅/关注)
                     switch (eventType) {
                         case WeChatConstant.EVENT_TYPE_SUBSCRIBE:
-                            respXml = wechatServerService.handleSubscribeMsg(requestMap);
+                            respXml = wechatServerService.handleSubscribeMsg(type,requestMap);
                             break;
                         // unsubscribe(取消订阅),取消订阅后用户不会再收到公众账号发送的消息,因此回复"success"
                         case WeChatConstant.EVENT_TYPE_UNSUBSCRIBE:
@@ -95,7 +97,7 @@ public class WechatServerApi {
                     break;
                 // 默认文本消息
                 default:
-                    respXml = wechatServerService.handleTextMsg(requestMap);
+                    respXml = wechatServerService.handleTextMsg(type,requestMap);
                     break;
             }
             if (!StringUtils.hasLength(respXml)) { respXml = "success"; }

+ 2 - 1
src/main/java/com/caimei365/wechat/dao/WeChatDao.java

@@ -4,6 +4,7 @@ import com.caimei365.wechat.entity.WeChatUser;
 import com.caimei365.wechat.entity.WechatArticleDetail;
 import com.caimei365.wechat.entity.WechatReply;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -36,7 +37,7 @@ public interface WeChatDao {
     /**
      * 获取微信用户
      */
-    WeChatUser getWeChatUserByOpenid(String openid);
+    WeChatUser getWeChatUserByOpenid(@Param("type") String type, @Param("openid")String openid,@Param("unionId")String unionId);
     List<WeChatUser> getWeChatUserList();
 
     /**

+ 4 - 0
src/main/java/com/caimei365/wechat/entity/WeChatUser.java

@@ -19,6 +19,10 @@ public class WeChatUser {
      * openid
      */
     private String openid;
+    /**
+     * heheOpenid
+     */
+    private String heheOpenid;
     /**
      * unionId
      */

+ 2 - 2
src/main/java/com/caimei365/wechat/service/WechatServerService.java

@@ -19,12 +19,12 @@ public interface WechatServerService {
     /**
      * 处理接收的文本消息
      */
-    String handleTextMsg(Map<String, String> requestMap);
+    String handleTextMsg(String type,Map<String, String> requestMap);
 
     /**
      * subscribe(订阅/关注)
      */
-    String handleSubscribeMsg(Map<String, String> requestMap);
+    String handleSubscribeMsg(String type,Map<String, String> requestMap);
 
     /**
      * 扫描带参数二维码事件

+ 35 - 19
src/main/java/com/caimei365/wechat/service/impl/WechatServerServiceImpl.java

@@ -11,6 +11,7 @@ import com.caimei365.wechat.service.WechatServerService;
 import com.caimei365.wechat.utils.HttpUtil;
 import com.caimei365.wechat.utils.MessageUtil;
 import com.caimei365.wechat.utils.SignUtil;
+import com.caimei365.wechat.utils.WechatType;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -88,7 +89,9 @@ public class WechatServerServiceImpl implements WechatServerService {
      * </xml>
      */
     @Override
-    public String handleTextMsg(Map<String, String> requestMap) {
+    public String handleTextMsg(String type, Map<String, String> requestMap) {
+        WechatType wechatType = WechatType.initWechatType(type);
+
         // 根据接收的关键字回复
         String keyword = requestMap.get(WeChatConstant.CONTENT);
         // 用户openid
@@ -97,7 +100,7 @@ public class WechatServerServiceImpl implements WechatServerService {
         String wxType = requestMap.get(WeChatConstant.TO_USER_NAME);
         log.info(">>>>>>>>>>处理接收的文本消息,keyword:" + keyword + ",openid:" + openid + ",wxType:" + wxType);
         // 根据口令获取用户openid
-        if("caimei:get_openid".equals(keyword)) {
+        if ("caimei:get_openid".equals(keyword)) {
             return MessageUtil.setTextXml(openid, wxType, openid);
         }
         // 根据keyword匹配数据库回复配置
@@ -109,16 +112,19 @@ public class WechatServerServiceImpl implements WechatServerService {
                 return replyXml;
             }
         }
+        if ("2".equals(wechatType.getTYPE())) {
+            return MessageUtil.setTextXml(openid, wxType, "小颜已经收到您的消息,会尽快回复哦!");
+        }
         log.info(">>>>>>>>>>模糊搜索:" + keyword);
         // 搜索商品: https://core.caimei365.com/commodity/search/query/product?keyword=
         WechatArticleDetail article = null;
         try {
-            String uri = coreDomain + "/commodity/search/query/product?keyword="+URLEncoder.encode(keyword, "UTF-8" );
+            String uri = coreDomain + "/commodity/search/query/product?keyword=" + URLEncoder.encode(keyword, "UTF-8");
             String jsonResult = HttpUtil.sendGet(uri);
-            if(StringUtils.hasLength(jsonResult)){
+            if (StringUtils.hasLength(jsonResult)) {
                 JSONObject jsonObject = JSON.parseObject(jsonResult);
                 String data = jsonObject.getString("data");
-                if(StringUtils.hasLength(data)){
+                if (StringUtils.hasLength(data)) {
                     JSONObject parse = JSONObject.parseObject(data);
                     if(parse != null){
                         Integer total = parse.getInteger("total");
@@ -130,7 +136,7 @@ public class WechatServerServiceImpl implements WechatServerService {
                             try {
                                 url = URLEncoder.encode(url, "UTF-8");
                             } catch (UnsupportedEncodingException ignored) {}
-                            article.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + caimeiAppid + "&redirect_uri=" + url +"&response_type=code&scope=snsapi_base&state=reLogin#wechat_redirect");
+                            article.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wechatType.getAPPID() + "&redirect_uri=" + url + "&response_type=code&scope=snsapi_base&state=reLogin#wechat_redirect");
                         }
                     }
                 }
@@ -236,22 +242,23 @@ public class WechatServerServiceImpl implements WechatServerService {
      * </xml>
      */
     @Override
-    public String handleSubscribeMsg(Map<String, String> requestMap) {
+    public String handleSubscribeMsg(String type, Map<String, String> requestMap) {
         // 用户openid
         String openid = requestMap.get(WeChatConstant.FROM_USER_NAME);
+        WechatType wechatType = WechatType.initWechatType(type);
         String unionId = null;
-        WeChatUser user = weChatDao.getWeChatUserByOpenid(openid);
-        if (null == user || !StringUtils.hasLength(user.getUnionId())){
+        WeChatUser user = weChatDao.getWeChatUserByOpenid(wechatType.getTYPE(), openid, null);
+        if (null == user || StringUtils.hasLength(user.getUnionId())) {
             try {
                 // 获取 access_token
-                String url = wechatApiUrl + "/cgi-bin/token?grant_type=client_credential&appid=" + caimeiAppid + "&secret=" + caimeiSecret;
+                String url = wechatApiUrl + "/cgi-bin/token?grant_type=client_credential&appid=" + wechatType.getAPPID() + "&secret=" + wechatType.getAPPSECRET();
                 String result1 = HttpUtil.httpRequest(url, "GET", null);
                 JSONObject parse = JSONObject.parseObject(result1, JSONObject.class);
                 String token = parse.getString("access_token");
                 // 获取用户unionId
-                String unionIdUrl = wechatApiUrl + "/cgi-bin/user/info?access_token="+ token + "&openid=" + openid + "&lang=zh_CN";
+                String unionIdUrl = wechatApiUrl + "/cgi-bin/user/info?access_token=" + token + "&openid=" + openid + "&lang=zh_CN";
                 String result2 = HttpUtil.httpRequest(unionIdUrl, "GET", null);
-                log.info("openid:" + openid + ",获取用户unionId:" + result2);
+                log.info("openid:" + openid + ",获取用户unionId:" + result2 + ",access_token:" + token);
                 JSONObject parse2 = JSONObject.parseObject(result2, JSONObject.class);
                 unionId = parse2.getString("unionid");
             } catch (Exception e) {
@@ -259,14 +266,23 @@ public class WechatServerServiceImpl implements WechatServerService {
             }
         }
         // 保存关注用户
-        if (null == user) {
-            user = new WeChatUser();
-            user.setOpenid(openid);
-            user.setUnionId(unionId);
-            weChatDao.insertWeChatUser(user);
-        } else {
-            if (!StringUtils.hasLength(user.getUnionId())) {
+        if (null != unionId && (null == user || null == user.getUnionId())) {
+            user = weChatDao.getWeChatUserByOpenid(null, null, unionId);
+            if (null == user) {
+                user = new WeChatUser();
+                if ("2".equals(wechatType.getTYPE())) {
+                    user.setHeheOpenid(openid);
+                } else {
+                    user.setOpenid(openid);
+                }
                 user.setUnionId(unionId);
+                weChatDao.insertWeChatUser(user);
+            } else {
+                if ("2".equals(wechatType.getTYPE())) {
+                    user.setHeheOpenid(openid);
+                } else {
+                    user.setOpenid(openid);
+                }
                 weChatDao.updateWeChatUser(user);
             }
         }

+ 66 - 0
src/main/java/com/caimei365/wechat/utils/WechatType.java

@@ -0,0 +1,66 @@
+package com.caimei365.wechat.utils;
+
+import lombok.AllArgsConstructor;
+import lombok.ToString;
+import lombok.extern.slf4j.Slf4j;
+
+
+/**
+ * @auther: Kaick
+ * @date: 2023/11/28 17:06
+ */
+@Slf4j
+@ToString
+@AllArgsConstructor
+public enum WechatType {
+
+    CAIMEI("wx91c4152b60ca91a3", "a563dd2c07c9c815a4e697c8b6cb73dc", "1","采美公众号"),
+    HEHE("wx778caddd81b66a2f", "eb5987c766198f45d7f2c489b5fd5100", "2","颜选美学公众号");
+    private String APPID;
+    private String APPSECRET;
+    private String TYPE;
+    private String description;
+
+    public static WechatType initWechatType(String type) {
+        log.info("微信公众号>>>" + type);
+        WechatType wechatType;
+        if ("2".equals(type))  {
+            wechatType = WechatType.HEHE;
+        } else {
+            wechatType = WechatType.CAIMEI;
+        }
+        return wechatType;
+    }
+
+    public String getTYPE() {
+        return TYPE;
+    }
+
+    public void setTYPE(String TYPE) {
+        this.TYPE = TYPE;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getAPPID() {
+        return APPID;
+    }
+
+    public void setAPPID(String APPID) {
+        this.APPID = APPID;
+    }
+
+    public String getAPPSECRET() {
+        return APPSECRET;
+    }
+
+    public void setAPPSECRET(String APPSECRET) {
+        this.APPSECRET = APPSECRET;
+    }
+}

+ 4 - 0
src/main/resources/config/prod/application-prod.yml

@@ -39,6 +39,10 @@ wechat:
     appid: wx91c4152b60ca91a3
     secret: a563dd2c07c9c815a4e697c8b6cb73dc
   hehe:
+    id: gh_4d0a30254952
+    appid: wx778caddd81b66a2f
+    secret: eb5987c766198f45d7f2c489b5fd5100
+  hehe-old:
     id: gh_eecada09617d
     appid: wxd81864ddacc0ed25
     secret: 7873323db2984c75556f8d04e76d1f02

+ 19 - 4
src/main/resources/mapper/WeChatDao.xml

@@ -19,15 +19,30 @@
         SELECT content FROM wechat_text WHERE id = #{id}
     </select>
     <select id="getWeChatUserByOpenid" resultType="com.caimei365.wechat.entity.WeChatUser">
-        SELECT id, openid, unionId FROM wechat_user WHERE openid = #{openid}
+        SELECT id, openid,heheOpenid, unionId FROM wechat_user WHERE
+        <if test="unionId != null and unionId != ''">
+            unionId = #{unionId}
+        </if>
+        <if test="type != null and type == 1">
+             openid = #{openid}
+        </if>
+        <if test="type != null and type == 2">
+             heheOpenid = #{openid}
+        </if>
+        limit 1
     </select>
     <select id="getWeChatUserList" resultType="com.caimei365.wechat.entity.WeChatUser">
-        SELECT id, openid, unionId FROM wechat_user
+        SELECT id, openid,heheOpenid, unionId FROM wechat_user
     </select>
     <insert id="insertWeChatUser">
-        INSERT INTO wechat_user (openid, unionId)  VALUES (#{openid}, #{unionId})
+        INSERT INTO wechat_user (openid, heheOpenid, unionId)
+        VALUES (#{openid}, #{heheOpenid}, #{unionId})
     </insert>
     <update id="updateWeChatUser">
-        UPDATE wechat_user SET openid = #{openid}, unionId = #{unionId} WHERE id = #{id}
+        UPDATE wechat_user
+        SET openid     = #{openid},
+            heheOpenid = #{heheOpenid},
+            unionId    = #{unionId}
+        WHERE id = #{id}
     </update>
 </mapper>