概述
需求:一个函数,即要返回消息(错误消息),又要返回对象(比如刷新前端数据),还要同时返回,如何处理?
1. JAVA增加一个返回类:_Result.java
public class _Result implements Serializable {
private static final long serialVersionUID = -25841602963099946L;
public final static int OK = 0;
public final static int ERR = -1;
private int code;
private String msg;
private Object data;
public _Result() {}
public _Result(int code) {
this.code = code;
}
public _Result(int code, String msg) {
this.code = code;
this.msg = msg;
}
public _Result(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static _Result ok() {
return new _Result(OK);
}
public static _Result ok(String msg) {
return new _Result(OK, msg);
}
public static _Result ok(String msg, Object data) {
return new _Result(OK, msg, data);
}
public static _Result other(int code, String msg, Object data) {
return new _Result(code, msg, data);
}
public static _Result err() {
return new _Result(ERR);
}
public static _Result err(String msg) {
return new _Result(ERR, msg);
}
public static _Result err(String msg, Object data) {
return new _Result(ERR, msg, data);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
@Override
public String toString() {
return "{"code":" + code + ", "msg":"" + msg + ""}";
}
}
2. JAVA案例:
@Override
public _Result confirmPurchase(Long id) {
_Result re = new _Result();
Purchase o = purchaseDao.findById(id).orElse(null);
if (ObjectUtil.isEmpty(o)) {
return _Result.err("无效的主键!");
}
if (!o.getAudit()) {
o.setAudit(Global.AUDIT_YES);
o.setAuditor(Oaa.getUser().getId());
o.setAuditTime(System.currentTimeMillis());
}
o = purchaseDao.save(o);
re.setData(o);
return _Result.ok("审核完成!");
}
3. Typescript实现的Result
export class Result {
constructor(code: number, msg?: string) {
this.code = code;
this.msg = msg;
}
code: number;
msg: string;
data: any;
}
4. Typescript实现
confirmed(id, i) {
this.loadingExec(this.marsService.confirmedPurchase(id), (re: Result) => {
if (re.code === 0) {
// 更新数据列表
this.saveTableEntity(re.data);
this.message.success(re.msg);
} else {
this.message.error(re.msg);
}
});
}
5. 国际化,后台处理,就可以了。
最后
以上就是粗犷眼睛为你收集整理的typescript是像java吗_JAVA、Typescript处理返回值的思考的全部内容,希望文章能够帮你解决typescript是像java吗_JAVA、Typescript处理返回值的思考所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复