wangliang пре 1 година
родитељ
комит
03fd0eab8f

+ 25 - 1
sop-business/src/main/java/com/qmth/sop/business/entity/BasicVerifyCode.java

@@ -3,6 +3,7 @@ package com.qmth.sop.business.entity;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 import com.qmth.sop.common.base.BaseEntity;
+import com.qmth.sop.common.contant.SystemConstant;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 
@@ -16,7 +17,7 @@ import java.io.Serializable;
  * @author wangliang
  * @since 2023-08-01
  */
-@ApiModel(value="BasicVerifyCode对象", description="短信验证码记录表")
+@ApiModel(value = "BasicVerifyCode对象", description = "短信验证码记录表")
 public class BasicVerifyCode extends BaseEntity implements Serializable {
 
     private static final long serialVersionUID = 1L;
@@ -41,6 +42,29 @@ public class BasicVerifyCode extends BaseEntity implements Serializable {
     @ApiModelProperty(value = "4位数字")
     private String verifyCode;
 
+    public BasicVerifyCode() {
+
+    }
+
+    public BasicVerifyCode(Long userId, String mobileNumber, Long expireTime, Integer validPeriod, String verifyCode) {
+        setId(SystemConstant.getDbUuid());
+        this.userId = userId;
+        this.mobileNumber = mobileNumber;
+        this.expireTime = expireTime;
+        this.validPeriod = validPeriod;
+        this.verifyCode = verifyCode;
+        setCreateId(userId);
+        setCreateTime(System.currentTimeMillis());
+    }
+
+    public void updateVerifyCodeInfo(Long userId, Long expireTime, Integer validPeriod, String verifyCode) {
+        this.expireTime = expireTime;
+        this.validPeriod = validPeriod;
+        this.verifyCode = verifyCode;
+        setUpdateId(userId);
+        setUpdateTime(System.currentTimeMillis());
+    }
+
     public Long getOrgId() {
         return orgId;
     }

+ 9 - 1
sop-business/src/main/java/com/qmth/sop/business/service/BasicVerifyCodeService.java

@@ -1,7 +1,8 @@
 package com.qmth.sop.business.service;
 
-import com.qmth.sop.business.entity.BasicVerifyCode;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.sop.business.entity.BasicVerifyCode;
+import com.qmth.sop.business.entity.SysUser;
 
 /**
  * <p>
@@ -13,4 +14,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface BasicVerifyCodeService extends IService<BasicVerifyCode> {
 
+    /**
+     * 发送验证码
+     * @param mobileNumber
+     * @param sysUser
+     * @return
+     */
+    public Boolean sendVeirfyCode(String mobileNumber, SysUser sysUser);
 }

+ 74 - 1
sop-business/src/main/java/com/qmth/sop/business/service/impl/BasicVerifyCodeServiceImpl.java

@@ -1,11 +1,23 @@
 package com.qmth.sop.business.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.sop.business.bean.result.SmsResponseResult;
+import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.BasicVerifyCode;
+import com.qmth.sop.business.entity.SysConfig;
+import com.qmth.sop.business.entity.SysUser;
 import com.qmth.sop.business.mapper.BasicVerifyCodeMapper;
 import com.qmth.sop.business.service.BasicVerifyCodeService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.sop.business.util.SmsSendUtil;
+import com.qmth.sop.common.contant.SystemConstant;
+import com.qmth.sop.common.enums.ExceptionResultEnum;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.time.Duration;
+import java.util.*;
+
 /**
  * <p>
  * 短信验证码记录表 服务实现类
@@ -17,4 +29,65 @@ import org.springframework.stereotype.Service;
 @Service
 public class BasicVerifyCodeServiceImpl extends ServiceImpl<BasicVerifyCodeMapper, BasicVerifyCode> implements BasicVerifyCodeService {
 
+    @Resource
+    CommonCacheService commonCacheService;
+
+    @Resource
+    SmsSendUtil smsSendUtil;
+
+    /**
+     * 发送验证码
+     *
+     * @param mobileNumber
+     * @param sysUser
+     * @return
+     */
+    @Override
+    public Boolean sendVeirfyCode(String mobileNumber, SysUser sysUser) {
+        SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SYS_CODE_ENABLE);
+        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("短信验证码启用开关未设置"));
+
+        if (!Boolean.valueOf(sysConfig.getConfigValue())) {
+            throw ExceptionResultEnum.ERROR.exception("短信验证码已关闭");
+        }
+        BasicVerifyCode basicVerifyCode = this.getOne(new QueryWrapper<BasicVerifyCode>().lambda().eq(BasicVerifyCode::getMobileNumber, mobileNumber).eq(BasicVerifyCode::getUserId, sysUser.getId()));
+        if (basicVerifyCode != null) {
+            Date oldCreateTime = new Date(basicVerifyCode.getCreateTime());
+            SysConfig sysConfigSmsSendInterval = commonCacheService.addSysConfigCache(SystemConstant.CODE_SEND_INTERVAL);
+            Optional.ofNullable(sysConfigSmsSendInterval).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置短信验证码发送间隔"));
+            Integer sendInterval = Integer.parseInt(sysConfigSmsSendInterval.getConfigValue());
+            if ((System.currentTimeMillis() - oldCreateTime.getTime()) < sendInterval * 1000) {
+                throw ExceptionResultEnum.ERROR.exception("发送验证码过于频繁,请" + sendInterval + "秒之后再试");
+            }
+        }
+        try {
+            StringBuilder sb = new StringBuilder();
+            sb.append((int) ((Math.random() * 9 + 1) * 1000));
+            String verifyCode = sb.toString();
+
+            Map<String, Object> templateParam = new HashMap<>();
+            templateParam.put("code", verifyCode);
+
+            // 调用短信发送公共接口
+            SmsResponseResult smsResponseResult = smsSendUtil.sendSms(mobileNumber, SystemConstant.SMS_TPL_CODE, templateParam);
+
+            if (SmsSendUtil.OK.equals(smsResponseResult.getCode())) {
+                // 请求成功
+                SysConfig sysConfigSmsExpiredTime = commonCacheService.addSysConfigCache(SystemConstant.CODE_EXPIRED_TIME);
+                Optional.ofNullable(sysConfigSmsExpiredTime).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置短信验证码有效时间"));
+                Integer codeExpiredTime = Integer.parseInt(sysConfigSmsExpiredTime.getConfigValue());
+                if (Objects.isNull(basicVerifyCode)) {
+                    basicVerifyCode = new BasicVerifyCode(sysUser.getId(), mobileNumber, SystemConstant.processMiniute(Duration.ofMinutes(2)), codeExpiredTime, verifyCode);
+                } else {
+                    basicVerifyCode.updateVerifyCodeInfo(sysUser.getId(), SystemConstant.processMiniute(Duration.ofMinutes(2)), codeExpiredTime, verifyCode);
+                }
+                this.saveOrUpdate(basicVerifyCode);
+            } else {
+                throw ExceptionResultEnum.ERROR.exception(smsResponseResult.getMessage());
+            }
+        } catch (Exception e) {
+            throw ExceptionResultEnum.ERROR.exception("请重新获取验证码");
+        }
+        return true;
+    }
 }

