| @@ -45,4 +45,8 @@ public interface RemoteTenantService { | |||
| @GetMapping("/tenant/staff") | |||
| R<SysEnterpriseStaff> existStaff(@RequestParam ("phone") String phone, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); | |||
| @PostMapping("/tenant/staff") | |||
| R<SysEnterpriseStaff> saveEnterpriseStaff(@RequestBody SysEnterpriseStaff staff, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); | |||
| } | |||
| @@ -47,6 +47,11 @@ public class RemoteTenantFallbackFactory implements FallbackFactory<RemoteTenant | |||
| public R<SysEnterpriseStaff> existStaff(String phone, String source) { | |||
| return R.fail("验证用户手机号失败:" + throwable.getMessage()); | |||
| } | |||
| @Override | |||
| public R<SysEnterpriseStaff> saveEnterpriseStaff(SysEnterpriseStaff staff, String source) { | |||
| return null; | |||
| } | |||
| }; | |||
| } | |||
| } | |||
| @@ -0,0 +1,42 @@ | |||
| package com.xueyi.common.core.utils.core; | |||
| import javax.crypto.Cipher; | |||
| import javax.crypto.spec.SecretKeySpec; | |||
| import java.security.Key; | |||
| import java.util.Base64; | |||
| /** | |||
| * @author yk | |||
| * @description | |||
| * @date 2024-01-03 18:48 | |||
| */ | |||
| public class CryptoUtil { | |||
| public static final String DEFAULT_KEY = "7uw823wuyeuwiyeehbsuuwgwvsyyeuii"; | |||
| public static String encrypt(String data) throws Exception{ | |||
| Key k = new SecretKeySpec(DEFAULT_KEY.getBytes(), "AES"); | |||
| // 创建 Cipher 对象 | |||
| Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | |||
| // 初始化 Cipher 对象 | |||
| cipher.init(Cipher.ENCRYPT_MODE, k); | |||
| // 加密数据 | |||
| byte[] ciphertext = cipher.doFinal(data.getBytes()); | |||
| // 输出密文 | |||
| return Base64.getEncoder().encodeToString(ciphertext); | |||
| } | |||
| public static String decrypt(String encryptedData) throws Exception { | |||
| byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData); | |||
| Key k = new SecretKeySpec(DEFAULT_KEY.getBytes(), "AES"); | |||
| Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | |||
| cipher.init(Cipher.DECRYPT_MODE, k); | |||
| byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes); | |||
| //base64加密 | |||
| return new String(decryptedBytes); | |||
| } | |||
| } | |||
| @@ -1,13 +1,37 @@ | |||
| /* | |||
| package com.xueyi.system.staff.graphql; | |||
| import com.coxautodev.graphql.tools.GraphQLQueryResolver; | |||
| import com.xueyi.system.api.digitalmans.domain.po.DmVisitorsPo; | |||
| import com.xueyi.system.staff.domain.model.DmVisitorsConverter; | |||
| import com.xueyi.system.staff.mapper.DmVisitorsMapper; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.List; | |||
| import java.util.stream.Collectors; | |||
| */ | |||
| /** | |||
| * @author yk | |||
| * @description | |||
| * @date 2023-12-25 19:55 | |||
| */ | |||
| *//* | |||
| @Service | |||
| public class VisitorGraphQLQuery { | |||
| class VisitorGraphQLQueryResolver implements GraphQLQueryResolver { | |||
| @Autowired | |||
| private DmVisitorsMapper mapper; | |||
| @Autowired | |||
| private DmVisitorsConverter converter; | |||
| public DmVisitorsPo findOneVisitor(Long id){ | |||
| return mapper.selectById(id); | |||
| } | |||
| public List<DmVisitorsPo> getVisitorList(){ | |||
| return mapper.selectList(null).stream().map(t->converter.mapperDto(t)).collect(Collectors.toList()); | |||
| } | |||
| } | |||
| */ | |||
| @@ -1,8 +1,10 @@ | |||
| package com.xueyi.system.wechat.controller; | |||
| import com.baomidou.dynamic.datasource.toolkit.CryptoUtils; | |||
| import com.fasterxml.jackson.core.JsonProcessingException; | |||
| import com.fasterxml.jackson.databind.ObjectMapper; | |||
| import com.xueyi.common.core.constant.basic.SecurityConstants; | |||
| import com.xueyi.common.core.utils.core.CryptoUtil; | |||
| import com.xueyi.common.core.utils.core.ObjectUtil; | |||
| import com.xueyi.common.core.web.result.AjaxResult; | |||
| import com.xueyi.common.core.web.result.R; | |||
| @@ -22,10 +24,12 @@ import org.springframework.web.bind.annotation.GetMapping; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| import org.springframework.web.bind.annotation.RequestBody; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.RequestMethod; | |||
| import org.springframework.web.bind.annotation.RequestParam; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| /** | |||
| @@ -51,35 +55,34 @@ public class WeChatController { | |||
| @GetMapping("/login") | |||
| public AjaxResult login(@RequestParam String code, HttpServletResponse response) { | |||
| @RequestMapping(value = "/login", method = {RequestMethod.GET}) | |||
| public AjaxResult login(@RequestParam("code") String code, HttpServletResponse response) { | |||
| // 根据code获取微信用户信息 | |||
| WeChatSessionResponse weChatUserInfo = null; | |||
| try { | |||
| weChatUserInfo = weChatService.getUserInfo(code); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| response.setStatus(500); | |||
| return AjaxResult.error("微信服务器请求失败"); | |||
| return AjaxResult.error("微信服务器请求openid失败"); | |||
| } | |||
| log.info("微信login返回信息:{}", weChatUserInfo); | |||
| R<SysEnterpriseStaff> staff = tenantService.existStaff(weChatUserInfo.getOpenid(), SecurityConstants.INNER); | |||
| if (staff.getData() == null) { | |||
| response.setStatus(500); | |||
| return AjaxResult.error("对应用户不存在", weChatUserInfo); | |||
| } else { | |||
| R<LoginUser> loginInfoResult = remoteLoginService.getLoginInfoInnerByPhone(staff.getData().getPhone(), SecurityConstants.INNER); | |||
| log.info("根据手机号获取用户信息返回信息:{}", loginInfoResult.getData().getEnterpriseName()); | |||
| if (ObjectUtil.isNull(loginInfoResult.getData())) { | |||
| response.setStatus(500); | |||
| return AjaxResult.error("手机号可能错误,请查证后重试!", weChatUserInfo); | |||
| } else { | |||
| Map<String, Object> map = tokenService.createToken(loginInfoResult.getData()); | |||
| map.put("openid", weChatUserInfo.getOpenid()); | |||
| Map<String, Object> map = new HashMap<>(); | |||
| try { | |||
| map.put("wechat_auth", CryptoUtil.encrypt(weChatUserInfo.toString())); | |||
| if (staff.getData() != null) { | |||
| R<LoginUser> loginInfoResult = remoteLoginService.getLoginInfoInnerByPhone(staff.getData().getPhone(), SecurityConstants.INNER); | |||
| map.putAll(tokenService.createToken(loginInfoResult.getData())); | |||
| return AjaxResult.success(map); | |||
| } | |||
| return AjaxResult.success(map); | |||
| } catch (Exception e){ | |||
| e.printStackTrace(); | |||
| response.setStatus(500); | |||
| return AjaxResult.error("服务器异常"); | |||
| } | |||
| } | |||
| @@ -5,6 +5,7 @@ package com.xueyi.system.wechat.domain.bean; | |||
| * @description | |||
| * @date 2024-01-02 12:45 | |||
| */ | |||
| import com.alibaba.fastjson2.JSONObject; | |||
| import com.fasterxml.jackson.annotation.JsonProperty; | |||
| import lombok.Data; | |||
| @@ -23,10 +24,7 @@ public class WeChatSessionResponse { | |||
| @Override | |||
| public String toString() { | |||
| return "WeChatSessionResponse{" + | |||
| "openid='" + openid + '\'' + | |||
| ", session_key='" + sessionKey + '\'' + | |||
| '}'; | |||
| return JSONObject.from(this).toJSONString(); | |||
| } | |||
| } | |||
| @@ -1,11 +1,14 @@ | |||
| package com.xueyi.system.wechat.service; | |||
| import com.alibaba.fastjson2.JSON; | |||
| import com.xueyi.common.redis.utils.RedisUtil; | |||
| import com.xueyi.system.wechat.domain.bean.WeChatSessionResponse; | |||
| import org.slf4j.Logger; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.beans.factory.annotation.Value; | |||
| import org.springframework.http.converter.StringHttpMessageConverter; | |||
| import org.springframework.stereotype.Service; | |||
| import org.springframework.web.client.RestClientException; | |||
| import org.springframework.web.client.RestTemplate; | |||
| import org.springframework.web.client.UnknownContentTypeException; | |||
| @@ -35,19 +38,26 @@ public class WeChatService { | |||
| @Autowired | |||
| private RestTemplate restTemplate; | |||
| public WeChatSessionResponse getUserInfo(String code) throws UnknownContentTypeException { | |||
| public WeChatSessionResponse getUserInfo(String code) { | |||
| log.info("appId: {}, appSecret: {}, code: {}",appId, appSecret, code); | |||
| // 向微信服务器发送请求,获取用户的openid和session_key | |||
| String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + | |||
| "&secret=" + appSecret + | |||
| "&js_code=" + code + | |||
| "&grant_type=authorization_code"; | |||
| WeChatSessionResponse sessionResponse = restTemplate.getForObject(url, WeChatSessionResponse.class); | |||
| log.info("url: {}", url); | |||
| restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter()); | |||
| // WeChatSessionResponse sessionResponse = restTemplate.getForObject(url, WeChatSessionResponse.class); | |||
| String str = restTemplate.getForObject(url, String.class); | |||
| log.info("sessionResponse: str {}", str); | |||
| WeChatSessionResponse sessionResponse = JSON.parseObject(str, WeChatSessionResponse.class); | |||
| log.info("sessionResponse: {}", sessionResponse); | |||
| RedisUtil.setVal(WECHAT_REDIS_SESSION_KEY+sessionResponse.getOpenid(), sessionResponse.getSessionKey()); | |||
| log.info("sessionResponse: {}", sessionResponse); | |||
| return sessionResponse; | |||
| } | |||