123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- package com.caimei365.user.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.caimei365.user.components.CommonService;
- import com.caimei365.user.components.RedisService;
- import com.caimei365.user.components.WeChatService;
- import com.caimei365.user.mapper.*;
- import com.caimei365.user.model.ResponseJson;
- import com.caimei365.user.model.dto.AuthBindDto;
- import com.caimei365.user.model.dto.LoginPasswordDto;
- import com.caimei365.user.model.dto.ScanBindDto;
- import com.caimei365.user.model.dto.SuperVipDto;
- import com.caimei365.user.model.po.OperationPo;
- import com.caimei365.user.model.po.SuperVipPo;
- import com.caimei365.user.model.po.UserBeansHistoryPo;
- import com.caimei365.user.model.vo.UserLoginVo;
- import com.caimei365.user.model.vo.UserVo;
- import com.caimei365.user.service.LoginService;
- import com.caimei365.user.utils.JwtUtil;
- import com.caimei365.user.utils.Md5Util;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.HttpHeaders;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.*;
- /**
- * Description
- *
- * @author : Charles
- * @date : 2021/3/8
- */
- @Slf4j
- @Service
- public class LoginServiceImpl implements LoginService {
- @Resource
- private SuperVipMapper vipMapper;
- @Resource
- private RedisService redisService;
- @Resource
- private WeChatService weChatService;
- @Resource
- private CommonService commonService;
- @Resource
- private LoginMapper loginMapper;
- @Resource
- private RegisterMapper registerMapper;
- @Resource
- private OperationMapper operationMapper;
- @Resource
- private ClubMapper clubMapper;
- @Resource
- private BaseMapper baseMapper;
- /**
- * 小程序邀请码过期天数
- */
- @Value("${caimei.validTime}")
- private Integer validTime;
- /**
- * 登录(用户名,密码)
- *
- * @param loginPasswordDto {
- * mobileOrEmail 手机号或邮箱
- * password 密码
- * unionId 微信unionId
- * }
- * @return BaseUser
- */
- @Override
- public ResponseJson<UserLoginVo> passwordLogin(LoginPasswordDto loginPasswordDto) {
- String mobileOrEmail = loginPasswordDto.getMobileOrEmail();
- String password = loginPasswordDto.getPassword();
- String unionId = loginPasswordDto.getUnionId();
- if (StringUtils.isBlank(mobileOrEmail)) {
- return ResponseJson.error("请填写账户名", null);
- }
- if (StringUtils.isBlank(password)) {
- return ResponseJson.error("请填写密码", null);
- }
- //处理比对密码
- UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
- if (baseUser != null) {
- // 如果前端传入unionId,则存入返回前端
- baseUser.setUnionId(unionId);
- // 比对密码
- String md5Password = Md5Util.md5(password);
- String dbPassword = baseUser.getPassword();
- if (md5Password.equals(dbPassword)) {
- if (baseUser.getUserIdentity() == 1) {
- // 协销登录
- return ResponseJson.success(baseUser);
- } else {
- // 返回登录校验结果
- return logonVerify(baseUser);
- }
- }
- }
- return ResponseJson.error("输入的密码和账户名不匹配", null);
- }
- /**
- * 微信授权登录(小程序)
- *
- * @param code 微信授权code
- * @param encryptedData 微信加密数据
- * @param iv 加密算法的初始向量
- * @param headers HttpHeaders
- */
- @Override
- public ResponseJson<UserLoginVo> appletsAuthorization(String code, String encryptedData, String iv, HttpHeaders headers) {
- if (StringUtils.isBlank(code)) {
- return ResponseJson.error("没有获取到微信授权code", null);
- }
- // 小程序微信授权获取登录信息
- ResponseJson<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, headers, 1);
- if (appletsInfo.getCode() == -1) {
- return ResponseJson.error(appletsInfo.getMsg(), null);
- }
- Map<String, Object> infoData = appletsInfo.getData();
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- String sessionKey = (String) infoData.get(WeChatService.Keys.SESSION_KEY);
- try {
- if (StringUtils.isEmpty(unionId) || StringUtils.isBlank(unionId)) {
- String result = WeChatService.decrypt(encryptedData, sessionKey, iv, "UTF-8");
- log.info("解密数据>>>>>>" + result);
- Map parseMap = JSONObject.parseObject(result, Map.class);
- assert parseMap != null;
- unionId = parseMap.get(WeChatService.Keys.UNION_ID).toString();
- infoData.put(WeChatService.Keys.UNION_ID, unionId);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return ResponseJson.error("微信解密失败", null);
- }
- // 用户数据存入Redis,key前缀:wxInfo:applets:
- redisService.setMap("wxInfo:applets:" + unionId, infoData);
- log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId);
- // 协销授权登录
- UserLoginVo seller = loginMapper.getServiceProviderUserByOpenId(openId);
- if (null != seller) {
- loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
- String token = JwtUtil.createToken(seller.getUserId());
- seller.setToken(token);
- seller.setUnionId(unionId);
- seller.setOpenId(openId);
- // 生成token给协销用户
- String sellerToken = JwtUtil.createToken(seller.getUserId());
- // 为了过期续签,将token存入redis,并设置超时时间
- redisService.set(sellerToken, sellerToken, JwtUtil.getExpireTime());
- seller.setToken(sellerToken);
- return ResponseJson.success(seller);
- }
- // 运营人员授权登录
- return operationAuthLogin(openId, unionId, "mini");
- }
- /**
- * 微信公众号授权链接(www)
- *
- * @param redirectUri 用于微信授权的中间页面
- * @param mode 授权方式:1静默授权,其他手动同意授权
- */
- @Override
- public ResponseJson<String> getAuthorizationLink(String redirectUri, Integer mode) {
- String link = weChatService.getAuthorizationLink(redirectUri, mode);
- String state = UUID.randomUUID().toString();
- redisService.set(state, state, 1800L);
- link = link.replace("STATE", state);
- return ResponseJson.success(link);
- }
- /**
- * 微信公众号授权登录(www)
- * <p>
- * spi旧接口:user/authorizationLogin
- *
- * @param code 微信code
- * @param state 安全认证
- * @param mode 1:静默授权,2:用户手动授权
- * @param headers HttpHeaders
- */
- @Override
- public ResponseJson<UserLoginVo> websiteAuthorization(String code, String state, Integer mode, HttpHeaders headers) {
- if (StringUtils.isBlank(code) || StringUtils.isBlank(state)) {
- return ResponseJson.error("参数异常:微信code和state不能为空!", null);
- }
- String wxState = (String) redisService.get(state);
- log.info("微信code>>>" + code + "state>>>" + wxState + "----" + state + "mode>>>" + mode);
- if (wxState.equals(state)) {
- try {
- // 通过code获取微信用户信息
- Map<String, Object> map = weChatService.getInfoMapByWeb(code, "crm");
- String openId = (String) map.get(WeChatService.Keys.OPEN_ID);
- if (mode == 1) {
- // 静默授权
- Integer userId = loginMapper.getUserIdByOpenId(openId, "www");
- if (null != userId && userId > 0) {
- UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
- // 返回登录用户
- return logonVerify(user);
- } else {
- return ResponseJson.error(-4, "您的微信尚未绑定任何机构", null);
- }
- } else {
- // 获取access_token
- String accessToken = weChatService.getAccessToken();
- // 获取微信用户信息
- Map<String, Object> infoData = weChatService.getUserInfo(accessToken, openId);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- // 用户数据存入Redis,key前缀:wxInfo:website:
- redisService.setMap("wxInfo:website:" + unionId, infoData);
- log.info("移动端授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:website:" + unionId);
- // 运营人员授权登录
- return operationAuthLogin(openId, unionId, "www");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return ResponseJson.error("获取微信信息异常", null);
- }
- }
- return ResponseJson.error("请从正确的途径打开链接", null);
- }
- /**
- * 运营人员授权登录
- *
- * @param openId 微信openId
- * @param unionId 微信unionId
- * @param source 来源:www网站, mini小程序
- * @return BaseUser
- */
- private ResponseJson<UserLoginVo> operationAuthLogin(String openId, String unionId, String source) {
- UserLoginVo operation = loginMapper.getLoginUserByUnionId(unionId, source);
- if (null == operation) {
- operation = loginMapper.getLoginUserByOpenId(openId, source);
- if (null == operation) {
- operation = new UserLoginVo();
- operation.setOpenId(openId);
- operation.setUnionId(unionId);
- return ResponseJson.error(-4, "您的微信尚未绑定任何机构", operation);
- } else {
- // 表示 openId存在, unionId不存在
- operationMapper.updateOperationUnionId(operation.getUserId(), unionId);
- }
- }
- // 如果unionId存在, openId不存在
- if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
- operationMapper.updateOperationOpenId(operation.getUserId(), openId);
- }
- operation.setOpenId(openId);
- operation.setUnionId(unionId);
- // 返回登录校验结果
- return logonVerify(operation);
- }
- /**
- * 登录校验
- *
- * @param loginUser 用户信息
- * @return UserLoginVo
- */
- private ResponseJson<UserLoginVo> logonVerify(UserLoginVo loginUser) {
- // 生成token给用户
- String token = JwtUtil.createToken(loginUser.getUserId());
- // 为了过期续签,将token存入redis,并设置超时时间
- redisService.set(token, token, JwtUtil.getExpireTime());
- loginUser.setToken(token);
- if (null != loginUser.getClubStatus() && 91 == loginUser.getClubStatus()) {
- //机构
- return ResponseJson.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
- }
- // 供应商
- if (null != loginUser.getShopStatus() && null != loginUser.getUserIdentity() && 3 == loginUser.getUserIdentity()) {
- if (3 == loginUser.getShopStatus()) {
- return ResponseJson.error(-2, "您的企业账号正在加速审核中,审核通过后即可登录", null);
- }
- if (91 == loginUser.getShopStatus()) {
- return ResponseJson.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
- }
- if (92 == loginUser.getShopStatus()) {
- return ResponseJson.error(-3, "您的企业账号审核未通过", loginUser);
- }
- }
- if (null != loginUser.getUserIdentity() && (2 == loginUser.getUserIdentity() || 4 == loginUser.getUserIdentity())) {
- Integer id = clubMapper.findLoginBeans(loginUser.getUserId());
- if (id == null) {
- //登录赠送10采美豆
- UserVo user = baseMapper.getUserByUserId(loginUser.getUserId());
- UserBeansHistoryPo beansHistory = new UserBeansHistoryPo();
- beansHistory.setUserId(user.getUserId());
- beansHistory.setBeansType(12);
- beansHistory.setType(1);
- beansHistory.setNum(10);
- beansHistory.setPushStatus(0);
- beansHistory.setAddTime(new Date());
- registerMapper.insertBeansHistory(beansHistory);
- Integer userBeans = user.getUserBeans();
- userBeans = userBeans == null ? 10 : userBeans + 10;
- clubMapper.updateUserBeans(loginUser.getUserId(), userBeans);
- }
- // 会员机构类型:1医美,2生
- if (loginUser.getUserIdentity() == 2) {
- Integer clubType = loginMapper.getClubTypeById(loginUser.getUserId());
- loginUser.setFirstClubType(clubType);
- }
- // 机构超级会员
- SuperVipDto end = findEnd(loginUser.getUserId());
- loginUser.setVipFlag(end.getVipFlag());
- if (1 == loginUser.getVipFlag() && 4 == loginUser.getUserIdentity()) {
- // 个人超级会员权限置为会员机构
- loginUser.setUserIdentity(2);
- loginUser.setUserPermission(2);
- }
- }
- // 改user表登录时间
- try {
- log.info("登陆时间录入");
- loginMapper.updateLogin(loginUser.getUserId());
- } catch (Exception e) {
- log.error("登录时间记录异常" + e);
- }
- return ResponseJson.success("登录成功", loginUser);
- }
- /**
- * 获取生成微信二维码的参数(www)
- *
- * @return Map<String, Object>
- */
- @Override
- public ResponseJson<Map<String, String>> getAuthParameters() {
- UUID state = UUID.randomUUID();
- Map<String, String> dataMap = new HashMap<>(3);
- dataMap.put("appId", weChatService.getAppId());
- dataMap.put("redirectUri", weChatService.getRedirectUri());
- dataMap.put("state", String.valueOf(state));
- redisService.set("state:" + state, String.valueOf(state), 1800L);
- return ResponseJson.success(dataMap);
- }
- /**
- * 微信用户扫码,微信服务器回调
- *
- * @param code 微信code
- * @param state 安全认证key(上一步获取参数时自定义生成的uuid)
- */
- @Override
- public void qrCodeAuthScan(String code, String state) {
- String errorMsg = "";
- // 简单验证,防止csrf攻击(跨站请求伪造攻击)
- String stateCache = (String) redisService.get("state:" + state);
- if (StringUtils.isBlank(stateCache) || "null".equals(stateCache)) {
- errorMsg = "请从正确的途径打开链接";
- }
- if (StringUtils.isEmpty(code)) {
- errorMsg = "请重新进行授权登录";
- }
- try {
- // 用code换取access_token
- Map<String, Object> tokenMap = weChatService.getInfoMapByWeb(code, "pc");
- String accessToken = (String) tokenMap.get("access_token");
- String openId = (String) tokenMap.get(WeChatService.Keys.OPEN_ID);
- log.info(">>>>>(code换取access_token)wx回调openId:" + openId + " ,accessToken:" + accessToken);
- // 用access_token获取微信用户信息
- Map<String, Object> infoData = weChatService.getUserInfoByWeb(accessToken, openId);
- log.info(">>>>>(用access_token获取用户信息)wx回调openId:" + infoData.get(WeChatService.Keys.OPEN_ID) + " ,unionId:" + infoData.get(WeChatService.Keys.UNION_ID));
- // 微信用户信息存入redis
- redisService.setMap("scan:" + state, infoData);
- } catch (Exception e) {
- errorMsg = "获取微信用户信息失败";
- }
- // 错误信息存入Redis
- redisService.set("error:" + state, errorMsg, 1800L);
- }
- /**
- * 校验扫码结果
- *
- * @param state 安全认证key(第一步获取参数时自定义生成的uuid)
- * @return UserLoginVo
- */
- @Override
- public ResponseJson<UserLoginVo> qrCodeAuthScanResult(String state) {
- if (StringUtils.isBlank(state)) {
- return ResponseJson.error("参数异常:state不能为空!", null);
- }
- String errorMsg = (String) redisService.get("error:" + state);
- if (StringUtils.isNotEmpty(errorMsg) && !"null".equals(errorMsg)) {
- return ResponseJson.error(errorMsg, null);
- }
- Map<Object, Object> infoData = redisService.getEntries("scan:" + state);
- if (null == infoData || infoData.size() == 0) {
- return ResponseJson.error(-90, "redis缓存的扫码数据没有拿到", null);
- }
- // 清除redis的扫码数据
- redisService.remove("scan:" + state);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- log.info(">>>>>>pc商城unionId:" + unionId + " ,openId:" + openId);
- // 用户数据存入Redis,key前缀:wxInfo:website:
- String infoDataStr = JSON.toJSONString(infoData);
- Map<String, Object> infoDataMap = JSON.parseObject(infoDataStr);
- redisService.setMap("wxInfo:website:" + unionId, infoDataMap);
- log.info("微信扫码登录,用户数据存入Redis,key:wxInfo:website:" + unionId);
- // 运营人员授权登录
- return operationAuthLogin(openId, unionId, "www");
- }
- /**
- * 微信扫码后,绑定机构账号
- *
- * @param scanBindDto {
- * mobileOrEmail 手机号或邮箱
- * password 密码
- * mobile 手机号
- * smsCode 手机验证码
- * linkName 联系人
- * }
- */
- @Override
- public ResponseJson<UserLoginVo> qrCodeAuthScanBind(ScanBindDto scanBindDto) {
- String mobileOrEmail = scanBindDto.getMobileOrEmail();
- String password = scanBindDto.getPassword();
- String mobile = scanBindDto.getMobile();
- String smsCode = scanBindDto.getSmsCode();
- String linkName = scanBindDto.getLinkName();
- String unionId = scanBindDto.getUnionId();
- // 参数校验
- if (StringUtils.isBlank(mobileOrEmail)) {
- return ResponseJson.error("参数异常:手机号或邮箱不能为空!", null);
- }
- if (StringUtils.isBlank(password)) {
- return ResponseJson.error("参数异常:密码不能为空!", null);
- }
- if (StringUtils.isBlank(mobile)) {
- return ResponseJson.error("参数异常:手机号不能为空!", null);
- }
- if (StringUtils.isBlank(smsCode)) {
- return ResponseJson.error("参数异常:短信验证码不能为空!", null);
- }
- if (StringUtils.isBlank(unionId)) {
- return ResponseJson.error("参数异常:unionId不能为空!", null);
- }
- //处理比对密码
- UserLoginVo user = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
- String md5Password = Md5Util.md5(password);
- if (null != user && md5Password.equals(user.getPassword())) {
- // 查询使用该手机号的运营人员或用户
- String checkRust = commonService.operationBindCheck(mobile, smsCode);
- if (checkRust != null) {
- return ResponseJson.error(checkRust, null);
- }
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:website:" + unionId);
- log.info("扫码绑定微信, 获取unionId>>>>>>" + unionId);
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- String nickName = (String) infoData.get("nickname");
- String avatarUrl = (String) infoData.get("headimgurl");
- // 判断微信是否已经绑定
- UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "www");
- if (operationByUnionId != null) {
- return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
- }
- /*
- 组装运营人员数据 operation
- */
- OperationPo operation = new OperationPo();
- // 用户Id
- operation.setUserId(user.getUserId());
- // 手机号
- operation.setMobile(mobile);
- operation.setLinkName(linkName);
- // 微信昵称头像
- operation.setNickName(nickName);
- operation.setAvatarUrl(avatarUrl);
- // unionId,openId
- operation.setUnionId(unionId);
- operation.setPcOpenId(openId);
- // 组织机构0
- operation.setOrganizeId(0);
- // 绑定的机构/供应商Id,绑定的用户类型
- if (3 == user.getUserIdentity()) {
- operation.setShopId(user.getShopId());
- operation.setUserType(2);
- } else {
- operation.setClubId(user.getClubId());
- operation.setUserType(1);
- }
- // 绑定状态
- operation.setStatus(2);
- // 删除标识
- operation.setDelFlag(0);
- Date time = new Date();
- // 添加时间
- operation.setAddTime(time);
- // 绑定时间
- operation.setBindTime(time);
- // 更新时间
- operation.setUpdateTime(time);
- /*
- 保存数据库 operation
- */
- registerMapper.insertOperation(operation);
- return logonVerify(user);
- }
- return ResponseJson.error("输入的密码和账户名不匹配", null);
- }
- /**
- * 邀请码登录
- *
- * @param invitationCode 邀请码
- * @param nickName 微信昵称
- * @param avatarUrl 微信头像(headimgurl)
- * @param unionId 微信unionId
- * @return UserLoginVo
- */
- @Override
- public ResponseJson<UserLoginVo> invitationCodeLogin(String invitationCode, String nickName, String avatarUrl, String unionId) {
- // 参数校验
- if (StringUtils.isBlank(invitationCode)) {
- return ResponseJson.error("邀请码不能为空", null);
- }
- UserLoginVo operation = loginMapper.getOperationUserByInvitationCode(invitationCode);
- if (operation == null) {
- return ResponseJson.error("邀请码错误", null);
- }
- Date date = new Date();
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(operation.getInvitationCodeTime());
- calendar.add(Calendar.DATE, validTime);
- if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
- return ResponseJson.error("邀请码已失效", null);
- }
- if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
- return ResponseJson.error("邀请码已被使用", null);
- }
- // 用户身份:1机构,2供应商
- int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
- if (1 == userIdentity && operation.getClubStatus() != null && 91 == operation.getClubStatus()) {
- return ResponseJson.error("您的机构已冻结", null);
- }
- if (2 == userIdentity && operation.getShopStatus() != null && 91 == operation.getShopStatus()) {
- return ResponseJson.error("您的企业账号已被冻结,请联系客服处理", null);
- }
- if (0 != operation.getDelFlag()) {
- return ResponseJson.error("您的邀请码已被删除,请重新添加运营人员", null);
- }
- OperationPo operationPo = new OperationPo();
- operationPo.setId(operation.getOperationId());
- // 微信unionId
- operationPo.setUnionId(unionId);
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
- // 微信openId
- operationPo.setOpenId((String) infoData.get(WeChatService.Keys.OPEN_ID));
- // 微信昵称
- operationPo.setNickName(nickName);
- // 微信头像
- operationPo.setAvatarUrl(avatarUrl);
- // 绑定状态,1未绑定,2已绑定
- operationPo.setStatus(2);
- if (1 == userIdentity) {
- // 机构Id
- operationPo.setClubId(operation.getClubId());
- // 用户类型
- operationPo.setUserType(1);
- } else {
- // 供应商Id
- operationPo.setShopId(operation.getShopId());
- // 用户类型
- operationPo.setUserType(2);
- }
- // 更新运营人员信息
- operationMapper.updateOperationByInvitation(operationPo);
- // 返回登录校验结果
- return logonVerify(operation);
- }
- /**
- * 运营人员绑定微信
- *
- * @param authBindDto {
- * userId 要绑定的用户Id(userID)
- * mobile 手机号
- * smsCode 手机验证码(verificationCode)
- * unionId 微信unionId
- * nickName 微信昵称
- * avatarUrl 微信头像(headimgurl)
- * }
- * @return OperationPo
- */
- @Override
- public ResponseJson<UserLoginVo> operationBindWeChat(AuthBindDto authBindDto) {
- Integer userId = authBindDto.getUserId();
- String mobile = authBindDto.getMobile();
- String smsCode = authBindDto.getSmsCode();
- String linkName = authBindDto.getLinkName();
- String unionId = authBindDto.getUnionId();
- String nickName = authBindDto.getNickName();
- String avatarUrl = authBindDto.getAvatarUrl();
- String isCheckSmsCode = authBindDto.getIsCheckSmsCode();
- // 参数校验
- if (null == userId) {
- return ResponseJson.error("参数异常:用户Id不能为空!", null);
- }
- if (StringUtils.isBlank(mobile)) {
- return ResponseJson.error("参数异常:手机号不能为空!", null);
- }
- boolean b = StringUtils.isBlank(isCheckSmsCode) || "0".equals(isCheckSmsCode);
- if (b && StringUtils.isBlank(smsCode)) {
- return ResponseJson.error("参数异常:短信验证码不能为空!", null);
- }
- if (StringUtils.isBlank(unionId)) {
- return ResponseJson.error("参数异常:unionId不能为空!", null);
- }
- // 查询使用该手机号的运营人员或用户
- String checkRust = commonService.operationBindCheck(mobile, smsCode);
- if (checkRust != null) {
- return ResponseJson.error(checkRust, null);
- }
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
- log.info("绑定微信bindingWx,获取unionId>>>>>>" + unionId);
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- // 判断微信是否已经绑定
- UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "mini");
- if (operationByUnionId != null) {
- return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
- }
- // 要绑定的用户
- UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
- /*
- 组装运营人员数据 operation
- */
- OperationPo operation = new OperationPo();
- // 用户Id
- operation.setUserId(userId);
- // 手机号
- operation.setMobile(mobile);
- operation.setLinkName(linkName);
- // 微信昵称头像
- operation.setNickName(nickName);
- operation.setAvatarUrl(avatarUrl);
- // unionId,openId
- operation.setUnionId(unionId);
- operation.setOpenId(openId);
- // 组织机构0
- operation.setOrganizeId(0);
- // 绑定的机构/供应商Id,绑定的用户类型
- if (user != null && 3 == user.getUserIdentity()) {
- operation.setShopId(user.getShopId());
- operation.setUserType(2);
- } else if (user != null) {
- operation.setClubId(user.getClubId());
- operation.setUserType(1);
- }
- // 绑定状态
- operation.setStatus(2);
- // 删除标识
- operation.setDelFlag(0);
- Date time = new Date();
- // 添加时间
- operation.setAddTime(time);
- // 绑定时间
- operation.setBindTime(time);
- // 更新时间
- operation.setUpdateTime(time);
- /*
- 保存数据库 operation
- */
- registerMapper.insertOperation(operation);
- return ResponseJson.success("绑定微信成功", user);
- }
- /**
- * 根据userId查是否过期,返回dto对象,flag=0未买过,-1过期,1有效,endTime过期时间
- */
- private SuperVipDto findEnd(Integer userId) {
- SuperVipPo superVip = vipMapper.findSuperVip(userId);
- SuperVipDto superVipDto = new SuperVipDto();
- if (superVip == null) {
- superVipDto.setVipFlag(0);
- } else {
- SuperVipPo endTime = vipMapper.findEndTime(userId);
- if (endTime == null) {
- superVipDto.setVipFlag(-1);
- superVipDto.setEndTime(superVip.getEndTime());
- } else {
- superVipDto.setVipFlag(1);
- superVipDto.setEndTime(endTime.getEndTime());
- }
- }
- return superVipDto;
- }
- }
|