Explorar o código

docs: "微信登录"

ritchie %!s(int64=2) %!d(string=hai) anos
pai
achega
a529c207c4

+ 71 - 0
src/main/java/com/szwl/controller/LogController.java

@@ -0,0 +1,71 @@
+package com.szwl.controller;
+
+import com.gexin.fastjson.JSON;
+import com.gexin.fastjson.JSONObject;
+import com.szwl.model.bo.JsonMessage;
+import com.szwl.model.entity.TEquipment;
+import com.szwl.model.utils.PushUtils;
+import com.szwl.service.TAdminService;
+import com.szwl.service.TEquipmentService;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.amqp.core.MessageProperties;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+
+@RestController
+@RequestMapping("/Log")
+public class LogController {
+    @Autowired
+    TEquipmentService tEquipmentService;
+
+    @Autowired
+    TAdminService tAdminService;
+
+    @Autowired
+    RabbitTemplate rabbitTemplate;
+
+    @ApiOperation(value = "上传日志")
+    @PostMapping("/uploadLog")
+    public JsonMessage uploadLog(@RequestBody Map<String, Object> body) {
+        String deviceId = String.valueOf(body.get("deviceId"));
+
+        Object logDate = body.get("logDate");
+        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
+        map.put("logDate", logDate);
+        String jsonString = JSON.toJSONString(map);
+        JSONObject jsonObject = JSON.parseObject(jsonString);
+        String day = jsonObject.getJSONObject("logDate").getString("_value");
+
+        if(day.length()!=8){
+            return JsonMessage.success("日期格式错误");
+        }
+        TEquipment equipment = tEquipmentService.getById(deviceId);
+
+        if (equipment == null) {
+            return JsonMessage.error("该设备不存在");
+        }
+        String kind = day+"-"+deviceId;
+        String channel = equipment.getChannel();
+        String equimentType = equipment.getEquimentType();
+        if(StringUtils.isEmpty(channel)||channel.equals("1")||StringUtils.isEmpty(equimentType)){
+
+            //用个推
+            PushUtils.push(equipment.getGtClientId(), "", "", PushUtils.buildJson("log", kind).toString());
+        }
+        if(StringUtils.isNotEmpty(channel)&&channel.equals("2")&&StringUtils.isNotEmpty(equimentType)){
+            //用Mq
+            //1 创建消息
+            MessageProperties messageProperties = new MessageProperties();
+            messageProperties.setContentType("application/json");
+            org.springframework.amqp.core.Message message = new org.springframework.amqp.core.Message(PushUtils.buildJson("log", kind).toString().getBytes(), messageProperties);
+            rabbitTemplate.send(equimentType, deviceId, message);
+        }
+        return JsonMessage.success("发送成功");
+    }
+}

+ 15 - 11
src/main/java/com/szwl/controller/TWechatController.java

@@ -62,7 +62,7 @@ import java.util.*;
 @Slf4j
 @Api(value = "/WeChatController", tags = {"微信绑定接口"})
 @RestController
