绑定微信小程序或公众号
This commit is contained in:
parent
fd8e0d5fc8
commit
a3aafaf552
|
|
@ -2,8 +2,6 @@ package com.mdd.front.controller;
|
||||||
|
|
||||||
import com.mdd.common.aop.NotLogin;
|
import com.mdd.common.aop.NotLogin;
|
||||||
import com.mdd.common.core.AjaxResult;
|
import com.mdd.common.core.AjaxResult;
|
||||||
import com.mdd.common.exception.LoginException;
|
|
||||||
import com.mdd.common.exception.OperateException;
|
|
||||||
import com.mdd.front.LikeFrontThreadLocal;
|
import com.mdd.front.LikeFrontThreadLocal;
|
||||||
import com.mdd.front.service.IUserService;
|
import com.mdd.front.service.IUserService;
|
||||||
import com.mdd.front.validate.users.*;
|
import com.mdd.front.validate.users.*;
|
||||||
|
|
@ -96,4 +94,24 @@ public class UserController {
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/bindMnp")
|
||||||
|
@ApiOperation(value="绑定小程序")
|
||||||
|
public AjaxResult<Object> bindMnp(@Validated @RequestBody UserBindWechatValidate BindMnpValidate) {
|
||||||
|
Integer userId = LikeFrontThreadLocal.getUserId();
|
||||||
|
iUserService.bindMnp(BindMnpValidate, userId);
|
||||||
|
return AjaxResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/bindOa")
|
||||||
|
@ApiOperation(value="绑定微信公众号")
|
||||||
|
public AjaxResult<Object> bindOa(@Validated @RequestBody UserBindWechatValidate BindOaValidate) {
|
||||||
|
Integer userId = LikeFrontThreadLocal.getUserId();
|
||||||
|
iUserService.bindOa(BindOaValidate, userId);
|
||||||
|
return AjaxResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
package com.mdd.front.service;
|
package com.mdd.front.service;
|
||||||
|
|
||||||
import com.mdd.front.validate.users.NewUserUpdateValidate;
|
import com.mdd.front.validate.users.*;
|
||||||
import com.mdd.front.validate.users.UserForgetPwdValidate;
|
|
||||||
import com.mdd.front.validate.users.UserPhoneBindValidate;
|
|
||||||
import com.mdd.front.validate.users.UserUpdateValidate;
|
|
||||||
import com.mdd.front.vo.users.UserCenterVo;
|
import com.mdd.front.vo.users.UserCenterVo;
|
||||||
import com.mdd.front.vo.users.UserInfoVo;
|
import com.mdd.front.vo.users.UserInfoVo;
|
||||||
|
|
||||||
|
|
@ -83,4 +80,21 @@ public interface IUserService {
|
||||||
* @param userId 用户id
|
* @param userId 用户id
|
||||||
*/
|
*/
|
||||||
void updateNewUserInfo(NewUserUpdateValidate newUserUpdateValidate, Integer userId);
|
void updateNewUserInfo(NewUserUpdateValidate newUserUpdateValidate, Integer userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定微信小程序
|
||||||
|
*
|
||||||
|
* @param bindMnpValidate
|
||||||
|
* @param userId
|
||||||
|
*/
|
||||||
|
void bindMnp(UserBindWechatValidate bindMnpValidate, Integer userId);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定微信公众号
|
||||||
|
*
|
||||||
|
* @param bindOaValidate
|
||||||
|
* @param userId
|
||||||
|
*/
|
||||||
|
void bindOa(UserBindWechatValidate bindOaValidate, Integer userId);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.mdd.front.service.impl;
|
||||||
|
|
||||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||||
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||||
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
||||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
|
@ -17,13 +18,12 @@ import com.mdd.common.plugin.notice.NoticeCheck;
|
||||||
import com.mdd.common.util.*;
|
import com.mdd.common.util.*;
|
||||||
import com.mdd.front.LikeFrontThreadLocal;
|
import com.mdd.front.LikeFrontThreadLocal;
|
||||||
import com.mdd.front.service.IUserService;
|
import com.mdd.front.service.IUserService;
|
||||||
import com.mdd.front.validate.users.NewUserUpdateValidate;
|
import com.mdd.front.validate.users.*;
|
||||||
import com.mdd.front.validate.users.UserForgetPwdValidate;
|
|
||||||
import com.mdd.front.validate.users.UserPhoneBindValidate;
|
|
||||||
import com.mdd.front.validate.users.UserUpdateValidate;
|
|
||||||
import com.mdd.front.vo.users.UserCenterVo;
|
import com.mdd.front.vo.users.UserCenterVo;
|
||||||
import com.mdd.front.vo.users.UserInfoVo;
|
import com.mdd.front.vo.users.UserInfoVo;
|
||||||
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
@ -321,4 +321,97 @@ public class UserServiceImpl implements IUserService {
|
||||||
userMapper.updateById(user);
|
userMapper.updateById(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定小程序
|
||||||
|
*
|
||||||
|
* @param bindMnpValidate
|
||||||
|
* @param userId
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void bindMnp(UserBindWechatValidate bindMnpValidate, Integer userId) {
|
||||||
|
try {
|
||||||
|
// 通过code获取微信信息
|
||||||
|
String code = bindMnpValidate.getCode();
|
||||||
|
WxMaService wxMaService = WeChatUtils.mnp();
|
||||||
|
WxMaJscode2SessionResult sessionResult = wxMaService.getUserService().getSessionInfo(code);
|
||||||
|
String openId = sessionResult.getOpenid();
|
||||||
|
String uniId = sessionResult.getUnionid();
|
||||||
|
String unionId = uniId == null ? "0" : uniId;
|
||||||
|
|
||||||
|
// 授权校验,未授权创建授权,已授权返回
|
||||||
|
bindWechatAuth(openId, unionId, ClientEnum.MNP.getCode(), userId);
|
||||||
|
|
||||||
|
} catch (WxErrorException e) {
|
||||||
|
throw new OperateException(e.getError().getErrorCode() + ", " + e.getError().getErrorMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定公众号
|
||||||
|
*
|
||||||
|
* @param bindOaValidate
|
||||||
|
* @param userId
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void bindOa(UserBindWechatValidate bindOaValidate, Integer userId) {
|
||||||
|
try {
|
||||||
|
// 通过code获取微信信息
|
||||||
|
WxMpService wxMpService = WeChatUtils.official();
|
||||||
|
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(bindOaValidate.getCode());
|
||||||
|
String uniId = wxOAuth2AccessToken.getUnionId();
|
||||||
|
String openId = wxOAuth2AccessToken.getOpenId();
|
||||||
|
String unionId = uniId == null ? "0" : uniId;
|
||||||
|
|
||||||
|
// 授权校验,未授权创建授权,已授权返回
|
||||||
|
bindWechatAuth(openId, unionId, ClientEnum.OA.getCode(), userId);
|
||||||
|
|
||||||
|
} catch (WxErrorException e) {
|
||||||
|
throw new OperateException(e.getError().getErrorCode() + ", " + e.getError().getErrorMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定微信授权
|
||||||
|
*
|
||||||
|
* @param openId
|
||||||
|
* @param unionId
|
||||||
|
* @param terminal
|
||||||
|
* @param userId
|
||||||
|
*/
|
||||||
|
public void bindWechatAuth(String openId, String unionId, Integer terminal, Integer userId) {
|
||||||
|
// 授权表中查找授权记录
|
||||||
|
UserAuth userAuthOpenId = userAuthMapper.selectOne(new QueryWrapper<UserAuth>()
|
||||||
|
.eq("openid", openId)
|
||||||
|
.last("limit 1"));
|
||||||
|
|
||||||
|
if (userAuthOpenId != null) {
|
||||||
|
// 该微信已绑定
|
||||||
|
throw new OperateException("该微信已绑定");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 已有授权,返回已绑定微信。 没有授权,绑定微信
|
||||||
|
if (!StringUtils.isBlank(unionId)) {
|
||||||
|
UserAuth userAuthUnionId = userAuthMapper.selectOne(new QueryWrapper<UserAuth>()
|
||||||
|
.eq("unionid", unionId)
|
||||||
|
.last("limit 1"));
|
||||||
|
|
||||||
|
if (userAuthUnionId != null && !userId.equals(userAuthUnionId.getUserId())) {
|
||||||
|
// 该微信已绑定
|
||||||
|
throw new OperateException("该微信已绑定");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
UserAuth authModel = new UserAuth();
|
||||||
|
authModel.setUserId(userId);
|
||||||
|
authModel.setUnionid(unionId);
|
||||||
|
authModel.setOpenid(openId);
|
||||||
|
authModel.setTerminal(terminal);
|
||||||
|
authModel.setCreateTime(System.currentTimeMillis() / 1000);
|
||||||
|
authModel.setUpdateTime(System.currentTimeMillis() / 1000);
|
||||||
|
userAuthMapper.insert(authModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.mdd.front.validate.users;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("绑定微信小程序或公众号")
|
||||||
|
public class UserBindWechatValidate implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@NotNull(message = "code参数缺失")
|
||||||
|
@ApiModelProperty(value = "微信code", required = true)
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue