You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_php/extend/traits/CurdControllerTrait.php

53 lines
2.4 KiB

9 months ago
<?php
// +----------------------------------------------------------------------
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
// +----------------------------------------------------------------------
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
// +----------------------------------------------------------------------
namespace traits;
use service\JsonService;
use think\Request;
trait CurdControllerTrait
{
public function change_field($id,$field)
{
if(!isset($this->bindModel)) return exception('方法不存在!');
if(!class_exists($this->bindModel)) return JsonService::fail('操作Model不存在!');
$model = new $this->bindModel;
$pk = $model->getPk();
if(strtolower($pk) == strtolower($field)) return JsonService::fail('主键不允许修改!');
$data = $model->where($pk,$id)->find();
if(!$data) JsonService::fail('记录不存在!');
$value = Request::instance()->post($field);
if($value === null) return JsonService::fail('请提交需要编辑的数据!');
$data->$field = $value;
return false !== $data->save() ? JsonService::successful('编辑成功!') : JsonService::fail('编辑失败!');
}
public function consult_field($id,$field)
{
if(!isset($this->bindModel)) return exception('方法不存在!');
if(!class_exists($this->bindModel)) return JsonService::fail('操作Model不存在!');
$model = new $this->bindModel;
$data = $model->where('nid',$id)->find();
$value = Request::instance()->post($field);
if($value === null) return JsonService::fail('请提交需要编辑的数据!');
if(!$data){
$data[$field] = $value;
$data['nid'] = $id;
return false !== $model->create($data) ? JsonService::successful('添加成功!') : JsonService::fail('添加失败!');
}else{
$data->$field = $value;
return false !== $data->save() ? JsonService::successful('编辑成功!') : JsonService::fail('编辑失败!');
}
}
}