com.faxintong.iruyi.controller.lawyer.UserController.java Source code

Java tutorial

Introduction

Here is the source code for com.faxintong.iruyi.controller.lawyer.UserController.java

Source

package com.faxintong.iruyi.controller.lawyer;

import com.faxintong.iruyi.controller.BaseController;
import com.faxintong.iruyi.model.mybatis.lawyer.Lawyer;
import com.faxintong.iruyi.utils.Config;
import com.faxintong.iruyi.utils.MD5;
import com.faxintong.iruyi.utils.RedisUtils;
import com.faxintong.iruyi.utils.SessionUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import static com.faxintong.iruyi.utils.Constants.*;

/**
 * Created by hehj on 15-1-3.
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 
     * @param lawyer
     * @param bindingResult
     * @param validCode
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "reg")
    public Map<String, Object> register(@Valid Lawyer lawyer, BindingResult bindingResult, String validCode,
            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = Maps.newHashMap();
        result.put(RESULT, false);
        try {
            if (bindingResult.hasErrors()) {
                result.put(ERR_MSG, bindingResult.getFieldError().getDefaultMessage());
            } else {
                String code = "123";//(String) request.getSession().getAttribute("code");
                if (code == null) {
                    result.put(ERR_MSG, "??");
                } else if (!code.equals(validCode)) {
                    result.put(ERR_MSG, "??");
                } else if (userService.containsPhone(lawyer.getPhone())) {
                    result.put(ERR_MSG, "??");
                } else if (!userService.regisValidate(lawyer.getPhone())) {
                    result.put(ERR_MSG, "??");
                } else {
                    lawyer.setPassword(MD5.newinstance().getMD5ofStr(lawyer.getPassword()));
                    Date date = new Date();
                    lawyer.setCreateDate(date);
                    lawyer.setUpdateDate(date);
                    userService.registerLawyer(lawyer);
                    result.put(RESULT, true);
                    result.put(ERR_MSG, "?");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(":" + e.getMessage());
            result.put(ERR_MSG, "!");
        }
        return result;
    }

    /**
     * 
     * @param phone
     * @param password
     * @param response
     * @return
     */
    @RequestMapping(value = "login")
    public Map<String, Object> login(String phone, String password, HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> result = Maps.newHashMap();
        result.put(RESULT, false);

        if (StringUtils.isEmpty(phone)) {
            result.put(ERR_MSG, "???");
        } else if (StringUtils.isEmpty(password)) {
            result.put(ERR_MSG, "??");
        } else {
            try {
                password = MD5.newinstance().getMD5ofStr(password);
                if (!userService.loginValidate(phone, password))
                    result.put(ERR_MSG, "?????");
                else {
                    String sessionId = SessionUtil.getSessionId();
                    result.put("sessionId", sessionId);
                    Lawyer lawyer = userService.getLawyer(phone);
                    RedisUtils.set(SESSION_PREFIX + sessionId, "" + lawyer.getId());
                    result.put(RESULT, true);
                }
            } catch (Exception e) {
                result.put(ERR_MSG, "");
                logger.error(":" + e.getMessage());
            }
        }
        return result;
    }

    /**
     * 
     * @param request
     * @param response
     */
    @RequestMapping(value = "logout")
    public Map<String, Object> logOut(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = Maps.newHashMap();
        result.put(RESULT, false);
        try {
            String sessionId = request.getParameter("sessionId");
            RedisUtils.del(SESSION_PREFIX + sessionId);
            result.put(RESULT, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * ?
     * @param request
     * @param response
     */
    @RequestMapping(value = "head/upload")
    public Map<String, Object> headImageUpload(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = Maps.newHashMap();
        result.put(RESULT, false);
        String sessionId = request.getSession().getId();
        String lawyerId = RedisUtils.get(SESSION_PREFIX + sessionId);
        if (lawyerId != null) {
            try {
                String fileName = uploadFile(request, Config.HEAD_DIR + sessionId);
                Lawyer lawyer = getLawyer(request);
                lawyer.setPhoto(fileName);
                userService.updateLawyerInfo(lawyer);
                result.put(RESULT, true);
            } catch (IOException e) {
                result.put(ERR_MSG, "?");
                logger.error(lawyerId + ":?:" + e.getMessage());
            }
        }
        return result;
    }

    /**
     * ?
     * @param request
     * @param response
     */
    @RequestMapping(value = "certificate/upload")
    public void jobInfoUpload(HttpServletRequest request, HttpServletResponse response) {
        String sessionId = request.getSession().getId();
        String lawyerId = RedisUtils.get(SESSION_PREFIX + sessionId);
        if (lawyerId != null) {
            try {
                String fileName = uploadFile(request, Config.CERTIFICATE_INFO + sessionId);
                Lawyer lawyer = getLawyer(request);
                lawyer.setBusinessLicense(fileName);
                userService.updateLawyerInfo(lawyer);
            } catch (IOException e) {
                logger.error(lawyerId + ":?:" + e.getMessage());
            }
        }
    }
}