From 951feaeef11f38701290ced94f71c9e103a419bf Mon Sep 17 00:00:00 2001
From: ZQN <364596817@qq.com>
Date: 星期一, 17 六月 2024 17:35:31 +0800
Subject: [PATCH] 企业信息管理,验证码登录,惠企政策添加

---
 project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java |  146 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 128 insertions(+), 18 deletions(-)

diff --git a/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java b/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java
index 1f8d563..44c3077 100644
--- a/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java
+++ b/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java
@@ -2,6 +2,21 @@
 
 import javax.annotation.Resource;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.aliyuncs.CommonRequest;
+import com.aliyuncs.CommonResponse;
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.http.MethodType;
+import com.aliyuncs.profile.DefaultProfile;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.project.common.constant.AliyunSmsConstants;
+import com.project.common.exception.base.BaseException;
+import com.project.system.domain.bo.editBo.UserPhoneLoginBo;
+import com.project.system.mapper.SysUserMapper;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.authentication.AuthenticationManager;
@@ -29,33 +44,30 @@
 import com.project.system.service.ISysConfigService;
 import com.project.system.service.ISysUserService;
 
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
 /**
  * 鐧诲綍鏍¢獙鏂规硶
- * 
+ *
  * @author project
  */
 @Component
 @Slf4j
+@RequiredArgsConstructor
 public class SysLoginService
 {
-    @Autowired
-    private TokenService tokenService;
+    private final TokenService tokenService;
+    private final AuthenticationManager authenticationManager;
+    private final RedisCache redisCache;
+    private final ISysUserService userService;
+    private final SysUserMapper userMapper;
+    private final ISysConfigService configService;
 
-    @Resource
-    private AuthenticationManager authenticationManager;
-
-    @Autowired
-    private RedisCache redisCache;
-    
-    @Autowired
-    private ISysUserService userService;
-
-    @Autowired
-    private ISysConfigService configService;
 
     /**
      * 鐧诲綍楠岃瘉
-     * 
+     *
      * @param username 鐢ㄦ埛鍚�
      * @param password 瀵嗙爜
      * @param code 楠岃瘉鐮�
@@ -71,7 +83,7 @@
             validateCaptcha(username, code, uuid);
         }
         // 鐢ㄦ埛楠岃瘉
-        Authentication authentication = null;
+        Authentication authentication;
         try
         {
             UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
@@ -104,13 +116,78 @@
         return tokenService.createToken(loginUser);
     }
 
+
+    /**
+     * 鑾峰彇楠岃瘉鐮�
+     * @param phone 鎵嬫満鍙�
+     * @return  楠岃瘉鐮�
+     */
+    public Boolean getVerifyCode(String phone)
+    {
+        SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber,phone));
+        if (user==null){
+            throw new BaseException("鎮ㄦ墜鏈哄彿灏氭湭娉ㄥ唽锛�");
+        }
+
+        // 鐢熸垚4浣嶉殢鏈烘暟
+        String code = "";
+        Random ran = new Random();
+        int randomNum = ran.nextInt(10000);
+        code = String.format("%04d", randomNum);
+        log.info("鎵嬫満鍙凤細"+phone+"->楠岃瘉鐮侊細"+code);
+        Boolean flag = true;
+        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", AliyunSmsConstants.SMS_APPID, AliyunSmsConstants.SMS_SECRET);
+        IAcsClient client = new DefaultAcsClient(profile);
+        CommonRequest request = new CommonRequest();
+        request.setMethod(MethodType.POST);
+        request.setDomain("dysmsapi.aliyuncs.com");
+        request.setVersion("2017-05-25");
+        request.setAction("SendSms");
+        request.putQueryParameter("PhoneNumbers", phone);
+        request.putQueryParameter("SignName", "鐩涘晢鐝犲疂");
+        request.putQueryParameter("TemplateCode", "SMS_460945884");
+        request.putQueryParameter("TemplateParam", "{code:" + code + "}");
+        try {
+            CommonResponse response = client.getCommonResponse(request);
+            JSONObject jsonObject = JSON.parseObject(response.getData());
+            if ("OK".equals(jsonObject.get("Code"))) {
+                log.info("鍙戦�佹垚鍔� ->楠岃瘉鐮侊細"+code);
+                redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
+                return true;
+            }
+
+        } catch (ClientException e) {
+            e.printStackTrace();
+        }
+        redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
+        return flag;
+    }
+
+    /**
+     * 楠岃瘉鐮佺櫥褰�
+     * @param bo    鍙傛暟
+     * @return  缁撴灉
+     */
+    public String phoneLogin(UserPhoneLoginBo bo)
+    {
+        String phone = bo.getPhone();
+        Boolean verified = verifyPhone(phone, bo.getCode());
+        if (!verified){
+            throw new BaseException("鎵嬫満鍙烽獙璇佺爜鏍¢獙澶辫触锛�");
+        }
+        SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber,bo.getPhone()));
+        if (user==null){
+            throw new BaseException("鎮ㄦ墜鏈哄彿灏氭湭娉ㄥ唽锛�");
+        }
+        return this.login(user.getUserName(), user.getRecommendUser(), null, null);
+    }
+
     /**
      * 鏍¢獙楠岃瘉鐮�
-     * 
+     *
      * @param username 鐢ㄦ埛鍚�
      * @param code 楠岃瘉鐮�
      * @param uuid 鍞竴鏍囪瘑
-     * @return 缁撴灉
      */
     public void validateCaptcha(String username, String code, String uuid)
     {
@@ -142,4 +219,37 @@
         sysUser.setLoginDate(DateUtils.getNowDate());
         userService.updateUserProfile(sysUser);
     }
+
+
+    /**
+     * 鑾峰彇楠岃瘉鐮佸墠缂�
+     * @param phone 鎵嬫満鍙�
+     * @return  楠岃瘉鐮�
+     */
+    private String getCacheKey(String phone)
+    {
+        return CacheConstants.PHONE_CODE_KEY + phone;
+    }
+
+    /**
+     * 鏍¢獙楠岃瘉鐮�
+     * @param phone 鎵嬫満鍙�
+     * @param code  楠岃瘉鐮�
+     * @return  鏍¢獙缁撴灉
+     */
+    private Boolean verifyPhone(String phone, String code)
+    {
+        String key = getCacheKey(phone);
+        if (!redisCache.hasKey(key)) {
+            return false;
+        }
+        String redisCode = redisCache.getCacheObject(key);
+
+        boolean verify = redisCode.equals(code);
+        if (verify){
+            redisCache.deleteObject(getCacheKey(phone));
+        }
+
+        return verify;
+    }
 }

--
Gitblit v1.9.1