308 lines
9.8 KiB
PHP
Executable File
308 lines
9.8 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* @Author: fm453
|
|
* @Date: 2018-06-04 11:06:07
|
|
* @Last Modified by: fm453
|
|
* @Last Modified time: 2021-09-11 08:07:18
|
|
* @Email: fm453@lukegzs.com
|
|
*/
|
|
namespace backend\controllers;
|
|
|
|
use Yii;
|
|
use yii\data\Pagination;
|
|
use yii\helpers\Url;
|
|
use addons\models\AcWeituoren;
|
|
use common\models\Fans;
|
|
|
|
class WeituorenController extends \yii\web\Controller
|
|
{
|
|
//替代常规的_construct 析构函数;其他方法调用前执行
|
|
public function init()
|
|
{
|
|
parent::init();
|
|
$session = Yii::$app->session;
|
|
if(isset($_GET['pid']) && (int)$_GET['pid']>0){
|
|
$session->set('pid',(int)$_GET['pid']);
|
|
}
|
|
$pid = $session->get('pid');
|
|
if(!$pid){
|
|
return Yii::$app->response->redirect(['index/index']);
|
|
}
|
|
}
|
|
|
|
//主界面
|
|
public function actionIndex()
|
|
{
|
|
return $this->render('../layouts/dev',[]);
|
|
}
|
|
|
|
//列表
|
|
public function actionList()
|
|
{
|
|
$model = new AcWeituoren();
|
|
$pid = Yii::$app->session->get('pid');
|
|
$session = Yii::$app->session;
|
|
$where = $where2 = [];
|
|
$where2[]='and';
|
|
$where['pid'] = $pid;
|
|
$post = Yii::$app->request->post();
|
|
$search = [];
|
|
$searchSession = 'weituoren::search';
|
|
if(isset($_GET['reset']) && $_GET['reset']==1){
|
|
$post['search'] = [];
|
|
}
|
|
if(isset($post['search'])){
|
|
$search = $post['search'];
|
|
$session->set($searchSession,$search);
|
|
}elseif($session->get($searchSession)){
|
|
$search = $session->get($searchSession);
|
|
}
|
|
|
|
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
|
if(!empty($search['title'])){
|
|
$where2[] = ['LIKE','company',$search['title']];
|
|
}
|
|
$where['deleted'] = 0;
|
|
$data = $model->find()->where($where);
|
|
if($search['title']){
|
|
$data = $data->andwhere($where2);
|
|
}
|
|
|
|
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
|
|
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
|
$status = Yii::$app->params['CommonStatus'];
|
|
|
|
return $this->render('list',[
|
|
'weituorens'=>$res,
|
|
'pager' => $pages,
|
|
'status'=>$status,
|
|
'search'=>$search
|
|
]);
|
|
}
|
|
|
|
public function actionSelect()
|
|
{
|
|
$model = new AcWeituoren();
|
|
$pid = Yii::$app->session->get('pid');
|
|
$session = Yii::$app->session;
|
|
$where = $where2 = [];
|
|
$where2[]='and';
|
|
$where['pid'] = $pid;
|
|
$post = Yii::$app->request->post();
|
|
$search = [];
|
|
$searchSession = 'weituoren::search';
|
|
if(isset($_GET['reset']) && $_GET['reset']==1){
|
|
$post['search'] = [];
|
|
}
|
|
if(isset($post['search'])){
|
|
$search = $post['search'];
|
|
$session->set($searchSession,$search);
|
|
}elseif($session->get($searchSession)){
|
|
$search = $session->get($searchSession);
|
|
}
|
|
|
|
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
|
if(!empty($search['title'])){
|
|
$where2[] = ['LIKE','company',$search['title']];
|
|
}
|
|
$where['deleted'] = 0;
|
|
$data = $model->find()->where($where);
|
|
if($search['title']){
|
|
$data = $data->andwhere($where2);
|
|
}
|
|
|
|
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
|
|
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
|
$status = Yii::$app->params['CommonStatus'];
|
|
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'weituoren';
|
|
return $this->render('select',[
|
|
'weituorens'=>$res,
|
|
'pager' => $pages,
|
|
'status'=>$status,
|
|
'search'=>$search,
|
|
'callback'=>$callback
|
|
]);
|
|
}
|
|
|
|
public function actionNew()
|
|
{
|
|
return $this->render('modify',[]);
|
|
}
|
|
|
|
//编辑资料
|
|
public function actionEdit()
|
|
{
|
|
$model = new AcWeituoren();
|
|
$id = Yii::$app->request->get('id');
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
if($res){
|
|
$res = $res->toArray();
|
|
}
|
|
|
|
return $this->render('modify',['detail'=>$res]);
|
|
}
|
|
|
|
//保存资料
|
|
public function actionSave()
|
|
{
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$post['id'];
|
|
$pid = Yii::$app->session->get('pid');
|
|
//格式化数据
|
|
//目标键=》POST键
|
|
$cols = ['company'=>'company','name'=>'name','mobile'=>'mobile'];
|
|
foreach($cols as $col=>$key){
|
|
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
|
|
}
|
|
//查询手机号是否在库
|
|
$hasWeituoren = AcWeituoren::find()->where(['mobile'=>$data['mobile']])->one();
|
|
if($hasWeituoren){
|
|
if($id && $id==$hasWeituoren->id){
|
|
|
|
}else{
|
|
$id = $hasWeituoren->id;
|
|
\Yii::$app->getSession()->setFlash('danger', '保存失败,该手机号对应的委托单位已在库,系统自动现在为您调出其资料!');
|
|
return $this->redirect(['edit','id'=>$id]);
|
|
}
|
|
}
|
|
//查询对应的系统用户
|
|
$userModel = new Fans();
|
|
$user = $userModel->findByUsername($data['mobile']);
|
|
if(empty($user)){
|
|
$pwd = Yii::$app->params['defaultFansPassword'];
|
|
$userModel->mobile = $data['mobile'];
|
|
$userModel->email = $data['mobile'].'@hiluker.com';
|
|
$userModel->setPassword($pwd);
|
|
$userModel->generateAuthKey();
|
|
$userModel->created_at = time();
|
|
$userModel->from = 'ctms';
|
|
$userModel->save();
|
|
}
|
|
|
|
$cols = ['status_code'=>'status_code','deleted'=>'deleted'];
|
|
foreach($cols as $col=>$key){
|
|
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
|
}
|
|
$data['create_at'] = time();
|
|
$data['update_at'] = $data['create_at'];
|
|
$data['pid'] = $pid;
|
|
|
|
//保存通讯录资料
|
|
$AcWeituoren = new AcWeituoren();
|
|
foreach($data as $key=>$val){
|
|
$AcWeituoren->$key = $val;
|
|
}
|
|
|
|
if($id){
|
|
$AcWeituoren->id = $id;
|
|
unset($data['create_at']);
|
|
$res = $AcWeituoren->updateAll($data,['id'=>$id]);
|
|
}else{
|
|
$res = $AcWeituoren->save();
|
|
$id = $AcWeituoren->attributes['id']; //获取插入后id
|
|
}
|
|
|
|
if($res){
|
|
\Yii::$app->getSession()->setFlash('success', '委托单位信息保存成功!');
|
|
return $this->redirect(['edit','id'=>$id]);
|
|
}else{
|
|
\Yii::$app->getSession()->setFlash('warning', '委托单位信息保存失败!');
|
|
return $this->goBack();
|
|
}
|
|
}
|
|
|
|
//删除
|
|
public function actionDelete()
|
|
{
|
|
$get = Yii::$app->request->get();
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$get['id'];
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
$data['deleted'] = 1;
|
|
|
|
$model = new AcWeituoren();
|
|
foreach($data as $key=>$val){
|
|
$model->$key = $val;
|
|
}
|
|
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,$where);
|
|
|
|
$return = [];
|
|
$return['url'] = Url::toRoute('weituoren/list');
|
|
$return['timeout'] = 3; //3秒后自动跳转
|
|
$return['status'] = 0;
|
|
if($res){
|
|
$return['msg'] = '委托单位删除成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = $id;
|
|
}else{
|
|
$return['msg'] = '委托单位删除失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
exit(json_encode($return,JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
public function actionAjax()
|
|
{
|
|
$get = Yii::$app->request->get();
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$get['id'];
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
|
|
$model = new AcWeituoren();
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
$return = [];
|
|
$return['timeout'] = 1; //3秒后自动跳转
|
|
$return['status'] = 0;
|
|
$return['ajax'] = 1;
|
|
if($res){
|
|
$res = $res->toArray();
|
|
}else{
|
|
$return['msg'] = '委托单位数据不存在';
|
|
$return['errorcode'] = 404;
|
|
exit(json_encode($return,JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
switch($get['do'])
|
|
{
|
|
case 'out':
|
|
$data['status'] = 0;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,$where);
|
|
if($res){
|
|
$return['msg'] = '委托单位隐藏标记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css'=>'info'];
|
|
}else{
|
|
$return['msg'] = '委托单位隐藏标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'in':
|
|
$data['status'] = 1;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,$where);
|
|
if($res){
|
|
$return['msg'] = '委托单位显示标记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css'=>'default'];
|
|
}else{
|
|
$return['msg'] = '委托单位显示标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
exit(json_encode($return,JSON_UNESCAPED_UNICODE));
|
|
}
|
|
}
|