Browse Source

添加了网页授权

master
wei 6 years ago
parent
commit
92168df964
  1. 22
      src/main/java/com/ccsens/opensource/wxconfigurer/bean/po/WxGzhAuthType.java
  2. 3
      src/main/java/com/ccsens/opensource/wxconfigurer/util/WechatUtil.java
  3. 15
      src/main/java/com/ccsens/opensource/wxconfigurer/util/WxGzhUtil.java
  4. 26
      src/main/java/com/ccsens/opensource/wxconfigurer/web/rest/WxController.java
  5. 4
      src/test/java/com/ccsens/opensource/wxconfigurer/WxUtilTest.java

22
src/main/java/com/ccsens/opensource/wxconfigurer/bean/po/WxGzhAuthType.java

@ -0,0 +1,22 @@
package com.ccsens.opensource.wxconfigurer.bean.po;
import lombok.Getter;
import lombok.Setter;
/**
* @author __zHangSan
*/
public enum WxGzhAuthType {
//基本授权类型,静默,只能获取openId
SNSAPI_BASE("snsapi_base"),
//信息授权类型,弹出提示框,可以获取openId,unionId,nickanme,city等用户信息
SNSAPI_USERINFO("snsapi_userinfo");
@Getter@Setter
private String text;
private WxGzhAuthType(String text){
this.text = text;
}
}

3
src/main/java/com/ccsens/opensource/wxconfigurer/util/WechatUtil.java

