typescript是像java吗_JAVA、Typescript处理返回值的思考

需求:一个函数,即要返回消息(错误消息),又要返回对象(比如刷新前端数据),还要同时返回,如何处理?

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. 国际化,后台处理,就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值