168 lines
5.2 KiB
PHP
168 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.cdcloudshop.com
|
|
|
|
* =========================================================
|
|
*/
|
|
|
|
namespace app\model;
|
|
use think\facade\Cache;
|
|
|
|
error_reporting(E_ALL ^ E_NOTICE);
|
|
|
|
|
|
class NewBaseModel extends \think\Model{
|
|
|
|
protected $table = false;
|
|
protected $name = false;
|
|
protected $autoWriteTimestamp = false; // 开启自动时间戳
|
|
protected $deleteTime = false; // 软删除字段
|
|
protected $site_id;
|
|
|
|
public function __construct(array $data = []){
|
|
parent::__construct($data);
|
|
|
|
$data['table_name'] = $this->name ?? $data['table_name'];
|
|
|
|
if($data['table_name']) {
|
|
cache('new_model_table_name',$data['table_name']);
|
|
$tableName = $data['table_name'];
|
|
}else{
|
|
$tableName = cache('new_model_table_name');
|
|
}
|
|
|
|
$this->name = $tableName;
|
|
$this->site_id = (int)request()->siteid();
|
|
}
|
|
|
|
// 操作成功返回值函数
|
|
public function success($data = '',$code_var = 'SUCCESS'){
|
|
$lang_array = $this->getLang();
|
|
$lang_var = isset($lang_array[$code_var]) ? $lang_array[$code_var] : $code_var;
|
|
if($code_var == 'SUCCESS'){
|
|
$code_var = 0;
|
|
}
|
|
else{
|
|
$code_array = array_keys($lang_array);
|
|
$code_index = array_search($code_var,$code_array);
|
|
if($code_index != FALSE){
|
|
$code_var = 10000 + $code_index;
|
|
}
|
|
}
|
|
return success($code_var,$lang_var,$data);
|
|
}
|
|
// 操作失败返回值函数
|
|
public function error($data = '',$code_var = 'FAIL'){
|
|
|
|
$lang_array = $this->getLang();
|
|
if(isset($lang_array[$code_var])){
|
|
$lang_var = $lang_array[$code_var];
|
|
}
|
|
else{
|
|
$lang_var = $code_var;
|
|
$code_var = 'FAIL';
|
|
}
|
|
$code_array = array_keys($lang_array);
|
|
$code_index = array_search($code_var,$code_array);
|
|
if($code_index != FALSE){
|
|
$code_var = -10000 - $code_index;
|
|
}
|
|
return error($code_var,$lang_var,$data);
|
|
}
|
|
// 获取语言包数组
|
|
public function getLang(){
|
|
$default_lang = config("lang.default_lang");
|
|
$cache_common = Cache::get("lang_app/lang/".$default_lang.'/model.php');
|
|
if(empty($cache_common)){
|
|
$cache_common = include 'app/lang/'.$default_lang.'/model.php';
|
|
Cache::tag("lang")
|
|
->set("lang_app/lang/".$default_lang,$cache_common);
|
|
}
|
|
$lang_path = isset($this->lang) ? $this->lang : '';
|
|
if(!empty($lang_path)){
|
|
$cache_path = Cache::get("lang_".$lang_path."/".$default_lang.'/model.php');
|
|
if(empty($cache_path)){
|
|
$cache_path = include $lang_path."/".$default_lang.'/model.php';
|
|
Cache::tag("lang")
|
|
->set("lang_".$lang_path."/".$default_lang,$cache_path);
|
|
}
|
|
$lang = array_merge($cache_common,$cache_path);
|
|
}
|
|
else{
|
|
$lang = $cache_common;
|
|
}
|
|
return $lang;
|
|
}
|
|
|
|
|
|
/**
|
|
* Common: 获取单条信息
|
|
* Author: wu-hui
|
|
* Time: 2022/10/14 15:41
|
|
* @param $id
|
|
* @param array $fields
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function singleInfo($id,$fields = []):array{
|
|
//根据id获取数据信息
|
|
$info = $this->field($fields)->find($id);
|
|
if(!$info) return [];
|
|
//处理相关数据信息
|
|
$info = $info ? $info->toArray() : [];
|
|
|
|
return $this->success($info);
|
|
}
|
|
/**
|
|
* Common: 添加/编辑内容(公共)
|
|
* Author: wu-hui
|
|
* Time: 2022/10/14 11:25
|
|
* @param $info
|
|
* @return array
|
|
*/
|
|
public function editInfo($info){
|
|
$this->startTrans();
|
|
try{
|
|
// 判断是添加还是修改
|
|
if((int)$info[$this->pk] > 0) self::update($info, [$this->pk => $info[$this->pk]]);// 修改内容
|
|
else self::create($info);
|
|
|
|
$this->commit();
|
|
return $this->success();
|
|
}catch(\Exception $e){
|
|
$this->rollback();
|
|
return $this->error('',$e->getMessage());
|
|
}
|
|
}
|
|
/**
|
|
* Common: 删除信息(公共)
|
|
* Author: wu-hui
|
|
* Time: 2022/10/14 12:01
|
|
* @param $id
|
|
* @return array
|
|
*/
|
|
public function delInfo($id){
|
|
// id 不存在,返回
|
|
if((int)$id <= 0) return $this->error('','删除失败,参数错误!');
|
|
// 删除操作
|
|
$this->startTrans();
|
|
try{
|
|
|
|
$this->where($this->pk,$id)
|
|
->useSoftDelete('delete_time',time())
|
|
->delete();
|
|
|
|
$this->commit();
|
|
return $this->success();
|
|
}catch(\Exception $e){
|
|
$this->rollback();
|
|
return $this->error('',$e->getMessage());
|
|
}
|
|
}
|
|
} |