PageRenderTime 41ms CodeModel.GetById 28ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/jeecms/cms/action/member/MemberAct.java

https://gitlab.com/MetadataDev/mcms
Java | 258 lines | 167 code | 15 blank | 76 comment | 24 complexity | 22642a7bb8cf72c333759abab168d1d4 MD5 | raw file
  1package com.jeecms.cms.action.member;
  2
  3import static com.jeecms.cms.Constants.TPLDIR_MEMBER;
  4
  5import java.io.IOException;
  6
  7import javax.servlet.http.HttpServletRequest;
  8import javax.servlet.http.HttpServletResponse;
  9
 10import org.slf4j.Logger;
 11import org.slf4j.LoggerFactory;
 12import org.springframework.beans.factory.annotation.Autowired;
 13import org.springframework.stereotype.Controller;
 14import org.springframework.ui.ModelMap;
 15import org.springframework.web.bind.annotation.RequestMapping;
 16import org.springframework.web.bind.annotation.RequestMethod;
 17
 18import com.jeecms.common.web.ResponseUtils;
 19import com.jeecms.core.entity.CmsSite;
 20import com.jeecms.core.entity.CmsUser;
 21import com.jeecms.core.entity.CmsUserExt;
 22import com.jeecms.core.entity.MemberConfig;
 23import com.jeecms.core.manager.CmsUserExtMng;
 24import com.jeecms.core.manager.CmsUserMng;
 25import com.jeecms.core.web.WebErrors;
 26import com.jeecms.core.web.util.CmsUtils;
 27import com.jeecms.core.web.util.FrontUtils;
 28
 29/**
 30 * 会员中心Action
 31 */
 32@Controller
 33public class MemberAct {
 34	private static final Logger log = LoggerFactory.getLogger(MemberAct.class);
 35
 36	public static final String MEMBER_CENTER = "tpl.memberCenter";
 37	public static final String MEMBER_PROFILE = "tpl.memberProfile";
 38	public static final String MEMBER_PORTRAIT = "tpl.memberPortrait";
 39	public static final String MEMBER_PASSWORD = "tpl.memberPassword";
 40
 41	/**
 42	 * 会员中心页
 43	 * 
 44	 * 如果没有登录则跳转到登陆页
 45	 * 
 46	 * @param request
 47	 * @param response
 48	 * @param model
 49	 * @return
 50	 */
 51	@RequestMapping(value = "/member/index.jspx", method = RequestMethod.GET)
 52	public String index(HttpServletRequest request,
 53			HttpServletResponse response, ModelMap model) {
 54		CmsSite site = CmsUtils.getSite(request);
 55		CmsUser user = CmsUtils.getUser(request);
 56		FrontUtils.frontData(request, model, site);
 57		MemberConfig mcfg = site.getConfig().getMemberConfig();
 58		// 没有开启会员功能
 59		if (!mcfg.isMemberOn()) {
 60			return FrontUtils.showMessage(request, model, "member.memberClose");
 61		}
 62		if (user == null) {
 63			return FrontUtils.showLogin(request, model, site);
 64		}
 65		return FrontUtils.getTplPath(request, site.getSolutionPath(),
 66				TPLDIR_MEMBER, MEMBER_CENTER);
 67	}
 68
 69	/**
 70	 * 个人资料输入页
 71	 * 
 72	 * @param request
 73	 * @param response
 74	 * @param model
 75	 * @return
 76	 */
 77	@RequestMapping(value = "/member/profile.jspx", method = RequestMethod.GET)
 78	public String profileInput(HttpServletRequest request,
 79			HttpServletResponse response, ModelMap model) {
 80		CmsSite site = CmsUtils.getSite(request);
 81		CmsUser user = CmsUtils.getUser(request);
 82		FrontUtils.frontData(request, model, site);
 83		MemberConfig mcfg = site.getConfig().getMemberConfig();
 84		// 没有开启会员功能
 85		if (!mcfg.isMemberOn()) {
 86			return FrontUtils.showMessage(request, model, "member.memberClose");
 87		}
 88		if (user == null) {
 89			return FrontUtils.showLogin(request, model, site);
 90		}
 91		return FrontUtils.getTplPath(request, site.getSolutionPath(),
 92				TPLDIR_MEMBER, MEMBER_PROFILE);
 93	}
 94	/**
 95	 * 更换头像
 96	 * @param request
 97	 * @param response
 98	 * @param model
 99	 * @return
100	 */
101	@RequestMapping(value = "/member/portrait.jspx", method = RequestMethod.GET)
102	public String portrait(HttpServletRequest request,
103			HttpServletResponse response, ModelMap model) {
104		CmsSite site = CmsUtils.getSite(request);
105		CmsUser user = CmsUtils.getUser(request);
106		FrontUtils.frontData(request, model, site);
107		MemberConfig mcfg = site.getConfig().getMemberConfig();
108		// 没有开启会员功能
109		if (!mcfg.isMemberOn()) {
110			return FrontUtils.showMessage(request, model, "member.memberClose");
111		}
112		if (user == null) {
113			return FrontUtils.showLogin(request, model, site);
114		}
115		return FrontUtils.getTplPath(request, site.getSolutionPath(),
116				TPLDIR_MEMBER, MEMBER_PORTRAIT);
117	}
118
119	/**
120	 * 个人资料提交页
121	 * 
122	 * @param request
123	 * @param response
124	 * @param model
125	 * @return
126	 * @throws IOException
127	 */
128	@RequestMapping(value = "/member/profile.jspx", method = RequestMethod.POST)
129	public String profileSubmit(CmsUserExt ext, String nextUrl,
130			HttpServletRequest request, HttpServletResponse response,
131			ModelMap model) throws IOException {
132		CmsSite site = CmsUtils.getSite(request);
133		CmsUser user = CmsUtils.getUser(request);
134		FrontUtils.frontData(request, model, site);
135		MemberConfig mcfg = site.getConfig().getMemberConfig();
136		// 没有开启会员功能
137		if (!mcfg.isMemberOn()) {
138			return FrontUtils.showMessage(request, model, "member.memberClose");
139		}
140		if (user == null) {
141			return FrontUtils.showLogin(request, model, site);
142		}
143		ext.setId(user.getId());
144		cmsUserExtMng.update(ext, user);
145		log.info("update CmsUserExt success. id={}", user.getId());
146		return FrontUtils.showSuccess(request, model, nextUrl);
147	}
148
149	/**
150	 * 密码修改输入页
151	 * 
152	 * @param request
153	 * @param response
154	 * @param model
155	 * @return
156	 */
157	@RequestMapping(value = "/member/pwd.jspx", method = RequestMethod.GET)
158	public String passwordInput(HttpServletRequest request,
159			HttpServletResponse response, ModelMap model) {
160		CmsSite site = CmsUtils.getSite(request);
161		CmsUser user = CmsUtils.getUser(request);
162		FrontUtils.frontData(request, model, site);
163		MemberConfig mcfg = site.getConfig().getMemberConfig();
164		// 没有开启会员功能
165		if (!mcfg.isMemberOn()) {
166			return FrontUtils.showMessage(request, model, "member.memberClose");
167		}
168		if (user == null) {
169			return FrontUtils.showLogin(request, model, site);
170		}
171		return FrontUtils.getTplPath(request, site.getSolutionPath(),
172				TPLDIR_MEMBER, MEMBER_PASSWORD);
173	}
174
175	/**
176	 * 密码修改提交页
177	 * 
178	 * @param origPwd
179	 *            原始密码
180	 * @param newPwd
181	 *            新密码
182	 * @param email
183	 *            邮箱
184	 * @param nextUrl
185	 *            下一个页面地址
186	 * @param request
187	 * @param response
188	 * @param model
189	 * @return
190	 * @throws IOException
191	 */
192	@RequestMapping(value = "/member/pwd.jspx", method = RequestMethod.POST)
193	public String passwordSubmit(String origPwd, String newPwd, String email,
194			String nextUrl, HttpServletRequest request,
195			HttpServletResponse response, ModelMap model) throws IOException {
196		CmsSite site = CmsUtils.getSite(request);
197		CmsUser user = CmsUtils.getUser(request);
198		FrontUtils.frontData(request, model, site);
199		MemberConfig mcfg = site.getConfig().getMemberConfig();
200		// 没有开启会员功能
201		if (!mcfg.isMemberOn()) {
202			return FrontUtils.showMessage(request, model, "member.memberClose");
203		}
204		if (user == null) {
205			return FrontUtils.showLogin(request, model, site);
206		}
207		WebErrors errors = validatePasswordSubmit(user.getId(), origPwd,
208				newPwd, email, request);
209		if(user.getId()==2){
210			errors.addErrorString("您不能修改密码!");
211		}
212		if (errors.hasErrors()) {
213			return FrontUtils.showError(request, response, model, errors);
214		}
215		cmsUserMng.updatePwdEmail(user.getId(), newPwd, email);
216		return FrontUtils.showSuccess(request, model, nextUrl);
217	}
218
219	/**
220	 * 验证密码是否正确
221	 * 
222	 * @param origPwd
223	 *            原密码
224	 * @param request
225	 * @param response
226	 */
227	@RequestMapping("/member/checkPwd.jspx")
228	public void checkPwd(String origPwd, HttpServletRequest request,
229			HttpServletResponse response) {
230		CmsUser user = CmsUtils.getUser(request);
231		boolean pass = cmsUserMng.isPasswordValid(user.getId(), origPwd);
232		ResponseUtils.renderJson(response, pass ? "true" : "false");
233	}
234
235	private WebErrors validatePasswordSubmit(Integer id, String origPwd,
236			String newPwd, String email, HttpServletRequest request) {
237		WebErrors errors = WebErrors.create(request);
238		if (errors.ifBlank(origPwd, "origPwd", 100)) {
239			return errors;
240		}
241		if (errors.ifMaxLength(newPwd, "newPwd", 100)) {
242			return errors;
243		}
244		if (errors.ifNotEmail(email, "email", 100)) {
245			return errors;
246		}
247		if (!cmsUserMng.isPasswordValid(id, origPwd)) {
248			errors.addErrorCode("member.origPwdInvalid");
249			return errors;
250		}
251		return errors;
252	}
253
254	@Autowired
255	private CmsUserMng cmsUserMng;
256	@Autowired
257	private CmsUserExtMng cmsUserExtMng;
258}