chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace addon\haonav\model;
|
||||
|
||||
use ywxapp\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 用户收藏夹分享配置(一个用户一条)
|
||||
*
|
||||
* 用户可把「我的导航」生成一个只读分享链接,他人凭 token 访问 shared 页浏览,
|
||||
* enabled=1 才对外可见;token 可重置(旧链接立即失效)。
|
||||
*
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class FavoriteShare extends BaseModel
|
||||
{
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'strict' => false,
|
||||
'name' => 'haonav_favorite_shares',
|
||||
'autoRelation' => [],
|
||||
'createTime' => 'create_at',
|
||||
'updateTime' => 'update_at',
|
||||
'dateFormat' => 'Y-m-d H:i:s',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个随机分享令牌(16位十六进制)
|
||||
*/
|
||||
public static function genToken(): string
|
||||
{
|
||||
try {
|
||||
return bin2hex(random_bytes(8));
|
||||
} catch (\Throwable $e) {
|
||||
return substr(md5(uniqid((string)mt_rand(), true)), 0, 16);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取当前用户的分享配置,无则创建一条(默认关闭、已生成 token)
|
||||
*/
|
||||
public static function forUser(int $uid): FavoriteShare
|
||||
{
|
||||
$row = self::where('user_id', $uid)->find();
|
||||
if (!$row) {
|
||||
$row = new self();
|
||||
$row->user_id = $uid;
|
||||
$row->token = self::genToken();
|
||||
$row->title = '我的导航收藏夹';
|
||||
$row->enabled = 0;
|
||||
$row->views = 0;
|
||||
$row->save();
|
||||
} elseif (empty($row->token)) {
|
||||
$row->token = self::genToken();
|
||||
$row->save();
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user