jiapeng 6 ans auparavant
Parent
commit
82bc8aa9d4

+ 3 - 0
src/main/java/com/ekexiu/project/bridge/system/dao/UserDao.java

@ -44,6 +44,9 @@ public interface UserDao {
44 44
    @Exclude("passwd")
45 45
    @Dynamic
46 46
    int update(Connection con, User user) throws SQLException;
47
    @UpdateWith
48
    @From(User.class)
49
    int updatePasswdWithMobile(Connection con,@Set String passwd,String account) throws SQLException;
47 50
48 51
    @UpdateWith
49 52
    @From(User.class)

+ 201 - 237
src/main/java/com/ekexiu/project/bridge/system/service/SysService.java

@ -1,15 +1,19 @@
1 1
package com.ekexiu.project.bridge.system.service;
2 2
3
import com.ekexiu.project.bridge.mobile.MobilePhoneService;
4
import com.ekexiu.project.bridge.system.dao.UserDao;
5
import com.ekexiu.project.bridge.system.po.User;
6
import com.ekexiu.project.bridge.system.vo.SessionUser;
3
import java.sql.Connection;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
6
import java.util.List;
7
import java.util.Random;
8
import java.util.concurrent.TimeUnit;
9
7 10
import org.jfw.apt.annotation.Autowrie;
8 11
import org.jfw.apt.annotation.DefaultValue;
9 12
import org.jfw.apt.annotation.Nullable;
10 13
import org.jfw.apt.web.annotation.LoginUser;
11 14
import org.jfw.apt.web.annotation.Path;
12 15
import org.jfw.apt.web.annotation.method.InvalidSession;
16
import org.jfw.apt.web.annotation.method.ResultToNull;
13 17
import org.jfw.apt.web.annotation.method.SetSession;
14 18
import org.jfw.apt.web.annotation.operate.Get;
15 19
import org.jfw.apt.web.annotation.operate.Post;
@ -21,14 +25,10 @@ import org.jfw.util.context.JfwAppContext;
21 25
import org.jfw.util.exception.JfwBaseException;
22 26
import org.jfw.util.state.StateCode;
23 27
24
import java.io.File;
25
import java.sql.Connection;
26
import java.sql.SQLException;
27
import java.util.ArrayList;
28
import java.util.List;
29
import java.util.Random;
30
import java.util.concurrent.TimeUnit;
31
import java.util.concurrent.atomic.AtomicInteger;
28
import com.ekexiu.project.bridge.mobile.MobilePhoneService;
29
import com.ekexiu.project.bridge.system.dao.UserDao;
30
import com.ekexiu.project.bridge.system.po.User;
31
import com.ekexiu.project.bridge.system.vo.SessionUser;
32 32
33 33
/**
34 34
 * Created by TT on 2018/8/7.
@ -36,229 +36,193 @@ import java.util.concurrent.atomic.AtomicInteger;
36 36
@Path("/sys")
37 37
public class SysService {
38 38
39
    public static final String DEFAULT_PW_STR = StringUtil.md5("123456");
40
41
    private static final AtomicInteger FN_IDX = new AtomicInteger(1);
42
43
    private File imgPath;
44
45
    private String regMobilePhoneReplaceKey;
46
    private String regMobilePhoneContentTemplate;
47
    private long timeLimitWithRegMobilePhone = 3 * 60 * 1000;
48
49
    @Autowrie
50
    private UserDao userDao;
51
    @Autowrie
52
    private MobilePhoneService mobilePhoneService;
53
54
    public UserDao getUserDao() {
55
        return userDao;
56
    }
57
58
    public void setUserDao(UserDao userDao) {
59
        this.userDao = userDao;
60
    }
61
62
    public MobilePhoneService getMobilePhoneService() {
63
        return mobilePhoneService;
64
    }
65
66
    public void setMobilePhoneService(MobilePhoneService mobilePhoneService) {
67
        this.mobilePhoneService = mobilePhoneService;
68
    }
69
70
    @SetSession("JFW_SESSION_LOGIN_USER=result")
71
    @Path("/login")
72
    @Post
73
    public SessionUser login(@JdbcConn Connection con, String account, String pw, String vc, @Nullable @SessionVal("PIC_LOGIN") String code, @DefaultValue("0") @SessionVal("TIMEOUT_PIC_LOGIN") long timeout) throws SQLException {
74
        if (code == null) {
75
            return null;
76
        }
77
        if (!vc.equals(code)) {
78
            return null;
79
        }
80
        if (System.currentTimeMillis() > timeout) {
81
            return null;
82
        }
83
        User user = userDao.login(con, account, StringUtil.md5(pw));
84
        if (user != null) {
85
            return makeSessionUser(user);
86
        }
87
        return null;
88
    }
89
90
    @Get
91
    @Path("")
92
    public SessionUser get(@LoginUser SessionUser user) {
93
        return user;
94
    }
95
96
    @Get
97
    @Path("/logout")
98
    @InvalidSession
99
    public void logout() {
100
    }
101
102
    @Post
103
    @Path("/insert")
104
    public void insert(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, User user) throws SQLException {
105
        user.setId(StringUtil.buildUUID());
106
        user.setActive(true);
107
        user.setPasswd(DEFAULT_PW_STR);
108
        user.setType(0);
109
        user.setCreator(sessionUser.getId());
110
        user.setModifier(sessionUser.getId());
111
        this.userDao.insert(con, user);
112
    }
113
114
    @Get
115
    @Path("/qo")
116
    public SessionUser query(@JdbcConn Connection con, String id) throws SQLException {
117
        return makeSessionUser(this.userDao.query(con, id));
118
    }
119
120
    @Post
121
    @Path("/update")
122
    public void update(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, User user) throws SQLException {
123
        user.setModifier(sessionUser.getId());
124
        this.userDao.update(con, user);
125
    }
126
127
    @Get
128
    @Path("/ban")
129
    public void ban(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, String id) throws SQLException {
130
        this.userDao.ban(con, sessionUser.getId(), id);
131
    }
132
133
    @Get
134
    @Path("/pq")
135
    public PageQueryResult<SessionUser> pageQuery(@JdbcConn Connection con, @Nullable String account, @Nullable String name, @Nullable String comp, int pageSize, int pageNo) throws SQLException {
136
        PageQueryResult<User> pageQueryResult = this.userDao.pageQuery(con, account, name == null ? null : "%" + name + "%", comp == null ? null : "%" + comp + "%", pageSize, pageNo);
137
        List<User> users = pageQueryResult.getData();
138
        List<SessionUser> sessionUsers = new ArrayList<>();
139
        if (!users.isEmpty()) {
140
            for (User user : users) {
141
                sessionUsers.add(makeSessionUser(user));
142
            }
143
            PageQueryResult<SessionUser> ret = new PageQueryResult<>();
144
            ret.setPageNo(pageQueryResult.getPageNo());
145
            ret.setPageSize(pageQueryResult.getPageSize());
146
            ret.setTotal(pageQueryResult.getTotal());
147
            ret.setData(sessionUsers);
148
            return ret;
149
        }
150
        return null;
151
    }
152
153
    @Get
154
    @Path("/resetPw")
155
    public void resetPw(@JdbcConn(true) Connection con, String id) throws SQLException {
156
        this.userDao.changePw(con, DEFAULT_PW_STR, id);
157
    }
158
159
    @Post
160
    @Path("/changePw")
161
    public void changePw(@JdbcConn(true) Connection con, String id, String oldPw, String newPw) throws SQLException, JfwBaseException {
162
        User user = this.userDao.query(con, id);
163
        if (user != null && StringUtil.md5(oldPw).equals(StringUtil.md5(user.getPasswd()))) {
164
            this.userDao.changePw(con, StringUtil.md5(newPw), id);
165
        } else {
166
            throw new JfwBaseException(100001, "用户不存在或密码不匹配");
167
        }
168
    }
169
170
    @Post
171
    @Path("/notice")
172
    public void updateNotice(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, String cnt) throws SQLException {
173
        this.userDao.updateNotice(con, cnt, sessionUser.getId());
174
    }
175
176
    @SetSession("")
177
    @Get
178
    @Path("/regmobilephone")
179
    public String regMobilePhone(@JdbcConn(false) Connection con, String account, @DefaultValue("true") boolean checkExists, String vc, @Nullable @SessionVal(value = "PIC_MOBILE", remove = true) String code, @Nullable String token)
180
            throws JfwBaseException, SQLException {
181
        if (token != null) {
182
            if (!vc.equals(code)) {
183
                throw new JfwBaseException(20001, "valid code error");
184
            }
185
        } else {
186
            if (code == null) {
187
                throw new IllegalArgumentException("not found session value:verification");
188
            }
189
            if (!vc.toUpperCase().equals(code)) {
190
                throw new JfwBaseException(20001, "valid code error");
191
            }
192
        }
193
        if (checkExists) {
194
            User user = this.userDao.queryByAccount(con, account);
195
            if (null != user) {
196
                return null;
197
            }
198
        }
199
        StateCode<String, String> sc = new StateCode<String, String>();
200
        final String key = JfwAppContext.cacheObjectAndGenKey(sc);
201
        try {
202
            Random rd = new Random();
203
            int vi = rd.nextInt(10000);
204
            String vcode = String.format("%04d", vi);
205
            sc.setKey(account);
206
            sc.setValue(vcode);
207
            this.mobilePhoneService.sendMessage(account, this.regMobilePhoneContentTemplate, this.regMobilePhoneReplaceKey, vcode);
208
            long ct = System.currentTimeMillis();
209
            long et = ct + this.timeLimitWithRegMobilePhone + 5000;
210
            sc.setBuildTime(ct);
211
            sc.setExpiredTime(et);
212
            JfwAppContext.getScheduledExecutorService().schedule(new Runnable() {
213
                @Override
214
                public void run() {
215
                    JfwAppContext.removeCachedObject(key);
216
                }
217
            }, this.timeLimitWithRegMobilePhone + 10000, TimeUnit.MILLISECONDS);
218
        } catch (Exception e) {
219
            JfwAppContext.removeCachedObject(key);
220
            throw new JfwBaseException(10012, "send mobile phone message to " + account + " error", e);
221
        }
222
        return key;
223
    }
224
225
    //@Post
226
    //@Path("/resetPasswordWithMobilePhone")
227
    //public boolean resetPassword(@JdbcConn(true) Connection con, String state, String mobilePhone, String pw, String vc) throws SQLException, JfwBaseException {
228
    //    @SuppressWarnings("unchecked")
229
    //    StateCode<String, String> sc = (StateCode<String, String>) JfwAppContext.getCachedObject(state);
230
    //    if (sc == null) {
231
    //        throw new JfwBaseException("验证超时");
232
    //    }
233
    //    if (sc.getExpiredTime() < System.currentTimeMillis()) {
234
    //        throw new JfwBaseException("验证超时");
235
    //    }
236
    //    try {
237
    //        if (!sc.getKey().equals(mobilePhone) || !sc.getValue().equals(vc))
238
    //            return false;
239
    //        int ret = this.userDao.updatePasswdWithMobile(con, StringUtil.md5(pw), DATE.format(new Date()), mobilePhone);
240
    //        if (ret == 0) {
241
    //            return this.userDao.updatePasswordWithMobileOrEmail(con, StringUtil.md5(pw), mobilePhone) > 0;
242
    //        }
243
    //        return ret > 0;
244
    //    } finally {
245
    //        JfwAppContext.removeCachedObject(state);
246
    //    }
247
    //}
248
249
250
    private static SessionUser makeSessionUser(User user) {
251
        SessionUser sessionUser = new SessionUser();
252
        sessionUser.setAccount(user.getAccount());
253
        sessionUser.setComp(user.getComp());
254
        sessionUser.setEmail(user.getEmail());
255
        sessionUser.setId(user.getId());
256
        sessionUser.setJob(user.getJob());
257
        sessionUser.setName(user.getName());
258
        sessionUser.setPhone(user.getPhone());
259
        sessionUser.setRemark(user.getRemark());
260
        sessionUser.setType(user.getType());
261
        sessionUser.setActive(user.isActive());
262
        return sessionUser;
263
    }
39
	public static final String DEFAULT_PW_STR = StringUtil.md5("123456");
40
41
	private String regMobilePhoneReplaceKey;
42
	private String regMobilePhoneContentTemplate;
43
	private long timeLimitWithRegMobilePhone = 3 * 60 * 1000;
44
45
	@Autowrie
46
	private UserDao userDao;
47
	@Autowrie
48
	private MobilePhoneService mobilePhoneService;
49
50
	public UserDao getUserDao() {
51
		return userDao;
52
	}
53
54
	public void setUserDao(UserDao userDao) {
55
		this.userDao = userDao;
56
	}
57
58
	public MobilePhoneService getMobilePhoneService() {
59
		return mobilePhoneService;
60
	}
61
62
	public void setMobilePhoneService(MobilePhoneService mobilePhoneService) {
63
		this.mobilePhoneService = mobilePhoneService;
64
	}
65
66
	@SetSession("JFW_SESSION_LOGIN_USER=result")
67
	@Path("/login")
68
	@Post
69
	public SessionUser login(@JdbcConn Connection con, String account, String pw, String vc, @Nullable @SessionVal(value ="PIC_LOGIN",remove= true) String code,
70
			@Nullable @SessionVal(value ="TIMEOUT_PIC_LOGIN",remove=true) Long timeout) throws SQLException {
71
		if (code == null || timeout == null) {
72
			return null;
73
		}
74
		if (!vc.equals(code)) {
75
			return null;
76
		}
77
		if (System.currentTimeMillis() > timeout) {
78
			return null;
79
		}
80
		User user = userDao.login(con, account, StringUtil.md5(pw));
81
		if (user != null) {
82
			return makeSessionUser(user);
83
		}
84
		return null;
85
	}
86
87
	@Get
88
	@Path("")
89
	public SessionUser get(@LoginUser SessionUser user) {
90
		return user;
91
	}
92
93
	@Get
94
	@Path("/logout")
95
	@InvalidSession
96
	public void logout() {
97
	}
98
99
	@Post
100
	@Path("/insert")
101
	public void insert(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, User user) throws SQLException {
102
		user.setId(StringUtil.buildUUID());
103
		user.setActive(true);
104
		user.setPasswd(DEFAULT_PW_STR);
105
		user.setType(0);
106
		user.setCreator(sessionUser.getId());
107
		user.setModifier(sessionUser.getId());
108
		this.userDao.insert(con, user);
109
	}
110
111
	@Get
112
	@Path("/qo")
113
	public SessionUser query(@JdbcConn Connection con, String id) throws SQLException {
114
		return makeSessionUser(this.userDao.query(con, id));
115
	}
116
117
	@Post
118
	@Path("/update")
119
	public void update(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, User user) throws SQLException {
120
		user.setModifier(sessionUser.getId());
121
		this.userDao.update(con, user);
122
	}
123
124
	@Get
125
	@Path("/ban")
126
	public void ban(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, String id) throws SQLException {
127
		this.userDao.ban(con, sessionUser.getId(), id);
128
	}
129
130
	@Get
131
	@Path("/pq")
132
	public PageQueryResult<SessionUser> pageQuery(@JdbcConn Connection con, @Nullable String account, @Nullable String name, @Nullable String comp,
133
			int pageSize, int pageNo) throws SQLException {
134
		PageQueryResult<User> pageQueryResult = this.userDao.pageQuery(con, account, name == null ? null : "%" + name + "%",
135
				comp == null ? null : "%" + comp + "%", pageSize, pageNo);
136
		List<User> users = pageQueryResult.getData();
137
		List<SessionUser> sessionUsers = new ArrayList<>();
138
		if (!users.isEmpty()) {
139
			for (User user : users) {
140
				sessionUsers.add(makeSessionUser(user));
141
			}
142
			PageQueryResult<SessionUser> ret = new PageQueryResult<>();
143
			ret.setPageNo(pageQueryResult.getPageNo());
144
			ret.setPageSize(pageQueryResult.getPageSize());
145
			ret.setTotal(pageQueryResult.getTotal());
146
			ret.setData(sessionUsers);
147
			return ret;
148
		}
149
		return null;
150
	}
151
152
	@Get
153
	@Path("/resetPw")
154
	public void resetPw(@JdbcConn(true) Connection con, String id) throws SQLException {
155
		this.userDao.changePw(con, DEFAULT_PW_STR, id);
156
	}
157
158
	@Post
159
	@Path("/changePw")
160
	public void changePw(@JdbcConn(true) Connection con, String id, String oldPw, String newPw) throws SQLException, JfwBaseException {
161
		User user = this.userDao.query(con, id);
162
		if (user != null && StringUtil.md5(oldPw).equals(StringUtil.md5(user.getPasswd()))) {
163
			this.userDao.changePw(con, StringUtil.md5(newPw), id);
164
		} else {
165
			throw new JfwBaseException(100001, "用户不存在或密码不匹配");
166
		}
167
	}
168
169
	@Post
170
	@Path("/notice")
171
	public void updateNotice(@JdbcConn(true) Connection con, @LoginUser SessionUser sessionUser, String cnt) throws SQLException {
172
		this.userDao.updateNotice(con, cnt, sessionUser.getId());
173
	}
174
175
	@SetSession({ "RESET_PW_MOBILE=result[0]", "RESET_PW_VC=result[1]", "RESET_PW_TO=result[2]" })
176
	@ResultToNull
177
	@Get
178
	@Path("/regmobilephone")
179
	public Object[] regMobilePhone(@JdbcConn(false) Connection con, String account, @DefaultValue("false") boolean checkExists) throws Exception {
180
		if (checkExists) {
181
			User user = this.userDao.queryByAccount(con, account);
182
			if (null != user) {
183
				throw new JfwBaseException(-60000, "not found account");
184
			}
185
		}
186
		Random rd = new Random();
187
		int vi = rd.nextInt(10000);
188
		String vcode = String.format("%04d", vi);
189
190
		this.mobilePhoneService.sendMessage(account, this.regMobilePhoneContentTemplate, this.regMobilePhoneReplaceKey, vcode);
191
		Object[] result = new Object[3];
192
		result[0] = account;
193
		result[1] = vcode;
194
		result[2] = System.currentTimeMillis() + 5000 + (5 * 60 * 1000);
195
		return result;
196
	}
197
198
	@Post
199
	@Path("/resetPasswordWithMobilePhone")
200
	public boolean resetPassword(@JdbcConn(true) Connection con, String pw, String vc,
201
			@Nullable @SessionVal(value = "RESET_PW_MOBILE", remove = true) String account,
202
			@Nullable @SessionVal(value = "RESET_PW_VC", remove = true) String vcode, @Nullable @SessionVal(value = "RESET_PW_TO", remove = true) Long timeout)
203
			throws SQLException, JfwBaseException {
204
		if (vcode == null || account == null || timeout == null) {
205
			return false;
206
		}
207
		if (System.currentTimeMillis() > timeout) {
208
			return false;
209
		}
210
		this.userDao.updatePasswdWithMobile(con, StringUtil.md5(pw), account);
211
		return true;
212
	}
213
214
	private static SessionUser makeSessionUser(User user) {
215
		SessionUser sessionUser = new SessionUser();
216
		sessionUser.setAccount(user.getAccount());
217
		sessionUser.setComp(user.getComp());
218
		sessionUser.setEmail(user.getEmail());
219
		sessionUser.setId(user.getId());
220
		sessionUser.setJob(user.getJob());
221
		sessionUser.setName(user.getName());
222
		sessionUser.setPhone(user.getPhone());
223
		sessionUser.setRemark(user.getRemark());
224
		sessionUser.setType(user.getType());
225
		sessionUser.setActive(user.isActive());
226
		return sessionUser;
227
	}
264 228
}