UserSaveComponent.java 2.65 KB
package com.ui.ctrl;

import com.ui.User.UserAuthLocal;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.User;
import com.ui.test.TestCommonAlarmData;
import com.ui.test.TestCommonData;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 测试转用
 */
@Component
public class UserSaveComponent {

    @Autowired
    HttpRestClient httpRestClient;

    @Autowired
    UserAuthLocal userAuthLocal;

    public BaseResponse saveUser( User user, HttpSession session) {
        BaseResponse baseResponse = null;
        if (StringUtils.isNotBlank(user.getModules()) && StringUtils.endsWith(user.getModules(), ",")) {
            user.setModules(user.getModules().substring(0, user.getModules().length() - 1));
        }
        if (StringUtils.isNotBlank(user.getModuleGroups()) && StringUtils.endsWith(user.getModuleGroups(), ",")) {
            user.setModuleGroups(user.getModuleGroups().substring(0, user.getModuleGroups().length() - 1));
        }
        if(StringUtils.isBlank(user.getEmail())||StringUtils.isBlank(user.getMobile())){
            return new BaseResponse(201, "邮箱、手机号不能为空");
        }
        if(StringUtils.isBlank(user.getLadpName())){
            user.setLadpName(user.getEmail().substring(0,user.getEmail().indexOf("@")));
        }
        if (user.getId() < 1) {//add
            if (userAuthLocal.getUserByname(user.getName()) != null) {
                return new BaseResponse(201, "用户已经存在");
            }
            user.setPwd(RandomStringUtils.random(8, true, true));
            baseResponse = httpRestClient.defaultPost(HttpUriContants.USER_INSERT, user, BaseResponse.class);
        } else {
            User u = (User) session.getAttribute("user");
            String username = u.getName();
            if ("admin".equals(user.getName()) && !"admin".equals(username)) {
                return new BaseResponse(201, "此用户无法修改");
            }
            baseResponse = httpRestClient.defaultPost(HttpUriContants.USER_UPDATE, user, BaseResponse.class);

        }
        userAuthLocal.flushUser(user.getName());
        return baseResponse;
    }
}