| @ -0,0 +1,53 @@ | |||
| package com.fxzy.warn.common.constants; | |||
| /** | |||
| * | |||
| * 用户常量 | |||
| * @param | |||
| * @return | |||
| * @author zhangjing | |||
| * @create 2024/12/4 | |||
| **/ | |||
| public class UserConstants { | |||
| /** | |||
| * 登录类型 | |||
| * PASSWORD_LOGIN:账户密码登录 | |||
| * SMS_CAPTCHA_LOGIN:短信验证码登录 | |||
| */ | |||
| public final static Integer PASSWORD_LOGIN = 1; | |||
| public final static Integer SMS_CAPTCHA_LOGIN = 2; | |||
| /** | |||
| * 用户是否被锁定 | |||
| * LOCKED:是 | |||
| * UN_LOCK:否 | |||
| */ | |||
| public final static Integer LOCKED = 1; | |||
| public final static Integer UN_LOCK = 0; | |||
| /** | |||
| * 用户密码错误默认次数 | |||
| */ | |||
| public final static Integer PASSWORD_WRONG_DEF = 0; | |||
| /** | |||
| * 用户密码错误上限次数 | |||
| */ | |||
| public final static Integer PASSWORD_WRONG_LIMIT = 5; | |||
| /** | |||
| * 锁定时间5分钟 | |||
| */ | |||
| public final static Integer LOCK_TIME = 300; | |||
| /** | |||
| * 登录验证码类型 | |||
| */ | |||
| public final static Integer RANDOM_CAPTCHA = 1; | |||
| } | |||
| @ -0,0 +1,43 @@ | |||
| package com.fxzy.warn.common.util; | |||
| import com.aliyuncs.DefaultAcsClient; | |||
| import com.aliyuncs.IAcsClient; | |||
| import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; | |||
| import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; | |||
| import com.aliyuncs.exceptions.ClientException; | |||
| import com.aliyuncs.profile.DefaultProfile; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/11/22 13:57 | |||
| * @description | |||
| */ | |||
| public class SMSUtils { | |||
| /** | |||
| * 发送短信 | |||
| * @param signName 签名 | |||
| * @param templateCode 模板 | |||
| * @param phoneNumbers 手机号 | |||
| * @param param 参数 | |||
| */ | |||
| public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param,String accessKeyId,String secret){ | |||
| //TODO 改成自己的 | |||
| DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret); | |||
| IAcsClient client = new DefaultAcsClient(profile); | |||
| SendSmsRequest request = new SendSmsRequest(); | |||
| request.setSysRegionId("cn-hangzhou"); | |||
| request.setPhoneNumbers(phoneNumbers); | |||
| request.setSignName(signName); | |||
| request.setTemplateCode(templateCode); | |||
| request.setTemplateParam("{\"code\":\""+param+"\"}"); | |||
| try { | |||
| SendSmsResponse response = client.getAcsResponse(request); | |||
| System.out.println("短信发送成功"); | |||
| }catch (ClientException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| } | |||
| @ -0,0 +1,122 @@ | |||
| package com.fxzy.warn.controller; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.fxzy.warn.common.request.RequestParameter; | |||
| import com.fxzy.warn.model.User; | |||
| import com.fxzy.warn.service.UserService; | |||
| import com.fxzy.warn.common.constants.ResponseMsgConstants; | |||
| import com.fxzy.warn.common.response.ApiResponse; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import io.swagger.annotations.ApiParam; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import org.apache.commons.lang.StringUtils; | |||
| import org.springframework.http.MediaType; | |||
| 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.RestController; | |||
| import javax.annotation.Resource; | |||
| import java.util.Objects; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/05/21 11:22 | |||
| * @description | |||
| */ | |||
| @Api(tags = "登录与账号相关") | |||
| @RestController | |||
| @RequestMapping("account/") | |||
| @Slf4j | |||
| public class AccountController { | |||
| @Resource | |||
| private UserService userService ; | |||
| @RequestMapping(value = "sendCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | |||
| @ApiOperation(value = "发送验证码") | |||
| public ApiResponse sendCode(@ApiParam("{\n" + | |||
| "\"phoneNumber\":\"电话号码\",\n" + | |||
| "}") @RequestBody JSONObject jsonObject) { | |||
| ApiResponse response = new ApiResponse(); | |||
| try { | |||
| return userService.sendCode(jsonObject); | |||
| } catch (Exception e) { | |||
| response.recordError(ResponseMsgConstants.OPERATE_FAIL); | |||
| } | |||
| return response; | |||
| } | |||
| @RequestMapping(value = "login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | |||
| @ApiOperation(value = "登录") | |||
| public ApiResponse login(@ApiParam("{\n" + | |||
| "\"loginType\":\"登录类型:1.账户密码登录,2:手机短信验证码登录\",\n" + | |||
| "\"phoneNumber\":\"电话号码\",\n" + | |||
| "\"password\":\"密码密文\",\n" + | |||
| "\"code\":\"随机验证码\"\n" + | |||
| "}") @RequestBody JSONObject jsonObject) { | |||
| ApiResponse response = new ApiResponse(); | |||
| try { | |||
| return userService.login(jsonObject); | |||
| } catch (Exception e) { | |||
| response.recordError(ResponseMsgConstants.OPERATE_FAIL); | |||
| } | |||
| return response; | |||
| } | |||
| @RequestMapping(value = "createUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | |||
| @ApiOperation(value = "创建用户") | |||
| public ApiResponse createUser(@ApiParam("{\n" + | |||
| "\"companyName\":\"企业名称\",\n" + | |||
| "\"creditCode\":\"统一社会信用代码\",\n" + | |||
| "\"phoneNumber\":\"手机号\",\n" + | |||
| "\"email\":\"邮箱\"\n" + | |||
| "\"province\":\"所在省\"\n" + | |||
| "\"city\":\"所在市\"\n" + | |||
| "\"district\":\"所在区\"\n" + | |||
| "\"detailedAddress\":\"详细地址\"\n" + | |||
| "\"businessPerson\":\"商务负责人\"\n" + | |||
| "\"remarks\":\"备注\"\n" + | |||
| "}") @RequestBody User entity, String ticket) { | |||
| ApiResponse response = new ApiResponse(); | |||
| try { | |||
| return userService.createUser(entity,ticket); | |||
| } catch (Exception e) { | |||
| response.recordError(ResponseMsgConstants.OPERATE_FAIL); | |||
| } | |||
| return response; | |||
| } | |||
| @RequestMapping(value = "queryPage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) | |||
| @ApiOperation(value = "分页查询") | |||
| public ApiResponse queryPage(@ApiParam() @RequestBody RequestParameter parameter) { | |||
| log.info("分页查询 ==== 参数{" + parameter.toString() + "}"); | |||
| ApiResponse apiResponse = new ApiResponse(); | |||
| if (!Objects.isNull(parameter) && !Objects.isNull(parameter.getParameter())) { | |||
| try { | |||
| apiResponse.setData(userService.queryPage(parameter)); | |||
| apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS); | |||
| } catch (Exception e) { | |||
| log.error("查询错误,errMsg==={}", e.getMessage()); | |||
| e.printStackTrace(); | |||
| apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL); | |||
| } | |||
| } else { | |||
| apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL); | |||
| } | |||
| return apiResponse; | |||
| } | |||
| // @RequestMapping(value = "loginOut", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | |||
| // @ApiOperation(value = "退出系统") | |||
| // public ApiResponse logOut(@ApiParam("{\n" | |||
| // + " \"ticket\":\"ticket\",\n" | |||
| // + "}") @RequestBody JSONObject object) { | |||
| // ApiResponse response = new ApiResponse(); | |||
| // String ticket = object.getString("ticket"); | |||
| // if (StringUtils.isBlank(ticket)) { | |||
| // response.recordError("ticket不能为空"); | |||
| // return response; | |||
| // } | |||
| // //调用全局登出 | |||
| // response = accountService.logout(ticket); | |||
| // return response; | |||
| // } | |||
| } | |||
| @ -0,0 +1,14 @@ | |||
| package com.fxzy.warn.mapper; | |||
| import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
| import com.fxzy.warn.model.User; | |||
| import org.apache.ibatis.annotations.Mapper; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/12/04 14:48 | |||
| * @description | |||
| */ | |||
| @Mapper | |||
| public interface UserMapper extends BaseMapper<User> { | |||
| } | |||
| @ -0,0 +1,35 @@ | |||
| package com.fxzy.warn.model; | |||
| import com.baomidou.mybatisplus.annotation.IdType; | |||
| import com.baomidou.mybatisplus.annotation.TableId; | |||
| import com.baomidou.mybatisplus.annotation.TableName; | |||
| import io.swagger.annotations.ApiModelProperty; | |||
| import lombok.Data; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/12/04 20:09 | |||
| * @description | |||
| */ | |||
| @Data | |||
| @TableName("t_authorization") | |||
| public class Authorization extends BaseField{ | |||
| /** | |||
| * id | |||
| */ | |||
| @ApiModelProperty("id") | |||
| @TableId(type = IdType.AUTO) | |||
| private Integer id; | |||
| @ApiModelProperty("授权开始日期") | |||
| private String startDate; | |||
| @ApiModelProperty("授权截止日期") | |||
| private String endDate; | |||
| @ApiModelProperty("授权书id") | |||
| private String fileId; | |||
| @ApiModelProperty("风险查询次数") | |||
| private Integer queryCount; | |||
| } | |||
| @ -0,0 +1,52 @@ | |||
| package com.fxzy.warn.model; | |||
| import com.baomidou.mybatisplus.annotation.FieldFill; | |||
| import com.baomidou.mybatisplus.annotation.TableField; | |||
| import com.baomidou.mybatisplus.extension.activerecord.Model; | |||
| import com.fasterxml.jackson.annotation.JsonFormat; | |||
| import io.swagger.annotations.ApiModelProperty; | |||
| import lombok.Data; | |||
| import java.util.Date; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/04/14 10:05 | |||
| * @description | |||
| */ | |||
| @Data | |||
| public class BaseField extends Model<BaseField> { | |||
| /** | |||
| * 创建时间 | |||
| */ | |||
| @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") | |||
| @ApiModelProperty("创建时间") | |||
| @TableField(value = "create_time", fill = FieldFill.INSERT) | |||
| private Date createTime; | |||
| /** | |||
| * 修改时间 | |||
| */ | |||
| @ApiModelProperty("修改时间") | |||
| @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") | |||
| @TableField(value = "update_time", fill = FieldFill.UPDATE) | |||
| private Date updateTime; | |||
| /** | |||
| * 创建人 | |||
| */ | |||
| @ApiModelProperty("创建人") | |||
| @TableField(value = "creator_id", fill = FieldFill.INSERT) | |||
| private Integer creatorId; | |||
| /** | |||
| * 修改人 | |||
| */ | |||
| @ApiModelProperty("修改人") | |||
| @TableField(value = "updater_id", fill = FieldFill.UPDATE) | |||
| private Integer updaterId; | |||
| /** | |||
| * 是否删除 | |||
| */ | |||
| @ApiModelProperty("是否删除") | |||
| @TableField(value = "is_del") | |||
| private Integer isDel; | |||
| } | |||
| @ -0,0 +1,65 @@ | |||
| package com.fxzy.warn.model; | |||
| import com.baomidou.mybatisplus.annotation.IdType; | |||
| import com.baomidou.mybatisplus.annotation.TableId; | |||
| import com.baomidou.mybatisplus.annotation.TableName; | |||
| import io.swagger.annotations.ApiModelProperty; | |||
| import lombok.Data; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/12/04 14:27 | |||
| * @description | |||
| */ | |||
| @Data | |||
| @TableName("t_user") | |||
| public class User extends BaseField{ | |||
| /** | |||
| * id | |||
| */ | |||
| @ApiModelProperty("id") | |||
| @TableId(type = IdType.AUTO) | |||
| private Integer id; | |||
| /** | |||
| * 企业名称 | |||
| */ | |||
| @ApiModelProperty("企业名称") | |||
| private String companyName; | |||
| @ApiModelProperty("统一社会信用代码") | |||
| private String creditCode; | |||
| @ApiModelProperty("手机号") | |||
| private String phoneNumber; | |||
| @ApiModelProperty("邮箱") | |||
| private String email; | |||
| @ApiModelProperty("所在省") | |||
| private String province; | |||
| @ApiModelProperty("所在市") | |||
| private String city; | |||
| @ApiModelProperty("所在区") | |||
| private String district; | |||
| @ApiModelProperty("详细地址") | |||
| private String detailedAddress; | |||
| @ApiModelProperty("商务负责人") | |||
| private String businessPerson; | |||
| @ApiModelProperty("备注") | |||
| private String remarks; | |||
| @ApiModelProperty("授权id") | |||
| private int authorizationId; | |||
| @ApiModelProperty("是否为正式账号") | |||
| private int isOfficialAccount; | |||
| @ApiModelProperty("密码") | |||
| private String password; | |||
| } | |||
| @ -0,0 +1,76 @@ | |||
| package com.fxzy.warn.service; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||
| import com.baomidou.mybatisplus.extension.service.IService; | |||
| import com.fxzy.warn.common.request.RequestParameter; | |||
| import com.fxzy.warn.common.response.ApiResponse; | |||
| import com.fxzy.warn.model.Test; | |||
| import com.fxzy.warn.model.User; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/12/04 14:49 | |||
| * @description | |||
| */ | |||
| public interface UserService extends IService<User> { | |||
| /** | |||
| * | |||
| * 登录 | |||
| * @param jsonObject | |||
| * @return com.fxzy.warn.common.response.ApiResponse | |||
| * @author zhangjing | |||
| * @create 2024/12/4 | |||
| **/ | |||
| ApiResponse sendCode(JSONObject jsonObject); | |||
| /** | |||
| * | |||
| * 登录 | |||
| * @param jsonObject | |||
| * @return com.fxzy.warn.common.response.ApiResponse | |||
| * @author zhangjing | |||
| * @create 2024/12/4 | |||
| **/ | |||
| ApiResponse login(JSONObject jsonObject); | |||
| /** | |||
| * | |||
| * 创建用户 | |||
| * @param entity | |||
| * @return com.fxzy.warn.common.response.ApiResponse | |||
| * @author zhangjing | |||
| * @create 2024/12/4 | |||
| **/ | |||
| ApiResponse createUser(User entity,String ticket); | |||
| /** | |||
| * 保存 | |||
| * @param entity | |||
| * @return | |||
| */ | |||
| boolean saveModel(User entity); | |||
| /** | |||
| * 修改 | |||
| * @param entity | |||
| * @return | |||
| */ | |||
| boolean updateModel(User entity); | |||
| /** | |||
| * 删除 | |||
| * @param entity | |||
| * @return | |||
| */ | |||
| boolean deleteModel(User entity); | |||
| /** | |||
| * 分页查询 | |||
| * @param parameter | |||
| * @return | |||
| */ | |||
| Page<User> queryPage(RequestParameter parameter); | |||
| } | |||
| @ -0,0 +1,161 @@ | |||
| package com.fxzy.warn.service.impl; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||
| import com.fxzy.warn.common.constants.ResponseMsgConstants; | |||
| import com.fxzy.warn.common.constants.UserConstants; | |||
| import com.fxzy.warn.common.request.RequestParameter; | |||
| import com.fxzy.warn.common.response.ApiResponse; | |||
| import com.fxzy.warn.common.util.RedisUtil; | |||
| import com.fxzy.warn.common.util.SMSUtils; | |||
| import com.fxzy.warn.mapper.UserMapper; | |||
| import com.fxzy.warn.model.User; | |||
| import com.fxzy.warn.service.UserService; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.beans.factory.annotation.Value; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.Random; | |||
| /** | |||
| * @author zhangjing | |||
| * @date 2024/12/04 14:51 | |||
| * @description | |||
| */ | |||
| @Service | |||
| public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements | |||
| UserService { | |||
| @Autowired | |||
| private RedisUtil redisUtil; | |||
| @Value("${ali.accessKeyId}") | |||
| private String accessKeyId; | |||
| @Value("${ali.secret}") | |||
| private String secret; | |||
| @Override | |||
| public ApiResponse sendCode(JSONObject jsonObject) { | |||
| ApiResponse response = new ApiResponse(); | |||
| //账户名:手机号 | |||
| String phoneNumber = jsonObject.getString("phoneNumber"); | |||
| QueryWrapper<User> queryWrapper = new QueryWrapper<>(); | |||
| queryWrapper.eq("phone_number", phoneNumber); | |||
| //用户是否存在 | |||
| User user = getOne(queryWrapper); | |||
| if (user == null) { | |||
| response.recordError(ResponseMsgConstants.USER_NOT_FOUND); | |||
| return response; | |||
| } | |||
| String number = redisUtil.getString("code:"+phoneNumber); | |||
| if (number != null) { | |||
| response.recordError(ResponseMsgConstants.CODE_SENT_SUCCESSFULLY); | |||
| return response; | |||
| } | |||
| //生成6为随机验证码 | |||
| number = String.format("%06d", new Random().nextInt(1000000)); | |||
| //存Redis | |||
| redisUtil.setString("code:"+phoneNumber,number,60*5); | |||
| SMSUtils.sendMessage("","","","",accessKeyId,secret); | |||
| return response; | |||
| } | |||
| @Override | |||
| public ApiResponse login(JSONObject jsonObject) { | |||
| ApiResponse response = new ApiResponse(); | |||
| //登录类型:1账户密码登录,2:手机短信验证码登录 | |||
| Integer loginType = jsonObject.getInteger("loginType"); | |||
| //账户名:手机号 | |||
| String phoneNumber = jsonObject.getString("phoneNumber"); | |||
| //密码 | |||
| String password = jsonObject.getString("password"); | |||
| //验证码 | |||
| String code = jsonObject.getString("code"); | |||
| if (loginType.equals(UserConstants.SMS_CAPTCHA_LOGIN)){ | |||
| //验证码登录 | |||
| String number = redisUtil.getString("code:"+phoneNumber); | |||
| if (number == null) { | |||
| response.recordError(ResponseMsgConstants.CODE_EXPIRED_PLEASE_RESEND); | |||
| return response; | |||
| } | |||
| //登录成功 | |||
| if (number.equals(code)){ | |||
| QueryWrapper<User> queryWrapper = new QueryWrapper<>(); | |||
| queryWrapper.eq("phone_number", phoneNumber); | |||
| User user = getOne(queryWrapper); | |||
| response.setData(user); | |||
| } | |||
| }else { | |||
| //密码登录 | |||
| QueryWrapper<User> queryWrapper = new QueryWrapper<>(); | |||
| queryWrapper.eq("phone_number", phoneNumber); | |||
| User user = getOne(queryWrapper); | |||
| if (user == null) { | |||
| response.recordError(ResponseMsgConstants.USER_NOT_FOUND); | |||
| return response; | |||
| } | |||
| if (!user.getPassword().equals(password)){ | |||
| response.recordError(ResponseMsgConstants.PASSWORD_ERROR); | |||
| return response; | |||
| } | |||
| response.setData(user); | |||
| } | |||
| return response; | |||
| } | |||
| @Override | |||
| public ApiResponse createUser(User entity, String ticket) { | |||
| ApiResponse response = new ApiResponse(); | |||
| QueryWrapper<User> queryWrapper = new QueryWrapper<>(); | |||
| queryWrapper.eq("phone_number", entity.getPhoneNumber()); | |||
| //用户是否存在 | |||
| User user = getOne(queryWrapper); | |||
| if (user!=null){ | |||
| response.recordError(ResponseMsgConstants.USER_PHONE_EXIST); | |||
| return response; | |||
| } | |||
| save(entity); | |||
| return response; | |||
| } | |||
| @Override | |||
| public boolean saveModel(User entity) { | |||
| boolean result = this.save(entity); | |||
| return result; | |||
| } | |||
| @Override | |||
| public boolean updateModel(User entity) { | |||
| boolean result = this.updateById(entity); | |||
| return result; | |||
| } | |||
| @Override | |||
| public boolean deleteModel(User entity) { | |||
| boolean result = this.removeById(entity); | |||
| return result; | |||
| } | |||
| @Override | |||
| public Page<User> queryPage(RequestParameter parameter) { | |||
| User entity = parameter.getParameter().toJavaObject(User.class); | |||
| Page<User> page = new Page<User>(parameter.getCurrent(), parameter.getSize()); | |||
| page.setSearchCount(true); | |||
| page.setOptimizeCountSql(true); | |||
| QueryWrapper<User> eWrapper = new QueryWrapper<User>(entity); | |||
| Page<User> result = this.page(page, eWrapper); | |||
| setInfo(result); | |||
| return result; | |||
| } | |||
| private void setInfo(Page<User> result) { | |||
| for (User user : result.getRecords()) { | |||
| // 这里可以添加需要返回的字段的填充 | |||
| } | |||
| } | |||
| } | |||