+ 0 - 3
sop-business/src/main/java/com/qmth/sop/business/util/SmsSendUtil.java

@@ -24,11 +24,8 @@ import java.util.Optional;
  */
 @Component
 public class SmsSendUtil {
-
     private static final Logger log = LoggerFactory.getLogger(SmsSendUtil.class);
-
     public static final String OK = "OK";
-    private static final String ERROR = "ERROR";
 
     @Resource
     private SmsService smsService;

+ 3 - 4
sop-business/src/main/resources/db/install/sop_db.sql

@@ -759,10 +759,9 @@ INSERT INTO `sys_privilege` VALUES (239, '任务管理', 'taskManage', 'MENU', 1
 INSERT INTO `sys_privilege` VALUES (240, '任务管理', 'task', 'MENU', 239, 1, NULL, NULL, 1, 0, 1);
 INSERT INTO `sys_privilege` VALUES (241, '列表', 'List', 'LIST', 240, 1, 'AUTH', NULL, 1, 0, 1);
 INSERT INTO `sys_privilege` VALUES (242, '查询条件', 'Condition', 'CONDITION', 240, 1, 'AUTH', NULL, 1, 0, 1);
-INSERT INTO `sys_privilege` VALUES (243, '新增', 'Add', 'BUTTON', 240, 1, 'AUTH', NULL, 1, 0, 1);
-INSERT INTO `sys_privilege` VALUES (244, '查询', 'Select', 'BUTTON', 240, 2, 'AUTH', NULL, 1, 0, 1);
-INSERT INTO `sys_privilege` VALUES (245, '导出日志', 'Export', 'LINK', 240, 1, 'AUTH', NULL, 1, 0, 1);
-INSERT INTO `sys_privilege` VALUES (246, '下载文件', 'Download', 'LINK', 240, 2, 'AUTH', NULL, 1, 0, 1);
+INSERT INTO `sys_privilege` VALUES (243, '查询', 'Select', 'BUTTON', 240, 2, 'AUTH', '72', 1, 0, 1);
+INSERT INTO `sys_privilege` VALUES (244, '导出日志', 'Export', 'LINK', 240, 1, 'AUTH', NULL, 1, 0, 1);
+INSERT INTO `sys_privilege` VALUES (245, '下载文件', 'Download', 'LINK', 240, 2, 'AUTH', NULL, 1, 0, 1);
 COMMIT;
 
 -- ----------------------------

+ 23 - 0
sop-common/src/main/java/com/qmth/sop/common/contant/SystemConstant.java

@@ -13,7 +13,9 @@ import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
+import java.time.Duration;
 import java.time.LocalDateTime;
+import java.time.ZoneOffset;
 import java.util.StringJoiner;
 
 /**
@@ -80,6 +82,11 @@ public class SystemConstant {
     public static final int PAGE_NUMBER_MIN = 1;
     public static final String STATIC = "static";
 
+    /**
+     * 表达式
+     */
+    public static final String REGULAR_EXPRESSION_OF_PHONE = "((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8}";
+
     /**
      * 系统配置
      */
@@ -98,6 +105,7 @@ public class SystemConstant {
     public static final String CODE_EXPIRED_TIME = "code.expired.time";
     public static final String CODE_SEND_INTERVAL = "code.send.interval";
     public static final String SMS_SIGN_NAME = "sms.sign.name";
+    public static final String SMS_TPL_CODE = "sms.tpl.code";
 
     /**
      * api前缀
@@ -237,6 +245,9 @@ public class SystemConstant {
 
     /**
      * URL 编码, Encode默认为UTF-8.
+     *
+     * @param part
+     * @return
      */
     public static String urlEncode(String part) {
         try {
@@ -245,4 +256,16 @@ public class SystemConstant {
             throw ExceptionResultEnum.ERROR.exception(e.getMessage());
         }
     }
+
+    /**
+     * 时间相加
+     *
+     * @param duration
+     * @return
+     */
+    public static Long processMiniute(Duration duration) {
+        LocalDateTime nowTime = LocalDateTime.now();
+        LocalDateTime newDateTime = nowTime.plus(duration);
+        return newDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
+    }
 }

+ 48 - 3
sop-server/src/main/java/com/qmth/sop/server/api/SysController.java

@@ -13,9 +13,7 @@ import com.qmth.sop.business.entity.BasicAttachment;
 import com.qmth.sop.business.entity.SysConfig;
 import com.qmth.sop.business.entity.SysUser;
 import com.qmth.sop.business.entity.TBTask;
-import com.qmth.sop.business.service.BasicAttachmentService;
-import com.qmth.sop.business.service.SysUserService;
-import com.qmth.sop.business.service.TBTaskService;
+import com.qmth.sop.business.service.*;
 import com.qmth.sop.business.util.ImportExportUtil;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.*;
@@ -74,6 +72,12 @@ public class SysController {
     @Resource
     TBTaskService tbTaskService;
 
+    @Resource
+    SysUserRoleService sysUserRoleService;
+
+    @Resource
+    BasicVerifyCodeService basicVerifyCodeService;
+
     @ApiOperation(value = "登录")
     @RequestMapping(value = "/login", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = LoginResult.class)})
@@ -196,4 +200,45 @@ public class SysController {
     public Result filePreview(@ApiParam(value = "附件id", required = true) @RequestParam Long id) throws Exception {
         return ResultUtil.ok(new EditResult(basicAttachmentService.filePreview(id)));
     }
+
+    @ApiOperation(value = "发送验证码")
+    @RequestMapping(value = "/get_verify_code", method = RequestMethod.POST)
+    @Aac(auth = BOOL.FALSE)
+    public Result getVerifyCode(@RequestBody LoginParam loginParam) {
+        Optional.ofNullable(loginParam.getMobileNumber()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("手机号不能为空"));
+        List<SysUser> userList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, loginParam.getMobileNumber()));
+
+        if (!loginParam.getMobileNumber().matches(SystemConstant.REGULAR_EXPRESSION_OF_PHONE)) {
+            throw ExceptionResultEnum.ERROR.exception("手机号[" + loginParam.getMobileNumber() + "]不符合输入规范");
+        }
+
+        //用户不存在
+        if (CollectionUtils.isEmpty(userList)) {
+            throw ExceptionResultEnum.ERROR.exception("手机号不存在");
+        }
+        if (userList.size() > 1) {
+            throw ExceptionResultEnum.ERROR.exception("手机号绑定了多个用户");
+        }
+
+        SysUser sysUser = userList.get(0);
+        if (!sysUser.getEnable()) {
+            throw ExceptionResultEnum.ERROR.exception("手机号被禁用");
+        }
+        return ResultUtil.ok(basicVerifyCodeService.sendVeirfyCode(loginParam.getMobileNumber(), sysUser));
+    }
+
+
+    @ApiOperation(value = "查询用户权限")
+    @RequestMapping(value = "/get_menu", method = RequestMethod.POST)
+    public Result getMenu() {
+//        return ResultUtil.ok(sysUserRoleService.listByUserId());
+        return ResultUtil.ok();
+    }
+
+    @ApiOperation(value = "获取服务器时间")
+    @RequestMapping(value = "/get_system_time", method = RequestMethod.POST)
+    @Aac(auth = BOOL.FALSE)
+    public Result getSystemTime() {
+        return ResultUtil.ok(System.currentTimeMillis());
+    }
 }