ソースを参照

feat:“七云添加微信登录“

soobin 1 年間 前
コミット
4e7bfb52c5

+ 30 - 0
src/main/java/com/szwl/constant/ConfigConsts.java

@@ -25,4 +25,34 @@ public class ConfigConsts {
     public static final String QINIU_CLOUD_BUCKET = "videomanage";
 
     public static final String DOMAIN_OF_BUCKET = "http://qiniuyun.sunzee.com.cn/";
+
+    /**
+     * 申泽商户公众账号ID
+     */
+    public static final String SZ_WX_SUB_APP_ID = "wxcd5b1b2636c9f611";
+
+    /**
+     * 七云商户公众账号ID
+     */
+    public static final String SC_WX_SUB_APP_ID = "wxd9c7c138a055ee66";
+
+    /**
+     * 申泽商户公众账号密钥
+     */
+    public static final String SZ_WX_APP_SECRET = "e2854aa99f8279f33b4f065b2ffb75b1";
+
+    /**
+     * 七云商户公众账号密钥
+     */
+    public static final String SC_WX_APP_SECRET = "f131c85c06a80e466bd7e366cd477b93";
+
+    /**
+     *
+     */
+    public static final String SZ_RETURN_URL = "http://szwlh.sunzee.com.cn";
+
+    /**
+     *
+     */
+    public static final String SC_RETURN_URL = "http://sevencloud.com.cn";
 }

+ 21 - 21
src/main/java/com/szwl/controller/IndexController.java

