|
|
@@ -20,7 +20,6 @@ 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; |
|
|
@@ -59,7 +58,10 @@ public class WxServiceImpl implements IWxService { |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public JsonResult checkAuthorize(String code) { |
|
|
|
public JsonResult openId(String code) { |
|
|
|
if (StringUtils.isEmpty(code)) { |
|
|
|
return JsonResult.error("code为空"); |
|
|
|
} |
|
|
|
String userId = CurrentUserUtil.getUserId(); |
|
|
|
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret); |
|
|
|
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") || |
|
|
@@ -72,64 +74,23 @@ public class WxServiceImpl implements IWxService { |
|
|
|
log.error("获取openid失败:", openidResult); |
|
|
|
return JsonResult.error("获取openid失败"); |
|
|
|
} |
|
|
|
|
|
|
|
//默认为false未授权(对应标识1 弹出授权页面给用户) |
|
|
|
boolean flag = false; |
|
|
|
//根据当前登录用户id和vx查询授权表是否存在一条授权数据 |
|
|
|
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() |
|
|
|
.eq(UserAuthorize::getUserId, userId) |
|
|
|
.eq(UserAuthorize::getOpenId,openid) |
|
|
|
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); |
|
|
|
|
|
|
|
//有: 该vx和当前账号绑定过 置为true (对应标识2) |
|
|
|
if (!ObjectUtil.isNull(beforeUserAuthorize)) { |
|
|
|
flag = true; |
|
|
|
} |
|
|
|
|
|
|
|
WxAuthorizeVo wxAuthorizeVo = new WxAuthorizeVo(); |
|
|
|
wxAuthorizeVo.setIsAuthroize(flag); |
|
|
|
wxAuthorizeVo.setOpenId(openid); |
|
|
|
return JsonResult.success(wxAuthorizeVo); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public JsonResult bind(String openId) { |
|
|
|
String userId = CurrentUserUtil.getUserId(); |
|
|
|
if(StringUtils.isEmpty(openId)){ |
|
|
|
return JsonResult.error(UserCodeEnum.USER_ID_IS_NULL.getCode(), UserCodeEnum.USER_ID_IS_NULL.getMsg()); |
|
|
|
} |
|
|
|
if(StringUtils.isEmpty(userId)){ |
|
|
|
return JsonResult.error(UserCodeEnum.USER_0PENID_IS_NULL.getCode(), UserCodeEnum.USER_0PENID_IS_NULL.getMsg()); |
|
|
|
} |
|
|
|
//openId入库 |
|
|
|
//根据当前登录用户id查询授权表是否存在一条授权数据 |
|
|
|
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() |
|
|
|
.eq(UserAuthorize::getUserId, userId) |
|
|
|
.eq(UserAuthorize::getOpenId, code) |
|
|
|
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); |
|
|
|
if (ObjectUtil.isNotNull(beforeUserAuthorize)) { |
|
|
|
|
|
|
|
beforeUserAuthorize.setOpenId(openId); |
|
|
|
beforeUserAuthorize.setUserId(userId); |
|
|
|
beforeUserAuthorize.setUpdateUser(userId); |
|
|
|
beforeUserAuthorize.setUpdateTime(DateUtils.now()); |
|
|
|
int count = userAuthorizeMapper.updateById(beforeUserAuthorize); |
|
|
|
if (count <= 0) { |
|
|
|
return JsonResult.error("用户更新绑定微信账号失败"); |
|
|
|
if (ObjectUtil.isEmpty(beforeUserAuthorize)) { |
|
|
|
UserAuthorize userAuthorize = new UserAuthorize(); |
|
|
|
userAuthorize.setOpenId(openid); |
|
|
|
userAuthorize.setUserId(userId); |
|
|
|
userAuthorize.setCreateTime(DateUtils.now()); |
|
|
|
userAuthorize.setCreateUser(userId); |
|
|
|
int result = userAuthorizeMapper.insert(userAuthorize); |
|
|
|
if (result <= 0) { |
|
|
|
return JsonResult.error("授权数据入库失败"); |
|
|
|
} |
|
|
|
return JsonResult.success(); |
|
|
|
} |
|
|
|
|
|
|
|
UserAuthorize userAuthorize = new UserAuthorize(); |
|
|
|
userAuthorize.setOpenId(openId); |
|
|
|
userAuthorize.setUserId(userId); |
|
|
|
userAuthorize.setCreateUser(userId); |
|
|
|
userAuthorize.setCreateTime(DateUtils.now()); |
|
|
|
int result = userAuthorizeMapper.insert(userAuthorize); |
|
|
|
|
|
|
|
if (result <= 0) { |
|
|
|
return JsonResult.error("授权数据入库失败"); |
|
|
|
} |
|
|
|
//修改authorize为已授权到用户表中 |
|
|
|
//关联登录用户,已经做过授权 修改授权标识 |
|
|
|
User user = CurrentUserUtil.getUserInfo(); |
|
|
|
if (ObjectUtil.isEmpty(user)) { |
|
|
|
return JsonResult.error(UserCodeEnum.USER_IS_NULL.getCode(), UserCodeEnum.USER_IS_NULL.getMsg()); |
|
|
@@ -173,7 +134,6 @@ public class WxServiceImpl implements IWxService { |
|
|
|
//失效时间 = 当前时间+有效期(提前一分钟,也可以不提前) |
|
|
|
expiresTime = System.currentTimeMillis() + (expires_in - 60) * 1000; |
|
|
|
} |
|
|
|
|
|
|
|
return accessToken; |
|
|
|
} |
|
|
|
|