package com.fdkankan.tk.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fdkankan.tk.common.ResultCode; import com.fdkankan.tk.common.util.JwtUtil; import com.fdkankan.tk.common.util.RoomUtil; import com.fdkankan.tk.entity.WxConfig; import com.fdkankan.tk.entity.WxUser; import com.fdkankan.tk.exception.BusinessException; import com.fdkankan.tk.httpClient.client.WxClient; import com.fdkankan.tk.httpClient.request.WxGetPhoneParam; import com.fdkankan.tk.httpClient.response.WxOpenIdVo; import com.fdkankan.tk.mapper.IWxUserMapper; import com.fdkankan.tk.response.UserMsgVo; import com.fdkankan.tk.response.WxUserVo; import com.fdkankan.tk.service.IWxConfigService; import com.fdkankan.tk.service.IWxService; import com.fdkankan.tk.service.IWxUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fdkankan.tk.util.MD5Utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.security.spec.ECField; import java.util.Date; import java.util.HashMap; /** *

* 服务实现类 *

* * @author * @since 2022-12-19 */ @Service @Slf4j public class WxUserServiceImpl extends ServiceImpl implements IWxUserService { @Resource WxClient wxClient; @Autowired IWxService wxService; @Autowired IWxConfigService wxConfigService; @Override public Object wxLogin(String code) { WxConfig wxConfig = wxConfigService.getWxConfig(); if(wxConfig == null){ throw new BusinessException(ResultCode.WX_CONFIG_ERROR); } WxOpenIdVo wxOpenIdVo = wxClient.getOpenIdUrl(wxConfig.getAppId(),wxConfig.getAppSecret(), code); if(wxOpenIdVo.getErrcode() == null && StringUtils.isNotBlank(wxOpenIdVo.getOpenId())){ WxUser wxUser = this.addUser(wxOpenIdVo); WxUserVo vo = new WxUserVo(); BeanUtils.copyProperties(wxUser,vo); String token = JwtUtil.createJWT(-1, wxUser.getWxUserId(), "wx"); vo.setToken(token); return vo; } throw new BusinessException(ResultCode.WX_LOGIN_ERROR); } @Override public WxUser addUser(WxOpenIdVo wxOpenIdVo) { WxUser wxUser = this.getByOpenId(wxOpenIdVo.getOpenId()); if(wxUser != null){ return wxUser; } wxUser = new WxUser(); wxUser.setWxUserId(MD5Utils.getPWD(RoomUtil.ev + wxOpenIdVo.getOpenId())); wxUser.setOpenid(wxOpenIdVo.getOpenId()); wxUser.setUnionid(wxOpenIdVo.getUnionid()); wxUser.setSessionKey(wxOpenIdVo.getSession_key()); this.save(wxUser); return wxUser; } @Override public WxUser getByOpenId(String openId) { try { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(WxUser::getOpenid,openId); return this.getOne(wrapper); }catch (Exception e){ e.printStackTrace(); } throw new BusinessException(ResultCode.WX_OPENID_ERROr); } @Override public void updateByVo(WxUserVo param) { if(StringUtils.isBlank(param.getWxUserId())){ throw new BusinessException(ResultCode.PARAM_MISS); } WxUser wxUser = new WxUser(); BeanUtils.copyProperties(param,wxUser); wxUser.setUpdateTime(new Date()); log.info("wxUser:{}",wxUser); if(StringUtils.isBlank(wxUser.getPhoneNumber())){ wxUser.setPhoneNumber(null); } this.updateById(wxUser); } @Override public Object getPhone(String code,Integer reCount) { try { String token = wxService.getToken(); if(StringUtils.isBlank(token)){ throw new BusinessException(ResultCode.WX_PHONE_ERROR); } JSONObject jsonObject = wxClient.getPhone(token, new WxGetPhoneParam(code)); if(jsonObject.getInteger("errcode") != null && jsonObject.getInteger("errcode") == 40001 && reCount < 2){ log.info("wx-getPhone-error:{},当前重试次数:{}",code,reCount); reCount ++; wxService.delToken(); return getPhone(code,reCount); } JSONObject phoneObj = (JSONObject) jsonObject.get("phone_info"); if(phoneObj==null){ throw new BusinessException(ResultCode.WX_PHONE_ERROR); } //return phoneObj.getString("phoneNumber"); return phoneObj; } catch (Exception e){ e.printStackTrace(); } throw new BusinessException(ResultCode.WX_PHONE_ERROR); } @Override public Object userInfo(String token) { if(StringUtils.isBlank(token)){ throw new BusinessException(ResultCode.USER_NOT_LOGIN); } String wxUserId = JwtUtil.getUserName(token); if(StringUtils.isBlank(wxUserId)){ throw new BusinessException(ResultCode.USER_NOT_LOGIN); } WxUser wxUser = this.getById(wxUserId); if(wxUser == null){ return null; } WxUserVo vo = new WxUserVo(); BeanUtils.copyProperties(wxUser,vo); return vo; } }