这里写目录标题
一、Model部分
php
复制代码
<?php
namespace app\common\model;
use think\Model;
use think\Request;
use think\Db;
use traits\model\SoftDelete;
class Visit Extends Model
{
// use SoftDelete;
// 开启自动写入时间戳字段
protected $name = 'visit_log';
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $append = [
];
protected $alias = 'u';
public function user(){
return $this->belongsTo('User', 'uid', 'id', [], 'LEFT')->bind(['username'=>'username']);
}
}
二、增加
php
复制代码
public function add(){
$log = new ModelVisit;
$log->uid = $this->auth->id;
$log->gid = $this->auth->group_id;
$log->visit_time = input('visit_time');
$log->status = input('status');
$log->status = 1;
if($log->save()){
$this->success("ok");
}else{
$this->error("添加失败,请稍后再试");
}
}
三、查询
php
复制代码
public function list(){
$row = ModelVisit::with('user')->where('gid',$this->auth->group_id)->order("id","desc")->select();
foreach ($row as $k => $v) {
$v->visit_time = datetime($v->visit_time, 'Y/m/d');
}
if($row){
$this->success('ok',$row);
}else{
$this->error("查询失败,请稍后再试");
}
}
四、删除
php
复制代码
public function del(){
$id = input('id');
$ret = ModelVisit::get($id);
if (!$ret) {
$this->error('记录不存在');
}
ModelVisit::destroy($id);
$this->success('删除成功');
}
五、修改
php
复制代码
public function edit(){
$id = input('id');
$row = ModelVisit::get($id);
if (!$row) {
$this->error('记录不存在');
}
$log = [];
$log['visit_time'] = input('visit_time');
$log['visit_type'] = input('visit_type');
if($row->allowField(true)->save($log) === false){
$this->error('修改失败');
}
$this->success('修改成功');
}