提交 57853c2a 编写于 作者: Veltuss.'s avatar Veltuss.

user功能模块

上级 f052fa4e
......@@ -2,6 +2,7 @@ package com.nav.controller;
import com.nav.pojo.User;
import com.nav.service.UserService;
import com.nav.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
......@@ -31,6 +32,7 @@ public class UserController {
result.put("name", userInfo.getName());
result.put("phone", userInfo.getPhone());
result.put("address", userInfo.getAddress());
result.put("ip", IpUtil.getIpAddr(req));
}
else{
result.put("msg", "failed");
......@@ -47,16 +49,36 @@ public class UserController {
@PostMapping("register")
@ResponseBody
public Map<String,Object> register(@RequestBody Map<String,String> user){
public Map<String,Object> register(@RequestBody Map<String,String> user,HttpServletRequest request){
Map<String,Object> result=new HashMap<>();
String phone=user.get("phone");
String password=user.get("password");
String name=user.get("name");
String address=user.get("address");
boolean record=userService.addUser(phone,password,name,address);
String IP=IpUtil.getIpAddr(request);
boolean record=userService.addUser(phone,password,name,address,IP);
if(!record){
result.put("message","failed");
}
return result;
}
@PostMapping("info")
@ResponseBody
public Map<String,Object> getUserInfo(@RequestBody Map<String,Object> user){
Map<String,Object> result=new HashMap<>();
Long userId=(Long)user.get("id");
User userInfo=userService.selectUserInfo(userId);
result.put("name", userInfo.getName());
result.put("phone", userInfo.getPhone());
result.put("address", userInfo.getAddress());
return result;
}
@RequestMapping(value = "/getIp", method = RequestMethod.GET)
@ResponseBody
public String getIp(HttpServletRequest request) {
return IpUtil.getIpAddr(request);
}
}
......@@ -16,10 +16,14 @@ public interface UserDao {
public List<User> getUserList();
//添加新用户
public void insertUser(String phone,String password,String name,String address);
public void insertUser(String phone,String password,String name,String address,String ip);
//修改用户状态
public void updateUserState();
//修改用户密码
public void updatePassword(String pssword);
//查询用户信息
public User selectUserById(Long id);
}
......@@ -3,9 +3,11 @@ package com.nav.service;
import com.nav.dao.ActivityDao;
import com.nav.pojo.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AdminServiceImpl implements AdminService{
@Autowired
ActivityDao activityDao;
......
......@@ -12,5 +12,12 @@ public interface UserService {
public List<User> getUserList();
//添加新用户
public boolean addUser(String phone,String password,String name,String address);
public boolean addUser(String phone,String password,String name,String address,String ip);
//修改密码
public void updatePassword(String password);
//根据id返回用户信息
public User selectUserInfo(Long id);
}
......@@ -32,14 +32,26 @@ public class UserServiceImpl implements UserService{
}
@Override
public boolean addUser(String phone, String password, String name, String address) {
public boolean addUser(String phone, String password, String name, String address,String ip) {
User user=userDao.selectUserByPhone(phone);
if(user!=null) {
return false;
}
else{
userDao.insertUser(phone,password,name,address);
userDao.insertUser(phone,password,name,address,ip);
return true;
}
}
@Override
public void updatePassword(String password) {
userDao.updatePassword(password);
}
@Override
public User selectUserInfo(Long id) {
return userDao.selectUserById(id);
}
}
package com.nav.util;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpUtil {
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress="";
}
// ipAddress = this.getRequest().getRemoteAddr();
return ipAddress;
}
}
\ No newline at end of file
......@@ -14,8 +14,8 @@
</select>
<insert id="insertUser" parameterType="String">
insert into lotterysystem.user(phone,password,name,address,state)
VALUES (#{phone},#{password},#{name},#{address},1);
insert into lotterysystem.user(phone,password,name,address,state,IP)
VALUES (#{phone},#{password},#{name},#{address},1,#{ip});
</insert>
<update id="updateUserState">
......@@ -23,4 +23,15 @@
where id=#{id}
</update>
<update id="updatePassword">
update lotterysystem.user set password=#{password}
where id=#{id}
</update>
<select id="selectUserById" resultType="com.nav.pojo.User">
select *
from lotterysystem.user where id=#{id};
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册