package cn.huge.module.pauser.service; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.huge.base.common.exception.ServiceException; import cn.huge.base.common.utils.*; import cn.huge.module.client.api.impl.UtilsClientImpl; import cn.huge.module.constant.BaseConsts; import cn.huge.module.cust.constant.UserBaseConsts; import cn.huge.module.pauser.dao.mapper.PaAccountMapper; import cn.huge.module.pauser.domain.po.PaAccount; import cn.huge.module.pauser.domain.po.PaUser; import cn.huge.module.oper.dto.SeCustWechatDTO; import cn.huge.module.pauser.dto.WeChatEmpowerDTO; import cn.huge.module.pauser.dto.WeChatLoginDTO; import cn.huge.module.utils.JwtUtils; import cn.huge.module.wechat.miniapp.service.WeixinMaService; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.common.collect.Maps; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.DigestUtils; import java.util.Date; import java.util.List; import java.util.Map; /** * @title: 公众用户账号表业务逻辑处理 * @Description 公众用户账号表业务逻辑处理 * @company hugeinfo * @author liyj * @Time 2024-08-19 20:04:19 * @version 1.0.0 */ @Slf4j @Service @Transactional(rollbackFor = Exception.class) public class PaAccountService extends ServiceImpl{ @Autowired private PaAccountMapper mapper; @Autowired private UtilsClientImpl utilsClient; @Autowired private WeixinMaService weixinMaService; @Autowired private WxMaService wxMaService; @Autowired private PaUserService paUserService; /** * 更新对象 * @param entity 对象 */ public void updatePaAccount(PaAccount entity){ try{ mapper.updatePaAccount(entity); }catch (Exception e){ log.error("[PaAccountService.updatePaAccount]调用失败,异常信息:"+e, e); throw new ServiceException("PaAccountService.updatePaAccount", e); } } /** * 条件更新对象 * @param entity 对象 * @param terms 条件 */ public void updatePaAccountTerms(PaAccount entity, Map terms){ try{ mapper.updatePaAccountTerms(entity, terms); }catch (Exception e){ log.error("[PaAccountService.updatePaAccountTerms]调用失败,异常信息:"+e, e); throw new ServiceException("PaAccountService.updatePaAccountTerms", e); } } /** * 根据编号物理删除 * @param id 查询条件集合 */ public void deletePaAccount(String id){ try{ mapper.deletePaAccount(id); }catch (Exception e){ log.error("[PaAccountService.deletePaAccount]调用失败,异常信息:"+e, e); throw new ServiceException("PaAccountService.deletePaAccount", e); } } /** * 按条件查询 * @param terms 条件 * @return List */ public List listTerms(Map terms){ return mapper.listTerms(terms); } /** * 按条件统计 * @param terms 条件 * @return long */ public long countTerms(Map terms){ return mapper.countTerms(terms); } /** * 按条件分页查询 * @param page 分页对象 * @param terms 条件 * @return Page */ public Page pageQuery(PageRequest page, Map terms){ long total = mapper.countTerms(terms); List content = mapper.pageTerms(page, terms); return new PageImpl(content, page, total); } /** * 新增或更新对象 * @param paAccount 实体对象 */ public void savePaAccount(PaAccount paAccount){ try{ Date nowDate = DateUtils.getNowDate(); // 判断是否新增 if (IdUtils.checkNewId(paAccount.getId())){ paAccount.setId(utilsClient.getNewTimeId()); paAccount.setCreateTime(nowDate); } paAccount.setUpdateTime(nowDate); this.saveOrUpdate(paAccount); }catch (Exception e){ log.error("[PaAccountService.savePaAccount]调用失败,异常信息:"+e, e); throw new ServiceException("PaAccountService.savePaAccount", e); } } /** * 当事人小程序-微信用户授权 * @param weChatEmpowerDTO 微信用户授权信息 * @return WeChatLoginDTO */ public WeChatLoginDTO wechatEmpower(WeChatEmpowerDTO weChatEmpowerDTO) { try{ Date nowDate = new Date(); WeChatLoginDTO weChatLoginDTO = new WeChatLoginDTO(); SeCustWechatDTO seCustWechatDTO = mapper.getSeCustWechatByAppid(weChatEmpowerDTO.getAppid()); weixinMaService.createConfig(seCustWechatDTO); WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(weChatEmpowerDTO.getCode()); weChatLoginDTO.setSessionKey(session.getSessionKey()); weChatLoginDTO.setOpenId(session.getOpenid()); PaUser paUser = null; PaAccount account = this.getByTypeAndAcc(UserBaseConsts.ACC_TYPE_2, session.getOpenid(), seCustWechatDTO.getCustId()); // 是否新注册 if (ObjectUtils.isEmpty(account)) { weChatLoginDTO.setLastLoginTime(nowDate); // 新建用户信息 paUser = new PaUser(); paUser.setId(utilsClient.getNewTimeId()); paUser.setAvatar(weChatEmpowerDTO.getAvatar()); paUser.setRealStatus(UserBaseConsts.REAL_STATUS_0); // paUser.setFaceStatus(UserBaseConsts.REAL_STATUS_0); paUser.setStatus(UserBaseConsts.USER_STATUS_1); paUser.setDeleteStatus(BaseConsts.DELETE_STATUS_0); paUser.setCustId(seCustWechatDTO.getCustId()); paUser.setCreateTime(nowDate); paUser.setUpdateTime(nowDate); paUserService.save(paUser); // 新增账号信息 account = new PaAccount(); account.setId(utilsClient.getNewTimeId()); account.setUserId(paUser.getId()); account.setAccType(UserBaseConsts.ACC_TYPE_2); account.setAcc(session.getOpenid()); account.setCipher(DigestUtils.md5DigestAsHex(session.getOpenid().getBytes())); account.setCipherOpen(session.getOpenid()); account.setLoginTime(nowDate); account.setCustId(seCustWechatDTO.getCustId()); account.setCreateTime(nowDate); account.setUpdateTime(nowDate); mapper.insert(account); }else { weChatLoginDTO.setLastLoginTime(account.getLoginTime()); // 更新登录时间 account.setLoginTime(nowDate); mapper.updatePaAccount(account); // 获取用户信息 paUser = paUserService.getById(account.getUserId()); } // 封装用户信息 weChatLoginDTO.setUserId(paUser.getId()); weChatLoginDTO.setTrueName(paUser.getTrueName()); weChatLoginDTO.setRealStatus(paUser.getRealStatus()); // weChatLoginDTO.setFaceStatus(paUser.getFaceStatus()); //生成JWT String token = JwtUtils.buildJWT(account.getCustId(), account.getUserId()); weChatLoginDTO.setToken(token); return weChatLoginDTO; }catch (Exception e){ log.error("service方法[PaAccountService.wechatEmpower]调用异常:"+e, e); throw new ServiceException("PaAccountService.wechatEmpower", e); } } /** * 通过账号类型和授权凭证查询 * @param accType 账户类型 * @param acc 账号 * @param custId 客户编号 * @return PaAccount */ public PaAccount getByTypeAndAcc(Integer accType, String acc, String custId){ QueryWrapper paAccountQueryWrapper = new QueryWrapper<>(); paAccountQueryWrapper.eq("acc_type", accType).eq("acc", acc).eq("cust_id", custId); PaAccount paAccount = mapper.selectOne(paAccountQueryWrapper); return paAccount; } /** * 当事人小程序-获取手机号码 * @param weChatEmpowerDTO 请求信息 * @return String */ public String wechatGetPhone(WeChatEmpowerDTO weChatEmpowerDTO) { try{ SeCustWechatDTO seCustWechatDTO = mapper.getSeCustWechatByAppid(weChatEmpowerDTO.getAppid()); weixinMaService.createConfig(seCustWechatDTO); String accessToken = wxMaService.getAccessToken(); String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken; Map map = Maps.newHashMap(); map.put("code", weChatEmpowerDTO.getCode()); JsonMapper jsonMapper = new JsonMapper(); String jsonStr = jsonMapper.toJson(map); String result = HttpClientUtils.httpPostRaw(url, jsonStr, null, null); JSONObject resultJson = JSONObject.parseObject(new String(result)); int errcode = resultJson.getIntValue("errcode"); if (errcode == 0){ JSONObject phoneInfoJson = resultJson.getJSONObject("phone_info"); String phoneNumber = phoneInfoJson.getString("phoneNumber"); return phoneNumber; }else { throw new ServiceException("errcode:"+errcode+";errmsg:"+resultJson.getIntValue("errmsg")); } }catch (Exception e){ log.error("service方法[PaAccountService.wechatGetPhone]调用异常:"+e, e); throw new ServiceException("PaAccountService.wechatGetPhone", e); } } }