小说数据库设计分表策略

根据书本id分表

书本主表

章节内容分表数由TABLE_CHAPTER_SLICE_NUM控制

const (
    TABLE_CHAPTER_SLICE_NUM uint32 = 100
)

// 获取分表表名
func getTable(id uint32) string {
    if id < 1 {
        return ""
    }

    return fmt.Sprintf("%schapter_%04d", "test_", id%TABLE_CHAPTER_SLICE_NUM)
}

golang复制文件夹和文件


// File copies a single file from src to dst
func File(src, dst string) error {
    var err error
    var srcfd *os.File
    var dstfd *os.File
    var srcinfo os.FileInfo

    if srcfd, err = os.Open(src); err != nil {
        return err
    }
    defer srcfd.Close()

    if dstfd, err = os.Create(dst); err != nil {
        return err
    }
    defer dstfd.Close()

    if _, err = io.Copy(dstfd, srcfd); err != nil {
        return err
    }
    if srcinfo, err = os.Stat(src); err != nil {
        return err
    }
    return os.Chmod(dst, srcinfo.Mode())
}

// CopyDir copies a whole directory recursively
func CopyDir(src string, dst string) error {
    var err error
    var fds []os.FileInfo
    var srcinfo os.FileInfo

    if srcinfo, err = os.Stat(src); err != nil {
        return err
    }

    if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
        return err
    }

    if fds, err = ioutil.ReadDir(src); err != nil {
        return err
    }
    for _, fd := range fds {
        srcfp := path.Join(src, fd.Name())
        dstfp := path.Join(dst, fd.Name())

        if fd.IsDir() {
            if err = CopyDir(srcfp, dstfp); err != nil {
                fmt.Println(err)
            }
        } else {
            if err = File(srcfp, dstfp); err != nil {
                fmt.Println(err)
            }
        }
    }
    return nil
}

golang给图片添加图片水印


//一个图片添加水印
func SetSY(logo, tp string) {
    //原始图片是sam.jpg
    imgb, err := os.Open(tp)
    if err != nil {
        fmt.Println("打开图片失败")
        return
    }
    img, format1, err := image.Decode(imgb)
    if err != nil {
        fmt.Println("解码图片失败-", format1, err.Error())
        return
    }
    defer imgb.Close()

    wmb, err := os.Open(logo)
    if err != nil {
        fmt.Println("打开Logo失败")
        return
    }
    watermark, format2, err := image.Decode(wmb)
    if err != nil {
        fmt.Println("解码Logo失败-", format2, err.Error())
        return
    }

    defer wmb.Close()

    //把水印写到右下角,并向0坐标各偏移10个像素
    offset := image.Pt(img.Bounds().Dx()-watermark.Bounds().Dx()-10, img.Bounds().Dy()-watermark.Bounds().Dy()-10)
    b := img.Bounds()
    m := image.NewNRGBA(b)

    draw.Draw(m, b, img, image.ZP, draw.Src)
    draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)

    //生成新图片new.jpg,并设置图片质量..
    imgw, _ := os.Create(tp)
    jpeg.Encode(imgw, m, &jpeg.Options{100})

    defer imgw.Close()

    fmt.Println("添加水印成功...")
}

windows创建文件夹时不能包含一些特殊符号

QQ图片20200922181733.pngQQ图片20200922181733.png

Go中使用字符串替换即可

//文件夹不能包含 \ / : * ? " < > |
dirName = strings.Replace(dirName, "\\", "", -1)
dirName = strings.Replace(dirName, "/", "", -1)
dirName = strings.Replace(dirName, ":", "", -1)
dirName = strings.Replace(dirName, "*", "", -1)
dirName = strings.Replace(dirName, "?", "", -1)
dirName = strings.Replace(dirName, "\"", "", -1)
dirName = strings.Replace(dirName, "<", "", -1)
dirName = strings.Replace(dirName, ">", "", -1)
dirName = strings.Replace(dirName, "|", "", -1)

typecho附件中的图片批量插入到编辑器

修改文件

admin/file-upload.php
111111.png111111.png
<li><button type="button" onclick="addImgAll()">批量插入图片</button></li>
<script>
    function addImgAll(){
        $("li[data-image='1']").each(function(index, element) {
            let imgUrl = $(element).attr("data-url")
            let title = $("#title").val()
            $("#text").val($("#text").val()+`<img alt="${title}" src="${imgUrl}">\n`)
        });
    }
</script>

效果图

lz222.giflz222.gif

上传图片后 需要刷新一下网页!!!

beego on range loop re-entry: {{range}} branches end in different contexts:

在html模板js中 ``字符串模板中渲染出现的这个错误

template Execute err: html/template:xxxx/xxxx.html:34:36: on range loop re-entry: {{range}} branches end in different contexts: {stateJSRegexp delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}, {stateJS delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}

最后改成这样写 就没问题了

QQ图片20200912145604.pngQQ图片20200912145604.png

centos7安装redis 和配置远程登录

安装redis

yum install redis

修改配置文件

cd /etc
vi redis.conf
注释掉(允许远程连接)
bind 127.0.0.1

protected-mode yes `改成` protected-mode no

# requirepass foobared `改成` requirepass `连接密码`

重启redis

systemctl restart redis

连接不上?

尝试关闭防火墙或者放行端口