123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698 |
- package com.caimei365.user.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.caimei365.user.components.CommonService;
- import com.caimei365.user.components.RedisService;
- import com.caimei365.user.components.WeChatService;
- import com.caimei365.user.mapper.*;
- import com.caimei365.user.model.ResponseJson;
- import com.caimei365.user.model.dto.*;
- import com.caimei365.user.model.po.OperationPo;
- import com.caimei365.user.model.po.SuperVipPo;
- import com.caimei365.user.model.vo.MessageCenter;
- import com.caimei365.user.model.vo.OperationVo;
- import com.caimei365.user.model.vo.UserLoginVo;
- import com.caimei365.user.service.LoginService;
- import com.caimei365.user.service.RemoteCallService;
- import com.caimei365.user.utils.JwtUtil;
- import com.caimei365.user.utils.Md5Util;
- import com.caimei365.user.utils.ValidateUtil;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.HttpHeaders;
- import org.springframework.stereotype.Service;
- import org.springframework.util.ObjectUtils;
- import javax.annotation.Resource;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * Description
- *
- * @author : Charles
- * @date : 2021/3/8
- */
- @Slf4j
- @Service
- public class LoginServiceImpl implements LoginService {
- @Resource
- private SuperVipMapper vipMapper;
- @Resource
- private RedisService redisService;
- @Resource
- private WeChatService weChatService;
- @Resource
- private CommonService commonService;
- @Resource
- private LoginMapper loginMapper;
- @Resource
- private RegisterMapper registerMapper;
- @Resource
- private OperationMapper operationMapper;
- @Resource
- private AsyncService asyncService;
- @Resource
- private SellerMapper sellerMapper;
- @Resource
- private MessageCenterMapper messageCenterMapper;
- @Resource
- private RemoteCallService remoteCallService;
- /**
- * 小程序邀请码过期天数
- */
- @Value("${caimei.validTime}")
- private Integer validTime;
- /**
- * 登录(用户名,密码)
- *
- * @param loginPasswordDto {
- * mobileOrEmail 手机号或邮箱
- * password 密码
- * unionId 微信unionId
- * }
- * @return BaseUser
- */
- @Override
- public ResponseJson<UserLoginVo> passwordLogin(LoginPasswordDto loginPasswordDto) throws ParseException {
- String mobileOrEmail = loginPasswordDto.getMobileOrEmail();
- String password = loginPasswordDto.getPassword();
- String unionId = loginPasswordDto.getUnionId();
- if (StringUtils.isBlank(mobileOrEmail)) {
- return ResponseJson.error("请填写账户名", null);
- }
- if (StringUtils.isBlank(password)) {
- return ResponseJson.error("请填写密码", null);
- }
- //处理比对密码
- UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
- if (null != baseUser) {
- String key = "login-" + baseUser.getUserId();
- boolean exists = redisService.exists(key);
- //如果30分钟内输入错误记录>=5,return该账号暂时被冻结,请(30-最前一次时间)分钟后重试或直接修改密码
- if (exists) {
- String val = (String) redisService.get(key);
- String[] split = val.split(",");
- int count = Integer.parseInt(split[0]);
- if (count >= 5) {
- long s = Long.parseLong(split[1]);
- int l = (int) Math.floor((System.currentTimeMillis() - s) / 1000 / 60);
- return ResponseJson.error("该账号暂时被冻结,请" + (30 - l) + "分钟后重试或直接修改密码", null);
- }
- }
- // 如果前端传入unionId,则存入返回前端
- baseUser.setUnionId(unionId);
- // 不是采美组织下
- if (0 != baseUser.getOrganizeId()) {
- if (1 == baseUser.getClubStatus()) {
- return ResponseJson.error(-1, "账号待审核,请耐心等待审核结果", null);
- }
- if (92 == baseUser.getClubStatus()) {
- return ResponseJson.error(-1, "账号审核未通过,请重新提交资料", null);
- }
- }
- // 比对密码
- String md5Password = Md5Util.md5(password);
- String dbPassword = baseUser.getPassword();
- if (md5Password.equals(dbPassword)) {
- // 游客id与用户关联
- if (StringUtils.isNotBlank(loginPasswordDto.getTouristId()) && StringUtils.isNotEmpty(loginPasswordDto.getTouristId())) {
- loginMapper.updateTourist(baseUser.getUserId(), loginPasswordDto.getTouristId());
- }
- if (baseUser.getUserIdentity() == 1) {
- // 协销登录
- return ResponseJson.success(baseUser);
- } else {
- // 返回登录校验结果
- return logonVerify(baseUser);
- }
- } else {
- // 增加一次错误输入密码记录,30分钟内连续五次冻结
- if (exists) {
- String val = (String) redisService.get(key);
- String[] split = val.split(",");
- int count = Integer.parseInt(split[0]);
- if (count < 5) {
- count++;
- String va = count + "," + System.currentTimeMillis();
- redisService.set(key, va);
- }
- if (count >= 5) {
- redisService.set(key, 5 + "," + System.currentTimeMillis(), 1800L);
- }
- } else {
- String val = 1 + "," + System.currentTimeMillis();
- redisService.set(key, val);
- }
- }
- }
- return ResponseJson.error("账户名与密码不匹配,请重新输入", null);
- }
- /**
- * 登录(用户名,密码)
- *
- * @param loginPasswordDto {
- * mobileOrEmail 手机号或邮箱
- * password 密码
- * unionId 微信unionId
- * }
- * @return BaseUser
- */
- @Override
- public ResponseJson<UserLoginVo> passwordOrganizeLogin(LoginPasswordDto loginPasswordDto) throws ParseException {
- String mobileOrEmail = loginPasswordDto.getMobileOrEmail();
- String password = loginPasswordDto.getPassword();
- String unionId = loginPasswordDto.getUnionId();
- if (StringUtils.isBlank(mobileOrEmail)) {
- return ResponseJson.error("请填写账户名", null);
- }
- if (StringUtils.isBlank(password)) {
- return ResponseJson.error("请填写密码", null);
- }
- //处理比对密码
- UserLoginVo baseUser = loginMapper.getLoginOrganizeUserByMobileOrEmail(mobileOrEmail);
- // 机构下线,登录失败
- if (91 == baseUser.getClubStatus()) {
- return ResponseJson.error(-1, "您的企业账号已被冻结,请联系客服处理", null);
- }
- if (null != baseUser) {
- // 绑定微信信息
- Integer operationUser = loginMapper.getOperationUser(mobileOrEmail);
- log.info("operationUser===" + operationUser);
- if (null == operationUser) {
- // 绑定微信
- OperationVo operationVo = new OperationVo();
- operationVo.setOrganizeId(baseUser.getOrganizeId());
- operationVo.setUserType(1);
- operationVo.setUserId(baseUser.getUserId());
- operationVo.setClubId(baseUser.getClubId());
- operationVo.setMobile(baseUser.getBindMobile());
- operationVo.setLinkName(baseUser.getClubLinkMan());
- operationVo.setStatus(2);
- operationVo.setUnionId(unionId);
- operationVo.setBindTime(new Date());
- operationVo.setAddTime(new Date());
- operationVo.setDelFlag(0);
- loginMapper.insertOperation(operationVo);
- log.info("operationVo***" + operationVo);
- }
- String key = "login-" + baseUser.getUserId();
- boolean exists = redisService.exists(key);
- //如果30分钟内输入错误记录>=5,return该账号暂时被冻结,请(30-最前一次时间)分钟后重试或直接修改密码
- if (exists) {
- String val = (String) redisService.get(key);
- String[] split = val.split(",");
- int count = Integer.parseInt(split[0]);
- if (count >= 5) {
- long s = Long.parseLong(split[1]);
- int l = (int) Math.floor((System.currentTimeMillis() - s) / 1000 / 60);
- return ResponseJson.error("该账号暂时被冻结,请" + (30 - l) + "分钟后重试或直接修改密码", null);
- }
- }
- // 如果前端传入unionId,则存入返回前端
- baseUser.setUnionId(unionId);
- // 比对密码
- String md5Password = Md5Util.md5(password);
- String dbPassword = baseUser.getPassword();
- if (md5Password.equals(dbPassword)) {
- if (baseUser.getUserIdentity() == 1) {
- // 协销登录
- return ResponseJson.success(baseUser);
- } else {
- // 返回登录校验结果
- return logonVerify(baseUser);
- }
- } else {
- // 增加一次错误输入密码记录,30分钟内连续五次冻结
- if (exists) {
- String val = (String) redisService.get(key);
- String[] split = val.split(",");
- int count = Integer.parseInt(split[0]);
- if (count < 5) {
- count++;
- String va = count + "," + System.currentTimeMillis();
- redisService.set(key, va);
- }
- if (count >= 5) {
- redisService.set(key, 5 + "," + System.currentTimeMillis(), 1800L);
- }
- } else {
- String val = 1 + "," + System.currentTimeMillis();
- redisService.set(key, val);
- }
- }
- }
- return ResponseJson.error(-1, "账户名与密码不匹配,请重新输入", null);
- }
- /**
- * @param loginCodeDto {
- * mobile 手机号
- * code 短信验证码
- * }
- * @return
- * @throws ParseException
- */
- @Override
- public ResponseJson<UserLoginVo> codeLogin(LoginCodeDto loginCodeDto) throws ParseException {
- if (ObjectUtils.isEmpty(loginCodeDto.getMobile())) {
- return ResponseJson.error("请填写手机号", null);
- }
- if (ObjectUtils.isEmpty(loginCodeDto.getCode())) {
- return ResponseJson.error("请输入短信验证码", null);
- }
- String mobile = loginCodeDto.getMobile();
- String code = loginCodeDto.getCode();
- String unionId = loginCodeDto.getUnionId();
- String result = ValidateUtil.validateMobile(mobile);
- if (result != null) {
- return ResponseJson.error(result);
- }
- // 判断redis中是否存在
- boolean exists = redisService.exists("code:" + mobile);
- if (exists) {
- // 查看验证码是否过期
- long expireTime = redisService.getExpireTime("code:" + mobile);
- if (expireTime < 0) {
- return ResponseJson.error("验证码已失效,请重新获取");
- }
- // 获取redis手机短信验证码
- Object randomCode = redisService.get("code:" + mobile);
- if (!ObjectUtils.isEmpty(randomCode)) {
- if (code.equals(randomCode.toString())) {
- redisService.remove("code:" + mobile);
- // 根据手机号获取用户信息
- UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobile);
- // 游客id与用户关联
- if (StringUtils.isNotBlank(loginCodeDto.getTouristId()) && StringUtils.isNotEmpty(loginCodeDto.getTouristId())) {
- loginMapper.updateTourist(baseUser.getUserId(), loginCodeDto.getTouristId());
- }
- // 如果前端传入unionId,则存入返回前端
- baseUser.setUnionId(unionId);
- if (baseUser.getUserIdentity() == 1) {
- // 协销登录
- return ResponseJson.success(baseUser);
- } else {
- // 返回登录校验结果
- return logonVerify(baseUser);
- }
- } else {
- return ResponseJson.error("验证码不匹配,请重新输入");
- }
- } else {
- return ResponseJson.error("验证码错误,请重新获取");
- }
- } else {
- return ResponseJson.error("验证码错误,请重新获取");
- }
- }
- /**
- * @param loginCodeDto {
- * mobile 手机号
- * code 短信验证码
- * }
- * @return
- * @throws ParseException
- */
- @Override
- public ResponseJson<UserLoginVo> codeOrganizeLogin(LoginCodeDto loginCodeDto) throws ParseException {
- if (ObjectUtils.isEmpty(loginCodeDto.getMobile())) {
- return ResponseJson.error("请填写手机号", null);
- }
- if (ObjectUtils.isEmpty(loginCodeDto.getCode())) {
- return ResponseJson.error("请输入短信验证码", null);
- }
- String mobile = loginCodeDto.getMobile();
- String code = loginCodeDto.getCode();
- String unionId = loginCodeDto.getUnionId();
- String result = ValidateUtil.validateMobile(mobile);
- if (result != null) {
- return ResponseJson.error(result);
- }
- // 判断redis中是否存在
- boolean exists = redisService.exists("code:" + mobile);
- if (exists) {
- // 查看验证码是否过期
- long expireTime = redisService.getExpireTime("code:" + mobile);
- if (expireTime < 0) {
- return ResponseJson.error(-1, "验证码已失效,请重新获取", null);
- }
- // 获取redis手机短信验证码
- Object randomCode = redisService.get("code:" + mobile);
- if (!ObjectUtils.isEmpty(randomCode)) {
- if (code.equals(randomCode.toString())) {
- redisService.remove("code:" + mobile);
- // 根据手机号获取用户信息
- UserLoginVo baseUser = loginMapper.getLoginOrganizeUserByMobileOrEmail(mobile);
- // 绑定微信信息
- Integer operationUser = loginMapper.getOperationUser(mobile);
- log.info("operationUser===" + operationUser);
- if (null == operationUser) {
- // 绑定微信
- OperationVo operationVo = new OperationVo();
- operationVo.setOrganizeId(baseUser.getOrganizeId());
- operationVo.setUserType(1);
- operationVo.setUserId(baseUser.getUserId());
- operationVo.setClubId(baseUser.getClubId());
- operationVo.setMobile(baseUser.getBindMobile());
- operationVo.setLinkName(baseUser.getUserName());
- operationVo.setStatus(2);
- operationVo.setUnionId(unionId);
- operationVo.setBindTime(new Date());
- operationVo.setAddTime(new Date());
- operationVo.setDelFlag(0);
- loginMapper.insertOperation(operationVo);
- }
- // 如果前端传入unionId,则存入返回前端
- baseUser.setUnionId(unionId);
- // 不是采美组织下
- if (0 != baseUser.getOrganizeId()) {
- if (1 == baseUser.getClubStatus()) {
- return ResponseJson.error(-1, "账号待审核,请耐心等待审核结果", null);
- }
- if (92 == baseUser.getClubStatus()) {
- return ResponseJson.error(-3, "账号审核未通过,请重新提交资料", null);
- }
- }
- if (baseUser.getUserIdentity() == 1) {
- // 协销登录
- return ResponseJson.success(baseUser);
- } else {
- // 返回登录校验结果
- return logonVerify(baseUser);
- }
- } else {
- return ResponseJson.error(-1, "验证码不匹配,请重新输入", null);
- }
- } else {
- return ResponseJson.error(-1, "验证码错误,请重新获取", null);
- }
- } else {
- return ResponseJson.error(-1, "验证码错误,请重新获取", null);
- }
- }
- /**
- * 微信授权登录(小程序)
- *
- * @param code 微信授权code
- * @param encryptedData 微信加密数据
- * @param iv 加密算法的初始向量
- * @param headers HttpHeaders
- */
- @Override
- public ResponseJson<UserLoginVo> appletsAuthorization(String code, String encryptedData, String iv, HttpHeaders headers) {
- if (StringUtils.isBlank(code)) {
- return ResponseJson.error("没有获取到微信授权code", null);
- }
- // 小程序微信授权获取登录信息
- ResponseJson<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, headers, 1);
- if (appletsInfo.getCode() == -1) {
- return ResponseJson.error(appletsInfo.getMsg(), null);
- }
- Map<String, Object> infoData = appletsInfo.getData();
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- String sessionKey = (String) infoData.get(WeChatService.Keys.SESSION_KEY);
- try {
- if (StringUtils.isEmpty(unionId) || StringUtils.isBlank(unionId)) {
- String result = WeChatService.decrypt(encryptedData, sessionKey, iv, "UTF-8");
- log.info("解密数据>>>>>>" + result);
- Map parseMap = JSONObject.parseObject(result, Map.class);
- assert parseMap != null;
- unionId = parseMap.get(WeChatService.Keys.UNION_ID).toString();
- infoData.put(WeChatService.Keys.UNION_ID, unionId);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return ResponseJson.error("微信解密失败", null);
- }
- // 用户数据存入Redis,key前缀:wxInfo:applets:
- redisService.setMap("wxInfo:applets:" + unionId, infoData);
- log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId);
- // 协销授权登录
- UserLoginVo seller = loginMapper.getServiceProviderUserByOpenId(openId, 0);
- if (null != seller) {
- loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
- String token = JwtUtil.createToken(seller.getUserId());
- seller.setToken(token);
- seller.setUnionId(unionId);
- seller.setOpenId(openId);
- // 生成token给协销用户
- String sellerToken = JwtUtil.createToken(seller.getUserId());
- // 为了过期续签,将token存入redis,并设置超时时间
- redisService.set(sellerToken, sellerToken, JwtUtil.getExpireTime());
- seller.setToken(sellerToken);
- seller.setManager(sellerMapper.findManagerByUser(seller.getUserId()));
- seller.setLeaderId(sellerMapper.findLeaderId(seller.getUserId()));
- // 更新用户openId、unionId 用于后续追踪
- try {
- loginMapper.updateUserInfo(openId, unionId, seller.getUserId());
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ResponseJson.success(seller);
- }
- // 运营人员授权登录
- return operationAuthLogin(openId, unionId, "mini");
- }
- /**
- * 微信授权登录(小程序) 组织
- *
- * @param code 微信授权code
- * @param encryptedData 微信加密数据
- * @param iv 加密算法的初始向量
- * @param headers
- * @return BaseUser
- */
- @Override
- public ResponseJson<UserLoginVo> appletsOrganizeAuthorization(String code, String encryptedData, String iv, HttpHeaders headers) throws ParseException {
- if (StringUtils.isBlank(code)) {
- return ResponseJson.error("没有获取到微信授权code", null);
- }
- // 小程序微信授权获取登录信息
- ResponseJson<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, headers, 3);
- log.info("appletsInfo***" + appletsInfo);
- if (appletsInfo.getCode() == -1) {
- return ResponseJson.error(appletsInfo.getMsg(), null);
- }
- Map<String, Object> infoData = appletsInfo.getData();
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- String sessionKey = (String) infoData.get(WeChatService.Keys.SESSION_KEY);
- try {
- if (StringUtils.isEmpty(unionId) || StringUtils.isBlank(unionId)) {
- String result = WeChatService.decrypt(encryptedData, sessionKey, iv, "UTF-8");
- log.info("解密数据>>>>>>" + result);
- Map parseMap = JSONObject.parseObject(result, Map.class);
- assert parseMap != null;
- unionId = parseMap.get(WeChatService.Keys.UNION_ID).toString();
- infoData.put(WeChatService.Keys.UNION_ID, unionId);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return ResponseJson.error("微信解密失败", null);
- }
- // 用户数据存入Redis,key前缀:wxInfo:applets:
- redisService.setMap("wxInfo:applets:" + unionId, infoData);
- log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId + "operId" + openId);
- // 采购员授权登录
- UserLoginVo seller = loginMapper.getServiceProviderUserByOpenId(openId, 4);
- if (null != seller) {
- loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
- String token = JwtUtil.createToken(seller.getUserId());
- seller.setToken(token);
- seller.setUnionId(unionId);
- seller.setOpenId(openId);
- // 生成token给协销用户
- String sellerToken = JwtUtil.createToken(seller.getUserId());
- // 为了过期续签,将token存入redis,并设置超时时间
- redisService.set(sellerToken, sellerToken, JwtUtil.getExpireTime());
- seller.setToken(sellerToken);
- seller.setManager(sellerMapper.findManagerByUser(seller.getUserId()));
- seller.setLeaderId(sellerMapper.findLeaderId(seller.getUserId()));
- return ResponseJson.success(seller);
- }
- return organizeOperationAuthLogin(openId, unionId, "mini");
- }
- @Override
- public ResponseJson<Map<String, Object>> ClubMessageCount(Integer messageType, Integer commonId) {
- Integer count = messageCenterMapper.Count(1, commonId);
- Integer tradeCount = messageCenterMapper.MessageCount(1, 1, commonId);
- Integer accountCount = messageCenterMapper.MessageCount(1, 2, commonId);
- Integer notificationCount = messageCenterMapper.MessageCount(1, 3, commonId);
- Integer promotionCount = messageCenterMapper.MessageCount(1, 4, commonId);
- Integer infoCount = messageCenterMapper.MessageCount(1, 5, commonId);
- Integer activityCount = messageCenterMapper.MessageCount(1, 6, commonId);
- Map<String, Object> map = new HashMap(10);
- map.put("count", count);
- map.put("tradeCount", tradeCount);
- map.put("account", accountCount);
- map.put("notificationCount", notificationCount);
- map.put("promotionCount", promotionCount);
- map.put("infoCount", infoCount);
- map.put("activityCount", activityCount);
- return ResponseJson.success(map);
- }
- @Override
- public ResponseJson<Map<String, Object>> shopMessageCount(Integer messageType, Integer commonId) {
- Integer count = messageCenterMapper.Count(2, commonId);
- Integer tradeCount = messageCenterMapper.MessageCount(2, 1, commonId);
- Integer accountCount = messageCenterMapper.MessageCount(2, 2, commonId);
- Integer notificationCount = messageCenterMapper.MessageCount(2, 3, commonId);
- Integer promotionCount = messageCenterMapper.MessageCount(2, 4, commonId);
- Map<String, Object> map = new HashMap(10);
- map.put("count", count);
- map.put("tradeCount", tradeCount);
- map.put("accountCount", accountCount);
- map.put("notificationCount", notificationCount);
- map.put("promotionCount", promotionCount);
- return ResponseJson.success(map);
- }
- @Override
- public ResponseJson<Map<String, Object>> SpCount(Integer messageType, Integer commonId) {
- Integer count = messageCenterMapper.Count(3, commonId);
- Integer tradeCount = messageCenterMapper.MessageCount(3, 1, commonId);
- Integer accountCount = messageCenterMapper.MessageCount(3, 2, commonId);
- Integer notificationCount = messageCenterMapper.MessageCount(3, 3, commonId);
- Integer promotionCount = messageCenterMapper.MessageCount(3, 4, commonId);
- Map<String, Object> map = new HashMap(10);
- map.put("count", count);
- map.put("tradeCount", tradeCount);
- map.put("accountCount", accountCount);
- map.put("notificationCount", notificationCount);
- map.put("promotionCount", promotionCount);
- return ResponseJson.success(map);
- }
- @Override
- public ResponseJson<Map<String, Object>> ShopCount(Integer messageType, Integer commonId) {
- Integer count = messageCenterMapper.Count(2, commonId);
- Map<String, Object> map = new HashMap(10);
- map.put("count", count);
- return ResponseJson.success(map);
- }
- @Override
- public ResponseJson<Map<String, Object>> ClubCount(Integer messageType, Integer commonId) {
- Integer count = messageCenterMapper.Count(1, commonId);
- Map<String, Object> map = new HashMap(10);
- map.put("count", count);
- return ResponseJson.success(map);
- }
- @Override
- public Integer deleteMessage(String id) {
- Integer num = 0;
- String[] IdArr = id.split(",");
- for (int i = 0; i < IdArr.length; i++) {
- num = messageCenterMapper.deleteMessage(IdArr[i]);
- }
- return num;
- }
- @Override
- public Integer updateRead(Integer userType, String Id) {
- Integer num = 0;
- String[] IdArr = Id.split(",");
- for (int i = 0; i < IdArr.length; i++) {
- num = messageCenterMapper.updateRead(userType, IdArr[i]);
- }
- return num;
- }
- @Override
- public ResponseJson<PageInfo<MessageCenter>> messageList(Integer commonId, Integer messageType, Integer userType, int pageNum, int pageSize) {
- PageHelper.startPage(pageNum, pageSize);
- List<MessageCenter> list = setMessageList(userType, messageType, commonId);
- PageInfo<MessageCenter> pageData = new PageInfo<>(list);
- return ResponseJson.success(pageData);
- }
- @Override
- public ResponseJson<Map<String, Object>> messageCount(Integer userType , Integer commonId) {
- return ResponseJson.success(setMessageCount(userType,commonId));
- }
- @Override
- public ResponseJson<Map<String, Object>> count(Integer userType,Integer messageType, Integer commonId) {
- Integer count = messageCenterMapper.Count(userType, commonId);
- Map<String, Object> map = new HashMap(1);
- map.put("count", count);
- return ResponseJson.success(map);
- }
- @Override
- public Integer updateMessageAsRead(Integer userType,Integer messageType, Integer commonId) {
- return messageCenterMapper.updateMessageAsRead(userType, messageType, commonId);
- }
- @Override
- public ResponseJson<PageInfo<MessageCenter>> SpMessageList(Integer commonId, Integer messageType, Integer source, int pageNum, int pageSize) {
- PageHelper.startPage(pageNum, pageSize);
- List<MessageCenter> list = messageCenterMapper.MessageList(3, messageType, commonId);
- list.forEach(mess -> {
- if (null != mess.getClubId()) {
- MessageCenter messageCenter = messageCenterMapper.MainImage(3, commonId, mess.getOrderId());
- Integer productCount = messageCenterMapper.productCount(mess.getOrderId());
- if (null != messageCenter) {
- mess.setMainImage(messageCenter.getMainImage());
- mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
- if (messageCenter.getProductName().length() > 10) {
- mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
- } else {
- mess.setProductName(messageCenter.getProductName());
- }
- mess.setProductCount(productCount);
- mess.setRefundType(messageCenter.getRefundType());
- mess.setStatus(messageCenter.getStatus());
- }
- }
- });
- if (source == 2) {
- messageCenterMapper.updateMessageAsRead(3, messageType, commonId);
- }
- PageInfo<MessageCenter> pageData = new PageInfo<>(list);
- return ResponseJson.success(pageData);
- }
- @Override
- public ResponseJson<PageInfo<MessageCenter>> ClubMessageList(Integer commonId, Integer messageType, Integer source, int pageNum, int pageSize) {
- PageHelper.startPage(pageNum, pageSize);
- List<MessageCenter> list = messageCenterMapper.MessageList(1, messageType, commonId);
- list.forEach(mess -> {
- if (null != mess.getClubId()) {
- MessageCenter messageCenter = messageCenterMapper.MainImage(1, commonId, mess.getOrderId());
- Integer productCount = messageCenterMapper.productCount(mess.getOrderId());
- if (null != messageCenter) {
- mess.setMainImage(messageCenter.getMainImage());
- mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
- if (messageCenter.getProductName().length() > 10) {
- mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
- } else {
- mess.setProductName(messageCenter.getProductName());
- }
- mess.setProductCount(productCount);
- mess.setRefundType(messageCenter.getRefundType());
- mess.setStatus(messageCenter.getStatus());
- }
- if (5 == mess.getMessageType() || 6 == mess.getMessageType()) {
- Map<String, String> typeValue = messageCenterMapper.getMessageTypeValues(mess.getMessageType(), mess.getThisId());
- mess.setMainImage(typeValue.get("image"));
- mess.setPcLink(typeValue.get("pcLink"));
- mess.setAppLink(typeValue.get("appLink"));
- }
- }
- });
- if (source == 2) {
- messageCenterMapper.updateMessageAsRead(1, messageType, commonId);
- }
- PageInfo<MessageCenter> pageData = new PageInfo<>(list);
- return ResponseJson.success(pageData);
- }
- @Override
- public ResponseJson<PageInfo<MessageCenter>> shopMessageList(Integer commonId, Integer messageType, Integer source, int pageNum, int pageSize) {
- PageHelper.startPage(pageNum, pageSize);
- List<MessageCenter> list = messageCenterMapper.MessageList(2, messageType, commonId);
- list.forEach(mess -> {
- String shopName = messageCenterMapper.shopName(commonId);
- MessageCenter messageCenter = messageCenterMapper.productImage(mess.getProductId(), commonId);
- if (messageCenter != null) {
- mess.setMainImage(messageCenter.getMainImage());
- mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
- if (messageCenter.getProductName().length() > 10) {
- mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
- } else {
- mess.setProductName(messageCenter.getProductName());
- }
- mess.setProductCount(messageCenter.getProductCount());
- mess.setRefundType(messageCenter.getRefundType());
- }
- mess.setShopName(shopName);
- });
- if (source == 2) {
- messageCenterMapper.updateMessageAsRead(2, messageType, commonId);
- }
- PageInfo<MessageCenter> pageData = new PageInfo<>(list);
- return ResponseJson.success(pageData);
- }
- /**
- * 微信公众号授权链接(www)
- *
- * @param redirectUri 用于微信授权的中间页面
- * @param mode 授权方式:1静默授权,其他手动同意授权
- */
- @Override
- public ResponseJson<String> getAuthorizationLink(String redirectUri, Integer mode) {
- String link = weChatService.getAuthorizationLink(redirectUri, mode);
- String state = UUID.randomUUID().toString();
- redisService.set(state, state, 1800L);
- link = link.replace("STATE", state);
- return ResponseJson.success(link);
- }
- /**
- * 微信公众号授权登录(www)
- * <p>
- * spi旧接口:user/authorizationLogin
- *
- * @param code 微信code
- * @param state 安全认证
- * @param mode 1:静默授权,2:用户手动授权
- * @param headers HttpHeaders
- */
- @Override
- public ResponseJson<UserLoginVo> websiteAuthorization(String code, String state, Integer mode, HttpHeaders headers) {
- if (StringUtils.isBlank(code) || StringUtils.isBlank(state)) {
- return ResponseJson.error("参数异常:微信code和state不能为空!", null);
- }
- String wxState = (String) redisService.get(state);
- log.info("微信code>>>" + code + "state>>>" + wxState + "----" + state + "mode>>>" + mode);
- if (wxState.equals(state)) {
- try {
- // 通过code获取微信用户信息
- Map<String, Object> map = weChatService.getInfoMapByWeb(code, "crm");
- String openId = (String) map.get(WeChatService.Keys.OPEN_ID);
- if (mode == 1) {
- // 静默授权
- Integer userId = loginMapper.getUserIdByOpenId(openId, "www");
- if (null != userId && userId > 0) {
- UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
- // 返回登录用户
- return logonVerify(user);
- } else {
- return ResponseJson.error(-4, "您的微信尚未绑定任何机构", null);
- }
- } else {
- // 获取access_token
- String accessToken = weChatService.getAccessToken();
- // 获取微信用户信息
- Map<String, Object> infoData = weChatService.getUserInfo(accessToken, openId);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- // 用户数据存入Redis,key前缀:wxInfo:website:
- redisService.setMap("wxInfo:website:" + unionId, infoData);
- log.info("移动端授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:website:" + unionId);
- // 运营人员授权登录
- return operationAuthLogin(openId, unionId, "www");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return ResponseJson.error("获取微信信息异常", null);
- }
- }
- return ResponseJson.error("请从正确的途径打开链接", null);
- }
- /**
- * 运营人员授权登录
- *
- * @param openId 微信openId
- * @param unionId 微信unionId
- * @param source 来源:www网站, mini小程序
- * @return BaseUser
- */
- private ResponseJson<UserLoginVo> operationAuthLogin(String openId, String unionId, String source) {
- UserLoginVo operation = loginMapper.getLoginUserByUnionId(unionId, source);
- if (null == operation) {
- operation = loginMapper.getLoginUserByOpenId(openId, source);
- if (null == operation) {
- operation = new UserLoginVo();
- operation.setOpenId(openId);
- operation.setUnionId(unionId);
- return ResponseJson.error(-4, "您的微信尚未绑定任何机构", operation);
- } else {
- // 表示 openId存在, unionId不存在
- operationMapper.updateOperationUnionId(operation.getUserId(), unionId);
- }
- }
- // 如果unionId存在, openId不存在
- if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
- operationMapper.updateOperationOpenId(operation.getUserId(), openId);
- }
- operation.setOpenId(openId);
- operation.setUnionId(unionId);
- // 返回登录校验结果
- return logonVerify(operation);
- }
- /**
- * 运营人员授权登录 -- 组织
- *
- * @param openId 微信openId
- * @param unionId 微信unionId
- * @param source 来源:www网站, mini小程序
- * @return BaseUser
- */
- private ResponseJson<UserLoginVo> organizeOperationAuthLogin(String openId, String unionId, String source) {
- UserLoginVo operation = loginMapper.getOrganizeLoginUserByUnionId(unionId, source);
- if (null == operation) {
- operation = loginMapper.getOrganizeLoginUserByOpenId(openId, source);
- if (null == operation) {
- operation = new UserLoginVo();
- operation.setOpenId(openId);
- operation.setUnionId(unionId);
- return ResponseJson.error(-4, "您的微信尚未绑定任何机构", operation);
- } else {
- // 表示 openId存在, unionId不存在
- operationMapper.updateOperationUnionId(operation.getUserId(), unionId);
- }
- }
- // 如果unionId存在, openId不存在
- if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
- operationMapper.updateOperationOpenId(operation.getUserId(), openId);
- }
- operation.setOpenId(openId);
- operation.setUnionId(unionId);
- // 审核未通过 待审核状态
- if (1 == operation.getClubStatus() || 92 == operation.getClubStatus()) {
- return ResponseJson.error(-1, "登陆失败", operation);
- }
- // 返回登录校验结果
- return logonVerify(operation);
- }
- /**
- * 登录校验
- *
- * @param loginUser 用户信息
- * @return UserLoginVo
- */
- private ResponseJson<UserLoginVo> logonVerify(UserLoginVo loginUser) {
- // 生成token给用户
- String token = JwtUtil.createToken(loginUser.getUserId());
- // 为了过期续签,将token存入redis,并设置超时时间
- redisService.set(token, token, JwtUtil.getExpireTime());
- loginUser.setToken(token);
- if (null != loginUser.getClubStatus() && 91 == loginUser.getClubStatus()) {
- //机构
- return ResponseJson.error(-1, "您的企业账号已被冻结,请联系客服处理", loginUser);
- }
- // 供应商
- if (null != loginUser.getShopStatus() && null != loginUser.getUserIdentity() && 3 == loginUser.getUserIdentity()) {
- if (3 == loginUser.getShopStatus()) {
- return ResponseJson.error(-1, "您的企业账号正在加速审核中,审核通过后即可登录", loginUser);
- }
- if (91 == loginUser.getShopStatus()) {
- return ResponseJson.error(-1, "您的企业账号已被冻结,请联系客服处理", loginUser);
- }
- if (92 == loginUser.getShopStatus()) {
- return ResponseJson.error(-3, "您的企业账号审核未通过", loginUser);
- }
- // 是否可为机构下单
- boolean flag = false;
- // 供应商商品上架平台信息
- List<String> shopProductList = loginMapper.getShopProductGroudMall(loginUser.getShopId());
- if (null != shopProductList) {
- for (String product : shopProductList) {
- if (StringUtils.isNotBlank(product)) {
- if (product.contains("4")) {
- flag = true;
- }
- }
- }
- } else {
- flag = false;
- }
- loginUser.setShopIsOrder(flag);
- }
- // 会员机构类型:1医美,2生
- if (null != loginUser.getUserIdentity() && loginUser.getUserIdentity() == 2) {
- Integer clubType = loginMapper.getClubTypeById(loginUser.getUserId());
- loginUser.setFirstClubType(clubType);
- }
- // 超级会员
- SuperVipDto end = findEnd(loginUser.getUserId());
- loginUser.setVipFlag(end.getVipFlag());
- if (loginUser.getUserId() != null) {
- if (loginUser.getClubId() != null && loginUser.getClubId() != 1342) {
- loginUser.setSpUserId(loginMapper.getClubSpUserId(loginUser.getClubId()));
- }
- Integer shopID = messageCenterMapper.shopID(loginUser.getUserId());
- Integer newReceiptType = messageCenterMapper.newReceiptType(shopID);
- // 上架费次数
- Integer listingFeeCount = messageCenterMapper.listingFee(shopID);
- // 上架费有效期内个数
- Integer listingfeeExpireCount = messageCenterMapper.listingfeeExpire(shopID);
- // 有免除上架费的次数
- Integer receStatctCount = messageCenterMapper.receStatct(shopID);
- Integer listingFee = 0;
- // 如果上架费次数为 0 ;所有上架费都不在在生效时间; 没有免除过上架费;
- if (listingFeeCount > 0 && listingfeeExpireCount == 0) {
- if (receStatctCount <= 0) {
- listingFee = 1;
- }
- }
- loginUser.setListingFee(listingFee);
- }
- // 更新用户openId、unionId 用于后续追踪
- try {
- loginMapper.updateUserInfo(loginUser.getOpenId(), loginUser.getUnionId(), loginUser.getUserId());
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 登录异步赠送10采美豆
- asyncService.loginUpdateBeans(loginUser.getUserId(), loginUser.getUserIdentity());
- loginUser.setManager(sellerMapper.findManagerByUser(loginUser.getUserId()));
- loginUser.setLeaderId(sellerMapper.findLeaderId(loginUser.getUserId()));
- log.info("【登录】-----登录成功,userId:" + loginUser.getUserId());
- return ResponseJson.success("登录成功", loginUser);
- }
- /**
- * 获取生成微信二维码的参数(www)
- *
- * @return Map<String, Object>
- */
- @Override
- public ResponseJson<Map<String, String>> getAuthParameters() {
- UUID state = UUID.randomUUID();
- Map<String, String> dataMap = new HashMap<>(3);
- dataMap.put("appId", weChatService.getAppId());
- dataMap.put("redirectUri", weChatService.getRedirectUri());
- dataMap.put("state", String.valueOf(state));
- redisService.set("state:" + state, String.valueOf(state), 1800L);
- return ResponseJson.success(dataMap);
- }
- /**
- * 微信用户扫码,微信服务器回调
- *
- * @param code 微信code
- * @param state 安全认证key(上一步获取参数时自定义生成的uuid)
- */
- @Override
- public void qrCodeAuthScan(String code, String state) {
- String errorMsg = "";
- // 简单验证,防止csrf攻击(跨站请求伪造攻击)
- String stateCache = (String) redisService.get("state:" + state);
- if (StringUtils.isBlank(stateCache) || "null".equals(stateCache)) {
- errorMsg = "请从正确的途径打开链接";
- }
- if (StringUtils.isEmpty(code)) {
- errorMsg = "请重新进行授权登录";
- }
- try {
- // 用code换取access_token
- Map<String, Object> tokenMap = weChatService.getInfoMapByWeb(code, "pc");
- String accessToken = (String) tokenMap.get("access_token");
- String openId = (String) tokenMap.get(WeChatService.Keys.OPEN_ID);
- log.info(">>>>>(code换取access_token)wx回调openId:" + openId + " ,accessToken:" + accessToken);
- // 用access_token获取微信用户信息
- Map<String, Object> infoData = weChatService.getUserInfoByWeb(accessToken, openId);
- log.info(">>>>>(用access_token获取用户信息)wx回调openId:" + infoData.get(WeChatService.Keys.OPEN_ID) + " ,unionId:" + infoData.get(WeChatService.Keys.UNION_ID));
- // 微信用户信息存入redis
- redisService.setMap("scan:" + state, infoData);
- } catch (Exception e) {
- errorMsg = "获取微信用户信息失败";
- }
- // 错误信息存入Redis
- redisService.set("error:" + state, errorMsg, 1800L);
- }
- /**
- * 校验扫码结果
- *
- * @param state 安全认证key(第一步获取参数时自定义生成的uuid)
- * @return UserLoginVo
- */
- @Override
- public ResponseJson<UserLoginVo> qrCodeAuthScanResult(String state) throws ParseException {
- if (StringUtils.isBlank(state)) {
- return ResponseJson.error("参数异常:state不能为空!", null);
- }
- String errorMsg = (String) redisService.get("error:" + state);
- if (StringUtils.isNotEmpty(errorMsg) && !"null".equals(errorMsg)) {
- return ResponseJson.error(errorMsg, null);
- }
- Map<Object, Object> infoData = redisService.getEntries("scan:" + state);
- if (null == infoData || infoData.size() == 0) {
- return ResponseJson.error(-90, "redis缓存的扫码数据没有拿到", null);
- }
- // 清除redis的扫码数据
- redisService.remove("scan:" + state);
- String unionId = (String) infoData.get(WeChatService.Keys.UNION_ID);
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- log.info(">>>>>>pc商城unionId:" + unionId + " ,openId:" + openId);
- Integer userId = messageCenterMapper.userId(openId);
- Integer clubId = messageCenterMapper.clubIdCule(userId);
- Integer savedCount = null;
- if (userId != null) {
- Integer shopID = messageCenterMapper.shopID(userId);
- Integer newReceiptType = messageCenterMapper.newReceiptType(shopID);
- if (shopID == null) {
- savedCount = messageCenterMapper.Count(1, clubId);
- System.out.println(savedCount);
- } else {
- savedCount = messageCenterMapper.Count(2, shopID);
- Integer listingFeeCount = messageCenterMapper.listingFee(shopID);
- Integer listingfeeExpireCount = messageCenterMapper.listingfeeExpire(shopID);
- Integer receStatctCount = messageCenterMapper.receStatct(shopID);
- Integer listingFee = 0;
- if (listingFeeCount > 0 && listingfeeExpireCount > 0) {
- if (receStatctCount <= 0) {
- listingFee = 1;
- }
- }
- }
- }
- // 用户数据存入Redis,key前缀:wxInfo:website:
- String infoDataStr = JSON.toJSONString(infoData);
- Map<String, Object> infoDataMap = JSON.parseObject(infoDataStr);
- redisService.setMap("wxInfo:website:" + unionId, infoDataMap);
- log.info("微信扫码登录,用户数据存入Redis,key:wxInfo:website:" + unionId);
- // 运营人员授权登录
- return operationAuthLogin(openId, unionId, "www");
- }
- /**
- * 微信扫码后,绑定机构账号
- *
- * @param scanBindDto {
- * mobileOrEmail 手机号或邮箱
- * password 密码
- * mobile 手机号
- * smsCode 手机验证码
- * linkName 联系人
- * passOrNote 0 密码验证 1 短信
- * }
- */
- @Override
- public ResponseJson<UserLoginVo> qrCodeAuthScanBind(ScanBindDto scanBindDto) {
- String mobileOrEmail = null;
- if (!ObjectUtils.isEmpty(scanBindDto.getMobileOrEmail())) {
- mobileOrEmail = scanBindDto.getMobileOrEmail();
- }
- String password = null;
- if (!ObjectUtils.isEmpty(scanBindDto.getPassword())) {
- password = scanBindDto.getPassword();
- }
- String mobile = scanBindDto.getMobile();
- String smsCode = scanBindDto.getSmsCode();
- String linkName = scanBindDto.getLinkName();
- String unionId = scanBindDto.getUnionId();
- // 0 密码验证 1 短信
- String passOrNote = "0";
- if (!ObjectUtils.isEmpty(scanBindDto.getPassOrNote())) {
- passOrNote = scanBindDto.getPassOrNote();
- }
- String companyMobile = null;
- if (!ObjectUtils.isEmpty(scanBindDto.getPassOrNote())) {
- companyMobile = scanBindDto.getCompanyMobile();
- }
- // 参数校验
- if ("0".equals(passOrNote)) {
- if (StringUtils.isBlank(mobileOrEmail)) {
- return ResponseJson.error("参数异常:手机号或邮箱不能为空!", null);
- }
- if (StringUtils.isBlank(password)) {
- return ResponseJson.error("参数异常:密码不能为空!", null);
- }
- }
- if (StringUtils.isBlank(mobile)) {
- return ResponseJson.error("参数异常:手机号不能为空!", null);
- }
- if (StringUtils.isBlank(smsCode)) {
- return ResponseJson.error("参数异常:短信验证码不能为空!", null);
- }
- if (StringUtils.isBlank(unionId)) {
- return ResponseJson.error("参数异常:unionId不能为空!", null);
- }
- //处理比对密码
- if ("0".equals(passOrNote)) {
- mobileOrEmail = mobileOrEmail;
- }
- if ("1".equals(passOrNote)) {
- mobileOrEmail = companyMobile;
- }
- // 根据手机号或者邮箱获取账户信息
- UserLoginVo user = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
- String md5Password = null;
- boolean item = false;
- // 账号密码验证、短信验证校验方式
- if ("0".equals(passOrNote)) {
- md5Password = Md5Util.md5(password);
- item = md5Password.equals(user.getPassword());
- }
- if ("1".equals(passOrNote)) {
- item = true;
- }
- if (null != user && item) {
- // 查询使用该手机号的运营人员或用户
- String checkRust = commonService.operationBindCheck(mobile, smsCode, 0);
- if (checkRust != null) {
- return ResponseJson.error(checkRust, null);
- }
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:website:" + unionId);
- log.info("扫码绑定微信, 获取unionId>>>>>>" + unionId);
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- String nickName = (String) infoData.get("nickname");
- String avatarUrl = (String) infoData.get("headimgurl");
- // 判断微信是否已经绑定
- UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "www");
- if (operationByUnionId != null) {
- return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
- }
- /*
- 组装运营人员数据 operation
- */
- OperationPo operation = new OperationPo();
- // 用户Id
- operation.setUserId(user.getUserId());
- // 手机号
- operation.setMobile(mobile);
- operation.setLinkName(linkName);
- // 微信昵称头像
- operation.setNickName(nickName);
- operation.setAvatarUrl(avatarUrl);
- // unionId,openId
- operation.setUnionId(unionId);
- operation.setPcOpenId(openId);
- // 组织机构0
- operation.setOrganizeId(0);
- // 绑定的机构/供应商Id,绑定的用户类型
- if (3 == user.getUserIdentity()) {
- operation.setShopId(user.getShopId());
- operation.setUserType(2);
- } else {
- operation.setClubId(user.getClubId());
- operation.setUserType(1);
- }
- // 绑定状态
- operation.setStatus(2);
- // 删除标识
- operation.setDelFlag(0);
- Date time = new Date();
- // 添加时间
- operation.setAddTime(time);
- // 绑定时间
- operation.setBindTime(time);
- // 更新时间
- operation.setUpdateTime(time);
- /*
- 保存数据库 operation
- */
- registerMapper.insertOperation(operation);
- return logonVerify(user);
- }
- return ResponseJson.error("输入的密码和账户名不匹配", null);
- }
- /**
- * 邀请码登录
- *
- * @param invitationCode 邀请码
- * @param nickName 微信昵称
- * @param avatarUrl 微信头像(headimgurl)
- * @param unionId 微信unionId
- * @return UserLoginVo
- */
- @Override
- public ResponseJson<UserLoginVo> invitationCodeLogin(String invitationCode, String nickName, String avatarUrl, String unionId) {
- // 参数校验
- if (StringUtils.isBlank(invitationCode)) {
- return ResponseJson.error("邀请码不能为空", null);
- }
- UserLoginVo operation = loginMapper.getOperationUserByInvitationCode(invitationCode, 0);
- if (operation == null) {
- return ResponseJson.error("邀请码错误", null);
- }
- Date date = new Date();
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(operation.getInvitationCodeTime());
- calendar.add(Calendar.DATE, validTime);
- if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
- return ResponseJson.error("邀请码已过期,请联系邀请人更新邀请码", null);
- }
- if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
- return ResponseJson.error("邀请码已被使用", null);
- }
- // 用户身份:1机构,2供应商
- int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
- if (1 == userIdentity && operation.getClubStatus() != null && 91 == operation.getClubStatus()) {
- return ResponseJson.error("您的机构已下线", null);
- }
- if (2 == userIdentity && operation.getShopStatus() != null && 91 == operation.getShopStatus()) {
- return ResponseJson.error("您的企业账号已下线,请联系客服处理", null);
- }
- if (0 != operation.getDelFlag()) {
- return ResponseJson.error("您的邀请码已被删除,请重新添加运营人员", null);
- }
- OperationPo operationPo = new OperationPo();
- operationPo.setId(operation.getOperationId());
- // 微信unionId
- operationPo.setUnionId(unionId);
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
- // 微信openId
- operationPo.setOpenId((String) infoData.get(WeChatService.Keys.OPEN_ID));
- // 微信昵称
- operationPo.setNickName(nickName);
- // 微信头像
- operationPo.setAvatarUrl(avatarUrl);
- // 绑定状态,1未绑定,2已绑定
- operationPo.setStatus(2);
- // 采美进行消息推送
- if (0 == operation.getOrganizeId()) {
- if (1 == userIdentity) {
- // 机构Id
- operationPo.setClubId(operation.getClubId());
- // 用户类型
- operationPo.setUserType(1);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String current = dateFormat.format(new Date());
- MessageCenter messageCenter = new MessageCenter();
- messageCenter.setShopId(null);
- messageCenter.setClubId(operation.getClubId());
- messageCenter.setUserType(1);
- messageCenter.setMessageType(2);
- messageCenter.setAccountType(7);
- messageCenter.setContent(nickName);
- messageCenter.setTime(current);
- messageCenterMapper.addMessageCenter(messageCenter);
- //发短信
- String message = "【采美365】恭喜您成功成为运营人员,您可通过微信直接登录采美商城进行采购。";
- String mobile = messageCenterMapper.contractMobile(operation.getClubId());
- if (mobile != null && mobile != "") {
- remoteCallService.remoteSendSms(2, 3, mobile, message);
- }
- try {
- log.info("***********机构通过邀请码登入微信公众号推送************");
- String accessToken = weChatService.getAccessToken();
- String openid = messageCenterMapper.getOpenidListByPermission(unionId);
- String time = current;
- String remarkText = "绑定成功后,您可通过微信直接访问采美商城进行采购。";
- // 跳转到【小程序付款-选择支付方式页面】
- String pagePath = "https://www.caimei365.com/";
- // sendTemplateMsg(openid, 标题, 金额, 收款日期, 备注, 跳转链接
- log.error("获取openid>>>>>" + openid);
- weChatService.sendTemplateMessg(accessToken, openid, nickName, time, remarkText, pagePath);
- } catch (Exception e) {
- log.error("【机构通过邀请码登入通知】获取微信公众号access_token异常!", e);
- }
- } else {
- // 供应商Id
- operationPo.setShopId(operation.getShopId());
- // 用户类型
- operationPo.setUserType(2);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String current = dateFormat.format(new Date());
- MessageCenter messageCenter = new MessageCenter();
- messageCenter.setShopId(operation.getShopId());
- messageCenter.setClubId(null);
- messageCenter.setUserType(2);
- messageCenter.setMessageType(2);
- messageCenter.setShopMessType(2);
- messageCenter.setContent(nickName);
- messageCenter.setTime(current);
- messageCenterMapper.addMessageCenter(messageCenter);
- //发短信
- String message = "【采美365】恭喜您成功成为运营人员,您可通过微信直接登录采美商城进行采购。";
- String mobile = messageCenterMapper.contractMobiles(operation.getShopId());
- if (mobile != null && mobile != "") {
- remoteCallService.remoteSendSms(2, 3, mobile, message);
- }
- try {
- log.info("***********供应商通过邀请码登入微信公众号推送************");
- String accessToken = weChatService.getAccessToken();
- String openid = messageCenterMapper.getOpenidListByPermission(unionId);
- String time = current;
- String remarkText = "绑定成功后,您可通过微信直接访问采美商城进行采购。";
- // 跳转到【小程序付款-选择支付方式页面】
- String pagePath = "https://www.caimei365.com/";
- // sendTemplateMsg(openid, 标题, 金额, 收款日期, 备注, 跳转链接
- log.error("获取openid>>>>>" + openid);
- weChatService.sendTemplateMessg(accessToken, openid, nickName, time, remarkText, pagePath);
- } catch (Exception e) {
- log.error("【供应商通过邀请码登入通知】获取微信公众号access_token异常!", e);
- }
- }
- } else {
- // 机构Id
- operationPo.setClubId(operation.getClubId());
- // 用户类型
- operationPo.setUserType(1);
- }
- operationPo.setUpdateTime(new Date());
- operationPo.setBindTime(new Date());
- // 更新运营人员信息
- operationMapper.updateOperationByInvitation(operationPo);
- // 返回登录校验结果
- return logonVerify(operation);
- }
- /**
- * 邀请码登录
- *
- * @param invitationCode 邀请码
- * @param nickName 微信昵称
- * @param avatarUrl 微信头像(headimgurl)
- * @param unionId 微信unionId
- * @param organizeId 组织Id
- * @return UserLoginVo
- */
- @Override
- public ResponseJson<UserLoginVo> invitationCodeOrganizeLogin(String invitationCode, String nickName, String avatarUrl, String unionId, Integer organizeId) {
- // 参数校验
- if (StringUtils.isBlank(invitationCode)) {
- return ResponseJson.error("邀请码不能为空", null);
- }
- UserLoginVo operation = loginMapper.getOperationOrganizeUserByInvitationCode(invitationCode, organizeId);
- if (operation == null) {
- return ResponseJson.error("邀请码错误", null);
- }
- Date date = new Date();
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(operation.getInvitationCodeTime());
- calendar.add(Calendar.DATE, validTime);
- if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
- return ResponseJson.error("邀请码已过期,请联系邀请人更新邀请码", null);
- }
- if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
- return ResponseJson.error("邀请码已被使用", null);
- }
- // 用户身份:1机构,2供应商
- int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
- if (1 == userIdentity && operation.getClubStatus() != null && 91 == operation.getClubStatus()) {
- return ResponseJson.error("您的机构已冻结", null);
- }
- if (2 == userIdentity && operation.getShopStatus() != null && 91 == operation.getShopStatus()) {
- return ResponseJson.error("您的企业账号已被冻结,请联系客服处理", null);
- }
- if (0 != operation.getDelFlag()) {
- return ResponseJson.error("您的邀请码已被删除,请重新添加运营人员", null);
- }
- OperationPo operationPo = new OperationPo();
- operationPo.setId(operation.getOperationId());
- // 微信unionId
- operationPo.setUnionId(unionId);
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
- // 微信openId
- operationPo.setOpenId((String) infoData.get(WeChatService.Keys.OPEN_ID));
- // 微信昵称
- operationPo.setNickName(nickName);
- // 微信头像
- operationPo.setAvatarUrl(avatarUrl);
- // 绑定状态,1未绑定,2已绑定
- operationPo.setStatus(2);
- // 联合丽格没有运营人员为机构运营人员
- // 机构Id
- operationPo.setClubId(operation.getClubId());
- // 用户类型
- operationPo.setUserType(1);
- operationPo.setUpdateTime(new Date());
- operationPo.setBindTime(new Date());
- // 更新运营人员信息
- operationMapper.updateOperationByInvitation(operationPo);
- // 返回登录校验结果
- return logonVerify(operation);
- }
- /**
- * 运营人员绑定微信
- *
- * @param authBindDto {
- * userId 要绑定的用户Id(userID)
- * mobile 手机号
- * smsCode 手机验证码(verificationCode)
- * unionId 微信unionId
- * nickName 微信昵称
- * avatarUrl 微信头像(headimgurl)
- * }
- * @return OperationPo
- */
- @Override
- public ResponseJson<UserLoginVo> operationBindWeChat(AuthBindDto authBindDto) {
- Integer userId = authBindDto.getUserId();
- String mobile = authBindDto.getMobile();
- String smsCode = authBindDto.getSmsCode();
- String linkName = authBindDto.getLinkName();
- String unionId = authBindDto.getUnionId();
- String nickName = authBindDto.getNickName();
- String avatarUrl = authBindDto.getAvatarUrl();
- String isCheckSmsCode = authBindDto.getIsCheckSmsCode();
- // 参数校验
- if (null == userId) {
- return ResponseJson.error("参数异常:用户Id不能为空!", null);
- }
- if (StringUtils.isBlank(mobile)) {
- return ResponseJson.error("参数异常:手机号不能为空!", null);
- }
- boolean b = StringUtils.isBlank(isCheckSmsCode) || "0".equals(isCheckSmsCode);
- if (b && StringUtils.isBlank(smsCode)) {
- return ResponseJson.error("参数异常:短信验证码不能为空!", null);
- }
- if (StringUtils.isBlank(unionId)) {
- return ResponseJson.error("参数异常:unionId不能为空!", null);
- }
- // 查询使用该手机号的运营人员或用户
- String checkRust = commonService.operationBindCheck(mobile, smsCode, 0);
- if (checkRust != null) {
- return ResponseJson.error(checkRust, null);
- }
- Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
- log.info("绑定微信bindingWx,获取unionId>>>>>>" + unionId);
- String openId = (String) infoData.get(WeChatService.Keys.OPEN_ID);
- // 判断微信是否已经绑定
- UserLoginVo operationByUnionId = loginMapper.getLoginUserByUnionId(unionId, "mini");
- if (operationByUnionId != null) {
- return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
- }
- // 要绑定的用户
- UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
- /*
- 组装运营人员数据 operation
- */
- OperationPo operation = new OperationPo();
- // 用户Id
- operation.setUserId(userId);
- // 手机号
- operation.setMobile(mobile);
- operation.setLinkName(linkName);
- // 微信昵称头像
- operation.setNickName(nickName);
- operation.setAvatarUrl(avatarUrl);
- // unionId,openId
- operation.setUnionId(unionId);
- operation.setOpenId(openId);
- // 组织机构0
- operation.setOrganizeId(0);
- // 绑定的机构/供应商Id,绑定的用户类型
- if (user != null && 3 == user.getUserIdentity()) {
- operation.setShopId(user.getShopId());
- operation.setUserType(2);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String current = dateFormat.format(new Date());
- MessageCenter messageCenter = new MessageCenter();
- messageCenter.setShopId(operation.getShopId());
- messageCenter.setClubId(null);
- messageCenter.setUserType(2);
- messageCenter.setMessageType(2);
- messageCenter.setShopMessType(2);
- messageCenter.setContent(nickName);
- messageCenter.setShopMessType(null);
- messageCenter.setTime(current);
- messageCenterMapper.addMessageCenter(messageCenter);
- } else if (user != null) {
- operation.setClubId(user.getClubId());
- operation.setUserType(1);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String current = dateFormat.format(new Date());
- MessageCenter messageCenter = new MessageCenter();
- messageCenter.setShopId(null);
- messageCenter.setClubId(user.getClubId());
- messageCenter.setUserType(1);
- messageCenter.setMessageType(2);
- messageCenter.setAccountType(7);
- messageCenter.setContent(nickName);
- messageCenter.setTime(current);
- messageCenterMapper.addMessageCenter(messageCenter);
- }
- // 绑定状态
- operation.setStatus(2);
- // 删除标识
- operation.setDelFlag(0);
- Date time = new Date();
- // 添加时间
- operation.setAddTime(time);
- // 绑定时间
- operation.setBindTime(time);
- // 更新时间
- operation.setUpdateTime(time);
- /*
- 保存数据库 operation
- */
- registerMapper.insertOperation(operation);
- return ResponseJson.success("绑定微信成功", user);
- }
- /**
- * 绑定运营人员短信验证
- *
- * @param loginCodeDto
- * @return
- */
- @Override
- public ResponseJson<String> operateVerification(LoginCodeDto loginCodeDto) {
- // 验证码或者账号密码
- String codeType = null;
- if (!ObjectUtils.isEmpty(loginCodeDto.getCodeType())) {
- codeType = loginCodeDto.getCodeType();
- }
- // 账号密码验证
- if ("0".equals(codeType)) {
- if (ObjectUtils.isEmpty(loginCodeDto.getMobileOrEmail())) {
- return ResponseJson.error("请输入账号", null);
- }
- if (ObjectUtils.isEmpty(loginCodeDto.getPassword())) {
- return ResponseJson.error("请输入密码", null);
- }
- // 账号
- String mobileOrEmail = loginCodeDto.getMobileOrEmail();
- // 密码
- String password = loginCodeDto.getPassword();
- // 根据手机号或者邮箱获取账户信息
- UserLoginVo user = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
- // 账号密码验证
- String md5Password = Md5Util.md5(password);
- if (md5Password.equals(user.getPassword())) {
- return ResponseJson.success("账号与密码匹配成功");
- }
- return ResponseJson.error("账号与密码不匹配,请检查后重新输入");
- } else {
- // 手机短信验证
- if (ObjectUtils.isEmpty(loginCodeDto.getMobile())) {
- return ResponseJson.error("请输入手机号", null);
- }
- if (ObjectUtils.isEmpty(loginCodeDto.getCode())) {
- return ResponseJson.error("请输入验证码", null);
- }
- String mobile = loginCodeDto.getMobile();
- String code = loginCodeDto.getCode();
- String result = ValidateUtil.validateMobile(mobile);
- if (result != null) {
- return ResponseJson.error(result);
- }
- // 判断redis中是否存在
- boolean exists = redisService.exists("code:" + mobile);
- if (exists) {
- // 校验验证码是否过期
- long expireTime = redisService.getExpireTime("code:" + mobile);
- if (expireTime < 0) {
- return ResponseJson.error("验证码已失效,请重新获取");
- }
- // 获取redis缓存验证码
- Object randomCode = redisService.get("code:" + mobile);
- if (!ObjectUtils.isEmpty(randomCode)) {
- if (code.equals(randomCode.toString())) {
- redisService.remove("code:" + mobile);
- return ResponseJson.success("验证码匹配成功");
- } else {
- return ResponseJson.error("验证码不匹配,请重新输入");
- }
- } else {
- return ResponseJson.error("验证码错误,请重新获取");
- }
- } else {
- return ResponseJson.error("验证码错误,请重新获取");
- }
- }
- }
- @Override
- public Integer updateMessageCenter(MessageCenter messageCenter) {
- return messageCenterMapper.updateMessageCenter(messageCenter);
- }
- @Override
- public Integer insertMessageCenter(MessageCenter messageCenter) {
- return messageCenterMapper.insertMessageCenter(messageCenter);
- }
- @Override
- public List<Integer> getMessageCenterListDay(Integer day) {
- return messageCenterMapper.getMessageCenterListDay(day);
- }
- /**
- * 根据userId查是否过期,返回dto对象,flag=0未买过,-1过期,1有效,endTime过期时间
- */
- private SuperVipDto findEnd(Integer userId) {
- SuperVipPo superVip = vipMapper.findSuperVip(userId);
- SuperVipDto superVipDto = new SuperVipDto();
- if (superVip == null) {
- superVipDto.setVipFlag(0);
- } else {
- SuperVipPo endTime = vipMapper.findEndTime(userId);
- if (endTime == null) {
- superVipDto.setVipFlag(-1);
- superVipDto.setEndTime(superVip.getEndTime());
- } else {
- superVipDto.setVipFlag(1);
- superVipDto.setEndTime(endTime.getEndTime());
- }
- }
- return superVipDto;
- }
- /**
- * 初始化站内信
- */
- private List<MessageCenter> setMessageList(Integer userType,Integer messageType,Integer commonId) {
- List<MessageCenter> list = messageCenterMapper.MessageList(userType, messageType, commonId);
- list.forEach(mess -> {
- if (null != mess.getOrderId()) {
- MessageCenter messageCenter = messageCenterMapper.MainImage(userType, commonId, mess.getOrderId());
- Integer productCount = messageCenterMapper.productCount(mess.getOrderId());
- if (null != messageCenter) {
- mess.setMainImage(messageCenter.getMainImage());
- mess.setOnlinePayFlag(messageCenter.getOnlinePayFlag());
- if (messageCenter.getProductName().length() > 10) {
- mess.setProductName(StringUtils.strip(messageCenter.getProductName().substring(0, 11)));
- } else {
- mess.setProductName(messageCenter.getProductName());
- }
- mess.setProductCount(productCount);
- mess.setRefundType(messageCenter.getRefundType());
- mess.setStatus(messageCenter.getStatus());
- }
- }
- });
- return list;
- }
- private Map<String, Object> setMessageCount(Integer userType, Integer commonId) {
- Integer count = messageCenterMapper.Count(userType, commonId);
- Integer tradeCount = messageCenterMapper.MessageCount(userType, 1, commonId);
- Integer accountCount = messageCenterMapper.MessageCount(userType, 2, commonId);
- Integer notificationCount = messageCenterMapper.MessageCount(userType, 3, commonId);
- Integer promotionCount = messageCenterMapper.MessageCount(userType, 4, commonId);
- Map<String, Object> map = new HashMap(5);
- map.put("count", count);
- map.put("tradeCount", tradeCount);
- map.put("accountCount", accountCount);
- map.put("notificationCount", notificationCount);
- map.put("promotionCount", promotionCount);
- return map;
- }
- }
|