2024年9月

var addresses []model.Address
if ret := s.db.WithContext(ctx).Model(&model.Address{}).
    Where("member_id = ?", memberId).Find(&addresses); ret.Error != nil {
    return
}
if len(addresses) > 0 {
    defineAddressIndex := 0
    for i, address := range addresses {
        if address.IsDefault == 1 {
            defineAddressIndex = i
            break
        }
    }
    result.Address = addresses[defineAddressIndex]
}

代码

package utils

import (
    "crypto/rand"
    "encoding/hex"
    "fmt"
)

func New() string {
    uuid, _ := GenerateUUID()
    return uuid
}

// GenerateRandomBytes is used to generate random bytes of given size.
func GenerateRandomBytes(size int) ([]byte, error) {
    buf := make([]byte, size)
    if _, err := rand.Read(buf); err != nil {
        return nil, fmt.Errorf("failed to read random bytes: %v", err)
    }
    return buf, nil
}

const uuidLen = 16

// GenerateUUID is used to generate a random UUID
func GenerateUUID() (string, error) {
    buf, err := GenerateRandomBytes(uuidLen)
    if err != nil {
        return "", err
    }
    return FormatUUID(buf)
}

func FormatUUID(buf []byte) (string, error) {
    if buflen := len(buf); buflen != uuidLen {
        return "", fmt.Errorf("wrong length byte slice (%d)", buflen)
    }

    return fmt.Sprintf("%x-%x-%x-%x-%x",
        buf[0:4],
        buf[4:6],
        buf[6:8],
        buf[8:10],
        buf[10:16]), nil
}

func ParseUUID(uuid string) ([]byte, error) {
    if len(uuid) != 2*uuidLen+4 {
        return nil, fmt.Errorf("uuid string is wrong length")
    }

    if uuid[8] != '-' ||
        uuid[13] != '-' ||
        uuid[18] != '-' ||
        uuid[23] != '-' {
        return nil, fmt.Errorf("uuid is improperly formatted")
    }

    hexStr := uuid[0:8] + uuid[9:13] + uuid[14:18] + uuid[19:23] + uuid[24:36]

    ret, err := hex.DecodeString(hexStr)
    if err != nil {
        return nil, err
    }
    if len(ret) != uuidLen {
        return nil, fmt.Errorf("decoded hex is the wrong length")
    }

    return ret, nil
}

使用

uud := utils.New()
fmt.Println(uud)
//9a317a2c-b5d1-ae15-7d76-27f2b48d539a

批量替换mysql一个表中某个字段的值里面的部分字符串,使用mysql replace函数:

UPDATE prezzie_category SET image = REPLACE (image, 'http://localhost:8727','https://a.liziyu.com') where id > 0;

注意,操作之前请先备份表:

CREATE TABLE prezzie_category_bak AS SELECT * FROM prezzie_category;

Nginx反代

server
{
    listen 80;
    # 您的域名
    server_name cms.demo.mallray.com;
    # 索引文档,建议将 index.html 放在第一个
    index index.html

    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md|package.json|package-lock.json|\.env) {
        return 404;
    }


    # HTTP反向代理相关配置开始 >>>
    location / {
        # 端口为3000,自定义后请注意修改
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_connect_timeout 30s;
        proxy_read_timeout 86400s;
        proxy_send_timeout 30s;
        proxy_redirect off;
    }
    # HTTP反向代理相关配置结束 <<<

    # 有可能会存在的默认规则-【请删除它们】,否则很多资源会404 - START
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null;
    }
    # 有可能会存在的默认规则-【请删除它们】,否则很多资源会404 - END
}

使用面板

  1. 进入面板,打开网站管理,切换到 Node 项目,并添加 Node 项目
  2. 项目目录选择 web-nuxt 根目录
  3. 启动选项请选择 start
  4. 真实端口填写 3000
  5. 填写正确的绑定域名,宝塔将自动配置好代理
  6. 项目添加成功后,即可使用域名访问站点,并可以方便的配置 SSL 证书等