NutzCN Logo
问答 nutzwk-dubbo版本BaseModel的opBy 没有生成
发布于 194天前 作者 herexu 267 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk
package cn.wizzer.framework.base.model;

import org.nutz.dao.entity.annotation.*;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.lang.random.R;
import org.nutz.mvc.Mvcs;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * Created by wizzer on 2016/6/21.
 */
public abstract class BaseModel implements Serializable {
    private static final long serialVersionUID = 1L;
    @Column
    @Comment("操作人")
    @Prev(els = @EL("$me.uid()"))
    @ColDefine(type = ColType.VARCHAR, width = 32)
    private String opBy;

    @Column
    @Comment("操作时间")
    @Prev(els = @EL("$me.now()"))
    //Long不要用ColDefine定义,兼容oracle/mysql,支持2038年以后的时间戳
    private Long opAt;

    @Column
    @Comment("删除标记")
    @Prev(els = @EL("$me.flag()"))
    @ColDefine(type = ColType.BOOLEAN)
    private Boolean delFlag;

    @Override
    public String toString() {
        return String.format("/*%s*/%s", super.toString(), Json.toJson(this, JsonFormat.compact()));
    }

    public Boolean flag() {
        return false;
    }

    public Long now() {
        return System.currentTimeMillis();
    }

    public String uid() {
        String uid = getOpBy();
        if (Strings.isNotBlank(uid)) {
            return uid;
        }
        try {
            HttpServletRequest request = Mvcs.getReq();
            if (request != null) {
                return Strings.sNull(request.getSession(true).getAttribute("platform_uid"));
            }
        } catch (Exception e) {
        }
        return "";
    }

    public String getOpBy() {
        return opBy;
    }

    public void setOpBy(String opBy) {
        this.opBy = opBy;
    }

    public Long getOpAt() {
        return opAt;
    }

    public void setOpAt(Long opAt) {
        this.opAt = opAt;
    }

    public Boolean getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(Boolean delFlag) {
        this.delFlag = delFlag;
    }

    public String uuid() {
        return R.UU32().toLowerCase();
    }
}

    public String uid() {
        String uid = getOpBy();
        if (Strings.isNotBlank(uid)) {
            return uid;
        }
        try {
            HttpServletRequest request = Mvcs.getReq();
            if (request != null) {
                return Strings.sNull(request.getSession(true).getAttribute("platform_uid"));
            }
        } catch (Exception e) {
        }
        return "";
    }

uid()不能使用了吗?
看到CmsArticleController.java 的addDo方法里面 需要手动赋值

 article.setPublishAt(time[0] / 1000);
            article.setEndAt(time[1] / 1000);
            article.setSiteid(siteid);
            article.setStatus(0);
            article.setOpBy(StringUtil.getPlatformUid());
            cmsArticleService.insert(article);
4 回复

微服务,只有在MVC模块里才有HttpServletRequest 对象,才能从session里获取,所以需要在控制类手动set值

@Wizzercn 再请教一下
basemodle

public String uid() {
        String uid = getOpBy();
        if (Strings.isNotBlank(uid)) {
            return uid;
        }
        try {
            HttpServletRequest request = Mvcs.getReq();
            if (request != null) {
                return Strings.sNull(request.getSession(true).getAttribute("platform_uid"));
            }
        } catch (Exception e) {
        }
        return "";
    }

StringUtil

    /**
     * 获取平台后台登陆UID
     *
     * @return
     */
    public static String getPlatformUid() {
        try {
            HttpServletRequest request = Mvcs.getReq();
            if (request != null) {
                return Strings.sNull(request.getSession(true).getAttribute("platform_uid"));
            }
        } catch (Exception e) {

        }
        return "";
    }

这两个写法并没有区别,为什么StringUtils能获取到request,而BaseModule获取不到

微服务啊~~比如sys服务模块,上下文里没有web的东西

哦哦,明白了,basemode是在framwork里面

添加回复
请先登陆
回到顶部