LoginServiceImpl.java 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  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.*;
  10. import com.caimei365.user.model.po.OperationPo;
  11. import com.caimei365.user.model.po.SuperVipPo;
  12. import com.caimei365.user.model.vo.MessageCenter;
  13. import com.caimei365.user.model.vo.OperationVo;
  14. import com.caimei365.user.model.vo.UserLoginVo;
  15. import com.caimei365.user.service.LoginService;
  16. import com.caimei365.user.service.RemoteCallService;
  17. import com.caimei365.user.utils.JwtUtil;
  18. import com.caimei365.user.utils.Md5Util;
  19. import com.caimei365.user.utils.ValidateUtil;
  20. import com.github.pagehelper.PageHelper;
  21. import com.github.pagehelper.PageInfo;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.http.HttpHeaders;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.util.ObjectUtils;
  28. import javax.annotation.Resource;
  29. import java.text.ParseException;
  30. import java.text.SimpleDateFormat;
  31. import java.util.*;
  32. /**
  33. * Description
  34. *
  35. * @author : Charles
  36. * @date : 2021/3/8
  37. */
  38. @Slf4j
  39. @Service
  40. public class LoginServiceImpl implements LoginService {
  41. @Resource
  42. private SuperVipMapper vipMapper;
  43. @Resource
  44. private RedisService redisService;
  45. @Resource
  46. private WeChatService weChatService;
  47. @Resource
  48. private CommonService commonService;
  49. @Resource
  50. private LoginMapper loginMapper;
  51. @Resource
  52. private RegisterMapper registerMapper;
  53. @Resource
  54. private OperationMapper operationMapper;
  55. @Resource
  56. private AsyncService asyncService;
  57. @Resource
  58. private SellerMapper sellerMapper;
  59. @Resource
  60. private MessageCenterMapper messageCenterMapper;
  61. @Resource
  62. private RemoteCallService remoteCallService;
  63. /**
  64. * 小程序邀请码过期天数
  65. */
  66. @Value("${caimei.validTime}")
  67. private Integer validTime;
  68. /**
  69. * 登录(用户名,密码)
  70. *
  71. * @param loginPasswordDto {
  72. * mobileOrEmail 手机号或邮箱
  73. * password 密码
  74. * unionId 微信unionId
  75. * }
  76. * @return BaseUser
  77. */
  78. @Override
  79. public ResponseJson<UserLoginVo> passwordLogin(LoginPasswordDto loginPasswordDto) throws ParseException {
  80. String mobileOrEmail = loginPasswordDto.getMobileOrEmail();
  81. String password = loginPasswordDto.getPassword();
  82. String unionId = loginPasswordDto.getUnionId();
  83. if (StringUtils.isBlank(mobileOrEmail)) {
  84. return ResponseJson.error("请填写账户名", null);
  85. }
  86. if (StringUtils.isBlank(password)) {
  87. return ResponseJson.error("请填写密码", null);
  88. }
  89. //处理比对密码
  90. UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
  91. if (null != baseUser) {
  92. String key = "login-" + baseUser.getUserId();
  93. boolean exists = redisService.exists(key);
  94. //如果30分钟内输入错误记录>=5,return该账号暂时被冻结,请(30-最前一次时间)分钟后重试或直接修改密码
  95. if (exists) {
  96. String val = (String) redisService.get(key);
  97. String[] split = val.split(",");
  98. int count = Integer.parseInt(split[0]);
  99. if (count >= 5) {
  100. long s = Long.parseLong(split[1]);
  101. int l = (int) Math.floor((System.currentTimeMillis() - s) / 1000 / 60);
  102. return ResponseJson.error("该账号暂时被冻结,请" + (30 - l) + "分钟后重试或直接修改密码", null);
  103. }
  104. }
  105. // 如果前端传入unionId,则存入返回前端
  106. baseUser.setUnionId(unionId);
  107. // 不是采美组织下
  108. if (0 != baseUser.getOrganizeId()) {
  109. if (1 == baseUser.getClubStatus()) {
  110. return ResponseJson.error(-1, "账号待审核,请耐心等待审核结果", null);
  111. }
  112. if (92 == baseUser.getClubStatus()) {
  113. return ResponseJson.error(-1, "账号审核未通过,请重新提交资料", null);
  114. }
  115. }
  116. // 比对密码
  117. String md5Password = Md5Util.md5(password);
  118. String dbPassword = baseUser.getPassword();
  119. if (md5Password.equals(dbPassword)) {
  120. // 游客id与用户关联
  121. if (StringUtils.isNotBlank(loginPasswordDto.getTouristId()) && StringUtils.isNotEmpty(loginPasswordDto.getTouristId())) {
  122. loginMapper.updateTourist(baseUser.getUserId(), loginPasswordDto.getTouristId());
  123. }
  124. if (baseUser.getUserIdentity() == 1) {
  125. // 协销登录
  126. return ResponseJson.success(baseUser);
  127. } else {
  128. // 返回登录校验结果
  129. return logonVerify(baseUser);
  130. }
  131. } else {
  132. // 增加一次错误输入密码记录,30分钟内连续五次冻结
  133. if (exists) {
  134. String val = (String) redisService.get(key);
  135. String[] split = val.split(",");
  136. int count = Integer.parseInt(split[0]);
  137. if (count < 5) {
  138. count++;
  139. String va = count + "," + System.currentTimeMillis();
  140. redisService.set(key, va);
  141. }
  142. if (count >= 5) {
  143. redisService.set(key, 5 + "," + System.currentTimeMillis(), 1800L);
  144. }
  145. } else {
  146. String val = 1 + "," + System.currentTimeMillis();
  147. redisService.set(key, val);
  148. }
  149. }
  150. }
  151. return ResponseJson.error("账户名与密码不匹配,请重新输入", null);
  152. }
  153. /**
  154. * 登录(用户名,密码)
  155. *
  156. * @param loginPasswordDto {
  157. * mobileOrEmail 手机号或邮箱
  158. * password 密码
  159. * unionId 微信unionId
  160. * }
  161. * @return BaseUser
  162. */
  163. @Override
  164. public ResponseJson<UserLoginVo> passwordOrganizeLogin(LoginPasswordDto loginPasswordDto) throws ParseException {
  165. String mobileOrEmail = loginPasswordDto.getMobileOrEmail();
  166. String password = loginPasswordDto.getPassword();
  167. String unionId = loginPasswordDto.getUnionId();
  168. if (StringUtils.isBlank(mobileOrEmail)) {
  169. return ResponseJson.error("请填写账户名", null);
  170. }
  171. if (StringUtils.isBlank(password)) {
  172. return ResponseJson.error("请填写密码", null);
  173. }
  174. //处理比对密码
  175. UserLoginVo baseUser = loginMapper.getLoginOrganizeUserByMobileOrEmail(mobileOrEmail);
  176. // 机构下线,登录失败
  177. if (91 == baseUser.getClubStatus()) {
  178. return ResponseJson.error(-1, "您的企业账号已被冻结,请联系客服处理", null);
  179. }
  180. if (null != baseUser) {
  181. // 绑定微信信息
  182. Integer operationUser = loginMapper.getOperationUser(mobileOrEmail);
  183. log.info("operationUser===" + operationUser);
  184. if (null == operationUser) {
  185. // 绑定微信
  186. OperationVo operationVo = new OperationVo();
  187. operationVo.setOrganizeId(baseUser.getOrganizeId());
  188. operationVo.setUserType(1);
  189. operationVo.setUserId(baseUser.getUserId());
  190. operationVo.setClubId(baseUser.getClubId());
  191. operationVo.setMobile(baseUser.getBindMobile());
  192. operationVo.setLinkName(baseUser.getClubLinkMan());
  193. operationVo.setStatus(2);
  194. operationVo.setUnionId(unionId);
  195. operationVo.setBindTime(new Date());
  196. operationVo.setAddTime(new Date());
  197. operationVo.setDelFlag(0);
  198. loginMapper.insertOperation(operationVo);
  199. log.info("operationVo***" + operationVo);
  200. }
  201. String key = "login-" + baseUser.getUserId();
  202. boolean exists = redisService.exists(key);
  203. //如果30分钟内输入错误记录>=5,return该账号暂时被冻结,请(30-最前一次时间)分钟后重试或直接修改密码
  204. if (exists) {
  205. String val = (String) redisService.get(key);
  206. String[] split = val.split(",");
  207. int count = Integer.parseInt(split[0]);
  208. if (count >= 5) {
  209. long s = Long.parseLong(split[1]);
  210. int l = (int) Math.floor((System.currentTimeMillis() - s) / 1000 / 60);
  211. return ResponseJson.error("该账号暂时被冻结,请" + (30 - l) + "分钟后重试或直接修改密码", null);
  212. }
  213. }
  214. // 如果前端传入unionId,则存入返回前端
  215. baseUser.setUnionId(unionId);
  216. // 比对密码
  217. String md5Password = Md5Util.md5(password);
  218. String dbPassword = baseUser.getPassword();
  219. if (md5Password.equals(dbPassword)) {
  220. if (baseUser.getUserIdentity() == 1) {
  221. // 协销登录
  222. return ResponseJson.success(baseUser);
  223. } else {
  224. // 返回登录校验结果
  225. return logonVerify(baseUser);
  226. }
  227. } else {
  228. // 增加一次错误输入密码记录,30分钟内连续五次冻结
  229. if (exists) {
  230. String val = (String) redisService.get(key);
  231. String[] split = val.split(",");
  232. int count = Integer.parseInt(split[0]);
  233. if (count < 5) {
  234. count++;
  235. String va = count + "," + System.currentTimeMillis();
  236. redisService.set(key, va);
  237. }
  238. if (count >= 5) {
  239. redisService.set(key, 5 + "," + System.currentTimeMillis(), 1800L);
  240. }
  241. } else {
  242. String val = 1 + "," + System.currentTimeMillis();
  243. redisService.set(key, val);
  244. }
  245. }
  246. }
  247. return ResponseJson.error(-1, "账户名与密码不匹配,请重新输入", null);
  248. }
  249. /**
  250. * @param loginCodeDto {
  251. * mobile 手机号
  252. * code 短信验证码
  253. * }
  254. * @return
  255. * @throws ParseException
  256. */
  257. @Override
  258. public ResponseJson<UserLoginVo> codeLogin(LoginCodeDto loginCodeDto) throws ParseException {
  259. if (ObjectUtils.isEmpty(loginCodeDto.getMobile())) {
  260. return ResponseJson.error("请填写手机号", null);
  261. }
  262. if (ObjectUtils.isEmpty(loginCodeDto.getCode())) {
  263. return ResponseJson.error("请输入短信验证码", null);
  264. }
  265. String mobile = loginCodeDto.getMobile();
  266. String code = loginCodeDto.getCode();
  267. String unionId = loginCodeDto.getUnionId();
  268. String result = ValidateUtil.validateMobile(mobile);
  269. if (result != null) {
  270. return ResponseJson.error(result);
  271. }
  272. // 判断redis中是否存在
  273. boolean exists = redisService.exists("code:" + mobile);
  274. if (exists) {
  275. // 查看验证码是否过期
  276. long expireTime = redisService.getExpireTime("code:" + mobile);
  277. if (expireTime < 0) {
  278. return ResponseJson.error("验证码已失效,请重新获取");
  279. }
  280. // 获取redis手机短信验证码
  281. Object randomCode = redisService.get("code:" + mobile);
  282. if (!ObjectUtils.isEmpty(randomCode)) {
  283. if (code.equals(randomCode.toString())) {
  284. redisService.remove("code:" + mobile);
  285. // 根据手机号获取用户信息
  286. UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobile);
  287. // 游客id与用户关联
  288. if (StringUtils.isNotBlank(loginCodeDto.getTouristId()) && StringUtils.isNotEmpty(loginCodeDto.getTouristId())) {
  289. loginMapper.updateTourist(baseUser.getUserId(), loginCodeDto.getTouristId());
  290. }
  291. // 如果前端传入unionId,则存入返回前端
  292. baseUser.setUnionId(unionId);
  293. if (baseUser.getUserIdentity() == 1) {
  294. // 协销登录
  295. return ResponseJson.success(baseUser);
  296. } else {
  297. // 返回登录校验结果
  298. return logonVerify(baseUser);
  299. }
  300. } else {
  301. return ResponseJson.error("验证码不匹配,请重新输入");
  302. }
  303. } else {
  304. return ResponseJson.error("验证码错误,请重新获取");
  305. }
  306. } else {
  307. return ResponseJson.error("验证码错误,请重新获取");
  308. }
  309. }
  310. /**
  311. * @param loginCodeDto {
  312. * mobile 手机号
  313. * code 短信验证码
  314. * }
  315. * @return
  316. * @throws ParseException
  317. */
  318. @Override
  319. public ResponseJson<UserLoginVo> codeOrganizeLogin(LoginCodeDto loginCodeDto) throws ParseException {
  320. if (ObjectUtils.isEmpty(loginCodeDto.getMobile())) {
  321. return ResponseJson.error("请填写手机号", null);
  322. }
  323. if (ObjectUtils.isEmpty(loginCodeDto.getCode())) {
  324. return ResponseJson.error("请输入短信验证码", null);
  325. }
  326. String mobile = loginCodeDto.getMobile();
  327. String code = loginCodeDto.getCode();
  328. String unionId = loginCodeDto.getUnionId();
  329. String result = ValidateUtil.validateMobile(mobile);
  330. if (result != null) {
  331. return ResponseJson.error(result);
  332. }
  333. // 判断redis中是否存在
  334. boolean exists = redisService.exists("code:" + mobile);
  335. if (exists) {
  336. // 查看验证码是否过期
  337. long expireTime = redisService.getExpireTime("code:" + mobile);
  338. if (expireTime < 0) {
  339. return ResponseJson.error(-1, "验证码已失效,请重新获取", null);
  340. }
  341. // 获取redis手机短信验证码
  342. Object randomCode = redisService.get("code:" + mobile);
  343. if (!ObjectUtils.isEmpty(randomCode)) {
  344. if (code.equals(randomCode.toString())) {
  345. redisService.remove("code:" + mobile);
  346. // 根据手机号获取用户信息
  347. UserLoginVo baseUser = loginMapper.getLoginOrganizeUserByMobileOrEmail(mobile);
  348. // 绑定微信信息
  349. Integer operationUser = loginMapper.getOperationUser(mobile);
  350. log.info("operationUser===" + operationUser);
  351. if (null == operationUser) {
  352. // 绑定微信
  353. OperationVo operationVo = new OperationVo();
  354. operationVo.setOrganizeId(baseUser.getOrganizeId());
  355. operationVo.setUserType(1);
  356. operationVo.setUserId(baseUser.getUserId());
  357. operationVo.setClubId(baseUser.getClubId());
  358. operationVo.setMobile(baseUser.getBindMobile());
  359. operationVo.setLinkName(baseUser.getUserName());
  360. operationVo.setStatus(2);
  361. operationVo.setUnionId(unionId);
  362. operationVo.setBindTime(new Date());
  363. operationVo.setAddTime(new Date());
  364. operationVo.setDelFlag(0);
  365. loginMapper.insertOperation(operationVo);
  366. }
  367. // 如果前端传入unionId,则存入返回前端
  368. baseUser.setUnionId(unionId);
  369. // 不是采美组织下
  370. if (0 != baseUser.getOrganizeId()) {
  371. if (1 == baseUser.getClubStatus()) {
  372. return ResponseJson.error(-1, "账号待审核,请耐心等待审核结果", null);
  373. }
  374. if (92 == baseUser.getClubStatus()) {
  375. return ResponseJson.error(-3, "账号审核未通过,请重新提交资料", null);
  376. }
  377. }
  378. if (baseUser.getUserIdentity() == 1) {
  379. // 协销登录
  380. return ResponseJson.success(baseUser);
  381. } else {
  382. // 返回登录校验结果
  383. return logonVerify(baseUser);
  384. }
  385. } else {
  386. return ResponseJson.error(-1, "验证码不匹配,请重新输入", null);
  387. }
  388. } else {
  389. return ResponseJson.error(-1, "验证码错误,请重新获取", null);
  390. }
  391. } else {
  392. return ResponseJson.error(-1, "验证码错误,请重新获取", null);
  393. }
  394. }
  395. /**
  396. * 微信授权登录(小程序)
  397. *
  398. * @param code 微信授权code
  399. * @param encryptedData 微信加密数据
  400. * @param iv 加密算法的初始向量
  401. * @param headers HttpHeaders
  402. */
  403. @Override
  404. public ResponseJson<UserLoginVo> appletsAuthorization(String code, String encryptedData, String iv, HttpHeaders headers) {
  405. if (StringUtils.isBlank(code)) {
  406. return ResponseJson.error("没有获取到微信授权code", null);
  407. }
  408. // 小程序微信授权获取登录信息
  409. ResponseJson<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, headers, 1);
  410. if (appletsInfo.getCode() == -1) {
  411. return ResponseJson.error(appletsInfo.getMsg(), null);
  412. }
  413. Map<String, Object> infoData = appletsInfo.getData();
  414. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  415. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  416. String sessionKey = (String) infoData.get(WeChatService.Keys.SESSION_KEY);
  417. try {
  418. if (StringUtils.isEmpty(unionId) || StringUtils.isBlank(unionId)) {
  419. String result = WeChatService.decrypt(encryptedData, sessionKey, iv, "UTF-8");
  420. log.info("解密数据>>>>>>" + result);
  421. Map parseMap = JSONObject.parseObject(result, Map.class);
  422. assert parseMap != null;
  423. unionId = parseMap.get(WeChatService.Keys.UNION_ID).toString();
  424. infoData.put(WeChatService.Keys.UNION_ID, unionId);
  425. }
  426. } catch (Exception e) {
  427. e.printStackTrace();
  428. return ResponseJson.error("微信解密失败", null);
  429. }
  430. // 用户数据存入Redis,key前缀:wxInfo:applets:
  431. redisService.setMap("wxInfo:applets:" + unionId, infoData);
  432. log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId);
  433. // 协销授权登录
  434. UserLoginVo seller = loginMapper.getServiceProviderUserByOpenId(openId, 0);
  435. if (null != seller) {
  436. loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
  437. String token = JwtUtil.createToken(seller.getUserId());
  438. seller.setToken(token);
  439. seller.setUnionId(unionId);
  440. seller.setOpenId(openId);
  441. // 生成token给协销用户
  442. String sellerToken = JwtUtil.createToken(seller.getUserId());
  443. // 为了过期续签,将token存入redis,并设置超时时间
  444. redisService.set(sellerToken, sellerToken, JwtUtil.getExpireTime());
  445. seller.setToken(sellerToken);
  446. seller.setManager(sellerMapper.findManagerByUser(seller.getUserId()));
  447. seller.setLeaderId(sellerMapper.findLeaderId(seller.getUserId()));
  448. // 更新用户openId、unionId 用于后续追踪
  449. try {
  450. loginMapper.updateUserInfo(openId, unionId, seller.getUserId());
  451. } catch (Exception e) {
  452. e.printStackTrace();
  453. }
  454. return ResponseJson.success(seller);
  455. }
  456. // 运营人员授权登录
  457. return operationAuthLogin(openId, unionId, "mini");
  458. }
  459. /**
  460. * 微信授权登录(小程序) 组织
  461. *
  462. * @param code 微信授权code
  463. * @param encryptedData 微信加密数据
  464. * @param iv 加密算法的初始向量
  465. * @param headers
  466. * @return BaseUser
  467. */
  468. @Override
  469. public ResponseJson<UserLoginVo> appletsOrganizeAuthorization(String code, String encryptedData, String iv, HttpHeaders headers) throws ParseException {
  470. if (StringUtils.isBlank(code)) {
  471. return ResponseJson.error("没有获取到微信授权code", null);
  472. }
  473. // 小程序微信授权获取登录信息
  474. ResponseJson<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, headers, 3);
  475. log.info("appletsInfo***" + appletsInfo);
  476. if (appletsInfo.getCode() == -1) {
  477. return ResponseJson.error(appletsInfo.getMsg(), null);
  478. }
  479. Map<String, Object> infoData = appletsInfo.getData();
  480. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  481. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  482. String sessionKey = (String) infoData.get(WeChatService.Keys.SESSION_KEY);
  483. try {
  484. if (StringUtils.isEmpty(unionId) || StringUtils.isBlank(unionId)) {
  485. String result = WeChatService.decrypt(encryptedData, sessionKey, iv, "UTF-8");
  486. log.info("解密数据>>>>>>" + result);
  487. Map parseMap = JSONObject.parseObject(result, Map.class);
  488. assert parseMap != null;
  489. unionId = parseMap.get(WeChatService.Keys.UNION_ID).toString();
  490. infoData.put(WeChatService.Keys.UNION_ID, unionId);
  491. }
  492. } catch (Exception e) {
  493. e.printStackTrace();
  494. return ResponseJson.error("微信解密失败", null);
  495. }
  496. // 用户数据存入Redis,key前缀:wxInfo:applets:
  497. redisService.setMap("wxInfo:applets:" + unionId, infoData);
  498. log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId + "operId" + openId);
  499. // 采购员授权登录
  500. UserLoginVo seller = loginMapper.getServiceProviderUserByOpenId(openId, 4);
  501. if (null != seller) {
  502. loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
  503. String token = JwtUtil.createToken(seller.getUserId());
  504. seller.setToken(token);
  505. seller.setUnionId(unionId);
  506. seller.setOpenId(openId);
  507. // 生成token给协销用户
  508. String sellerToken = JwtUtil.createToken(seller.getUserId());
  509. // 为了过期续签,将token存入redis,并设置超时时间
  510. redisService.set(sellerToken, sellerToken, JwtUtil.getExpireTime());
  511. seller.setToken(sellerToken);
  512. seller.setManager(sellerMapper.findManagerByUser(seller.getUserId()));
  513. seller.setLeaderId(sellerMapper.findLeaderId(seller.getUserId()));
  514. return ResponseJson.success(seller);
  515. }
  516. return organizeOperationAuthLogin(openId, unionId, "mini");
  517. }
  518. @Override
  519. public ResponseJson<Map<String, Object>> ClubMessageCount(Integer messageType, Integer commonId) {
  520. Integer count = messageCenterMapper.Count(1, commonId);
  521. Integer tradeCount = messageCenterMapper.MessageCount(1, 1, commonId);
  522. Integer accountCount = messageCenterMapper.MessageCount(1, 2, commonId);
  523. Integer notificationCount = messageCenterMapper.MessageCount(1, 3, commonId);
  524. Integer promotionCount = messageCenterMapper.MessageCount(1, 4, commonId);
  525. Integer infoCount = messageCenterMapper.MessageCount(1, 5, commonId);
  526. Integer activityCount = messageCenterMapper.MessageCount(1, 6, commonId);
  527. Map<String, Object> map = new HashMap(10);
  528. map.put("count", count);
  529. map.put("tradeCount", tradeCount);
  530. map.put("account", accountCount);
  531. map.put("notificationCount", notificationCount);
  532. map.put("promotionCount", promotionCount);
  533. map.put("infoCount", infoCount);
  534. map.put("activityCount", activityCount);
  535. return ResponseJson.success(map);
  536. }
  537. @Override
  538. public ResponseJson<Map<String, Object>> shopMessageCount(Integer messageType, Integer commonId) {
  539. Integer count = messageCenterMapper.Count(2, commonId);
  540. Integer tradeCount = messageCenterMapper.MessageCount(2, 1, commonId);
  541. Integer accountCount = messageCenterMapper.MessageCount(2, 2, commonId);
  542. Integer notificationCount = messageCenterMapper.MessageCount(2, 3, commonId);
  543. Integer promotionCount = messageCenterMapper.MessageCount(2, 4, commonId);
  544. Map<String, Object> map = new HashMap(10);
  545. map.put("count", count);
  546. map.put("tradeCount", tradeCount);
  547. map.put("accountCount", accountCount);
  548. map.put("notificationCount", notificationCount);
  549. map.put("promotionCount", promotionCount);
  550. return ResponseJson.success(map);
  551. }
  552. @Override
  553. public ResponseJson<Map<String, Object>> SpCount(Integer messageType, Integer commonId) {
  554. Integer count = messageCenterMapper.Count(3, commonId);
  555. Integer tradeCount = messageCenterMapper.MessageCount(3, 1, commonId);
  556. Integer accountCount = messageCenterMapper.MessageCount(3, 2, commonId);
  557. Integer notificationCount = messageCenterMapper.MessageCount(3, 3, commonId);
  558. Integer promotionCount = messageCenterMapper.MessageCount(3, 4, commonId);
  559. Map<String, Object> map = new HashMap(10);
  560. map.put("count", count);
  561. map.put("tradeCount", tradeCount);
  562. map.put("accountCount", accountCount);
  563. map.put("notificationCount", notificationCount);
  564. map.put("promotionCount", promotionCount);
  565. return ResponseJson.success(map);
  566. }
  567. @Override
  568. public ResponseJson<Map<String, Object>> ShopCount(Integer messageType, Integer commonId) {
  569. Integer count = messageCenterMapper.Count(2, commonId);
  570. Map<String, Object> map = new HashMap(10);
  571. map.put("count", count);
  572. return ResponseJson.success(map);
  573. }
  574. @Override
  575. public ResponseJson<Map<String, Object>> ClubCount(Integer messageType, Integer commonId) {
  576. Integer count = messageCenterMapper.Count(1, commonId);
  577. Map<String, Object> map = new HashMap(10);
  578. map.put("count", count);
  579. return ResponseJson.success(map);
  580. }
  581. @Override
  582. public Integer deleteMessage(String id) {
  583. Integer num = 0;
  584. String[] IdArr = id.split(",");
  585. for (int i = 0; i < IdArr.length; i++) {
  586. num = messageCenterMapper.deleteMessage(IdArr[i]);
  587. }
  588. return num;
  589. }
  590. @Override
  591. public Integer updateRead(Integer userType, String Id) {
  592. Integer num = 0;
  593. String[] IdArr = Id.split(",");
  594. for (int i = 0; i < IdArr.length; i++) {
  595. num = messageCenterMapper.updateRead(userType, IdArr[i]);
  596. }
  597. return num;
  598. }
  599. @Override
  600. public ResponseJson<PageInfo<MessageCenter>> messageList(Integer commonId, Integer messageType, Integer userType, int pageNum, int pageSize) {
  601. PageHelper.startPage(pageNum, pageSize);
  602. List<MessageCenter> list = setMessageList(userType, messageType, commonId);
  603. PageInfo<MessageCenter> pageData = new PageInfo<>(list);
  604. return ResponseJson.success(pageData);
  605. }
  606. @Override
  607. public ResponseJson<Map<String, Object>> messageCount(Integer userType , Integer commonId) {
  608. return ResponseJson.success(setMessageCount(userType,commonId));
  609. }
  610. @Override
  611. public ResponseJson<Map<String, Object>> count(Integer userType,Integer messageType, Integer commonId) {
  612. Integer count = messageCenterMapper.Count(userType, commonId);
  613. Map<String, Object> map = new HashMap(1);
  614. map.put("count", count);
  615. return ResponseJson.success(map);
  616. }
  617. @Override
  618. public Integer updateMessageAsRead(Integer userType,Integer messageType, Integer commonId) {
  619. return messageCenterMapper.updateMessageAsRead(userType, messageType, commonId);
  620. }
  621. @Override
  622. public ResponseJson<PageInfo<MessageCenter>> SpMessageList(Integer commonId, Integer messageType, Integer source, int pageNum, int pageSize) {
  623. PageHelper.startPage(pageNum, pageSize);
  624. List<MessageCenter> list = messageCenterMapper.MessageList(3, messageType, commonId);
  625. list.forEach(mess -> {
  626. if (null != mess.getClubId()) {
  627. MessageCenter messageCenter = messageCenterMapper.MainImage(3, commonId, mess.getOrderId());
  628. Integer productCount = messageCenterMapper.productCount(mess.getOrderId());
  629. if (null != messageCenter) {
  630. mess.setMainImage(messageCenter.getMainImage());
  631. mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
  632. if (messageCenter.getProductName().length() > 10) {
  633. mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
  634. } else {
  635. mess.setProductName(messageCenter.getProductName());
  636. }
  637. mess.setProductCount(productCount);
  638. mess.setRefundType(messageCenter.getRefundType());
  639. mess.setStatus(messageCenter.getStatus());
  640. }
  641. }
  642. });
  643. if (source == 2) {
  644. messageCenterMapper.updateMessageAsRead(3, messageType, commonId);
  645. }
  646. PageInfo<MessageCenter> pageData = new PageInfo<>(list);
  647. return ResponseJson.success(pageData);
  648. }
  649. @Override
  650. public ResponseJson<PageInfo<MessageCenter>> ClubMessageList(Integer commonId, Integer messageType, Integer source, int pageNum, int pageSize) {
  651. PageHelper.startPage(pageNum, pageSize);
  652. List<MessageCenter> list = messageCenterMapper.MessageList(1, messageType, commonId);
  653. list.forEach(mess -> {
  654. if (null != mess.getClubId()) {
  655. MessageCenter messageCenter = messageCenterMapper.MainImage(1, commonId, mess.getOrderId());
  656. Integer productCount = messageCenterMapper.productCount(mess.getOrderId());
  657. if (null != messageCenter) {
  658. mess.setMainImage(messageCenter.getMainImage());
  659. mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
  660. if (messageCenter.getProductName().length() > 10) {
  661. mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
  662. } else {
  663. mess.setProductName(messageCenter.getProductName());
  664. }
  665. mess.setProductCount(productCount);
  666. mess.setRefundType(messageCenter.getRefundType());
  667. mess.setStatus(messageCenter.getStatus());
  668. }
  669. if (5 == mess.getMessageType() || 6 == mess.getMessageType()) {
  670. Map<String, String> typeValue = messageCenterMapper.getMessageTypeValues(mess.getMessageType(), mess.getThisId());
  671. mess.setMainImage(typeValue.get("image"));
  672. mess.setPcLink(typeValue.get("pcLink"));
  673. mess.setAppLink(typeValue.get("appLink"));
  674. }
  675. }
  676. });
  677. if (source == 2) {
  678. messageCenterMapper.updateMessageAsRead(1, messageType, commonId);
  679. }
  680. PageInfo<MessageCenter> pageData = new PageInfo<>(list);
  681. return ResponseJson.success(pageData);
  682. }
  683. @Override
  684. public ResponseJson<PageInfo<MessageCenter>> shopMessageList(Integer commonId, Integer messageType, Integer source, int pageNum, int pageSize) {
  685. PageHelper.startPage(pageNum, pageSize);
  686. List<MessageCenter> list = messageCenterMapper.MessageList(2, messageType, commonId);
  687. list.forEach(mess -> {
  688. String shopName = messageCenterMapper.shopName(commonId);
  689. MessageCenter messageCenter = messageCenterMapper.productImage(mess.getProductId(), commonId);
  690. if (messageCenter != null) {
  691. mess.setMainImage(messageCenter.getMainImage());
  692. mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
  693. if (messageCenter.getProductName().length() > 10) {
  694. mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
  695. } else {
  696. mess.setProductName(messageCenter.getProductName());
  697. }
  698. mess.setProductCount(messageCenter.getProductCount());
  699. mess.setRefundType(messageCenter.getRefundType());
  700. }
  701. mess.setShopName(shopName);
  702. });
  703. if (source == 2) {
  704. messageCenterMapper.updateMessageAsRead(2, messageType, commonId);
  705. }
  706. PageInfo<MessageCenter> pageData = new PageInfo<>(list);
  707. return ResponseJson.success(pageData);
  708. }
  709. /**
  710. * 微信公众号授权链接(www)
  711. *
  712. * @param redirectUri 用于微信授权的中间页面
  713. * @param mode 授权方式:1静默授权,其他手动同意授权
  714. */
  715. @Override
  716. public ResponseJson<String> getAuthorizationLink(String redirectUri, Integer mode) {
  717. String link = weChatService.getAuthorizationLink(redirectUri, mode);
  718. String state = UUID.randomUUID().toString();
  719. redisService.set(state, state, 1800L);
  720. link = link.replace("STATE", state);
  721. return ResponseJson.success(link);
  722. }
  723. /**
  724. * 微信公众号授权登录(www)
  725. * <p>
  726. * spi旧接口:user/authorizationLogin
  727. *
  728. * @param code 微信code
  729. * @param state 安全认证
  730. * @param mode 1:静默授权,2:用户手动授权
  731. * @param headers HttpHeaders
  732. */
  733. @Override
  734. public ResponseJson<UserLoginVo> websiteAuthorization(String code, String state, Integer mode, HttpHeaders headers) {
  735. if (StringUtils.isBlank(code) || StringUtils.isBlank(state)) {
  736. return ResponseJson.error("参数异常:微信code和state不能为空!", null);
  737. }
  738. String wxState = (String) redisService.get(state);
  739. log.info("微信code>>>" + code + "state>>>" + wxState + "----" + state + "mode>>>" + mode);
  740. if (wxState.equals(state)) {
  741. try {
  742. // 通过code获取微信用户信息
  743. Map<String, Object> map = weChatService.getInfoMapByWeb(code, "crm");
  744. String openId = (String) map.get(WeChatService.Keys.OPEN_ID);
  745. if (mode == 1) {
  746. // 静默授权
  747. Integer userId = loginMapper.getUserIdByOpenId(openId, "www");
  748. if (null != userId && userId > 0) {
  749. UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
  750. // 返回登录用户
  751. return logonVerify(user);
  752. } else {
  753. return ResponseJson.error(-4, "您的微信尚未绑定任何机构", null);
  754. }
  755. } else {
  756. // 获取access_token
  757. String accessToken = weChatService.getAccessToken();
  758. // 获取微信用户信息
  759. Map<String, Object> infoData = weChatService.getUserInfo(accessToken, openId);
  760. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  761. // 用户数据存入Redis,key前缀:wxInfo:website:
  762. redisService.setMap("wxInfo:website:" + unionId, infoData);
  763. log.info("移动端授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:website:" + unionId);
  764. // 运营人员授权登录
  765. return operationAuthLogin(openId, unionId, "www");
  766. }
  767. } catch (Exception e) {
  768. e.printStackTrace();
  769. return ResponseJson.error("获取微信信息异常", null);
  770. }
  771. }
  772. return ResponseJson.error("请从正确的途径打开链接", null);
  773. }
  774. /**
  775. * 运营人员授权登录
  776. *
  777. * @param openId 微信openId
  778. * @param unionId 微信unionId
  779. * @param source 来源:www网站, mini小程序
  780. * @return BaseUser
  781. */
  782. private ResponseJson<UserLoginVo> operationAuthLogin(String openId, String unionId, String source) {
  783. UserLoginVo operation = loginMapper.getLoginUserByUnionId(unionId, source);
  784. if (null == operation) {
  785. operation = loginMapper.getLoginUserByOpenId(openId, source);
  786. if (null == operation) {
  787. operation = new UserLoginVo();
  788. operation.setOpenId(openId);
  789. operation.setUnionId(unionId);
  790. return ResponseJson.error(-4, "您的微信尚未绑定任何机构", operation);
  791. } else {
  792. // 表示 openId存在, unionId不存在
  793. operationMapper.updateOperationUnionId(operation.getUserId(), unionId);
  794. }
  795. }
  796. // 如果unionId存在, openId不存在
  797. if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
  798. operationMapper.updateOperationOpenId(operation.getUserId(), openId);
  799. }
  800. operation.setOpenId(openId);
  801. operation.setUnionId(unionId);
  802. // 返回登录校验结果
  803. return logonVerify(operation);
  804. }
  805. /**
  806. * 运营人员授权登录 -- 组织
  807. *
  808. * @param openId 微信openId
  809. * @param unionId 微信unionId
  810. * @param source 来源:www网站, mini小程序
  811. * @return BaseUser
  812. */
  813. private ResponseJson<UserLoginVo> organizeOperationAuthLogin(String openId, String unionId, String source) {
  814. UserLoginVo operation = loginMapper.getOrganizeLoginUserByUnionId(unionId, source);
  815. if (null == operation) {
  816. operation = loginMapper.getOrganizeLoginUserByOpenId(openId, source);
  817. if (null == operation) {
  818. operation = new UserLoginVo();
  819. operation.setOpenId(openId);
  820. operation.setUnionId(unionId);
  821. return ResponseJson.error(-4, "您的微信尚未绑定任何机构", operation);
  822. } else {
  823. // 表示 openId存在, unionId不存在
  824. operationMapper.updateOperationUnionId(operation.getUserId(), unionId);
  825. }
  826. }
  827. // 如果unionId存在, openId不存在
  828. if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
  829. operationMapper.updateOperationOpenId(operation.getUserId(), openId);
  830. }
  831. operation.setOpenId(openId);
  832. operation.setUnionId(unionId);
  833. // 审核未通过 待审核状态
  834. if (1 == operation.getClubStatus() || 92 == operation.getClubStatus()) {
  835. return ResponseJson.error(-1, "登陆失败", operation);
  836. }
  837. // 返回登录校验结果
  838. return logonVerify(operation);
  839. }
  840. /**
  841. * 登录校验
  842. *
  843. * @param loginUser 用户信息
  844. * @return UserLoginVo
  845. */
  846. private ResponseJson<UserLoginVo> logonVerify(UserLoginVo loginUser) {
  847. // 生成token给用户
  848. String token = JwtUtil.createToken(loginUser.getUserId());
  849. // 为了过期续签,将token存入redis,并设置超时时间
  850. redisService.set(token, token, JwtUtil.getExpireTime());
  851. loginUser.setToken(token);
  852. if (null != loginUser.getClubStatus() && 91 == loginUser.getClubStatus()) {
  853. //机构
  854. return ResponseJson.error(-1, "您的企业账号已被冻结,请联系客服处理", loginUser);
  855. }
  856. // 供应商
  857. if (null != loginUser.getShopStatus() && null != loginUser.getUserIdentity() && 3 == loginUser.getUserIdentity()) {
  858. if (3 == loginUser.getShopStatus()) {
  859. return ResponseJson.error(-1, "您的企业账号正在加速审核中,审核通过后即可登录", loginUser);
  860. }
  861. if (91 == loginUser.getShopStatus()) {
  862. return ResponseJson.error(-1, "您的企业账号已被冻结,请联系客服处理", loginUser);
  863. }
  864. if (92 == loginUser.getShopStatus()) {
  865. return ResponseJson.error(-3, "您的企业账号审核未通过", loginUser);
  866. }
  867. // 是否可为机构下单
  868. boolean flag = false;
  869. // 供应商商品上架平台信息
  870. List<String> shopProductList = loginMapper.getShopProductGroudMall(loginUser.getShopId());
  871. if (null != shopProductList) {
  872. for (String product : shopProductList) {
  873. if (StringUtils.isNotBlank(product)) {
  874. if (product.contains("4")) {
  875. flag = true;
  876. }
  877. }
  878. }
  879. } else {
  880. flag = false;
  881. }
  882. loginUser.setShopIsOrder(flag);
  883. }
  884. // 会员机构类型:1医美,2生
  885. if (null != loginUser.getUserIdentity() && loginUser.getUserIdentity() == 2) {
  886. Integer clubType = loginMapper.getClubTypeById(loginUser.getUserId());
  887. loginUser.setFirstClubType(clubType);
  888. }
  889. // 超级会员
  890. SuperVipDto end = findEnd(loginUser.getUserId());
  891. loginUser.setVipFlag(end.getVipFlag());
  892. if (loginUser.getUserId() != null) {
  893. if (loginUser.getClubId() != null && loginUser.getClubId() != 1342) {
  894. loginUser.setSpUserId(loginMapper.getClubSpUserId(loginUser.getClubId()));
  895. }
  896. Integer shopID = messageCenterMapper.shopID(loginUser.getUserId());
  897. Integer newReceiptType = messageCenterMapper.newReceiptType(shopID);
  898. // 上架费次数
  899. Integer listingFeeCount = messageCenterMapper.listingFee(shopID);
  900. // 上架费有效期内个数
  901. Integer listingfeeExpireCount = messageCenterMapper.listingfeeExpire(shopID);
  902. // 有免除上架费的次数
  903. Integer receStatctCount = messageCenterMapper.receStatct(shopID);
  904. Integer listingFee = 0;
  905. // 如果上架费次数为 0 ;所有上架费都不在在生效时间; 没有免除过上架费;
  906. if (listingFeeCount > 0 && listingfeeExpireCount == 0) {
  907. if (receStatctCount <= 0) {
  908. listingFee = 1;
  909. }
  910. }
  911. loginUser.setListingFee(listingFee);
  912. }
  913. // 更新用户openId、unionId 用于后续追踪
  914. try {
  915. loginMapper.updateUserInfo(loginUser.getOpenId(), loginUser.getUnionId(), loginUser.getUserId());
  916. } catch (Exception e) {
  917. e.printStackTrace();
  918. }
  919. // 登录异步赠送10采美豆
  920. asyncService.loginUpdateBeans(loginUser.getUserId(), loginUser.getUserIdentity());
  921. loginUser.setManager(sellerMapper.findManagerByUser(loginUser.getUserId()));
  922. loginUser.setLeaderId(sellerMapper.findLeaderId(loginUser.getUserId()));
  923. log.info("【登录】-----登录成功,userId:" + loginUser.getUserId());
  924. return ResponseJson.success("登录成功", loginUser);
  925. }
  926. /**
  927. * 获取生成微信二维码的参数(www)
  928. *
  929. * @return Map<String, Object>
  930. */
  931. @Override
  932. public ResponseJson<Map<String, String>> getAuthParameters() {
  933. UUID state = UUID.randomUUID();
  934. Map<String, String> dataMap = new HashMap<>(3);
  935. dataMap.put("appId", weChatService.getAppId());
  936. dataMap.put("redirectUri", weChatService.getRedirectUri());
  937. dataMap.put("state", String.valueOf(state));
  938. redisService.set("state:" + state, String.valueOf(state), 1800L);
  939. return ResponseJson.success(dataMap);
  940. }
  941. /**
  942. * 微信用户扫码,微信服务器回调
  943. *
  944. * @param code 微信code
  945. * @param state 安全认证key(上一步获取参数时自定义生成的uuid)
  946. */
  947. @Override
  948. public void qrCodeAuthScan(String code, String state) {
  949. String errorMsg = "";
  950. // 简单验证,防止csrf攻击(跨站请求伪造攻击)
  951. String stateCache = (String) redisService.get("state:" + state);
  952. if (StringUtils.isBlank(stateCache) || "null".equals(stateCache)) {
  953. errorMsg = "请从正确的途径打开链接";
  954. }
  955. if (StringUtils.isEmpty(code)) {
  956. errorMsg = "请重新进行授权登录";
  957. }
  958. try {
  959. // 用code换取access_token
  960. Map<String, Object> tokenMap = weChatService.getInfoMapByWeb(code, "pc");
  961. String accessToken = (String) tokenMap.get("access_token");
  962. String openId = (String) tokenMap.get(WeChatService.Keys.OPEN_ID);
  963. log.info(">>>>>(code换取access_token)wx回调openId:" + openId + " ,accessToken:" + accessToken);
  964. // 用access_token获取微信用户信息
  965. Map<String, Object> infoData = weChatService.getUserInfoByWeb(accessToken, openId);
  966. log.info(">>>>>(用access_token获取用户信息)wx回调openId:" + infoData.get(WeChatService.Keys.OPEN_ID) + " ,unionId:" + infoData.get(WeChatService.Keys.UNION_ID));
  967. // 微信用户信息存入redis
  968. redisService.setMap("scan:" + state, infoData);
  969. } catch (Exception e) {
  970. errorMsg = "获取微信用户信息失败";
  971. }
  972. // 错误信息存入Redis
  973. redisService.set("error:" + state, errorMsg, 1800L);
  974. }
  975. /**
  976. * 校验扫码结果
  977. *
  978. * @param state 安全认证key(第一步获取参数时自定义生成的uuid)
  979. * @return UserLoginVo
  980. */
  981. @Override
  982. public ResponseJson<UserLoginVo> qrCodeAuthScanResult(String state) throws ParseException {
  983. if (StringUtils.isBlank(state)) {
  984. return ResponseJson.error("参数异常:state不能为空!", null);
  985. }
  986. String errorMsg = (String) redisService.get("error:" + state);
  987. if (StringUtils.isNotEmpty(errorMsg) && !"null".equals(errorMsg)) {
  988. return ResponseJson.error(errorMsg, null);
  989. }
  990. Map<Object, Object> infoData = redisService.getEntries("scan:" + state);
  991. if (null == infoData || infoData.size() == 0) {
  992. return ResponseJson.error(-90, "redis缓存的扫码数据没有拿到", null);
  993. }
  994. // 清除redis的扫码数据
  995. redisService.remove("scan:" + state);
  996. String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
  997. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  998. log.info(">>>>>>pc商城unionId:" + unionId + " ,openId:" + openId);
  999. Integer userId = messageCenterMapper.userId(openId);
  1000. Integer clubId = messageCenterMapper.clubIdCule(userId);
  1001. Integer savedCount = null;
  1002. if (userId != null) {
  1003. Integer shopID = messageCenterMapper.shopID(userId);
  1004. Integer newReceiptType = messageCenterMapper.newReceiptType(shopID);
  1005. if (shopID == null) {
  1006. savedCount = messageCenterMapper.Count(1, clubId);
  1007. System.out.println(savedCount);
  1008. } else {
  1009. savedCount = messageCenterMapper.Count(2, shopID);
  1010. Integer listingFeeCount = messageCenterMapper.listingFee(shopID);
  1011. Integer listingfeeExpireCount = messageCenterMapper.listingfeeExpire(shopID);
  1012. Integer receStatctCount = messageCenterMapper.receStatct(shopID);
  1013. Integer listingFee = 0;
  1014. if (listingFeeCount > 0 && listingfeeExpireCount > 0) {
  1015. if (receStatctCount <= 0) {
  1016. listingFee = 1;
  1017. }
  1018. }
  1019. }
  1020. }
  1021. // 用户数据存入Redis,key前缀:wxInfo:website:
  1022. String infoDataStr = JSON.toJSONString(infoData);
  1023. Map<String, Object> infoDataMap = JSON.parseObject(infoDataStr);
  1024. redisService.setMap("wxInfo:website:" + unionId, infoDataMap);
  1025. log.info("微信扫码登录,用户数据存入Redis,key:wxInfo:website:" + unionId);
  1026. // 运营人员授权登录
  1027. return operationAuthLogin(openId, unionId, "www");
  1028. }
  1029. /**
  1030. * 微信扫码后,绑定机构账号
  1031. *
  1032. * @param scanBindDto {
  1033. * mobileOrEmail 手机号或邮箱
  1034. * password 密码
  1035. * mobile 手机号
  1036. * smsCode 手机验证码
  1037. * linkName 联系人
  1038. * passOrNote 0 密码验证 1 短信
  1039. * }
  1040. */
  1041. @Override
  1042. public ResponseJson<UserLoginVo> qrCodeAuthScanBind(ScanBindDto scanBindDto) {
  1043. String mobileOrEmail = null;
  1044. if (!ObjectUtils.isEmpty(scanBindDto.getMobileOrEmail())) {
  1045. mobileOrEmail = scanBindDto.getMobileOrEmail();
  1046. }
  1047. String password = null;
  1048. if (!ObjectUtils.isEmpty(scanBindDto.getPassword())) {
  1049. password = scanBindDto.getPassword();
  1050. }
  1051. String mobile = scanBindDto.getMobile();
  1052. String smsCode = scanBindDto.getSmsCode();
  1053. String linkName = scanBindDto.getLinkName();
  1054. String unionId = scanBindDto.getUnionId();
  1055. // 0 密码验证 1 短信
  1056. String passOrNote = "0";
  1057. if (!ObjectUtils.isEmpty(scanBindDto.getPassOrNote())) {
  1058. passOrNote = scanBindDto.getPassOrNote();
  1059. }
  1060. String companyMobile = null;
  1061. if (!ObjectUtils.isEmpty(scanBindDto.getPassOrNote())) {
  1062. companyMobile = scanBindDto.getCompanyMobile();
  1063. }
  1064. // 参数校验
  1065. if ("0".equals(passOrNote)) {
  1066. if (StringUtils.isBlank(mobileOrEmail)) {
  1067. return ResponseJson.error("参数异常:手机号或邮箱不能为空!", null);
  1068. }
  1069. if (StringUtils.isBlank(password)) {
  1070. return ResponseJson.error("参数异常:密码不能为空!", null);
  1071. }
  1072. }
  1073. if (StringUtils.isBlank(mobile)) {
  1074. return ResponseJson.error("参数异常:手机号不能为空!", null);
  1075. }
  1076. if (StringUtils.isBlank(smsCode)) {
  1077. return ResponseJson.error("参数异常:短信验证码不能为空!", null);
  1078. }
  1079. if (StringUtils.isBlank(unionId)) {
  1080. return ResponseJson.error("参数异常:unionId不能为空!", null);
  1081. }
  1082. //处理比对密码
  1083. if ("0".equals(passOrNote)) {
  1084. mobileOrEmail = mobileOrEmail;
  1085. }
  1086. if ("1".equals(passOrNote)) {
  1087. mobileOrEmail = companyMobile;
  1088. }
  1089. // 根据手机号或者邮箱获取账户信息
  1090. UserLoginVo user = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
  1091. String md5Password = null;
  1092. boolean item = false;
  1093. // 账号密码验证、短信验证校验方式
  1094. if ("0".equals(passOrNote)) {
  1095. md5Password = Md5Util.md5(password);
  1096. item = md5Password.equals(user.getPassword());
  1097. }
  1098. if ("1".equals(passOrNote)) {
  1099. item = true;
  1100. }
  1101. if (null != user && item) {
  1102. // 查询使用该手机号的运营人员或用户
  1103. String checkRust = commonService.operationBindCheck(mobile, smsCode, 0);
  1104. if (checkRust != null) {
  1105. return ResponseJson.error(checkRust, null);
  1106. }
  1107. Map<Object, Object> infoData = redisService.getEntries("wxInfo:website:" + unionId);
  1108. log.info("扫码绑定微信, 获取unionId>>>>>>" + unionId);
  1109. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  1110. String nickName = (String) infoData.get("nickname");
  1111. String avatarUrl = (String) infoData.get("headimgurl");
  1112. // 判断微信是否已经绑定
  1113. UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "www");
  1114. if (operationByUnionId != null) {
  1115. return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
  1116. }
  1117. /*
  1118. 组装运营人员数据 operation
  1119. */
  1120. OperationPo operation = new OperationPo();
  1121. // 用户Id
  1122. operation.setUserId(user.getUserId());
  1123. // 手机号
  1124. operation.setMobile(mobile);
  1125. operation.setLinkName(linkName);
  1126. // 微信昵称头像
  1127. operation.setNickName(nickName);
  1128. operation.setAvatarUrl(avatarUrl);
  1129. // unionId,openId
  1130. operation.setUnionId(unionId);
  1131. operation.setPcOpenId(openId);
  1132. // 组织机构0
  1133. operation.setOrganizeId(0);
  1134. // 绑定的机构/供应商Id,绑定的用户类型
  1135. if (3 == user.getUserIdentity()) {
  1136. operation.setShopId(user.getShopId());
  1137. operation.setUserType(2);
  1138. } else {
  1139. operation.setClubId(user.getClubId());
  1140. operation.setUserType(1);
  1141. }
  1142. // 绑定状态
  1143. operation.setStatus(2);
  1144. // 删除标识
  1145. operation.setDelFlag(0);
  1146. Date time = new Date();
  1147. // 添加时间
  1148. operation.setAddTime(time);
  1149. // 绑定时间
  1150. operation.setBindTime(time);
  1151. // 更新时间
  1152. operation.setUpdateTime(time);
  1153. /*
  1154. 保存数据库 operation
  1155. */
  1156. registerMapper.insertOperation(operation);
  1157. return logonVerify(user);
  1158. }
  1159. return ResponseJson.error("输入的密码和账户名不匹配", null);
  1160. }
  1161. /**
  1162. * 邀请码登录
  1163. *
  1164. * @param invitationCode 邀请码
  1165. * @param nickName 微信昵称
  1166. * @param avatarUrl 微信头像(headimgurl)
  1167. * @param unionId 微信unionId
  1168. * @return UserLoginVo
  1169. */
  1170. @Override
  1171. public ResponseJson<UserLoginVo> invitationCodeLogin(String invitationCode, String nickName, String avatarUrl, String unionId) {
  1172. // 参数校验
  1173. if (StringUtils.isBlank(invitationCode)) {
  1174. return ResponseJson.error("邀请码不能为空", null);
  1175. }
  1176. UserLoginVo operation = loginMapper.getOperationUserByInvitationCode(invitationCode, 0);
  1177. if (operation == null) {
  1178. return ResponseJson.error("邀请码错误", null);
  1179. }
  1180. Date date = new Date();
  1181. Calendar calendar = Calendar.getInstance();
  1182. calendar.setTime(operation.getInvitationCodeTime());
  1183. calendar.add(Calendar.DATE, validTime);
  1184. if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
  1185. return ResponseJson.error("邀请码已过期,请联系邀请人更新邀请码", null);
  1186. }
  1187. if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
  1188. return ResponseJson.error("邀请码已被使用", null);
  1189. }
  1190. // 用户身份:1机构,2供应商
  1191. int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
  1192. if (1 == userIdentity && operation.getClubStatus() != null && 91 == operation.getClubStatus()) {
  1193. return ResponseJson.error("您的机构已下线", null);
  1194. }
  1195. if (2 == userIdentity && operation.getShopStatus() != null && 91 == operation.getShopStatus()) {
  1196. return ResponseJson.error("您的企业账号已下线,请联系客服处理", null);
  1197. }
  1198. if (0 != operation.getDelFlag()) {
  1199. return ResponseJson.error("您的邀请码已被删除,请重新添加运营人员", null);
  1200. }
  1201. OperationPo operationPo = new OperationPo();
  1202. operationPo.setId(operation.getOperationId());
  1203. // 微信unionId
  1204. operationPo.setUnionId(unionId);
  1205. Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
  1206. // 微信openId
  1207. operationPo.setOpenId((String) infoData.get(WeChatService.Keys.OPEN_ID));
  1208. // 微信昵称
  1209. operationPo.setNickName(nickName);
  1210. // 微信头像
  1211. operationPo.setAvatarUrl(avatarUrl);
  1212. // 绑定状态,1未绑定,2已绑定
  1213. operationPo.setStatus(2);
  1214. // 采美进行消息推送
  1215. if (0 == operation.getOrganizeId()) {
  1216. if (1 == userIdentity) {
  1217. // 机构Id
  1218. operationPo.setClubId(operation.getClubId());
  1219. // 用户类型
  1220. operationPo.setUserType(1);
  1221. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1222. String current = dateFormat.format(new Date());
  1223. MessageCenter messageCenter = new MessageCenter();
  1224. messageCenter.setShopId(null);
  1225. messageCenter.setClubId(operation.getClubId());
  1226. messageCenter.setUserType(1);
  1227. messageCenter.setMessageType(2);
  1228. messageCenter.setAccountType(7);
  1229. messageCenter.setContent(nickName);
  1230. messageCenter.setTime(current);
  1231. messageCenterMapper.addMessageCenter(messageCenter);
  1232. //发短信
  1233. String message = "【采美365】恭喜您成功成为运营人员,您可通过微信直接登录采美商城进行采购。";
  1234. String mobile = messageCenterMapper.contractMobile(operation.getClubId());
  1235. if (mobile != null && mobile != "") {
  1236. remoteCallService.remoteSendSms(2, 3, mobile, message);
  1237. }
  1238. try {
  1239. log.info("***********机构通过邀请码登入微信公众号推送************");
  1240. String accessToken = weChatService.getAccessToken();
  1241. String openid = messageCenterMapper.getOpenidListByPermission(unionId);
  1242. String time = current;
  1243. String remarkText = "绑定成功后,您可通过微信直接访问采美商城进行采购。";
  1244. // 跳转到【小程序付款-选择支付方式页面】
  1245. String pagePath = "https://www.caimei365.com/";
  1246. // sendTemplateMsg(openid, 标题, 金额, 收款日期, 备注, 跳转链接
  1247. log.error("获取openid>>>>>" + openid);
  1248. weChatService.sendTemplateMessg(accessToken, openid, nickName, time, remarkText, pagePath);
  1249. } catch (Exception e) {
  1250. log.error("【机构通过邀请码登入通知】获取微信公众号access_token异常!", e);
  1251. }
  1252. } else {
  1253. // 供应商Id
  1254. operationPo.setShopId(operation.getShopId());
  1255. // 用户类型
  1256. operationPo.setUserType(2);
  1257. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1258. String current = dateFormat.format(new Date());
  1259. MessageCenter messageCenter = new MessageCenter();
  1260. messageCenter.setShopId(operation.getShopId());
  1261. messageCenter.setClubId(null);
  1262. messageCenter.setUserType(2);
  1263. messageCenter.setMessageType(2);
  1264. messageCenter.setShopMessType(2);
  1265. messageCenter.setContent(nickName);
  1266. messageCenter.setTime(current);
  1267. messageCenterMapper.addMessageCenter(messageCenter);
  1268. //发短信
  1269. String message = "【采美365】恭喜您成功成为运营人员,您可通过微信直接登录采美商城进行采购。";
  1270. String mobile = messageCenterMapper.contractMobiles(operation.getShopId());
  1271. if (mobile != null && mobile != "") {
  1272. remoteCallService.remoteSendSms(2, 3, mobile, message);
  1273. }
  1274. try {
  1275. log.info("***********供应商通过邀请码登入微信公众号推送************");
  1276. String accessToken = weChatService.getAccessToken();
  1277. String openid = messageCenterMapper.getOpenidListByPermission(unionId);
  1278. String time = current;
  1279. String remarkText = "绑定成功后,您可通过微信直接访问采美商城进行采购。";
  1280. // 跳转到【小程序付款-选择支付方式页面】
  1281. String pagePath = "https://www.caimei365.com/";
  1282. // sendTemplateMsg(openid, 标题, 金额, 收款日期, 备注, 跳转链接
  1283. log.error("获取openid>>>>>" + openid);
  1284. weChatService.sendTemplateMessg(accessToken, openid, nickName, time, remarkText, pagePath);
  1285. } catch (Exception e) {
  1286. log.error("【供应商通过邀请码登入通知】获取微信公众号access_token异常!", e);
  1287. }
  1288. }
  1289. } else {
  1290. // 机构Id
  1291. operationPo.setClubId(operation.getClubId());
  1292. // 用户类型
  1293. operationPo.setUserType(1);
  1294. }
  1295. operationPo.setUpdateTime(new Date());
  1296. operationPo.setBindTime(new Date());
  1297. // 更新运营人员信息
  1298. operationMapper.updateOperationByInvitation(operationPo);
  1299. // 返回登录校验结果
  1300. return logonVerify(operation);
  1301. }
  1302. /**
  1303. * 邀请码登录
  1304. *
  1305. * @param invitationCode 邀请码
  1306. * @param nickName 微信昵称
  1307. * @param avatarUrl 微信头像(headimgurl)
  1308. * @param unionId 微信unionId
  1309. * @param organizeId 组织Id
  1310. * @return UserLoginVo
  1311. */
  1312. @Override
  1313. public ResponseJson<UserLoginVo> invitationCodeOrganizeLogin(String invitationCode, String nickName, String avatarUrl, String unionId, Integer organizeId) {
  1314. // 参数校验
  1315. if (StringUtils.isBlank(invitationCode)) {
  1316. return ResponseJson.error("邀请码不能为空", null);
  1317. }
  1318. UserLoginVo operation = loginMapper.getOperationOrganizeUserByInvitationCode(invitationCode, organizeId);
  1319. if (operation == null) {
  1320. return ResponseJson.error("邀请码错误", null);
  1321. }
  1322. Date date = new Date();
  1323. Calendar calendar = Calendar.getInstance();
  1324. calendar.setTime(operation.getInvitationCodeTime());
  1325. calendar.add(Calendar.DATE, validTime);
  1326. if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
  1327. return ResponseJson.error("邀请码已过期,请联系邀请人更新邀请码", null);
  1328. }
  1329. if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
  1330. return ResponseJson.error("邀请码已被使用", null);
  1331. }
  1332. // 用户身份:1机构,2供应商
  1333. int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
  1334. if (1 == userIdentity && operation.getClubStatus() != null && 91 == operation.getClubStatus()) {
  1335. return ResponseJson.error("您的机构已冻结", null);
  1336. }
  1337. if (2 == userIdentity && operation.getShopStatus() != null && 91 == operation.getShopStatus()) {
  1338. return ResponseJson.error("您的企业账号已被冻结,请联系客服处理", null);
  1339. }
  1340. if (0 != operation.getDelFlag()) {
  1341. return ResponseJson.error("您的邀请码已被删除,请重新添加运营人员", null);
  1342. }
  1343. OperationPo operationPo = new OperationPo();
  1344. operationPo.setId(operation.getOperationId());
  1345. // 微信unionId
  1346. operationPo.setUnionId(unionId);
  1347. Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
  1348. // 微信openId
  1349. operationPo.setOpenId((String) infoData.get(WeChatService.Keys.OPEN_ID));
  1350. // 微信昵称
  1351. operationPo.setNickName(nickName);
  1352. // 微信头像
  1353. operationPo.setAvatarUrl(avatarUrl);
  1354. // 绑定状态,1未绑定,2已绑定
  1355. operationPo.setStatus(2);
  1356. // 联合丽格没有运营人员为机构运营人员
  1357. // 机构Id
  1358. operationPo.setClubId(operation.getClubId());
  1359. // 用户类型
  1360. operationPo.setUserType(1);
  1361. operationPo.setUpdateTime(new Date());
  1362. operationPo.setBindTime(new Date());
  1363. // 更新运营人员信息
  1364. operationMapper.updateOperationByInvitation(operationPo);
  1365. // 返回登录校验结果
  1366. return logonVerify(operation);
  1367. }
  1368. /**
  1369. * 运营人员绑定微信
  1370. *
  1371. * @param authBindDto {
  1372. * userId 要绑定的用户Id(userID)
  1373. * mobile 手机号
  1374. * smsCode 手机验证码(verificationCode)
  1375. * unionId 微信unionId
  1376. * nickName 微信昵称
  1377. * avatarUrl 微信头像(headimgurl)
  1378. * }
  1379. * @return OperationPo
  1380. */
  1381. @Override
  1382. public ResponseJson<UserLoginVo> operationBindWeChat(AuthBindDto authBindDto) {
  1383. Integer userId = authBindDto.getUserId();
  1384. String mobile = authBindDto.getMobile();
  1385. String smsCode = authBindDto.getSmsCode();
  1386. String linkName = authBindDto.getLinkName();
  1387. String unionId = authBindDto.getUnionId();
  1388. String nickName = authBindDto.getNickName();
  1389. String avatarUrl = authBindDto.getAvatarUrl();
  1390. String isCheckSmsCode = authBindDto.getIsCheckSmsCode();
  1391. // 参数校验
  1392. if (null == userId) {
  1393. return ResponseJson.error("参数异常:用户Id不能为空!", null);
  1394. }
  1395. if (StringUtils.isBlank(mobile)) {
  1396. return ResponseJson.error("参数异常:手机号不能为空!", null);
  1397. }
  1398. boolean b = StringUtils.isBlank(isCheckSmsCode) || "0".equals(isCheckSmsCode);
  1399. if (b && StringUtils.isBlank(smsCode)) {
  1400. return ResponseJson.error("参数异常:短信验证码不能为空!", null);
  1401. }
  1402. if (StringUtils.isBlank(unionId)) {
  1403. return ResponseJson.error("参数异常:unionId不能为空!", null);
  1404. }
  1405. // 查询使用该手机号的运营人员或用户
  1406. String checkRust = commonService.operationBindCheck(mobile, smsCode, 0);
  1407. if (checkRust != null) {
  1408. return ResponseJson.error(checkRust, null);
  1409. }
  1410. Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
  1411. log.info("绑定微信bindingWx,获取unionId>>>>>>" + unionId);
  1412. String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
  1413. // 判断微信是否已经绑定
  1414. UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "mini");
  1415. if (operationByUnionId != null) {
  1416. return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
  1417. }
  1418. // 要绑定的用户
  1419. UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
  1420. /*
  1421. 组装运营人员数据 operation
  1422. */
  1423. OperationPo operation = new OperationPo();
  1424. // 用户Id
  1425. operation.setUserId(userId);
  1426. // 手机号
  1427. operation.setMobile(mobile);
  1428. operation.setLinkName(linkName);
  1429. // 微信昵称头像
  1430. operation.setNickName(nickName);
  1431. operation.setAvatarUrl(avatarUrl);
  1432. // unionId,openId
  1433. operation.setUnionId(unionId);
  1434. operation.setOpenId(openId);
  1435. // 组织机构0
  1436. operation.setOrganizeId(0);
  1437. // 绑定的机构/供应商Id,绑定的用户类型
  1438. if (user != null && 3 == user.getUserIdentity()) {
  1439. operation.setShopId(user.getShopId());
  1440. operation.setUserType(2);
  1441. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1442. String current = dateFormat.format(new Date());
  1443. MessageCenter messageCenter = new MessageCenter();
  1444. messageCenter.setShopId(operation.getShopId());
  1445. messageCenter.setClubId(null);
  1446. messageCenter.setUserType(2);
  1447. messageCenter.setMessageType(2);
  1448. messageCenter.setShopMessType(2);
  1449. messageCenter.setContent(nickName);
  1450. messageCenter.setShopMessType(null);
  1451. messageCenter.setTime(current);
  1452. messageCenterMapper.addMessageCenter(messageCenter);
  1453. } else if (user != null) {
  1454. operation.setClubId(user.getClubId());
  1455. operation.setUserType(1);
  1456. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1457. String current = dateFormat.format(new Date());
  1458. MessageCenter messageCenter = new MessageCenter();
  1459. messageCenter.setShopId(null);
  1460. messageCenter.setClubId(user.getClubId());
  1461. messageCenter.setUserType(1);
  1462. messageCenter.setMessageType(2);
  1463. messageCenter.setAccountType(7);
  1464. messageCenter.setContent(nickName);
  1465. messageCenter.setTime(current);
  1466. messageCenterMapper.addMessageCenter(messageCenter);
  1467. }
  1468. // 绑定状态
  1469. operation.setStatus(2);
  1470. // 删除标识
  1471. operation.setDelFlag(0);
  1472. Date time = new Date();
  1473. // 添加时间
  1474. operation.setAddTime(time);
  1475. // 绑定时间
  1476. operation.setBindTime(time);
  1477. // 更新时间
  1478. operation.setUpdateTime(time);
  1479. /*
  1480. 保存数据库 operation
  1481. */
  1482. registerMapper.insertOperation(operation);
  1483. return ResponseJson.success("绑定微信成功", user);
  1484. }
  1485. /**
  1486. * 绑定运营人员短信验证
  1487. *
  1488. * @param loginCodeDto
  1489. * @return
  1490. */
  1491. @Override
  1492. public ResponseJson<String> operateVerification(LoginCodeDto loginCodeDto) {
  1493. // 验证码或者账号密码
  1494. String codeType = null;
  1495. if (!ObjectUtils.isEmpty(loginCodeDto.getCodeType())) {
  1496. codeType = loginCodeDto.getCodeType();
  1497. }
  1498. // 账号密码验证
  1499. if ("0".equals(codeType)) {
  1500. if (ObjectUtils.isEmpty(loginCodeDto.getMobileOrEmail())) {
  1501. return ResponseJson.error("请输入账号", null);
  1502. }
  1503. if (ObjectUtils.isEmpty(loginCodeDto.getPassword())) {
  1504. return ResponseJson.error("请输入密码", null);
  1505. }
  1506. // 账号
  1507. String mobileOrEmail = loginCodeDto.getMobileOrEmail();
  1508. // 密码
  1509. String password = loginCodeDto.getPassword();
  1510. // 根据手机号或者邮箱获取账户信息
  1511. UserLoginVo user = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
  1512. // 账号密码验证
  1513. String md5Password = Md5Util.md5(password);
  1514. if (md5Password.equals(user.getPassword())) {
  1515. return ResponseJson.success("账号与密码匹配成功");
  1516. }
  1517. return ResponseJson.error("账号与密码不匹配,请检查后重新输入");
  1518. } else {
  1519. // 手机短信验证
  1520. if (ObjectUtils.isEmpty(loginCodeDto.getMobile())) {
  1521. return ResponseJson.error("请输入手机号", null);
  1522. }
  1523. if (ObjectUtils.isEmpty(loginCodeDto.getCode())) {
  1524. return ResponseJson.error("请输入验证码", null);
  1525. }
  1526. String mobile = loginCodeDto.getMobile();
  1527. String code = loginCodeDto.getCode();
  1528. String result = ValidateUtil.validateMobile(mobile);
  1529. if (result != null) {
  1530. return ResponseJson.error(result);
  1531. }
  1532. // 判断redis中是否存在
  1533. boolean exists = redisService.exists("code:" + mobile);
  1534. if (exists) {
  1535. // 校验验证码是否过期
  1536. long expireTime = redisService.getExpireTime("code:" + mobile);
  1537. if (expireTime < 0) {
  1538. return ResponseJson.error("验证码已失效,请重新获取");
  1539. }
  1540. // 获取redis缓存验证码
  1541. Object randomCode = redisService.get("code:" + mobile);
  1542. if (!ObjectUtils.isEmpty(randomCode)) {
  1543. if (code.equals(randomCode.toString())) {
  1544. redisService.remove("code:" + mobile);
  1545. return ResponseJson.success("验证码匹配成功");
  1546. } else {
  1547. return ResponseJson.error("验证码不匹配,请重新输入");
  1548. }
  1549. } else {
  1550. return ResponseJson.error("验证码错误,请重新获取");
  1551. }
  1552. } else {
  1553. return ResponseJson.error("验证码错误,请重新获取");
  1554. }
  1555. }
  1556. }
  1557. @Override
  1558. public Integer updateMessageCenter(MessageCenter messageCenter) {
  1559. return messageCenterMapper.updateMessageCenter(messageCenter);
  1560. }
  1561. @Override
  1562. public Integer insertMessageCenter(MessageCenter messageCenter) {
  1563. return messageCenterMapper.insertMessageCenter(messageCenter);
  1564. }
  1565. @Override
  1566. public List<Integer> getMessageCenterListDay(Integer day) {
  1567. return messageCenterMapper.getMessageCenterListDay(day);
  1568. }
  1569. /**
  1570. * 根据userId查是否过期,返回dto对象,flag=0未买过,-1过期,1有效,endTime过期时间
  1571. */
  1572. private SuperVipDto findEnd(Integer userId) {
  1573. SuperVipPo superVip = vipMapper.findSuperVip(userId);
  1574. SuperVipDto superVipDto = new SuperVipDto();
  1575. if (superVip == null) {
  1576. superVipDto.setVipFlag(0);
  1577. } else {
  1578. SuperVipPo endTime = vipMapper.findEndTime(userId);
  1579. if (endTime == null) {
  1580. superVipDto.setVipFlag(-1);
  1581. superVipDto.setEndTime(superVip.getEndTime());
  1582. } else {
  1583. superVipDto.setVipFlag(1);
  1584. superVipDto.setEndTime(endTime.getEndTime());
  1585. }
  1586. }
  1587. return superVipDto;
  1588. }
  1589. /**
  1590. * 初始化站内信
  1591. */
  1592. private List<MessageCenter> setMessageList(Integer userType,Integer messageType,Integer commonId) {
  1593. List<MessageCenter> list = messageCenterMapper.MessageList(userType, messageType, commonId);
  1594. list.forEach(mess -> {
  1595. if (null != mess.getOrderId()) {
  1596. MessageCenter messageCenter = messageCenterMapper.MainImage(userType, commonId, mess.getOrderId());
  1597. Integer productCount = messageCenterMapper.productCount(mess.getOrderId());
  1598. if (null != messageCenter) {
  1599. mess.setMainImage(messageCenter.getMainImage());
  1600. mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
  1601. if (messageCenter.getProductName().length() > 10) {
  1602. mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
  1603. } else {
  1604. mess.setProductName(messageCenter.getProductName());
  1605. }
  1606. mess.setProductCount(productCount);
  1607. mess.setRefundType(messageCenter.getRefundType());
  1608. mess.setStatus(messageCenter.getStatus());
  1609. }
  1610. }
  1611. });
  1612. return list;
  1613. }
  1614. private Map<String, Object> setMessageCount(Integer userType, Integer commonId) {
  1615. Integer count = messageCenterMapper.Count(userType, commonId);
  1616. Integer tradeCount = messageCenterMapper.MessageCount(userType, 1, commonId);
  1617. Integer accountCount = messageCenterMapper.MessageCount(userType, 2, commonId);
  1618. Integer notificationCount = messageCenterMapper.MessageCount(userType, 3, commonId);
  1619. Integer promotionCount = messageCenterMapper.MessageCount(userType, 4, commonId);
  1620. Map<String, Object> map = new HashMap(5);
  1621. map.put("count", count);
  1622. map.put("tradeCount", tradeCount);
  1623. map.put("accountCount", accountCount);
  1624. map.put("notificationCount", notificationCount);
  1625. map.put("promotionCount", promotionCount);
  1626. return map;
  1627. }
  1628. }