LoginServiceImpl.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package com.caimei365.user.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.caimei365.user.components.CommonService;
  5. import com.caimei365.user.components.RedisService;
  6. import com.caimei365.user.components.WeChatService;
  7. import com.caimei365.user.mapper.*;
  8. import com.caimei365.user.model.ResponseJson;
  9. import com.caimei365.user.model.dto.AuthBindDto;
  10. import com.caimei365.user.model.dto.LoginPasswordDto;
  11. import com.caimei365.user.model.dto.ScanBindDto;
  12. import com.caimei365.user.model.dto.SuperVipDto;
  13. import com.caimei365.user.model.po.OperationPo;
  14. import com.caimei365.user.model.po.SuperVipPo;
  15. import com.caimei365.user.model.po.UserBeansHistoryPo;
  16. import com.caimei365.user.model.vo.UserLoginVo;
  17. import com.caimei365.user.model.vo.UserVo;
  18. import com.caimei365.user.service.LoginService;
  19. import com.caimei365.user.utils.JwtUtil;
  20. import com.caimei365.user.utils.Md5Util;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.stereotype.Service;
  26. import javax.annotation.Resource;
  27. import java.util.*;
  28. /**
  29. * Description
  30. *
  31. * @author : Charles
  32. * @date : 2021/3/8
  33. */
  34. @Slf4j
  35. @Service
  36. public class LoginServiceImpl implements LoginService {
  37. @Resource
  38. private SuperVipMapper vipMapper;
  39. @Resource
  40. private RedisService redisService;
  41. @Resource
  42. private WeChatService weChatService;
  43. @Resource
  44. private CommonService commonService;
  45. @Resource
  46. private LoginMapper loginMapper;
  47. @Resource
  48. private RegisterMapper registerMapper;
  49. @Resource
  50. private OperationMapper operationMapper;
  51. @Resource
  52. private ClubMapper clubMapper;
  53. @Resource
  54. private BaseMapper baseMapper;
  55. /**
  56. * 小程序邀请码过期天数
  57. */
  58. @Value("${caimei.validTime}")
  59. private Integer validTime;
  60. /**
  61. * 登录(用户名,密码)
  62. *
  63. * @param loginPasswordDto {
  64. * mobileOrEmail 手机号或邮箱
  65. * password 密码
  66. * unionId 微信unionId
  67. * }
  68. * @return BaseUser
  69. */
  70. @Override
  71. public ResponseJson<UserLoginVo> passwordLogin(LoginPasswordDto loginPasswordDto) {
  72. String mobileOrEmail = loginPasswordDto.getMobileOrEmail();
  73. String password = loginPasswordDto.getPassword();
  74. String unionId = loginPasswordDto.getUnionId();
  75. if (StringUtils.isBlank(mobileOrEmail)) {
  76. return ResponseJson.error("请填写账户名", null);
  77. }
  78. if (StringUtils.isBlank(password)) {
  79. return ResponseJson.error("请填写密码", null);
  80. }
  81. //处理比对密码
  82. UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
  83. if (baseUser != null) {
  84. // 如果前端传入unionId,则存入返回前端
  85. baseUser.setUnionId(unionId);
  86. // 比对密码
  87. String md5Password = Md5Util.md5(password);
  88. String dbPassword = baseUser.getPassword();
  89. if (md5Password.equals(dbPassword)) {
  90. if (baseUser.getUserIdentity() == 1) {
  91. // 协销登录
  92. return ResponseJson.success(baseUser);
  93. } else {
  94. // 返回登录校验结果
  95. return logonVerify(baseUser);
  96. }
  97. }
  98. }
  99. return ResponseJson.error("输入的密码和账户名不匹配", null);
  100. }
  101. /**
  102. * 微信授权登录(小程序)
  103. *
  104. * @param code 微信授权code
  105. * @param encryptedData 微信加密数据
  106. * @param iv 加密算法的初始向量
  107. * @param headers HttpHeaders
  108. */
  109. @Override
  110. public ResponseJson<UserLoginVo> appletsAuthorization(String code, String encryptedData, String iv, HttpHeaders headers) {
  111. if (StringUtils.isBlank(code)) {
  112. return ResponseJson.error("没有获取到微信授权code", null);
  113. }
  114. // 小程序微信授权获取登录信息
  115. ResponseJson<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, headers, 1);
  116. if (appletsInfo.getCode() == -1) {
  117. return ResponseJson.error(appletsInfo.getMsg(), null);
  118. }
  119. Map<String, Object> infoData = appletsInfo.getData();
  120. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  121. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  122. String sessionKey = (String) infoData.get(WeChatService.Keys.SESSION_KEY);
  123. try {
  124. if (StringUtils.isEmpty(unionId) || StringUtils.isBlank(unionId)) {
  125. String result = WeChatService.decrypt(encryptedData, sessionKey, iv, "UTF-8");
  126. log.info("解密数据>>>>>>" + result);
  127. Map parseMap = JSONObject.parseObject(result, Map.class);
  128. assert parseMap != null;
  129. unionId = parseMap.get(WeChatService.Keys.UNION_ID).toString();
  130. infoData.put(WeChatService.Keys.UNION_ID, unionId);
  131. }
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. return ResponseJson.error("微信解密失败", null);
  135. }
  136. // 用户数据存入Redis,key前缀:wxInfo:applets:
  137. redisService.setMap("wxInfo:applets:" + unionId, infoData);
  138. log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId);
  139. // 协销授权登录
  140. UserLoginVo seller = loginMapper.getServiceProviderUserByOpenId(openId);
  141. if (null != seller) {
  142. loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
  143. String token = JwtUtil.createToken(seller.getUserId());
  144. seller.setToken(token);
  145. seller.setUnionId(unionId);
  146. seller.setOpenId(openId);
  147. // 生成token给协销用户
  148. String sellerToken = JwtUtil.createToken(seller.getUserId());
  149. // 为了过期续签,将token存入redis,并设置超时时间
  150. redisService.set(sellerToken, sellerToken, JwtUtil.getExpireTime());
  151. seller.setToken(sellerToken);
  152. return ResponseJson.success(seller);
  153. }
  154. // 运营人员授权登录
  155. return operationAuthLogin(openId, unionId, "mini");
  156. }
  157. /**
  158. * 微信公众号授权链接(www)
  159. *
  160. * @param redirectUri 用于微信授权的中间页面
  161. * @param mode 授权方式:1静默授权,其他手动同意授权
  162. */
  163. @Override
  164. public ResponseJson<String> getAuthorizationLink(String redirectUri, Integer mode) {
  165. String link = weChatService.getAuthorizationLink(redirectUri, mode);
  166. String state = UUID.randomUUID().toString();
  167. redisService.set(state, state, 1800L);
  168. link = link.replace("STATE", state);
  169. return ResponseJson.success(link);
  170. }
  171. /**
  172. * 微信公众号授权登录(www)
  173. * <p>
  174. * spi旧接口:user/authorizationLogin
  175. *
  176. * @param code 微信code
  177. * @param state 安全认证
  178. * @param mode 1:静默授权,2:用户手动授权
  179. * @param headers HttpHeaders
  180. */
  181. @Override
  182. public ResponseJson<UserLoginVo> websiteAuthorization(String code, String state, Integer mode, HttpHeaders headers) {
  183. if (StringUtils.isBlank(code) || StringUtils.isBlank(state)) {
  184. return ResponseJson.error("参数异常:微信code和state不能为空!", null);
  185. }
  186. String wxState = (String) redisService.get(state);
  187. log.info("微信code>>>" + code + "state>>>" + wxState + "----" + state + "mode>>>" + mode);
  188. if (wxState.equals(state)) {
  189. try {
  190. // 通过code获取微信用户信息
  191. Map<String, Object> map = weChatService.getInfoMapByWeb(code, "crm");
  192. String openId = (String) map.get(WeChatService.Keys.OPEN_ID);
  193. if (mode == 1) {
  194. // 静默授权
  195. Integer userId = loginMapper.getUserIdByOpenId(openId, "www");
  196. if (null != userId && userId > 0) {
  197. UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
  198. // 返回登录用户
  199. return logonVerify(user);
  200. } else {
  201. return ResponseJson.error(-4, "您的微信尚未绑定任何机构", null);
  202. }
  203. } else {
  204. // 获取access_token
  205. String accessToken = weChatService.getAccessToken();
  206. // 获取微信用户信息
  207. Map<String, Object> infoData = weChatService.getUserInfo(accessToken, openId);
  208. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  209. // 用户数据存入Redis,key前缀:wxInfo:website:
  210. redisService.setMap("wxInfo:website:" + unionId, infoData);
  211. log.info("移动端授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:website:" + unionId);
  212. // 运营人员授权登录
  213. return operationAuthLogin(openId, unionId, "www");
  214. }
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. return ResponseJson.error("获取微信信息异常", null);
  218. }
  219. }
  220. return ResponseJson.error("请从正确的途径打开链接", null);
  221. }
  222. /**
  223. * 运营人员授权登录
  224. *
  225. * @param openId 微信openId
  226. * @param unionId 微信unionId
  227. * @param source 来源:www网站, mini小程序
  228. * @return BaseUser
  229. */
  230. private ResponseJson<UserLoginVo> operationAuthLogin(String openId, String unionId, String source) {
  231. UserLoginVo operation = loginMapper.getLoginUserByUnionId(unionId, source);
  232. if (null == operation) {
  233. operation = loginMapper.getLoginUserByOpenId(openId, source);
  234. if (null == operation) {
  235. operation = new UserLoginVo();
  236. operation.setOpenId(openId);
  237. operation.setUnionId(unionId);
  238. return ResponseJson.error(-4, "您的微信尚未绑定任何机构", operation);
  239. } else {
  240. // 表示 openId存在, unionId不存在
  241. operationMapper.updateOperationUnionId(operation.getUserId(), unionId);
  242. }
  243. }
  244. // 如果unionId存在, openId不存在
  245. if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
  246. operationMapper.updateOperationOpenId(operation.getUserId(), openId);
  247. }
  248. operation.setOpenId(openId);
  249. operation.setUnionId(unionId);
  250. // 返回登录校验结果
  251. return logonVerify(operation);
  252. }
  253. /**
  254. * 登录校验
  255. *
  256. * @param loginUser 用户信息
  257. * @return UserLoginVo
  258. */
  259. private ResponseJson<UserLoginVo> logonVerify(UserLoginVo loginUser) {
  260. // 生成token给用户
  261. String token = JwtUtil.createToken(loginUser.getUserId());
  262. // 为了过期续签,将token存入redis,并设置超时时间
  263. redisService.set(token, token, JwtUtil.getExpireTime());
  264. loginUser.setToken(token);
  265. if (null != loginUser.getClubStatus() && 91 == loginUser.getClubStatus()) {
  266. //机构
  267. return ResponseJson.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
  268. }
  269. // 供应商
  270. if (null != loginUser.getShopStatus() && null != loginUser.getUserIdentity() && 3 == loginUser.getUserIdentity()) {
  271. if (3 == loginUser.getShopStatus()) {
  272. return ResponseJson.error(-2, "您的企业账号正在加速审核中,审核通过后即可登录", null);
  273. }
  274. if (91 == loginUser.getShopStatus()) {
  275. return ResponseJson.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
  276. }
  277. if (92 == loginUser.getShopStatus()) {
  278. return ResponseJson.error(-3, "您的企业账号审核未通过", loginUser);
  279. }
  280. }
  281. if (null != loginUser.getUserIdentity() && (2 == loginUser.getUserIdentity() || 4 == loginUser.getUserIdentity())) {
  282. Integer id = clubMapper.findLoginBeans(loginUser.getUserId());
  283. if (id == null) {
  284. //登录赠送10采美豆
  285. UserVo user = baseMapper.getUserByUserId(loginUser.getUserId());
  286. UserBeansHistoryPo beansHistory = new UserBeansHistoryPo();
  287. beansHistory.setUserId(user.getUserId());
  288. beansHistory.setBeansType(12);
  289. beansHistory.setType(1);
  290. beansHistory.setNum(10);
  291. beansHistory.setPushStatus(0);
  292. beansHistory.setAddTime(new Date());
  293. registerMapper.insertBeansHistory(beansHistory);
  294. Integer userBeans = user.getUserBeans();
  295. userBeans = userBeans == null ? 10 : userBeans + 10;
  296. clubMapper.updateUserBeans(loginUser.getUserId(), userBeans);
  297. }
  298. // 会员机构类型:1医美,2生
  299. if (loginUser.getUserIdentity() == 2) {
  300. Integer clubType = loginMapper.getClubTypeById(loginUser.getUserId());
  301. loginUser.setFirstClubType(clubType);
  302. }
  303. // 机构超级会员
  304. SuperVipDto end = findEnd(loginUser.getUserId());
  305. loginUser.setVipFlag(end.getVipFlag());
  306. if (1 == loginUser.getVipFlag() && 4 == loginUser.getUserIdentity()) {
  307. // 个人超级会员权限置为会员机构
  308. loginUser.setUserIdentity(2);
  309. loginUser.setUserPermission(2);
  310. }
  311. }
  312. // 改user表登录时间
  313. try {
  314. log.info("登陆时间录入");
  315. loginMapper.updateLogin(loginUser.getUserId());
  316. } catch (Exception e) {
  317. log.error("登录时间记录异常" + e);
  318. }
  319. return ResponseJson.success("登录成功", loginUser);
  320. }
  321. /**
  322. * 获取生成微信二维码的参数(www)
  323. *
  324. * @return Map<String, Object>
  325. */
  326. @Override
  327. public ResponseJson<Map<String, String>> getAuthParameters() {
  328. UUID state = UUID.randomUUID();
  329. Map<String, String> dataMap = new HashMap<>(3);
  330. dataMap.put("appId", weChatService.getAppId());
  331. dataMap.put("redirectUri", weChatService.getRedirectUri());
  332. dataMap.put("state", String.valueOf(state));
  333. redisService.set("state:" + state, String.valueOf(state), 1800L);
  334. return ResponseJson.success(dataMap);
  335. }
  336. /**
  337. * 微信用户扫码,微信服务器回调
  338. *
  339. * @param code 微信code
  340. * @param state 安全认证key(上一步获取参数时自定义生成的uuid)
  341. */
  342. @Override
  343. public void qrCodeAuthScan(String code, String state) {
  344. String errorMsg = "";
  345. // 简单验证,防止csrf攻击(跨站请求伪造攻击)
  346. String stateCache = (String) redisService.get("state:" + state);
  347. if (StringUtils.isBlank(stateCache) || "null".equals(stateCache)) {
  348. errorMsg = "请从正确的途径打开链接";
  349. }
  350. if (StringUtils.isEmpty(code)) {
  351. errorMsg = "请重新进行授权登录";
  352. }
  353. try {
  354. // 用code换取access_token
  355. Map<String, Object> tokenMap = weChatService.getInfoMapByWeb(code, "pc");
  356. String accessToken = (String) tokenMap.get("access_token");
  357. String openId = (String) tokenMap.get(WeChatService.Keys.OPEN_ID);
  358. log.info(">>>>>(code换取access_token)wx回调openId:" + openId + " ,accessToken:" + accessToken);
  359. // 用access_token获取微信用户信息
  360. Map<String, Object> infoData = weChatService.getUserInfoByWeb(accessToken, openId);
  361. log.info(">>>>>(用access_token获取用户信息)wx回调openId:" + infoData.get(WeChatService.Keys.OPEN_ID) + " ,unionId:" + infoData.get(WeChatService.Keys.UNION_ID));
  362. // 微信用户信息存入redis
  363. redisService.setMap("scan:" + state, infoData);
  364. } catch (Exception e) {
  365. errorMsg = "获取微信用户信息失败";
  366. }
  367. // 错误信息存入Redis
  368. redisService.set("error:" + state, errorMsg, 1800L);
  369. }
  370. /**
  371. * 校验扫码结果
  372. *
  373. * @param state 安全认证key(第一步获取参数时自定义生成的uuid)
  374. * @return UserLoginVo
  375. */
  376. @Override
  377. public ResponseJson<UserLoginVo> qrCodeAuthScanResult(String state) {
  378. if (StringUtils.isBlank(state)) {
  379. return ResponseJson.error("参数异常:state不能为空!", null);
  380. }
  381. String errorMsg = (String) redisService.get("error:" + state);
  382. if (StringUtils.isNotEmpty(errorMsg) && !"null".equals(errorMsg)) {
  383. return ResponseJson.error(errorMsg, null);
  384. }
  385. Map<Object, Object> infoData = redisService.getEntries("scan:" + state);
  386. if (null == infoData || infoData.size() == 0) {
  387. return ResponseJson.error(-90, "redis缓存的扫码数据没有拿到", null);
  388. }
  389. // 清除redis的扫码数据
  390. redisService.remove("scan:" + state);
  391. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  392. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  393. log.info(">>>>>>pc商城unionId:" + unionId + " ,openId:" + openId);
  394. // 用户数据存入Redis,key前缀:wxInfo:website:
  395. String infoDataStr = JSON.toJSONString(infoData);
  396. Map<String, Object> infoDataMap = JSON.parseObject(infoDataStr);
  397. redisService.setMap("wxInfo:website:" + unionId, infoDataMap);
  398. log.info("微信扫码登录,用户数据存入Redis,key:wxInfo:website:" + unionId);
  399. // 运营人员授权登录
  400. return operationAuthLogin(openId, unionId, "www");
  401. }
  402. /**
  403. * 微信扫码后,绑定机构账号
  404. *
  405. * @param scanBindDto {
  406. * mobileOrEmail 手机号或邮箱
  407. * password 密码
  408. * mobile 手机号
  409. * smsCode 手机验证码
  410. * linkName 联系人
  411. * }
  412. */
  413. @Override
  414. public ResponseJson<UserLoginVo> qrCodeAuthScanBind(ScanBindDto scanBindDto) {
  415. String mobileOrEmail = scanBindDto.getMobileOrEmail();
  416. String password = scanBindDto.getPassword();
  417. String mobile = scanBindDto.getMobile();
  418. String smsCode = scanBindDto.getSmsCode();
  419. String linkName = scanBindDto.getLinkName();
  420. String unionId = scanBindDto.getUnionId();
  421. // 参数校验
  422. if (StringUtils.isBlank(mobileOrEmail)) {
  423. return ResponseJson.error("参数异常:手机号或邮箱不能为空!", null);
  424. }
  425. if (StringUtils.isBlank(password)) {
  426. return ResponseJson.error("参数异常:密码不能为空!", null);
  427. }
  428. if (StringUtils.isBlank(mobile)) {
  429. return ResponseJson.error("参数异常:手机号不能为空!", null);
  430. }
  431. if (StringUtils.isBlank(smsCode)) {
  432. return ResponseJson.error("参数异常:短信验证码不能为空!", null);
  433. }
  434. if (StringUtils.isBlank(unionId)) {
  435. return ResponseJson.error("参数异常:unionId不能为空!", null);
  436. }
  437. //处理比对密码
  438. UserLoginVo user = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
  439. String md5Password = Md5Util.md5(password);
  440. if (null != user && md5Password.equals(user.getPassword())) {
  441. // 查询使用该手机号的运营人员或用户
  442. String checkRust = commonService.operationBindCheck(mobile, smsCode);
  443. if (checkRust != null) {
  444. return ResponseJson.error(checkRust, null);
  445. }
  446. Map<Object, Object> infoData = redisService.getEntries("wxInfo:website:" + unionId);
  447. log.info("扫码绑定微信, 获取unionId>>>>>>" + unionId);
  448. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  449. String nickName = (String) infoData.get("nickname");
  450. String avatarUrl = (String) infoData.get("headimgurl");
  451. // 判断微信是否已经绑定
  452. UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "www");
  453. if (operationByUnionId != null) {
  454. return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
  455. }
  456. /*
  457. 组装运营人员数据 operation
  458. */
  459. OperationPo operation = new OperationPo();
  460. // 用户Id
  461. operation.setUserId(user.getUserId());
  462. // 手机号
  463. operation.setMobile(mobile);
  464. operation.setLinkName(linkName);
  465. // 微信昵称头像
  466. operation.setNickName(nickName);
  467. operation.setAvatarUrl(avatarUrl);
  468. // unionId,openId
  469. operation.setUnionId(unionId);
  470. operation.setPcOpenId(openId);
  471. // 组织机构0
  472. operation.setOrganizeId(0);
  473. // 绑定的机构/供应商Id,绑定的用户类型
  474. if (3 == user.getUserIdentity()) {
  475. operation.setShopId(user.getShopId());
  476. operation.setUserType(2);
  477. } else {
  478. operation.setClubId(user.getClubId());
  479. operation.setUserType(1);
  480. }
  481. // 绑定状态
  482. operation.setStatus(2);
  483. // 删除标识
  484. operation.setDelFlag(0);
  485. Date time = new Date();
  486. // 添加时间
  487. operation.setAddTime(time);
  488. // 绑定时间
  489. operation.setBindTime(time);
  490. // 更新时间
  491. operation.setUpdateTime(time);
  492. /*
  493. 保存数据库 operation
  494. */
  495. registerMapper.insertOperation(operation);
  496. return logonVerify(user);
  497. }
  498. return ResponseJson.error("输入的密码和账户名不匹配", null);
  499. }
  500. /**
  501. * 邀请码登录
  502. *
  503. * @param invitationCode 邀请码
  504. * @param nickName 微信昵称
  505. * @param avatarUrl 微信头像(headimgurl)
  506. * @param unionId 微信unionId
  507. * @return UserLoginVo
  508. */
  509. @Override
  510. public ResponseJson<UserLoginVo> invitationCodeLogin(String invitationCode, String nickName, String avatarUrl, String unionId) {
  511. // 参数校验
  512. if (StringUtils.isBlank(invitationCode)) {
  513. return ResponseJson.error("邀请码不能为空", null);
  514. }
  515. UserLoginVo operation = loginMapper.getOperationUserByInvitationCode(invitationCode);
  516. if (operation == null) {
  517. return ResponseJson.error("邀请码错误", null);
  518. }
  519. Date date = new Date();
  520. Calendar calendar = Calendar.getInstance();
  521. calendar.setTime(operation.getInvitationCodeTime());
  522. calendar.add(Calendar.DATE, validTime);
  523. if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
  524. return ResponseJson.error("邀请码已失效", null);
  525. }
  526. if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
  527. return ResponseJson.error("邀请码已被使用", null);
  528. }
  529. // 用户身份:1机构,2供应商
  530. int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
  531. if (1 == userIdentity && operation.getClubStatus() != null && 91 == operation.getClubStatus()) {
  532. return ResponseJson.error("您的机构已冻结", null);
  533. }
  534. if (2 == userIdentity && operation.getShopStatus() != null && 91 == operation.getShopStatus()) {
  535. return ResponseJson.error("您的企业账号已被冻结,请联系客服处理", null);
  536. }
  537. if (0 != operation.getDelFlag()) {
  538. return ResponseJson.error("您的邀请码已被删除,请重新添加运营人员", null);
  539. }
  540. OperationPo operationPo = new OperationPo();
  541. operationPo.setId(operation.getOperationId());
  542. // 微信unionId
  543. operationPo.setUnionId(unionId);
  544. Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
  545. // 微信openId
  546. operationPo.setOpenId((String) infoData.get(WeChatService.Keys.OPEN_ID));
  547. // 微信昵称
  548. operationPo.setNickName(nickName);
  549. // 微信头像
  550. operationPo.setAvatarUrl(avatarUrl);
  551. // 绑定状态,1未绑定,2已绑定
  552. operationPo.setStatus(2);
  553. if (1 == userIdentity) {
  554. // 机构Id
  555. operationPo.setClubId(operation.getClubId());
  556. // 用户类型
  557. operationPo.setUserType(1);
  558. } else {
  559. // 供应商Id
  560. operationPo.setShopId(operation.getShopId());
  561. // 用户类型
  562. operationPo.setUserType(2);
  563. }
  564. // 更新运营人员信息
  565. operationMapper.updateOperationByInvitation(operationPo);
  566. // 返回登录校验结果
  567. return logonVerify(operation);
  568. }
  569. /**
  570. * 运营人员绑定微信
  571. *
  572. * @param authBindDto {
  573. * userId 要绑定的用户Id(userID)
  574. * mobile 手机号
  575. * smsCode 手机验证码(verificationCode)
  576. * unionId 微信unionId
  577. * nickName 微信昵称
  578. * avatarUrl 微信头像(headimgurl)
  579. * }
  580. * @return OperationPo
  581. */
  582. @Override
  583. public ResponseJson<UserLoginVo> operationBindWeChat(AuthBindDto authBindDto) {
  584. Integer userId = authBindDto.getUserId();
  585. String mobile = authBindDto.getMobile();
  586. String smsCode = authBindDto.getSmsCode();
  587. String linkName = authBindDto.getLinkName();
  588. String unionId = authBindDto.getUnionId();
  589. String nickName = authBindDto.getNickName();
  590. String avatarUrl = authBindDto.getAvatarUrl();
  591. String isCheckSmsCode = authBindDto.getIsCheckSmsCode();
  592. // 参数校验
  593. if (null == userId) {
  594. return ResponseJson.error("参数异常:用户Id不能为空!", null);
  595. }
  596. if (StringUtils.isBlank(mobile)) {
  597. return ResponseJson.error("参数异常:手机号不能为空!", null);
  598. }
  599. boolean b = StringUtils.isBlank(isCheckSmsCode) || "0".equals(isCheckSmsCode);
  600. if (b && StringUtils.isBlank(smsCode)) {
  601. return ResponseJson.error("参数异常:短信验证码不能为空!", null);
  602. }
  603. if (StringUtils.isBlank(unionId)) {
  604. return ResponseJson.error("参数异常:unionId不能为空!", null);
  605. }
  606. // 查询使用该手机号的运营人员或用户
  607. String checkRust = commonService.operationBindCheck(mobile, smsCode);
  608. if (checkRust != null) {
  609. return ResponseJson.error(checkRust, null);
  610. }
  611. Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
  612. log.info("绑定微信bindingWx,获取unionId>>>>>>" + unionId);
  613. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  614. // 判断微信是否已经绑定
  615. UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "mini");
  616. if (operationByUnionId != null) {
  617. return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
  618. }
  619. // 要绑定的用户
  620. UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
  621. /*
  622. 组装运营人员数据 operation
  623. */
  624. OperationPo operation = new OperationPo();
  625. // 用户Id
  626. operation.setUserId(userId);
  627. // 手机号
  628. operation.setMobile(mobile);
  629. operation.setLinkName(linkName);
  630. // 微信昵称头像
  631. operation.setNickName(nickName);
  632. operation.setAvatarUrl(avatarUrl);
  633. // unionId,openId
  634. operation.setUnionId(unionId);
  635. operation.setOpenId(openId);
  636. // 组织机构0
  637. operation.setOrganizeId(0);
  638. // 绑定的机构/供应商Id,绑定的用户类型
  639. if (user != null && 3 == user.getUserIdentity()) {
  640. operation.setShopId(user.getShopId());
  641. operation.setUserType(2);
  642. } else if (user != null) {
  643. operation.setClubId(user.getClubId());
  644. operation.setUserType(1);
  645. }
  646. // 绑定状态
  647. operation.setStatus(2);
  648. // 删除标识
  649. operation.setDelFlag(0);
  650. Date time = new Date();
  651. // 添加时间
  652. operation.setAddTime(time);
  653. // 绑定时间
  654. operation.setBindTime(time);
  655. // 更新时间
  656. operation.setUpdateTime(time);
  657. /*
  658. 保存数据库 operation
  659. */
  660. registerMapper.insertOperation(operation);
  661. return ResponseJson.success("绑定微信成功", user);
  662. }
  663. /**
  664. * 根据userId查是否过期,返回dto对象,flag=0未买过,-1过期,1有效,endTime过期时间
  665. */
  666. private SuperVipDto findEnd(Integer userId) {
  667. SuperVipPo superVip = vipMapper.findSuperVip(userId);
  668. SuperVipDto superVipDto = new SuperVipDto();
  669. if (superVip == null) {
  670. superVipDto.setVipFlag(0);
  671. } else {
  672. SuperVipPo endTime = vipMapper.findEndTime(userId);
  673. if (endTime == null) {
  674. superVipDto.setVipFlag(-1);
  675. superVipDto.setEndTime(superVip.getEndTime());
  676. } else {
  677. superVipDto.setVipFlag(1);
  678. superVipDto.setEndTime(endTime.getEndTime());
  679. }
  680. }
  681. return superVipDto;
  682. }
  683. }