-    @RequestMapping("/tWechat")
+@RequestMapping("/tWechat")
 public class TWechatController {
 
     @Autowired
@@ -78,10 +78,11 @@ public class TWechatController {
     @Value("${oauth.wx.appsecret}")
     private String appsecret;
 
-    @Value("${oauth.callback.http:http://szwltest.sunzee.com.cn:49002}")
+    @Value("${oauth.callback.http}")
     private String http;
 
     @ApiOperation(value = "绑定微信")
+    @CrossOrigin(value = "https://open.weixin.qq.com/")
     @GetMapping("/bindWechat")
     public R bindWechat(@RequestParam Long adminId) throws Exception {
         if (adminId==null) {
@@ -105,7 +106,7 @@ public class TWechatController {
             throw new MyException("用户不存在!");
         }
 //        String path = http + "/tWechat/callback?";
-        String path = http + "http://szwltest.sunzee.com.cn/SZWL-SERVER/tWechat/callback?";
+        String path = http + "/SZWL-SERVER/tWechat/callback?";
 
         try {
             path = URLEncoder.encode(path, "UTF-8");
@@ -114,7 +115,7 @@ public class TWechatController {
         }
 
         // 第一步:用户同意授权,获取code
-        String url = "http://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
+        String url = "https://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
 //        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
                 + "appid=" + appid
                 + "&redirect_uri=" + path
@@ -152,24 +153,27 @@ public class TWechatController {
                 "&lang=zh_CN";
         JSONObject userInfo = HttpClientUtils.get(url);
 
-        List<TWechat> list = tWechatService.lambdaQuery()
-                .eq(TWechat::getAdminId, adminId)
-                .list();
+        LambdaQueryWrapper<TWechat> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(TWechat::getAdminId, adminId);
+        TWechat tWechat = tWechatService.getOne(wrapper);
 
-        if (Objects.nonNull(list)) {
-            TWechat tWechat = list.get(0);
+        if (Objects.isNull(tWechat)) {
+            return null;
+        }
+        String openId = tWechat.getOpenId();
+
+        if (Objects.nonNull(openId)) {
             tWechat.setOpenId(userInfo.getString("openid"));
             tWechat.setNickName(userInfo.getString("nickname"));
             tWechat.setAvatarUrl(userInfo.getString("headimgurl"));
             tWechat.setModifyDate(new Date());
             tWechatService.updateById(tWechat);
         } else {
-            TWechat tWechat = new TWechat();
             tWechat.setOpenId(userInfo.getString("openid"));
             tWechat.setNickName(userInfo.getString("nickname"));
             tWechat.setAvatarUrl(userInfo.getString("headimgurl"));
             tWechat.setCreateDate(new Date());
-            tWechatService.save(tWechat);
+            tWechatService.updateById(tWechat);
         }
 
         return userInfo;

+ 9 - 5
src/main/java/com/szwl/controller/WxLoginController.java

@@ -1,5 +1,6 @@
 package com.szwl.controller;
 
+import com.szwl.model.bo.R;
 import com.szwl.model.bo.ResponseModel;
 import com.szwl.model.bo.UserDetailBO;
 import com.szwl.model.entity.TAdmin;
@@ -40,14 +41,15 @@ public class WxLoginController {
     @Value("${oauth.wx.appsecret}")
     private String appsecret;
 
-    @Value("${oauth.callback.http:http://szwltest.sunzee.com.cn:49002}")
+    @Value("${oauth.callback.http:http://szwltest.sunzee.com.cn}")
     private String http;
 
     @ApiOperation(value = "用户默认授权")
     @CrossOrigin(value = "https://open.weixin.qq.com/")
     @GetMapping("/menuOauth")
-    public void menuOauth(HttpServletResponse response) throws IOException {
-        String path = http + "/wxLogin/callback?";
+    public R menuOauth(HttpServletResponse response) throws IOException {
+        String path = http + "/SZWL-SERVER/wxLogin/callback?";
+//        String path = http + "/wxLogin/callback?";
 
         try {
             path = URLEncoder.encode(path, "UTF-8");
@@ -56,14 +58,16 @@ public class WxLoginController {
         }
         // 第一步:用户同意授权,获取code
         // 静默授权,只能获取用户openid
-        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
+        String url = "https://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
+//        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
                 + "appid=" + appid
                 + "&redirect_uri=" + path
                 + "&response_type=code"
                 + "&scope=snsapi_base"
                 + "&state=STATE" +
                 "#wechat_redirect";
-        response.sendRedirect(url);
+        return R.ok(url);
+//        response.sendRedirect(url);
     }
 
     @ApiOperation(value = "微信登录回调")

+ 6 - 2
src/main/resources/bootstrap.yml

@@ -25,16 +25,18 @@ management:
 
 oauth:
   wx:
+#    测试帐号
 #    appid: wx6959f112e9ffbfa3
 #    appsecret: 32f6fdf12bc25361110e1786f386eaab
+#    正式账户
     appid: wxcd5b1b2636c9f611
     appsecret: e2854aa99f8279f33b4f065b2ffb75b1
 
   callback:
 #    个人测试
-#    http: https://ab.free.svipss.top
+#    http: http://d.freehk.svipss.top
 #    系统测试
-#    http: http://szwl.sunzee.com.cn
+    http: http://szwltest.sunzee.com.cn:49002
 #    正式服务
 #    http: https://szwlh.sunzee.com.cn
 
@@ -71,6 +73,8 @@ eureka:
     serviceUrl:
       defaultZone: http://120.25.151.99:49001/eureka/
     register-with-eureka: true
+#    本地测试的时候改成 false
+#    register-with-eureka: false
 #  instance:
 #    prefer-ip-address: true
 #    ip-address: 112.96.106.247