Files
ntonpower/app/api/model/BaseModel.php
2026-10-14 14:00:29 +08:00

418 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types = 1);
namespace app\api\model;
use app\library\SSOApi;
use think\facade\Env;
use think\Model;
/**
* 后台模型基类
* Class BaseModel
* @package app\api\model
*/
class BaseModel extends Model
{
use SSOApi;
/**
* 查看列表数据
* @param int $page 分页页码
* @param int $pageSize 分页类目数量
* @param array $field 需要输入的字段,默认为全部输出,筛选表字段
* @param array $vague 需要进行模糊查询的字段,如: a like %a%
* @param array $focus 精确搜索条件,如a=1,b=2这种精确等于某个值的
* @param array $order 排序字段,如:['id'=>'desc','sort'=>'desc']
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getIndexList(int $page,int $pageSize,array $field = [], array $vague = [],array $focus = [],array $order = []):array
{
$fields = array_keys($this->schema);
$collection = $this->field($field)->where(function ($query) use ($fields,$focus) {
//精准查询
if (!empty($focus)) {
foreach ($focus as $key => $item) {
if (in_array($key,$fields)) {
$query->where($key,$item);
}
}
}
})->where(function($query) use ($fields,$vague) {
//模糊查询
if (!empty($vague)) {
foreach ($vague as $key => $item) {
if (in_array($key,$fields)) {
$query->whereOr($key,'like',"%".$item."%");
}
}
}
})->order($order);
//判断是否需要分页
if (0 == $page || 0 == $pageSize) {
//不需要分页
return $collection->select()->toArray();
} else {
//需要分布
$totalNum = $collection->count();
$data = $collection->page($page)->limit($pageSize)->select()->toArray();;
return ['count'=>$totalNum,'data'=>$data];
}
}
/**
* 新增数据
* @param array $data
* @return bool
*/
public function addData(array $data) : bool
{
$result = $this->create($data);
return (bool)$result;
}
/**
* 编辑数据
* @param array $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function editData(array $data) : bool
{
if (isset($data['id']) && $data['id'] > 0) {
$result = $this->find($data['id']);
return $result && $result->save($data);
}
return false;
}
/**
* 获取详情
* @param array $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getInfo(array $data) : array
{
if (isset($data['id'])) {
$result = $this->find($data['id']);
return $result ? $result->toArray() : [];
}
return [];
}
/**
* 删除
* @param array $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function delData(array $data) : bool
{
if (isset($data['id'])) {
$result = $this->find($data['id']);
return $result && $result->delete();
}
return false;
}
/**
* 通过ID获取数据
* @param $ids
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getExportData($ids): array
{
$result = $this->whereIn($this->pk,$ids['ids'])->order('create_time','desc')->select();
return $result ? $result->toArray() : [];
}
/**
* 批量修改
* @param $data
* @return bool
* @throws \Exception
*/
public function batchEditData($data):bool
{
$result = $this->saveAll($data);
return (bool)$result;
}
/**
* 批量删除
* @param array $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function batchDelData(array $data) : bool
{
$res = true;
foreach ($data as $item) {
if (isset($item['id']) && $item['id'] > 0) {
$result = $this->find($item['id']);
$result = $result->delete();
$res = $res && $result;
}
}
return $res;
}
/**
* 获取地区名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAreaNameById($id):string
{
$data = Area::find($id);
return $data ? $data['name'] : '';
}
/**
* 获取币种名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCoinName($id):string
{
$result = Currency::find($id);
return $result ? $result['name'] : '';
}
/**
* 获取站点语言名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getSiteLanguageName($id):string
{
$lang = Site::find($id);
$result = Language::find($lang['language_id']);
return $result ? $result['name'] : '';
}
/**
* 获取语言名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getLanguageName($id):string
{
$result = Language::find($id);
return $result ? $result['name'] : '';
}
/**
* 获取语言名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCountryName($id):string
{
$result = Country::find($id);
return $result ? $result['zh'] : '其他';
}
/**
* 获取币种名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getPlatformName($id):string
{
$result = Platform::find($id);
return $result ? $result['name'] : '';
}
/**
* 通过id获取名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getFileTypeName($id):string
{
$result = FileType::find($id);
return $result ? $result['name'] : '';
}
/**
* 通过id获取名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getFaqTypeName($id):string
{
$result = FaqType::find($id);
return $result ? $result['name'] : '';
}
/**
* 通过id获取名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAdvertisePageName($id):string
{
$result = AdvertisePage::find($id);
return $result ? $result['name'] : '';
}
/**
* 通过id获取名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAdertiseSpaceName($id):string
{
$result = AdvertiseSpace::find($id);
return $result ? $result['name'] : '';
}
/**
* 通过id获取尺寸
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAdvertiseSpaceSpec($id):string
{
$result = AdvertiseSpace::find($id);
if ($result) {
$width = $result['width'];
$height = $result['height'];
return $width. 'x' . $height;
}
return '';
}
/**
* 通过id获取名称
* @param $id
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getGoodsName($id):string
{
$result = Goods::find($id);
return $result ? $result['name'] : '';
}
/**
* 启用禁用
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function changeStatus($data):bool
{
$result = $this->find($data['id']);
return $result && $result->save($data);
}
/**
* 排序
* @param $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function changeSort($data):bool
{
$result = $this->find($data['id']);
return $result && $result->save($data);
}
/**
* 获取子类ID
* @param $id
* @return array
*/
public function getChildCategory($id):array
{
$res = $this->where('pid',$id)->column('id');
array_push($res,intval($id));
return $res ?: [];
}
/**
* 获取父类ID
* @param $id
* @return array
*/
public function getPCategory($id):array
{
$res = $this->where('id',$id)->column('id');
array_push($res,intval($id));
return $res ?: [];
}
/**
* 动态数据检测,检测提交过来的数据中ID是否合法
* @param $model
* @param $condition
* @return bool
*/
public function dynamicCheck($model,$condition):bool
{
return (bool) $model::where($condition)->find();
}
}