@@ -133,46 +133,46 @@ public class IndexController {
         //有记录,则进行修改
         LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
         query.eq(TEquipment::getClientId, clientId);
-        TEquipment _new = equipmentService.getOne(query);
-        if (_new != null) {
-            _new.setNetWorkingMode(netWorkingMode);
+        TEquipment newEquipment = equipmentService.getOne(query);
+        if (newEquipment != null) {
+            newEquipment.setNetWorkingMode(netWorkingMode);
             if (StringUtils.isNotEmpty(cabinetTm)) {
-                _new.setCabinetTm(cabinetTm);
+                newEquipment.setCabinetTm(cabinetTm);
             }
             if (StringUtils.isNotEmpty(cabinetHd)) {
-                _new.setCabinetHd(cabinetHd);
+                newEquipment.setCabinetHd(cabinetHd);
             }
             if (StringUtils.isNotEmpty(furnaceSp)) {
-                _new.setFurnaceSp(furnaceSp);
+                newEquipment.setFurnaceSp(furnaceSp);
             }
             if (StringUtils.isNotEmpty(furnaceTm)) {
-                _new.setFurnaceTm(furnaceTm);
+                newEquipment.setFurnaceTm(furnaceTm);
             }
-            _new.setLastUpdateTime(lastUpdateTime);
-//            _new.setLongitude(longitude);
+            newEquipment.setLastUpdateTime(lastUpdateTime);
+//            newEquipment.setLongitude(longitude);
             if (eqeStatus != null) {
-                _new.setEqeStatus(eqeStatus);
+                newEquipment.setEqeStatus(eqeStatus);
             }
             if (is_sleep != null) {
-                _new.setIsSleep(is_sleep);
+                newEquipment.setIsSleep(is_sleep);
             }
             if (!StringUtils.isEmpty(furnaceTm)) {
                 int fur = Integer.parseInt(furnaceTm);
-                if (_new.getEqeStatus() == 0 && fur > 150) {
-                    _new.setEqeStatus(1);
+                if (newEquipment.getEqeStatus() == 0 && fur > 150) {
+                    newEquipment.setEqeStatus(1);
                 }
-                if (_new.getEqeStatus() == null && fur > 150) {
-                    _new.setEqeStatus(1);
+                if (newEquipment.getEqeStatus() == null && fur > 150) {
+                    newEquipment.setEqeStatus(1);
                 }
             }
             if (!StringUtils.isEmpty(volume)) {
-                _new.setVolume(volume);
+                newEquipment.setVolume(volume);
             }
-//            _new.setLatitude(latitude);
-            _new.setGtClientId(gtClientId);
-            _new.setModifyDate(new Date());
-            equipmentService.updateById(_new);
-            TEquipmentDesc equipmentDesc = equipmentDescService.getById(_new.getId());
+//            newEquipment.setLatitude(latitude);
+            newEquipment.setGtClientId(gtClientId);
+            newEquipment.setModifyDate(new Date());
+            equipmentService.updateById(newEquipment);
+            TEquipmentDesc equipmentDesc = equipmentDescService.getById(newEquipment.getId());
             if (StringUtils.isNotEmpty(equipmentVo.getBlueSugar())) {
                 equipmentDesc.setBlueSugar(equipmentVo.getBlueSugar());
             }

+ 53 - 12
src/main/java/com/szwl/controller/TWechatController.java

@@ -2,7 +2,9 @@ package com.szwl.controller;
 
 
 import cn.com.crbank.ommo.exception.MyException;
+import com.szwl.constant.ConfigConsts;
 import com.szwl.model.bo.R;
+import com.szwl.model.entity.TAdmin;
 import com.szwl.model.entity.TWechat;
 import com.szwl.model.param.WxBindParam;
 import com.szwl.model.utils.HttpClientUtils;
@@ -41,19 +43,19 @@ import java.util.*;
 public class TWechatController {
 
     @Autowired
-    private TAdminService tAdminService;
+    private TAdminService adminService;
 
     @Autowired
     private TWechatService tWechatService;
 
     // 从 yml 文件中获取
-    @Value("${oauth.wx.appid}")
+//    @Value("${oauth.wx.appid}")
     private String appid;
 
-    @Value("${oauth.wx.appsecret}")
+//    @Value("${oauth.wx.appsecret}")
     private String appsecret;
 
-    @Value("${oauth.callback.http}")
+//    @Value("${oauth.callback.http}")
     private String http;
 
     @ApiOperation(value = "绑定微信")
@@ -63,16 +65,30 @@ public class TWechatController {
         if (adminId == null) {
             throw new MyException("参数为空");
         }
-        String path = http + "/SZWL-SERVER/tWechat/callback";
+        TAdmin admin = adminService.getById(adminId);
+        String companyType = admin.getCompanyType();
+        String path = "";
+        String appId = "";
+        String head = "";
+
+        if (StringUtils.isNotEmpty(companyType) && companyType.equals("1")) {
+            path = ConfigConsts.SC_RETURN_URL + "/SZWL-SERVER/tWechat/callback";
+            appId =  ConfigConsts.SC_WX_SUB_APP_ID;
+            head = ConfigConsts.SC_RETURN_URL;
+        } else {
+            path = ConfigConsts.SZ_RETURN_URL + "/SZWL-SERVER/tWechat/callback";
+            appId = ConfigConsts.SZ_WX_SUB_APP_ID;
+            head = ConfigConsts.SZ_RETURN_URL;
+        }
 //        String path = http + "/tWechat/callback";
         try {
             // redirectUrl 用于处理微信授权回调请求的页面
             String redirectUrl = URLEncoder.encode(path, "UTF-8");
             // 第一步:用户同意授权,获取code
-            String url = "http://szwlh.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
+            String url = head + "/openWeixin/connect/oauth2/authorize?"
 //            String url = "http://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
 //            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
-                    + "appid=" + appid
+                    + "appid=" + appId
                     + "&redirect_uri=" + redirectUrl
                     + "&response_type=code"
                     + "&scope=snsapi_userinfo"
@@ -91,11 +107,25 @@ public class TWechatController {
         // 获取code
         String code = request.getParameter("code");
         String adminId = request.getParameter("state");
+        TAdmin admin = adminService.getById(adminId);
+        String companyType = admin.getCompanyType();
+        String appId = "";
+        String appSecret = "";
+        String redirectUrl = "";
+        if (StringUtils.isNotEmpty(companyType) && companyType.equals("1")) {
+            appId =  ConfigConsts.SC_WX_SUB_APP_ID;
+            appSecret = ConfigConsts.SC_WX_APP_SECRET;
+            redirectUrl = ConfigConsts.SC_RETURN_URL + "/sc/#/user";
+        } else {
+            appId = ConfigConsts.SZ_WX_SUB_APP_ID;
+            appSecret = ConfigConsts.SZ_WX_APP_SECRET;
+            redirectUrl = ConfigConsts.SZ_RETURN_URL + "/shenze/#/user";
+        }
 
         // 第二步:通过 code 换取网页授权 access_token 和 openid
         String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
-                "appid=" + appid +
-                "&secret=" + appsecret +
+                "appid=" + appId +
+                "&secret=" + appSecret +
                 "&code=" + code +
                 "&grant_type=authorization_code";
 
@@ -165,7 +195,7 @@ public class TWechatController {
             // 获取微信授权失败
             return R.fail("微信授权失败,没有openid");
         }
-        String redirectUrl = http + "/shenze/#/user";
+//        String redirectUrl = http + "/shenze/#/user";
         response.sendRedirect(redirectUrl);
         return R.ok(userInfo);
     }
@@ -190,12 +220,23 @@ public class TWechatController {
     public Map<String, Object> auth(@RequestBody WxBindParam wxBindParam) {
         String adminId = wxBindParam.getState();
         String code = wxBindParam.getCode();
+        TAdmin admin = adminService.getById(adminId);
+        String companyType = admin.getCompanyType();
+        String appId = "";
+        String appSecret = "";
+        if (StringUtils.isNotEmpty(companyType) && companyType.equals("1")) {
+            appId =  ConfigConsts.SC_WX_SUB_APP_ID;
+            appSecret = ConfigConsts.SC_WX_APP_SECRET;
+        } else {
+            appId = ConfigConsts.SZ_WX_SUB_APP_ID;
+            appSecret = ConfigConsts.SZ_WX_APP_SECRET;
+        }
         Map<String, Object> result = new HashMap<>();
         try {
             // 第二步:通过 code 换取网页授权 access_token 和 openid
             String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
-                    "appid=" + appid +
-                    "&secret=" + appsecret +
+                    "appid=" + appId +
+                    "&secret=" + appSecret +
                     "&code=" + code +
                     "&grant_type=authorization_code";
             System.out.println(openUrl);

+ 67 - 3
src/main/java/com/szwl/controller/WxLoginController.java

@@ -3,6 +3,7 @@ package com.szwl.controller;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import com.alibaba.fastjson.JSON;
+import com.szwl.constant.ConfigConsts;
 import com.szwl.exception.MyException;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
@@ -99,9 +100,6 @@ public class WxLoginController {
         }
     }
 
-    public static void main(String[] args) {
-
-    }
     @ApiOperation(value = "用微信code 获取openid,再获取用户信息")
     @GetMapping("/getUserDetailByWxCode")
     public R getUserDetailByWxCode(String code) {
@@ -126,6 +124,72 @@ public class WxLoginController {
         return R.ok(userDetailBO);
     }
 
+    @ApiOperation(value = "用户默认授权,获取code")
+    @GetMapping("/newMenuOauth")
+    public ResponseModel<?> newMenuOauth(String hostName) {
+        String path = "";
+        String appId = "";
+        String head = "";
+        if(hostName.equals("Sevencloud")) {
+            path = ConfigConsts.SC_RETURN_URL + "/shenzeVue/wxLogin";
+            appId =  ConfigConsts.SC_WX_SUB_APP_ID;
+            head = ConfigConsts.SC_RETURN_URL;
+        } else {
+            path = ConfigConsts.SZ_RETURN_URL + "/shenzeVue/wxLogin";
+            appId = ConfigConsts.SZ_WX_SUB_APP_ID;
+            head = ConfigConsts.SZ_RETURN_URL;
+        }
+        try {
+            String redirectUrl = URLEncoder.encode(path, "UTF-8");
+            // 第一步:用户静默授权,获取code
+            String authUrl = head + "/openWeixin/connect/oauth2/authorize?"
+//            String authUrl = "http://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
+//            String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?"
+                    + "appid=" + appId
+                    + "&redirect_uri=" + redirectUrl
+                    + "&response_type=code"
+                    + "&scope=snsapi_base"
+                    + "&state=STATE"
+                    + "#wechat_redirect";
+            return R.ok(authUrl);
+        } catch (UnsupportedEncodingException e) {
+            return R.fail("微信授权失败");
+        }
+    }
+
+    @ApiOperation(value = "用微信code 获取openid,再获取用户信息")
+    @GetMapping("/newGetUserDetailByWxCode")
+    public ResponseModel<?> newGetUserDetailByWxCode(String code, String hostName) {
+        String appId = "";
+        String appSecret = "";
+        if(hostName.equals("Sevencloud")) {
+            appId =  ConfigConsts.SC_WX_SUB_APP_ID;
+            appSecret = ConfigConsts.SC_WX_APP_SECRET;
+        } else {
+            appId = ConfigConsts.SZ_WX_SUB_APP_ID;
+            appSecret = ConfigConsts.SZ_WX_APP_SECRET;
+        }
+        // 第二步:使用code换取access_token和openid
+        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
+                + "appid=" + appId
+                + "&secret=" + appSecret
+                + "&code=" + code
+                + "&grant_type=authorization_code";
+        HttpResponse httpResponse = HttpRequest.get(url).execute();
+        JSONObject jsonObject = JSON.parseObject(httpResponse.body());
+        log.info("getUserDetailByWxCode:{}",jsonObject);
+        String openid = jsonObject.getString("openid");
+        // 根据openid 获取绑定的用户信息
+        TWechat tWechat = tWechatService.lambdaQuery().eq(TWechat::getOpenId, openid).one();
+        if(null == tWechat){
+            log.info("没有找到绑定的用户信息,请绑定后再登录,openid:",openid);
+            throw new MyException("没有找到绑定的用户信息,请绑定后再登录");
+        }
+        TAdmin tAdmin = tAdminService.getById(tWechat.getAdminId());
+        UserDetailBO userDetailBO = tAdminService.getUserDetailBO(tAdmin);
+        return R.ok(userDetailBO);
+    }
+
     @ApiOperation(value = "使用 Openid 登录")
     @PostMapping("/loginWithOpenid")
     @Audit(type = AuditEnum.LOGIN,content = "#username + '请求登录'")