import java.io.Serializable;
/**
* @ClassName: SuccessOrFailure
* @Description: json对象结果类
* @author: private
* @date: 2018年2月1日 下午3:46:18
*/
public class SuccessOrFailure implements Serializable {
private static final long serialVersionUID = 2782974536561798941L;
private boolean success;
private Object message;
public SuccessOrFailure() {}
private SuccessOrFailure(boolean success, Object message) {
this.success = success;
this.message = message;
}
public static SuccessOrFailure SUCCESS = new SuccessOrFailure(true, null);
public static SuccessOrFailure SUCCESS(Object message) {
return new SuccessOrFailure(true, message);
}
public static SuccessOrFailure FAILURE(Object message) {
return new SuccessOrFailure(false, message);
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
@Override
public String toString() {
return "SuccessOrFailure [success=" + success + ", message=" + message + "]";
}
}
用法示例
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fh.controller.base.BaseController;
import com.fh.service.system.carappuser.CarAppUserManager;
import com.fh.util.AppUtil;
import com.fh.util.PageData;
import sun.misc.BASE64Decoder;
@Controller
@RequestMapping("/app")
public class AppController extends BaseController {
private static final Logger logger = Logger.getLogger(AppController.class);
@Resource
private CarAppUserManager carAppUserService;
@RequestMapping("/login")
@ResponseBody
public SuccessOrFailure login(String data,HttpServletRequest request) throws Exception {
AppUtil appUtil = new AppUtil(data, request);
try {
// 效验秘钥
/*appUtil.verifySign();*/
// 获取手机号
String phone = appUtil.getParam("phone") == null ? "" : appUtil.getParam("phone").toString();
// 获取账号
String account = appUtil.getParam("account") == null ? "" : appUtil.getParam("account").toString();
// 获取密码密码
String password = appUtil.getParam("password") == null ? "" : appUtil.getParam("password").toString();
// 创建参数map
PageData pd = this.getPageData();
// 判断登录方式
if (!Tools.isEmpty(phone)) { // 手机号登录
pd.put("USER_PHONE", phone);
pd.put("loginType", LoginType.LOGIN_TYPE_PHONE);
} else if (Tools.isEmpty(account)) { // 账号登录
pd.put("USER_ACCOUNT", account);
pd.put("loginType", LoginType.LOGIN_TYPE_ACCOUNT);
}
// 获取用户信息
PageData appUser = carAppUserService.findUserByLoginType(pd);
// 获取用户密码
String appUserPassword = appUser.getString("PASSWORD");
// 密码是否匹配
if (appUserPassword.equals(MD5.md5(password + Const.SALT))) {
return SuccessOrFailure.SUCCESS(appUser);
} else {
return SuccessOrFailure.FAILURE("密码错误");
}
} catch (SignErrorException e) {
return SuccessOrFailure.FAILURE(e.getMessage());
}
}