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.
100 lines
2.5 KiB
100 lines
2.5 KiB
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use think\Controller;
|
|
use think\Db;
|
|
use think\Exception;
|
|
use think\db\exception\DataNotFoundException;
|
|
use think\db\exception\ModelNotFoundException;
|
|
use think\exception\DbException;
|
|
use think\exception\PDOException;
|
|
|
|
class Base extends Controller
|
|
{
|
|
protected $ERROR_CODE = 0;
|
|
protected $SUCCESS_CODE = 1;
|
|
|
|
protected function jsonError($msg = '失败',$data = [])
|
|
{
|
|
$arr = [
|
|
'code' => $this->ERROR_CODE,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
return json_encode($arr);
|
|
}
|
|
|
|
protected function jsonSuccess($msg = '成功',$data = [])
|
|
{
|
|
$arr = [
|
|
'code' => $this->SUCCESS_CODE,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
return json_encode($arr);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $tableName
|
|
* @param $queryData
|
|
* @param $data
|
|
* @param $idArr
|
|
* @return array
|
|
* @throws Exception
|
|
* @throws PDOException
|
|
*/
|
|
protected function queryUpdate($tableName,$queryData,$data,$idArr)
|
|
{
|
|
$idArr = [];
|
|
if ($queryData) {
|
|
|
|
$id = $queryData['id'];
|
|
|
|
$update = [];
|
|
foreach ($queryData as $key => $row) {
|
|
if (isset($data[$key]) && $data[$key] != $row) {
|
|
$update[$key] = $row;
|
|
}
|
|
}
|
|
|
|
if ($update) {
|
|
Db::table($tableName)->where('id',$id)->update($update);
|
|
}
|
|
|
|
$idArr[] = $id;
|
|
} else {
|
|
|
|
Db::table($tableName)->insert($data);
|
|
$idArr[] = Db::table($tableName)->getLastInsID();
|
|
}
|
|
return $idArr;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $tableName
|
|
* @param $idArr
|
|
* @param $updateStatus
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws Exception
|
|
* @throws ModelNotFoundException
|
|
* @throws PDOException
|
|
*/
|
|
protected function updateStatus($tableName,$idArr,$updateStatus = ['status' => 0])
|
|
{
|
|
if ($idArr) {
|
|
# 查询不存在的id
|
|
$notList = Db::table($tableName)->where('id', 'not in', $idArr)->field('id')->select();
|
|
$notIdArr = [];
|
|
foreach ($notList as $notListRow) $notList[] = $notListRow['id'];
|
|
if ($notIdArr) {
|
|
Db::table($tableName)->where('id','in',$notIdArr)->update($updateStatus);
|
|
}
|
|
} else {
|
|
Db::table($tableName)->update($updateStatus);
|
|
}
|
|
}
|
|
}
|