|
|
@@ -8,6 +8,7 @@ import com.jfinal.kit.StrKit;
|
|
|
import common.interceptor.LoginInterceptor;
|
|
|
import common.interceptor.empty.EmptyInterceptor;
|
|
|
import common.interceptor.empty.EmptyInterface;
|
|
|
+import common.interceptor.role.RequiredRoleInterface;
|
|
|
import common.model.User;
|
|
|
import common.utils.http.MyController;
|
|
|
import common.utils.http.MyRet;
|
|
|
@@ -30,7 +31,7 @@ public class UserController extends MyController {
|
|
|
renderText(service.hello());
|
|
|
}
|
|
|
|
|
|
- @EmptyInterface(keyArray = {"mobile_number"})
|
|
|
+ @EmptyInterface({"mobile_number"})
|
|
|
public void sendVerifyCode() {
|
|
|
// --- 核心修改部分 ---
|
|
|
// 通过 MyController 获取解析后的 JSON 对象,拦截器也使用了这个方法
|
|
|
@@ -73,7 +74,7 @@ public class UserController extends MyController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @EmptyInterface(keyArray = {"mobile_number", "pwd_md5", "repeat_pwd_md5", "verify_code"})
|
|
|
+ @EmptyInterface({"mobile_number", "pwd_md5", "repeat_pwd_md5", "verify_code"})
|
|
|
public void register() {
|
|
|
// --- 核心修改部分:从 JSON 请求体中获取参数 ---
|
|
|
JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
@@ -120,9 +121,9 @@ public class UserController extends MyController {
|
|
|
renderJson(service.saveUser(user));
|
|
|
}
|
|
|
|
|
|
- @EmptyInterface(keyArray = {"mobile_number"})
|
|
|
+ @EmptyInterface({"mobile_number"})
|
|
|
public void login() {
|
|
|
- // --- 核心修改部分:从 JSON 请求体中获取参数 ---
|
|
|
+ // --- 从 JSON 请求体中获取参数 ---
|
|
|
JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
|
|
|
// 因为 EmptyInterceptor 已经保证了这些字段不为空,这里可以直接获取
|
|
|
@@ -139,11 +140,11 @@ public class UserController extends MyController {
|
|
|
// 2.判断是使用密码还是验证码登录
|
|
|
MyRet ret = MyRet.fail("违规操作会导致ip封禁!");
|
|
|
// 如果传入了密码优先使用密码登录
|
|
|
- if (!StrKit.isBlank(pwdMd5)) {
|
|
|
+ if (StrKit.notBlank(pwdMd5)) {
|
|
|
ret = service.login(mobileNumber, HashKit.md5(pwdMd5));
|
|
|
}
|
|
|
// 如果有验证码传入进行验证码校验
|
|
|
- if (!StrKit.isBlank(verifyCode)) {
|
|
|
+ if (StrKit.notBlank(verifyCode)) {
|
|
|
MyRet verifyCodeRet = checkVerifyCode(verifyCode);
|
|
|
if (!verifyCodeRet.isOk()) {
|
|
|
renderJson(verifyCodeRet);
|
|
|
@@ -154,8 +155,13 @@ public class UserController extends MyController {
|
|
|
}
|
|
|
|
|
|
if (ret.isOk()) {
|
|
|
+ // 更新用户时间
|
|
|
+ service.updateUserLoginTime(mobileNumber);
|
|
|
+
|
|
|
+ // 将token传回前端
|
|
|
ret.set("token", createToken("dl-token"));
|
|
|
|
|
|
+ // 其它参数的封装
|
|
|
setSessionAttr("id", ((User)ret.get("data")).getStr("id"));
|
|
|
setSessionAttr("mobile_number", ((User)ret.get("data")).getStr("mobile_number"));
|
|
|
setSessionAttr("role", ((User)ret.get("data")).getStr("role"));
|
|
|
@@ -178,7 +184,7 @@ public class UserController extends MyController {
|
|
|
renderJson(MyRet.ok("获取成功").setData(user));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void logout() {
|
|
|
removeSessionAttr("id");
|
|
|
removeSessionAttr("mobile_number");
|
|
|
@@ -188,29 +194,122 @@ public class UserController extends MyController {
|
|
|
renderJson(MyRet.ok("已成功登出。"));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新用户的方法,传什么更新什么,不传就不更新,id必须传
|
|
|
+ */
|
|
|
+ @Before(LoginInterceptor.class)
|
|
|
+ @RequiredRoleInterface({UserController.ROLE_SUPER_ADMIN})
|
|
|
+ @EmptyInterface({"id"})
|
|
|
+ public void updateByAdmin() {
|
|
|
+ JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
+ String id = requestBodyJson.getString("id");
|
|
|
+
|
|
|
+ User user = service.findUserById(id);
|
|
|
+
|
|
|
+ // 检查用户合法性
|
|
|
+ if (user == null) {
|
|
|
+ renderJson(MyRet.fail("用户获取不合法,该id对应的用户不存在,不要乱传参数。"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验昵称是否需要修改
|
|
|
+ String nickname = requestBodyJson.getString("nickname");
|
|
|
+ if (StrKit.notBlank(nickname)) {
|
|
|
+ user.set("nickname", nickname);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验手机号是否需要修改
|
|
|
+ String mobileNumber = requestBodyJson.getString("mobile_number");
|
|
|
+ if (StrKit.notBlank(mobileNumber)) {
|
|
|
+ user.set("mobile_number", mobileNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 密码修改
|
|
|
+ String pwdMd5 = requestBodyJson.getString("pwd_md5");
|
|
|
+ if (StrKit.notBlank(pwdMd5)) {
|
|
|
+ user.set("pwd_md5_md5", HashKit.md5(pwdMd5));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 权限修改
|
|
|
+ String roleStr = requestBodyJson.getString("role");
|
|
|
+ if (StrKit.notBlank(roleStr)) {
|
|
|
+ try {
|
|
|
+ int roleInt = Integer.parseInt(roleStr); // 转换为 int 类型
|
|
|
+
|
|
|
+ if (roleInt == ROLE_SUPER_ADMIN) {
|
|
|
+ renderJson(MyRet.fail("为了安全考虑!不能在该接口设置超级管理员!请联系开发者处理!"));
|
|
|
+ return;
|
|
|
+ } else if (user.getInt("role") == 0) {
|
|
|
+ renderJson(MyRet.fail("为了安全考虑!不能在该接口将超级管理员降级!请联系开发者处理!"));
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ user.set("role", roleInt);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ // 处理转换失败的情况,例如记录日志,或者返回错误信息给前端
|
|
|
+ renderJson(MyRet.fail("角色值格式不正确"));
|
|
|
+ return; // 中断后续操作
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 推荐人修改
|
|
|
+ String referrerIdStr = requestBodyJson.getString("referrer_id");
|
|
|
+ if (StrKit.notBlank(referrerIdStr)) {
|
|
|
+ try {
|
|
|
+ long referrerIdLong = Long.parseLong(referrerIdStr); // 或者 int,根据数据库字段大小
|
|
|
+ user.set("referrer_id", referrerIdLong);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ renderJson(MyRet.fail("推荐人ID格式不正确"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 积分修改
|
|
|
+ String integralStr = requestBodyJson.getString("integral");
|
|
|
+ if (StrKit.notBlank(integralStr)) {
|
|
|
+ try {
|
|
|
+ int integralInt = Integer.parseInt(integralStr);
|
|
|
+ user.set("integral", integralInt);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ renderJson(MyRet.fail("积分值格式不正确"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新时间
|
|
|
+ user.set("update_time", System.currentTimeMillis());
|
|
|
+
|
|
|
+ // 执行更新业务
|
|
|
+ if (user.update()) {
|
|
|
+ renderJson(MyRet.ok("用户更新成功").setData(service.findUserByMobileNumber(user.getMobileNumber())));
|
|
|
+ } else {
|
|
|
+ renderJson(MyRet.fail("用户更新失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
*
|
|
|
-### 验证码修改密码
|
|
|
-POST {{ baseUrl }}/user/updatePwd
|
|
|
-Content-Type: application/json
|
|
|
-
|
|
|
-{
|
|
|
- "mobile_number": "17781855864",
|
|
|
- "new_pwd_md5": "e10adc3949ba59abbe56e057f20f883e",
|
|
|
- "verify_code": "9119"
|
|
|
-}
|
|
|
+ ### 验证码修改密码
|
|
|
+ POST {{ baseUrl }}/user/updatePwd
|
|
|
+ Content-Type: application/json
|
|
|
+
|
|
|
+ {
|
|
|
+ "mobile_number": "17781855864",
|
|
|
+ "new_pwd_md5": "e10adc3949ba59abbe56e057f20f883e",
|
|
|
+ "verify_code": "9119"
|
|
|
+ }
|
|
|
*
|
|
|
* */
|
|
|
-// @EmptyInterface(keyArray = {"mobile_number", "new_pwd_md5", "verify_code"})
|
|
|
-// public void updatePwd() {
|
|
|
-// // --- 核心修改部分:从 JSON 请求体中获取参数 ---
|
|
|
-// JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
-//
|
|
|
-// // 因为 EmptyInterceptor 已经保证了这些字段不为空,这里可以直接获取
|
|
|
-// String mobileNumber = requestBodyJson.getString("mobile_number");
|
|
|
-// String pwdMd5 = requestBodyJson.getString("new_pwd_md5");
|
|
|
-// String verifyCode = requestBodyJson.getString("verify_code");
|
|
|
-// }
|
|
|
+ // @EmptyInterface(keyArray = {"mobile_number", "new_pwd_md5", "verify_code"})
|
|
|
+ // public void updatePwd() {
|
|
|
+ // // --- 核心修改部分:从 JSON 请求体中获取参数 ---
|
|
|
+ // JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
+ //
|
|
|
+ // // 因为 EmptyInterceptor 已经保证了这些字段不为空,这里可以直接获取
|
|
|
+ // String mobileNumber = requestBodyJson.getString("mobile_number");
|
|
|
+ // String pwdMd5 = requestBodyJson.getString("new_pwd_md5");
|
|
|
+ // String verifyCode = requestBodyJson.getString("verify_code");
|
|
|
+ // }
|
|
|
|
|
|
private MyRet checkVerifyCode(String userVerifyCode) {
|
|
|
// 3. 验证码校验
|