|
@@ -0,0 +1,111 @@
|
|
|
+package com.fdkankan.tk.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.fdkankan.tk.common.ResultCode;
|
|
|
+import com.fdkankan.tk.common.util.RoomUtil;
|
|
|
+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.WxUserVo;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2022-12-19
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class WxUserServiceImpl extends ServiceImpl<IWxUserMapper, WxUser> implements IWxUserService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ WxClient wxClient;
|
|
|
+ @Autowired
|
|
|
+ IWxService wxService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object wxLogin(String code) {
|
|
|
+ WxOpenIdVo wxOpenIdVo = wxClient.getOpenIdUrl(WxConfigServiceImpl.wxConfig.getAppId(), WxConfigServiceImpl.wxConfig.getAppSecret(), code);
|
|
|
+ log.info("wxLogin-微信获取openid:{}",wxOpenIdVo);
|
|
|
+ if(wxOpenIdVo.getErrcode() == 0){
|
|
|
+ WxUser wxUser = this.addUser(wxOpenIdVo);
|
|
|
+ WxUserVo vo = new WxUserVo();
|
|
|
+ BeanUtils.copyProperties(wxUser,vo);
|
|
|
+ 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(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<WxUser> 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.setCreateTime(null);
|
|
|
+ wxUser.setUpdateTime(null);
|
|
|
+ this.updateById(wxUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getPhone(String code) {
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = wxClient.getPhone(wxService.getToken(), new WxGetPhoneParam(code));
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|