## app\message\model\Message.php
<?php
namespace app\message\model;
use Exception;
use think\exception\ValidateException;
use think\Model;
use app\message\validate\Add;
class Message extends Model
{
// 默认是 message_message 表,这里子定义表结构
protected $table = 'message_index';
// 默认主键为id
protected $pk = 'uid';
// 模型类里面单独开启自动时间戳功能,写入数据的时候,系统会自动写入create_time和update_time字段
// protected $autoWriteTimestamp = true;
// 定义时间戳字段名
protected $createTime = 'create_at';
// 关闭自动写入update_time字段
protected $updateTime = false;
// // 获取器,转变字符为数组;例如:数据库中 name 字段 "a,b" -> ['a','b']
// public function getNameAttr($value)
// {
// return explode(',', $value);
// }
// // 设置器,转变字符为数组;例如:前端数据 name 字段 ['a','b'] -> "a,b"
// public function setNameAttr($value)
// {
// return implode(',', $value);
// }
// 静态方法获取列表
public static function list()
{
return self::order('uid', 'desc')->paginate(10);
// return self::order('uid')->select();
}
public static function add($data)
{
try{
validate(Add::class)->check($data);
self::create($data);
$arr=['code'=>1, 'msg'=>'添加成功'];
}catch (ValidateException $e) {
$arr=['code'=>0, 'msg'=>$e->getError()];
}catch (Exception $s) {
$arr=['code'=>0, 'msg'=>'系统错误,请稍后再试'];
}
return $arr;
}
public static function edit($data)
{
try{
validate(Add::class)->check($data);
self::update($data); // 数据中含有 uid 字段,所以这里不用写先查询再更新
$arr=['code'=>1, 'msg'=>'添加成功'];
}catch (ValidateException $e) {
$arr=['code'=>0, 'msg'=>$e->getError()];
}catch (Exception $s) {
$arr=['code'=>0, 'msg'=>'系统错误,请稍后再试'];
}
return $arr;
}
public static function del($id)
{
try {
self::destroy($id);
$res = ['code'=>1, 'msg'=>'删除成功'];
} catch (Exception $e) {
$res = ['code'=>0, 'msg'=>'删除失败'];
}
return $res;
}
}