Files
fm453 270b881437 ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-07-13 19:40:10 +08:00

264 lines
7.5 KiB
PHP
Executable File

<?php
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use addons\models\AcContact;
class ContactController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list'=>'联系人列表',
'detail'=>'详情',
'add'=>'添加',
'edit'=>'编辑',
'delete'=>'删除',
'show'=>'显示',
'hide'=>'隐藏',
'ajax'=>'ajax操作'
];
$this->result('您正使用CMTS-GM系统联系人管理接口!', $apis, 200);
}
//通讯录列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$return = [];
$return['code'] = 200;
$return['msg'] = '联系人查询成功!';
$return['data']= [
'total'=>$s['query']->count(),
'contactors' => $res,
'page'=>$this->page
];
$this->result($return['msg'], $return['data'], $return['code']);
}
public function actionDetail()
{
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误,未携带ID参数');
$model = new AcContact();
$res = $model->findOne($id)->toArray();
if(!$res) $this->result('未查询到相应数据');
$this->result('车主信息查询完成', $res, 200);
}
public function actionAdd()
{
//数据预检查、编排
$data = $this->preSave('add');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
$model = new AcContact();
foreach ($data as $key=>$val) {
$model->$key = $val;
}
$res = $model->save();
$msg = '数据保存失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据保存成功!';
$return = [];
$return['id']= $model->attributes['id']; //获取插入后id;
$this->result($msg,$return, 200);
}
//编辑通讯录资料
public function actionEdit()
{
$detail = $this->preUpdate();
//数据预检查、编排
$data = $this->preSave('edit');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '数据编辑失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据编辑成功!';
$return = [];
$return['data']= $detail;
$this->result($msg,$return, 200);
}
//删除
public function actionDelete()
{
$detail = $this->preUpdate();
$data = [];
$data['update_at'] = time();
$data['deleted'] = $detail->deleted + 1;
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '通讯录数据删除失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '通讯录数据删除成功!';
$this->result($msg,[], 200);
}
public function actionAjax()
{
$detail = $this->preUpdate();
$get = Yii::$app->request->get();
$msg = '';
$errorCode = 0;
$detail->update_at = time();
switch ($get['do']) {
case 'hide':
$detail->status_code = 0;
$res = $detail->save();
if ($res) {
$msg = '通讯录隐藏标记成功';
$errorCode = 200;
} else {
$msg = '通讯录隐藏标记失败';
}
break;
case 'show':
$detail->status_code = 1;
$res = $detail->save();
if ($res) {
$msg = '通讯录显示标记成功';
$errorCode = 200;
} else {
$msg = '通讯录显示标记失败';
}
break;
default:
break;
}
$this->result($msg,[], $errorCode);
}
private function search(){
$pid = $this->pid;
$return = [];
$model = new AcContact();
$where = $where2 = [];
$where['pid'] = $pid;
$post = $this->postdata;
$search = $post['search'] ?? [];
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
if (!empty($search['title'])) {
$where2[] = 'or';
$where2[] = ['LIKE', 'name', $search['title']];
$where2[] = ['LIKE', 'mobile', $search['title']];
}
$search['company'] = isset($search['company']) ? trim(htmlspecialchars_decode($search['company'])) : '';
if(!empty($search['company'])){
$where2[] = 'and';
$where2[] = ['like', 'company', $search['company']];
}
$where['deleted'] = 0;
$data = $model->find()->where($where);
if ($where2) {
$data = $data->andwhere($where2);
}
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
return ['res'=>$res,'query'=>$data];
}
/*
* 数据更新前的预检查,返回对应关联数据
* 必须确保get与post数据中均包含需更新的数据id且一致
* */
private function preUpdate()
{
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误,未携带ID参数');
$post = $this->postdata;
if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403);
$model = new AcContact();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* 要保存的数据[],直接从post中取出
* @op,操作类型(add,edit……)
* 校验机制:
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的数据
* */
private function preSave($op)
{
$post = $this->postdata;
$name = trim($post['name']) ?? '';
if(!$name) $this->result('请传入客户名称');
$data = [];
//格式化数据
//目标键=》POST键
$cols = ['company' => 'company', 'idcard' => 'idcard'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$model = new AcContact();
switch ($op) {
case 'add':
$mobile = trim($post['mobile']) ?? '';
if(!$mobile) $this->result('请传入手机号');
//查询通讯录手机号是否在库(如果在库,则取出相应资料,剩余逻辑由前端完成)
$hasOne = AcContact::find()->where(['mobile' => $mobile])->one();
if($hasOne){
$detail = $hasOne->toArray();
$this->result('保存失败,该手机号对应的通讯录已在库,系统自动现在为您调出该通讯录资料!',$detail,100);
}
$data['mobile'] = $mobile;
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
break;
case 'edit':
//编辑模式下,没有改mobile,避免校验重复通讯录
$id = (int)Yii::$app->request->get('id');
$_id = (int)$post['id'];
if(!$id != $_id) $this->result('id参数不匹配,请检查');
$data['update_at'] = time();
break;
}
$data['pid'] = $this->pid;
$cols = ['status_code' => 'status_code', 'deleted' => 'deleted'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
return $data;
}
}