@ -2,7 +2,6 @@ package com.ccsens.opensource.wxconfigurer.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ccsens.opensource.wxconfigurer.bean.po.WxAccessToken;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
@ -167,7 +166,7 @@ public class WechatUtil {
* @throws Exception
*/
public static void getWxCode(String page,String scene,String path) throws Exception {
String url = String.format(URL_GET_WX_CODE_B,WxUtil.getAccessToken());
String url = String.format(URL_GET_WX_CODE_B, WxGzhUtil.getAccessToken());
WechatCode wechatCode = new WechatCode();
wechatCode.page = page;

15
src/main/java/com/ccsens/opensource/wxconfigurer/util/WxUtil.java → src/main/java/com/ccsens/opensource/wxconfigurer/util/WxGzhUtil.java

@ -2,13 +2,16 @@ package com.ccsens.opensource.wxconfigurer.util;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import com.ccsens.opensource.wxconfigurer.bean.po.WxGzhAuthType;
import com.ccsens.opensource.wxconfigurer.bean.po.WxGzhMenu;
import com.ccsens.opensource.wxconfigurer.bean.po.WxAccessToken;
import com.ccsens.opensource.wxconfigurer.exception.BaseException;
import com.ccsens.opensource.wxconfigurer.exception.BusinessException;
import com.ccsens.opensource.wxconfigurer.exception.WxException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.sun.jndi.toolkit.url.UrlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,8 +23,8 @@ import java.util.Map;
/**
* @author __zHangSan
*/
public class WxUtil {
private static final Logger logger = LoggerFactory.getLogger(WxUtil.class);
public class WxGzhUtil {
private static final Logger logger = LoggerFactory.getLogger(WxGzhUtil.class);
/**
* 全局唯一accessToken
*/
@ -52,6 +55,8 @@ public class WxUtil {
private static final String URL_QUERY_MENU
= "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=%1$s";
private static final String GZH_AUTH_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%1$s&redirect_uri=%2$s&response_type=code&scope=%3$s&state=STATE#wechat_redirect";
private static final String APPID = "wx7af1bf1e14facf82";
private static final String SECRET = "a6613fae11b497639c0224b820aaf6d9";
private static final String TOKEN = "nNzkL9KkZUOIS8uU";
@ -105,7 +110,7 @@ public class WxUtil {
* @param echostr
* @return
*/
public static boolean checkGzhSignature(String signature,String timestamp,String nonce,String echostr){
public static boolean checkSignature(String signature,String timestamp,String nonce,String echostr){
if(StrUtil.isEmpty(signature) || StrUtil.isEmpty(timestamp) || StrUtil.isEmpty(nonce) || StrUtil.isEmpty(echostr)){
return false;
}
@ -194,4 +199,8 @@ public class WxUtil {
throw new WxException((Integer)errcode,(String)map.get("errmsg"));
}
}
public static String getAuthedUrl(String url, WxGzhAuthType wxGzhAuthType){
return String.format(GZH_AUTH_URL,APPID, URLUtil.encode(url),wxGzhAuthType.getText());
}
}

26
src/main/java/com/ccsens/opensource/wxconfigurer/web/rest/WxController.java

@ -4,26 +4,21 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ccsens.opensource.wxconfigurer.bean.dto.WxGzhAction;
import com.ccsens.opensource.wxconfigurer.bean.po.WxGzhAuthType;
import com.ccsens.opensource.wxconfigurer.bean.po.WxGzhMenu;
import com.ccsens.opensource.wxconfigurer.util.JsonResponse;
import com.ccsens.opensource.wxconfigurer.util.WxUtil;
import com.ccsens.opensource.wxconfigurer.util.WxGzhUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author __zHangSan
*/
@ -55,7 +50,7 @@ public class WxController {
@ResponseBody
public String responseValidate(String signature,String timestamp, String nonce, String echostr) throws Exception {
log.info("responseValidate...");
if (WxUtil.checkGzhSignature(signature, timestamp, nonce, echostr)) {
if (WxGzhUtil.checkSignature(signature, timestamp, nonce, echostr)) {
return echostr;
} else {
return "error";
@ -83,7 +78,7 @@ public class WxController {
@ResponseBody
public JsonResponse<String> queryMenu() throws Exception {
log.info("QueryMenu...");
String queryMenu = WxUtil.queryMenu();
String queryMenu = WxGzhUtil.queryMenu();
return JsonResponse.newInstance(String.class).ok(Base64.encode(queryMenu));
}
@ -98,7 +93,7 @@ public class WxController {
String commonUrl = "https://www.ccsens.com";
String wikiUrl = "https://www.yuque.com/ccsens";
String htproUrl = "https://test.tall.wiki/ht-dev/zy";
String mtproUrl = "https://api.ccsens.com/mt/";
String mtproUrl = "https://www.tall.wiki/mt/";
String ygczUrl = "https://test.tall.wiki/pt-dev/zy";
String xccUrl = "https://business.aijiatui.com/article-h5/manual/626434620463869952/657503679879946240?cardId=628239211027324928&shareId=657503679879946240&targetId=645928070145994752&pid=645928070145994752&originId=3004";
String dmUrl = "https://business.aijiatui.com/article-h5/case/626434620463869952/657504299550572544?cardId=628239211027324928&shareId=657504299550572544&targetId=656535993763037184&originId=3001";
@ -118,9 +113,12 @@ public class WxController {
)
.add(WxGzhMenu.Button.builder().name("时物链条").sub_button(
CollectionUtil.newArrayList(
WxGzhMenu.Button.builder().name("htpro").type(WxGzhMenu.ButtonType.VIEW).url(htproUrl).build(),
WxGzhMenu.Button.builder().name("mtpro").type(WxGzhMenu.ButtonType.VIEW).url(mtproUrl).build(),
WxGzhMenu.Button.builder().name("员工成长").type(WxGzhMenu.ButtonType.VIEW).url(ygczUrl).build(),
WxGzhMenu.Button.builder().name("htpro").type(WxGzhMenu.ButtonType.VIEW)
.url(WxGzhUtil.getAuthedUrl(htproUrl, WxGzhAuthType.SNSAPI_USERINFO)).build(),
WxGzhMenu.Button.builder().name("mtpro").type(WxGzhMenu.ButtonType.VIEW)
.url(WxGzhUtil.getAuthedUrl(mtproUrl, WxGzhAuthType.SNSAPI_USERINFO)).build(),
WxGzhMenu.Button.builder().name("员工成长").type(WxGzhMenu.ButtonType.VIEW)
.url(WxGzhUtil.getAuthedUrl(ygczUrl, WxGzhAuthType.SNSAPI_USERINFO)).build(),
WxGzhMenu.Button.builder().name("宣传册").type(WxGzhMenu.ButtonType.VIEW).url(xccUrl).build(),
WxGzhMenu.Button.builder().name("DM").type(WxGzhMenu.ButtonType.VIEW).url(dmUrl).build()
)
@ -142,7 +140,7 @@ public class WxController {
)
).build()
);
WxUtil.createMenu(wxGzhMenu);
WxGzhUtil.createMenu(wxGzhMenu);
return JsonResponse.newInstance(String.class).ok();
}
}

4
src/test/java/com/ccsens/opensource/wxconfigurer/WxUtilTest.java

@ -3,7 +3,7 @@ package com.ccsens.opensource.wxconfigurer;
import cn.hutool.core.collection.CollectionUtil;
import com.ccsens.opensource.wxconfigurer.bean.po.WxGzhMenu;
import com.ccsens.opensource.wxconfigurer.exception.BaseException;
import com.ccsens.opensource.wxconfigurer.util.WxUtil;
import com.ccsens.opensource.wxconfigurer.util.WxGzhUtil;
import org.junit.Test;
public class WxUtilTest {
@ -48,7 +48,7 @@ public class WxUtilTest {
);
try {
WxUtil.createMenu(wxGzhMenu);
WxGzhUtil.createMenu(wxGzhMenu);
} catch (BaseException e) {
e.printStackTrace();
}

Loading…
Cancel
Save