瀏覽代碼

修改微信授权接口

pull/386/head
wanjing 10 月之前
父節點
當前提交
acc4d1fc29
共有 4 個文件被更改,包括 65 次插入0 次删除
  1. +7
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/controller/WxController.java
  2. +7
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IWxService.java
  3. +27
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/WxServiceImpl.java
  4. +24
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/vo/WxAuthorizeVo.java

+ 7
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/controller/WxController.java 查看文件

@@ -19,6 +19,13 @@ public class WxController {
@Autowired
private IWxService iWxService;

//根据code获取openId且判断用户是否授权
@GetMapping("/checkAuthorize/{code}")
public JsonResult checkAuthorize(@PathVariable("code") String code) {
log.info("进入获取openId接口");
return iWxService.checkAuthorize(code);
}

//根据code获取openId
@GetMapping("/getOpenId/{code}")
public JsonResult getOpenId(@PathVariable("code") String code) {

+ 7
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IWxService.java 查看文件

@@ -9,6 +9,13 @@ import com.tuoheng.miniprogram.entity.wx.WxMessageRequest;
*/
public interface IWxService {

/**
* 根据code获取openId且判断用户是否授权
* @param code
* @return
*/
JsonResult checkAuthorize(String code);

/**
* 根据code获取openId
* @param code

+ 27
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/WxServiceImpl.java 查看文件

@@ -20,6 +20,7 @@ import com.tuoheng.miniprogram.param.WxSendMessageParam;
import com.tuoheng.miniprogram.service.IWxService;
import com.tuoheng.miniprogram.utils.CurrentUserUtil;
import com.tuoheng.miniprogram.utils.GetOpenIdUtil;
import com.tuoheng.miniprogram.vo.WxAuthorizeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
@@ -50,6 +51,32 @@ public class WxServiceImpl implements IWxService {

private static long expiresTime;

public JsonResult checkAuthorize(String code) {
User user = CurrentUserUtil.getUserInfo();
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret);
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") || StringUtils.isNotEmpty(JSONObject.parseObject(openidResult).getString("errmsg"))) {
return JsonResult.error(JSONObject.parseObject(openidResult).getInteger("errcode").intValue(), JSONObject.parseObject(openidResult).getString("errmsg"));
}
String openid = JSONObject.parseObject(openidResult).getString("openid");
if (StringUtils.isEmpty(openid)) {
log.error("获取openid失败:", openidResult);
return JsonResult.error("获取openid失败");
}
// 根据当前登录用户id和vx查询授权表是否存在一条授权数据
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery()
.eq(UserAuthorize::getUserId, user.getId())
.eq(UserAuthorize::getOpenId, openid)
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode()));
WxAuthorizeVo wxAuthorizeVo = new WxAuthorizeVo();
wxAuthorizeVo.setOpenId(openid);
if (ObjectUtil.isNotEmpty(beforeUserAuthorize)) {
wxAuthorizeVo.setIsAuthroize(true);
} else {
wxAuthorizeVo.setIsAuthroize(false);
}
return JsonResult.success(wxAuthorizeVo);
}


/**
* 根据code获取openId

+ 24
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/vo/WxAuthorizeVo.java 查看文件

@@ -0,0 +1,24 @@
package com.tuoheng.miniprogram.vo;


import lombok.Data;

/**
* 微信授权登录判断类
*
* @Author suqin
* @Date 2023/01/09
*/
@Data
public class WxAuthorizeVo {

/**
* 是否授权 true授权过 false未授权
*/
private Boolean isAuthroize;

/**
* 微信唯一用户标识
*/
private String openId;
}

Loading…
取消
儲存