|
|
@@ -1,10 +1,14 @@
|
|
|
package com.cyksj.web.controller.manage;
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.cyksj.common.constant.WeChatTemplateConst;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.util.Codec;
|
|
|
@@ -12,9 +16,10 @@ import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.dto.Result;
|
|
|
import com.cyksj.enums.GatewayResponse;
|
|
|
import com.cyksj.mapper.CustomerServiceMapper;
|
|
|
+import com.cyksj.mapper.PhoneCodeMapper;
|
|
|
import com.cyksj.model.dto.WxMpTemplateData;
|
|
|
import com.cyksj.model.dto.WxMpTemplateMessage;
|
|
|
-import com.cyksj.model.entity.CustomerService;
|
|
|
+import com.cyksj.model.entity.PhoneCode;
|
|
|
import com.cyksj.model.manage.dto.AdminInfo;
|
|
|
import com.cyksj.model.manage.entity.Admin;
|
|
|
import com.cyksj.service.mange.AdminService;
|
|
|
@@ -22,6 +27,7 @@ import com.cyksj.service.wechat.WeChatService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
@@ -52,12 +58,14 @@ public class AdminController {
|
|
|
|
|
|
private final CustomerServiceMapper customerServiceMapper;
|
|
|
|
|
|
+ private final PhoneCodeMapper phoneCodeMapper;
|
|
|
+
|
|
|
private static final List<String> SEND_LIST = List.of("o_i5g6V5z5uDpx_SBhJ4ePek8g8k","o_i5g6X99wu2OmC4DcQKXKNmJexA");
|
|
|
|
|
|
@PostMapping(value = "/login")
|
|
|
- public Result<AdminInfo> login(@RequestBody Admin adminRequest) throws Exception {
|
|
|
+ public Result<String> login(@RequestBody Admin adminRequest) throws Exception {
|
|
|
Admin admin = adminService.getOne(
|
|
|
- new QueryWrapper<Admin>().lambda().eq(Admin::getUserName,adminRequest.getUserName()));
|
|
|
+ new QueryWrapper<Admin>().lambda().eq(Admin::getPhone,adminRequest.getPhone()));
|
|
|
|
|
|
if (null == admin) {
|
|
|
throw BusinessRuntimeException.getInstance("该用户不存在.");
|
|
|
@@ -72,46 +80,150 @@ public class AdminController {
|
|
|
if (!StringUtils.equals(admin.getPassWord(), encodePasscode)) {
|
|
|
throw BusinessRuntimeException.getInstance("密码有误.");
|
|
|
}
|
|
|
-
|
|
|
- // 生成token
|
|
|
-
|
|
|
- StpUtil.login(admin.getId());
|
|
|
-
|
|
|
- String token = StpUtil.getTokenValue();
|
|
|
-
|
|
|
- admin.setPassWord("");
|
|
|
-
|
|
|
- AdminInfo info = new AdminInfo();
|
|
|
- info.setInfo(admin);
|
|
|
- info.setToken(token);
|
|
|
- return GatewayResponse.SUCCESS.newBuilder().toResult(info);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
}
|
|
|
|
|
|
@PostMapping("/post")
|
|
|
public Result<String> saveAdmin(@RequestBody @Validated Admin admin) throws Exception {
|
|
|
- if(StringUtils.isNotBlank(admin.getPassWord())){
|
|
|
+ Long adminId = StpUtil.getLoginIdAsLong();
|
|
|
+ Admin exist = adminService.getById(adminId);
|
|
|
+ if (exist == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("账号不存在,请重新登录");
|
|
|
+ }
|
|
|
+ if (exist.getCategory() != Admin.Category.superoot) {
|
|
|
+ throw BusinessRuntimeException.getInstance("无超级管理员权限");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(admin.getPassWord())) {
|
|
|
admin.setPassWord(Codec.DoDigest.custom().setAlgorithm(Codec.DoDigest.Algorithm.SHA256).setStringData(admin.getPassWord()).toBase64String());
|
|
|
}
|
|
|
- if(admin.getId() == null){
|
|
|
- Admin ad = adminService.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUserName, admin.getUserName()));
|
|
|
+ if (admin.getId() == null) {
|
|
|
+ Admin ad = adminService.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getPhone, admin.getPhone()));
|
|
|
if (ad != null) {
|
|
|
throw BusinessRuntimeException.getInstance("该账号已存在.");
|
|
|
}
|
|
|
}
|
|
|
+ boolean mobile = Validator.isMobile(admin.getPhone());
|
|
|
+ if (!mobile) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的手机号");
|
|
|
+ }
|
|
|
|
|
|
- adminService.saveOrUpdate(admin);
|
|
|
+ try {
|
|
|
+ adminService.saveOrUpdate(admin);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机号重复");
|
|
|
+ }
|
|
|
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 删除管理员
|
|
|
+ */
|
|
|
+ @DeleteMapping("/delete/{id}")
|
|
|
+ public Result<String> deleteAdminById(@PathVariable Long id) {
|
|
|
+ Long adminId = StpUtil.getLoginIdAsLong();
|
|
|
+ Admin exist = adminService.getById(adminId);
|
|
|
+ if (exist == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("账号不存在,请重新登录");
|
|
|
+ }
|
|
|
+ if (exist.getCategory() != Admin.Category.superoot) {
|
|
|
+ throw BusinessRuntimeException.getInstance("无超级管理员权限");
|
|
|
+ }
|
|
|
+ if (id.equals(adminId)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("无法删除自己");
|
|
|
+ }
|
|
|
+ adminService.removeById(id);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult("删除成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑管理员
|
|
|
+ */
|
|
|
+ @PutMapping("/put")
|
|
|
+ public Result<String> editAdmin(@RequestBody Admin admin) throws Exception {
|
|
|
+ Long adminId = StpUtil.getLoginIdAsLong();
|
|
|
+ Admin exist = adminService.getById(adminId);
|
|
|
+ if (exist == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("账号不存在,请重新登录");
|
|
|
+ }
|
|
|
+ if (exist.getCategory() != Admin.Category.superoot) {
|
|
|
+ throw BusinessRuntimeException.getInstance("无超级管理员权限");
|
|
|
+ }
|
|
|
+ Admin user = adminService.getById(admin.getId());
|
|
|
+ if (admin.getId() == null || user == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("账号不存在");
|
|
|
+ }
|
|
|
+ Boolean flag = false;
|
|
|
+ if (StrUtil.isNotEmpty(admin.getPhone())) {
|
|
|
+ boolean mobile = Validator.isMobile(admin.getPhone());
|
|
|
+ if (!mobile) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的手机号");
|
|
|
+ }
|
|
|
+ if (!admin.getPhone().equals(user.getPhone())) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotEmpty(admin.getPassWord())) {
|
|
|
+ String encodePwd = Codec.DoDigest.custom().setAlgorithm(Codec.DoDigest.Algorithm.SHA256).setStringData(admin.getPassWord()).toBase64String();
|
|
|
+ admin.setPassWord(encodePwd);
|
|
|
+ if (!encodePwd.equals(user.getPassWord())) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ adminService.saveOrUpdate(admin);
|
|
|
+ if (flag) {
|
|
|
+ StpUtil.logoutByLoginId(admin.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult("编辑成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验手机号获取登录信息
|
|
|
+ */
|
|
|
+ @GetMapping("/get/loginInfo")
|
|
|
+ public Result<AdminInfo> getLoginInfo(String phone, String code) {
|
|
|
+ Admin admin = adminService.getOne(
|
|
|
+ new QueryWrapper<Admin>().lambda().eq(Admin::getPhone, phone));
|
|
|
+ if (null == admin) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该用户不存在.");
|
|
|
+ }
|
|
|
+ if (StrUtil.hasEmpty(phone, code)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入对应信息");
|
|
|
+ }
|
|
|
+ DateTime now = DateTime.now();
|
|
|
+ PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, phone).last("limit 1"));
|
|
|
+ if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
|
|
|
+ }
|
|
|
+ //3分钟失效
|
|
|
+ Date updateTime = phoneCode.getUpdateTime();
|
|
|
+ DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
|
|
|
+ if (now.isAfter(afterFiveMinutes)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
|
|
|
+ }
|
|
|
+ if (!StrUtil.equals(code, phoneCode.getCode())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
|
|
|
+ }
|
|
|
+ // 生成token
|
|
|
+
|
|
|
+ StpUtil.login(admin.getId());
|
|
|
+
|
|
|
+ String token = StpUtil.getTokenValue();
|
|
|
+
|
|
|
+ admin.setPassWord("");
|
|
|
+
|
|
|
+ AdminInfo info = new AdminInfo();
|
|
|
+ info.setInfo(admin);
|
|
|
+ info.setToken(token);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(info);
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("/get/list")
|
|
|
- public Result<List<Admin>> list(){
|
|
|
- List<Admin> list = adminService.list(new LambdaQueryWrapper<Admin>().eq(Admin::getStatus,true));
|
|
|
- list.forEach((admin)->{
|
|
|
- admin.setPassWord("");
|
|
|
- admin.setUserName("");
|
|
|
- });
|
|
|
- return GatewayResponse.SUCCESS.newBuilder().toResult(list);
|
|
|
+ public Result<Page<Admin>> list(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
|
|
|
+ Page<Admin> page = adminService.page(new Page<>(start, limit), new LambdaQueryWrapper<Admin>().eq(Admin::getStatus, true));
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(page);
|
|
|
}
|
|
|
|
|
|
@GetMapping("/send/template")
|
|
|
@@ -175,4 +287,4 @@ public class AdminController {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|