|
@@ -0,0 +1,61 @@
|
|
|
|
|
+package com.zhixinghe1.ots.service.manager.impl;
|
|
|
|
|
+
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import com.zhixinghe1.ots.atomic.entity.WechatUser;
|
|
|
|
|
+import com.zhixinghe1.ots.atomic.service.IWechatUserService;
|
|
|
|
|
+import com.zhixinghe1.ots.common.exception.BusinessException;
|
|
|
|
|
+import com.zhixinghe1.ots.constant.CacheConstant;
|
|
|
|
|
+import com.zhixinghe1.ots.domain.dto.wechat.Code2SessionRequest;
|
|
|
|
|
+import com.zhixinghe1.ots.domain.dto.wechat.Code2SessionResponse;
|
|
|
|
|
+import com.zhixinghe1.ots.domain.dto.wechat.LoginRequest;
|
|
|
|
|
+import com.zhixinghe1.ots.feign.WechatMiniProgramFeign;
|
|
|
|
|
+import com.zhixinghe1.ots.service.manager.IWechatMiniProgramManager;
|
|
|
|
|
+import com.zhixinghe1.ots.utiis.CacheUtils;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 微信小程序服务类
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class WechatMiniProgramManagerImpl implements IWechatMiniProgramManager {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private WechatMiniProgramFeign wechatMiniProgramFeign;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private IWechatUserService wechatUserService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private CacheManager cacheManager;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String login(LoginRequest request) {
|
|
|
|
|
+ Code2SessionRequest code2SessionRequest = new Code2SessionRequest();
|
|
|
|
|
+ code2SessionRequest.setGrantType("authorization_code");
|
|
|
|
|
+ code2SessionRequest.setJsCode(request.getCode());
|
|
|
|
|
+ Code2SessionResponse loginResponse = wechatMiniProgramFeign.code2Session(code2SessionRequest);
|
|
|
|
|
+ if (loginResponse.getErrCode() != 0) {
|
|
|
|
|
+ throw BusinessException.fail(loginResponse.getErrMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 新增加用户记录
|
|
|
|
|
+ WechatUser wechatUser = wechatUserService.getByOpenId(loginResponse.getOpenId());
|
|
|
|
|
+ if (wechatUser == null) {
|
|
|
|
|
+ wechatUserService.add(loginResponse.getOpenId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 缓存微信用户对应的session_key
|
|
|
|
|
+ CacheUtils.put(CacheConstant.WECHAT_MINI_PROGRAM_SESSION_KEY, loginResponse.getOpenId(),
|
|
|
|
|
+ loginResponse.getSessionKey());
|
|
|
|
|
+
|
|
|
|
|
+ // TODO 颁发token
|
|
|
|
|
+ CacheUtils.put(CacheConstant.SYSTEM_USER_TOKEN, loginResponse.getOpenId(), loginResponse.getSessionKey());
|
|
|
|
|
+
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|