Commit 7001fde2 authored by kongfm's avatar kongfm

tzs-微信公众号 根据code 返回注册用户电话信息及openid

parent 40af7ccc
package com.yeejoin.amos.boot.module.tzs.api.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 通话记录附件
*
* @author kongfm
* @date 2021-09-23
*/
@Data
@ApiModel(value="WechatAccessDto", description="微信认证dto")
public class WechatAccessDto {
@ApiModelProperty(value = "微信openId")
private String openId;
@ApiModelProperty(value = "手机号")
private String tel;
}
......@@ -11,4 +11,10 @@ public interface IWechatService {
* @return
*/
String getAccessToken();
/**
* 获取微信openId
* @return
*/
String getOpenId(String code);
}
package com.yeejoin.amos.boot.module.tzs.biz.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.boot.module.tzs.api.dto.WechatAccessDto;
import com.yeejoin.amos.boot.module.tzs.api.entity.WechatRelation;
import com.yeejoin.amos.boot.module.tzs.api.service.IWechatService;
import com.yeejoin.amos.boot.module.tzs.biz.service.impl.WechatRelationServiceImpl;
import com.yeejoin.amos.component.feign.model.FeignClientResult;
import com.yeejoin.amos.feign.systemctl.Systemctl;
import com.yeejoin.amos.feign.systemctl.model.SmsRecordModel;
......@@ -55,6 +59,9 @@ public class WechatController extends BaseController {
@Autowired
private IWechatService wechatService;
@Autowired
WechatRelationServiceImpl wechatRelationServiceImpl;
/**
......@@ -134,7 +141,7 @@ public class WechatController extends BaseController {
*
* @return
*/
@TycloudOperation(ApiLevel = UserType.AGENCY)
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@GetMapping(value = "/getAccessToken")
@ApiOperation(httpMethod = "GET", value = "获取token信息", notes = "获取token信息")
public ResponseModel<String> getAccessToken() {
......@@ -167,7 +174,26 @@ public class WechatController extends BaseController {
}
/**
* 根据微信code获取openId和手机号接口
*
* @return
*/
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@GetMapping(value = "/getOpenIdTel/{code}")
@ApiOperation(httpMethod = "GET", value = "根据微信code获取openId和手机号接口", notes = "根据微信code获取openId和手机号接口")
public ResponseModel<WechatAccessDto> getAccessToken(@PathVariable String code) {
WechatAccessDto wechatAccessDto = new WechatAccessDto();
String openId = wechatService.getOpenId(code);
if(StringUtils.isNotEmpty(openId)) {
wechatAccessDto.setOpenId(openId);
WechatRelation temp = wechatRelationServiceImpl.getOne(new LambdaQueryWrapper<WechatRelation>().eq(WechatRelation::getOpenId,openId));
if(temp != null ) {
wechatAccessDto.setTel(temp.getPhone());
}
}
return ResponseHelper.buildResponse(wechatAccessDto);
}
public static String getrandom(){
String code = "";
......
......@@ -27,7 +27,7 @@ import org.typroject.tyboot.core.foundation.enumeration.UserType;
* @date 2021-09-22
*/
@RestController
@Api(tags = "微信公众号openid与电话号对应关系表Api")
@Api(tags = "openId与手机号对应关系表")
@RequestMapping(value = "/wechat-relation")
public class WechatRelationController extends BaseController {
......
......@@ -42,7 +42,7 @@ public class WechatServiceImpl implements IWechatService {
Object obj= redisUtils.get(RedisKey.WECHAT_TOKEN);
return obj.toString();
} else {
String tokenAccessUrl = wechatUrl+ "/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + SECRET_KEY;
String tokenAccessUrl = wechatUrl+ "/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + SECRET_KEY;
String responseStr = HttpUtils.doGet(tokenAccessUrl);
JSONObject response = null;
try {
......@@ -64,10 +64,26 @@ public class WechatServiceImpl implements IWechatService {
}
}
@Override
public String getOpenId(String code) {
String openIdUrl = wechatUrl+ "/sns/oauth2/access_token?appid=" +APP_ID + "&secret=" + SECRET_KEY + "&code=" + code + "&grant_type=authorization_code";
String responseStr = HttpUtils.doGet(openIdUrl);
JSONObject response = null;
try {
response = JSONObject.parseObject(responseStr);
} catch (Exception e) {
throw new BadRequest("获取openId 出错:" + e.getMessage());
}
if(response.get("openid") != null) { // 获取token 成功
try {
String openid = response.getString("openid");
return openid;
} catch (Exception e) {
throw new BadRequest("获取openId 出错:" + e.getMessage());
}
} else {
throw new BadRequest("获取openId 出错" + response);
}
}
}
\ No newline at end of file
......@@ -41,7 +41,7 @@ tzs.cti.appkey=4e805006-3fef-ae43-3915-a153731007c4
tzs.cti.secretkey=7bd29115-99ee-4f7d-1fb1-7c4719d5f43a
tzs.cti.url=http://36.46.151.113:8000
tzs.wechat.url=https://api.weixin.qq.com/cgi-bin
tzs.wechat.url=https://api.weixin.qq.com
##wechatAPPID
tzs.wechat.appid=wx8918c1aaad956617
##wechatSECRET
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment