Commit eba67b24 authored by 陈勇's avatar 陈勇

合并德才-添加返回的用户信息

parent cb33404f
package com.archser.aserver.controller;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import com.archser.aserver.model.User;
import com.archser.aserver.service.UserService;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;
/**
* 用户相关操作
*
* @author dgq
*
*/
public class UserController extends Controller {
@Inject
UserService userService;
private static final String _INFO = "_INFO";
/**
* 获取用户信息
*/
public void info() {
String username = this.getAttr("username");
User user = User.dao.template("getUser", username).findFirst();
user.remove("password");
this.renderJson(Ret.ok("userInfo", user));
}
/**
* 修改用户密码
* @throws SQLException
*/
public void updatePwd() {
String username = this.getAttr("username");
String password = this.getPara("newPwd");
password = HashKit.sha256(password);
int flag = Db.update(Db.getSql("updatePassword"), password, username);
if (flag == 0) {
this.renderJson(Ret.fail("msg","修改失败"));
return ;
}
try {
Redis.use().hset(username+ _INFO, "password", password);
}catch (Exception e) {
e.printStackTrace();
}
this.renderJson(Ret.ok("msg","修改成功"));
}
/**
* 修改用户
*/
public void updateUser() {
User user = this.getModel(User.class, "user", true);
if (user == null) {
renderJson("msg", "数据参数错误,请重新修改");
return;
}
boolean flag = user.update();
if (flag) {
updateUserForRedis(user);
renderJson(Ret.ok("msg", "数据修改成功!"));
return;
} else {
renderJson(Ret.fail("msg", "Error ! 请联系管理员解决。"));
}
}
/**
* 修改Redis中的用户信息
*@Time:2020年7月10日 - 上午8:48:48
* @author:李德才
* @param: @param user
* @return: void
* @throws
*/
public void updateUserForRedis(User user) {
try {
Iterator<Entry<String, Object>> userIterator = user._getAttrsEntrySet().iterator();
Map<Object, Object> userMap = new HashMap<>();
Entry<String, Object> userAttr = null;
while (userIterator.hasNext()) {
userAttr = userIterator.next();
userMap.put(userAttr.getKey().toString().trim().toLowerCase(), userAttr.getValue());
}
Redis.use().hmset(user.getUsername() + _INFO, userMap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询角色
*/
public void getRoleData() {
String username = this.getAttr("username");
User user = User.dao.template("getUser", username).findFirst();
int userId = user.getId();
List<Record> records = Db.find(Db.getSql("getRolesByUserId"), userId);
List<Integer> ids = new ArrayList<Integer>();
for (Record record : records) {
ids.add(record.getInt("ROLE_ID"));
}
List<Record> dataList = Db.find(Db.getSqlPara("getRole",Kv.by("ids", ids)));
if (dataList != null) {
this.renderJson(Ret.ok("list", dataList));
} else {
this.renderJson(Ret.fail("msg", "未找到数据"));
}
}
/**
* 验证原密码输入的是否正确
*/
public void validateOldPassword() {
String username = this.getAttr("username");
User user = User.dao.template("getUser", username).findFirst();
String oldPwd = getPara("oldPwd");
String encryptionString = HashKit.sha256(oldPwd);
if (encryptionString.equals(user.getPassword())) {
renderJson(Ret.ok());
} else {
renderJson(Ret.fail());
}
}
}
package com.archser.aserver.controller;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import com.archser.aserver.model.Organ;
import com.archser.aserver.model.User;
import com.archser.aserver.service.UserService;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;
/**
* 用户相关操作
*
* @author dgq
*
*/
public class UserController extends Controller {
@Inject
UserService userService;
private static final String _INFO = "_INFO";
/**
* 获取用户信息
*/
public void info() {
String username = this.getAttr("username");
User user = User.dao.template("getUser", username).findFirst();
user.remove("password");
Organ organ = Organ.dao.findById(user.getOrganId());
if (organ != null) {
Iterator<Entry<String, Object>> organIterator = organ._getAttrsEntrySet().iterator();
Entry<String, Object> organAttr = null;
while (organIterator.hasNext()) {
organAttr = organIterator.next();
user.put("ORGAN_" + organAttr.getKey(), organAttr.getValue());
}
}
this.renderJson(Ret.ok("userInfo", user));
}
/**
* 修改用户密码
* @throws SQLException
*/
public void updatePwd() {
String username = this.getAttr("username");
String password = this.getPara("newPwd");
password = HashKit.sha256(password);
int flag = Db.update(Db.getSql("updatePassword"), password, username);
if (flag == 0) {
this.renderJson(Ret.fail("msg","修改失败"));
return ;
}
try {
Redis.use().hset(username+ _INFO, "password", password);
}catch (Exception e) {
e.printStackTrace();
}
this.renderJson(Ret.ok("msg","修改成功"));
}
/**
* 修改用户
*/
public void updateUser() {
User user = this.getModel(User.class, "user", true);
if (user == null) {
renderJson("msg", "数据参数错误,请重新修改");
return;
}
boolean flag = user.update();
if (flag) {
updateUserForRedis(user);
renderJson(Ret.ok("msg", "数据修改成功!"));
return;
} else {
renderJson(Ret.fail("msg", "Error ! 请联系管理员解决。"));
}
}
/**
* 修改Redis中的用户信息
*@Time:2020年7月10日 - 上午8:48:48
* @author:李德才
* @param: @param user
* @return: void
* @throws
*/
public void updateUserForRedis(User user) {
try {
Iterator<Entry<String, Object>> userIterator = user._getAttrsEntrySet().iterator();
Map<Object, Object> userMap = new HashMap<>();
Entry<String, Object> userAttr = null;
while (userIterator.hasNext()) {
userAttr = userIterator.next();
userMap.put(userAttr.getKey().toString().trim().toLowerCase(), userAttr.getValue());
}
Redis.use().hmset(user.getUsername() + _INFO, userMap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询角色
*/
public void getRoleData() {
String username = this.getAttr("username");
User user = User.dao.template("getUser", username).findFirst();
int userId = user.getId();
List<Record> records = Db.find(Db.getSql("getRolesByUserId"), userId);
List<Integer> ids = new ArrayList<Integer>();
for (Record record : records) {
ids.add(record.getInt("ROLE_ID"));
}
List<Record> dataList = Db.find(Db.getSqlPara("getRole",Kv.by("ids", ids)));
if (dataList != null) {
this.renderJson(Ret.ok("list", dataList));
} else {
this.renderJson(Ret.fail("msg", "未找到数据"));
}
}
/**
* 验证原密码输入的是否正确
*/
public void validateOldPassword() {
String username = this.getAttr("username");
User user = User.dao.template("getUser", username).findFirst();
String oldPwd = getPara("oldPwd");
String encryptionString = HashKit.sha256(oldPwd);
if (encryptionString.equals(user.getPassword())) {
renderJson(Ret.ok());
} else {
renderJson(Ret.fail());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment