手机号授权
This commit is contained in:
parent
b1dff3834d
commit
d9de7f85f5
|
|
@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject;
|
|||
import com.ghy.common.config.WxConfig;
|
||||
import com.ghy.common.core.controller.BaseController;
|
||||
import com.ghy.common.core.domain.AjaxResult;
|
||||
import com.ghy.common.enums.UserPhoneEnum;
|
||||
import com.ghy.common.exception.ServiceException;
|
||||
import com.ghy.common.utils.ExceptionUtil;
|
||||
import com.ghy.common.utils.StringUtils;
|
||||
import com.ghy.common.utils.WxUtils;
|
||||
|
|
@ -109,6 +111,27 @@ public class WxController extends BaseController {
|
|||
}
|
||||
}
|
||||
|
||||
@GetMapping("/auth/phone")
|
||||
@ResponseBody
|
||||
public AjaxResult authPhone(HttpServletRequest request){
|
||||
try {
|
||||
String code = request.getHeader("code");
|
||||
String from = request.getHeader("from");
|
||||
String deptId = request.getHeader("deptId");
|
||||
SysDeptConfig sysDeptConfig = sysDeptConfigService.selectByDeptId(Long.parseLong(deptId));
|
||||
String accessToken;
|
||||
if("customer".equals(from)){
|
||||
accessToken = getAccessToken(sysDeptConfig.getWxAppId(), sysDeptConfig.getWxSecret());
|
||||
}else {
|
||||
accessToken = getAccessToken(sysDeptConfig.getServWxAppId(), sysDeptConfig.getServWxSecret());
|
||||
}
|
||||
return AjaxResult.success(getPhoneNumber(code, accessToken));
|
||||
}catch (Exception e){
|
||||
logger.error(ExceptionUtil.getExceptionMessage(e));
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/auth")
|
||||
@ResponseBody
|
||||
public AjaxResult auth(HttpServletRequest request) {
|
||||
|
|
@ -136,6 +159,30 @@ public class WxController extends BaseController {
|
|||
return wxUser.getString("openid");
|
||||
}
|
||||
|
||||
private static String getAccessToken(String appId, String appSecret){
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId +"&secret=" + appSecret;
|
||||
String result = HttpUtils.sendGet(url);
|
||||
String code = JSONObject.parseObject(result).getString("errcode");
|
||||
if("0".equals(code)){
|
||||
return JSONObject.parseObject(result).getString("access_token");
|
||||
}else {
|
||||
throw new ServiceException("获取授权码异常");
|
||||
}
|
||||
}
|
||||
|
||||
private static String getPhoneNumber(String code, String accessToken){
|
||||
JSONObject params = new JSONObject();
|
||||
params.put("code", code);
|
||||
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
|
||||
String result = HttpUtils.sendSSLPost(url, params.toString());
|
||||
String errCode = JSONObject.parseObject(result).getString("errCode");
|
||||
if("0".equals(errCode)){
|
||||
return JSONObject.parseObject(result).getJSONObject("phoneInfo").getString("purePhoneNumber");
|
||||
}else {
|
||||
throw new ServiceException("获取手机号码异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证微信签名
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package com.ghy.common.enums;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public enum UserPhoneEnum {
|
||||
|
||||
|
||||
CUSTOMER_PHONE("customer"){
|
||||
@Override
|
||||
public Object getPhone(Long customerId) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
WORKER_PHONE("worker"){
|
||||
@Override
|
||||
public Object getPhone(Long workerId) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
private String code;
|
||||
|
||||
UserPhoneEnum(String code){
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public abstract Object getPhone(Long userId);
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue