|
@@ -0,0 +1,82 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2018-2025, lengleng All rights reserved.
|
|
|
+ *
|
|
|
+ * Redistribution and use in source and binary forms, with or without
|
|
|
+ * modification, are permitted provided that the following conditions are met:
|
|
|
+ *
|
|
|
+ * Redistributions of source code must retain the above copyright notice,
|
|
|
+ * this list of conditions and the following disclaimer.
|
|
|
+ * Redistributions in binary form must reproduce the above copyright
|
|
|
+ * notice, this list of conditions and the following disclaimer in the
|
|
|
+ * documentation and/or other materials provided with the distribution.
|
|
|
+ * Neither the name of the pig4cloud.com developer nor the names of its
|
|
|
+ * contributors may be used to endorse or promote products derived from
|
|
|
+ * this software without specific prior written permission.
|
|
|
+ * Author: lengleng (wangiegie@gmail.com)
|
|
|
+ */
|
|
|
+
|
|
|
+package com.pig4cloud.pigx.admin.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
+import com.pig4cloud.pigx.admin.api.entity.SysUser;
|
|
|
+import com.pig4cloud.pigx.admin.mapper.SysUserMapper;
|
|
|
+import com.pig4cloud.pigx.admin.service.MobileService;
|
|
|
+import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
|
|
+import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
|
|
+import com.pig4cloud.pigx.common.core.util.R;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author lengleng
|
|
|
+ * @date 2018/11/14
|
|
|
+ * <p>
|
|
|
+ * 手机登录相关业务实现
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class MobileServiceImpl implements MobileService {
|
|
|
+ private final RedisTemplate redisTemplate;
|
|
|
+ private final SysUserMapper userMapper;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送手机验证码
|
|
|
+ * TODO: 调用短信网关发送验证码,测试返回前端
|
|
|
+ *
|
|
|
+ * @param mobile mobile
|
|
|
+ * @return code
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<Boolean> sendSmsCode(String mobile) {
|
|
|
+ SysUser condition = new SysUser();
|
|
|
+ condition.setPhone(mobile);
|
|
|
+ List<SysUser> userList = userMapper.selectList(new EntityWrapper<>(condition));
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(userList)) {
|
|
|
+ log.info("手机号未注册:{}", mobile);
|
|
|
+ return new R<>(Boolean.FALSE, "手机号未注册");
|
|
|
+ }
|
|
|
+
|
|
|
+ Object codeObj = redisTemplate.opsForValue().get(CommonConstant.DEFAULT_CODE_KEY + mobile);
|
|
|
+
|
|
|
+ if (codeObj != null) {
|
|
|
+ log.info("手机号验证码未过期:{},{}", mobile, codeObj);
|
|
|
+ return new R<>(Boolean.FALSE, "手机号未注册");
|
|
|
+ }
|
|
|
+
|
|
|
+ String code = RandomUtil.randomNumbers(4);
|
|
|
+ log.debug("手机号生成验证码成功:{},{}", mobile, code);
|
|
|
+ redisTemplate.opsForValue().set(CommonConstant.DEFAULT_CODE_KEY + mobile, code
|
|
|
+ , SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
|
|
|
+ return new R<>(Boolean.TRUE, code);
|
|
|
+ }
|
|
|
+}
|