|
@@ -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);
|