| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- /**
- * 只需要登入,无需检测权限的公共方法
- * 前端直接请求的话必须添加header Authorization: userStore.accessToken
- */
- class CommonController extends Controller
- {
- // 首页统计
- public function actionStat()
- {
- $ret = [];
- $today = date('Y-m-d');
- $cri = DbCriteria::simpleCompare([])
- ->addBetweenCondition('create_date', $today, $today . ' 23:59:59')
- ->setSelect('count(id) as num');
- // 今日新增校方跟进记录
- $ret[] = [
- 'num' => DB::getScalerWithCriteria('school_follow', $cri),
- 'des' => '今日新增校方跟进记录',
- 'detail_path' => '/school/follow'
- ];
- // 今日新增食堂跟进记录
- $ret[] = [
- 'num' => DB::getScalerWithCriteria('canteen_follow', $cri),
- 'des' => '今日新增食堂跟进记录',
- 'detail_path' => '/canteen/follow'
- ];
- // 今日新增餐饮公司跟进记录
- $ret[] = [
- 'num' => DB::getScalerWithCriteria('company_follow', $cri),
- 'des' => '今日新增餐饮公司跟进记录',
- 'detail_path' => '/company/follow'
- ];
- $cri->addCondition('is_del = 0');
- // 今日新增校方关系
- $ret[] = [
- 'num' => DB::getScalerWithCriteria('school_contact', $cri),
- 'des' => '今日新增校方关系',
- 'detail_path' => '/school/relation'
- ];
- // 今日新增餐饮公司关系
- $ret[] = [
- 'num' => DB::getScalerWithCriteria('company_contact', $cri),
- 'des' => '今日新增餐饮公司关系',
- 'detail_path' => '/company/relation'
- ];
- Helper::ok($ret);
- }
- /**
- * 统一获取下拉列表: 主要是减少前端请求数量,以及方便统一的操作及权限管理
- * @return void
- * @throws CException
- */
- public function actionGetSelectList()
- {
- $types = Helper::getArrParam($_POST, 'types', Helper::PARAM_KEY_TYPE['array_string']);
- $ret = [];
- foreach ($types as $type) {
- switch ($type) {
- case 'user':
- $cri = DbCriteria::simpleCompare(['id' => '!=1', 'status' => 1])
- ->setSelect('id, username as name');
- $data = DB::getListWithCriteria('useradmin', $cri);
- $ret['user'] = $data['records']??[];
- break;
- case 'role':
- $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
- $data = DB::getListWithCriteria('role', $cri);
- $ret['role'] = $data['records']??[];
- break;
- case 'school':
- $cri = DbCriteria::simpleCompare(['is_del' => 0, 'id' => $this->getSchoolFilter()])
- ->setSelect('id, name');
- $data = DB::getListWithCriteria('school', $cri);
- $ret['school'] = $data['records']??[];
- break;
- case 'canteen':
- $cri = DbCriteria::simpleCompare(['is_del' => 0, 'school_id' => $this->getSchoolFilter()])
- ->setSelect('id, name');
- $data = DB::getListWithCriteria('canteen', $cri);
- $ret['canteen'] = $data['records']??[];
- break;
- case 'company':
- $cri = DbCriteria::simpleCompare(['is_del' => 0, 'id' => $this->getCompanyFilter()])
- ->setSelect('id, name');
- $data = DB::getListWithCriteria('company', $cri);
- $ret['company'] = $data['records']??[];
- break;
- case 'school_canteen':
- $filter = ['is_del' => 0, 'id' => $this->getSchoolFilter()];
- $company_id = Helper::getPostInt('company_id');
- $exclude_canteen_ids = [];
- if ($company_id) {
- $exclude_canteen_ids = Helper::arrayColumn(DB::getListWithCriteria('company_canteen_relation', DbCriteria::simpleCompare(['company_id' => '!=' . $company_id])), 'canteen_id');
- }
- $cri = DbCriteria::simpleCompare($filter)->setSelect('id, name');
- $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id');
- if (empty($schools)) {
- $ret['school_canteen'] = [];
- break;
- }
- $cri1 = DbCriteria::simpleCompare(['is_del' => 0, 'school_id' => array_keys($schools)])
- ->setSelect('id, name, school_id as parent_id')
- ->addNotInCondition('id', $exclude_canteen_ids);
- $relations = DB::getListWithCriteria('canteen', $cri1);
- $ret['school_canteen'] = $this->formatRelationList($relations['records'], $schools);
- break;
- case 'school_relation':
- $filter = ['is_del' => 0, 'id' => $this->getSchoolFilter()];
- $cri = DbCriteria::simpleCompare($filter)->setSelect('id, name');
- $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id');
- if (empty($schools)) {
- $ret['school_relation'] = [];
- break;
- }
- $cri1 = DbCriteria::simpleCompare(['is_del' => 0, 'school_id' => array_keys($schools)])->setSelect('id, name, school_id as parent_id');
- $relations = DB::getListWithCriteria('school_contact', $cri1);
- $ret['school_relation'] = $this->formatRelationList($relations['records'], $schools);
- break;
- case 'company_relation':
- $filter = ['is_del' => 0, 'id' => $this->getCompanyFilter()];
- $cri = DbCriteria::simpleCompare($filter)->setSelect('id, name');
- $compamnys = Helper::arrayColumn(DB::getListWithCriteria('company', $cri), null, 'id');
- if (empty($compamnys)) {
- $ret['company_relation'] = [];
- break;
- }
- $cri1 = DbCriteria::simpleCompare(['is_del' => 0, 'company_id' => array_keys($compamnys)])->setSelect('id, name, company_id as parent_id');
- $relations = DB::getListWithCriteria('company_contact', $cri1);
- $ret['company_relation'] = $this->formatRelationList($relations['records'], $compamnys);
- break;
- default:
- Helper::error('参数错误');
- break;
- }
- }
- Helper::ok($ret);
- }
- private function formatRelationList($list, $parentList)
- {
- foreach ($list as $relation) {
- $sid = $relation['parent_id'];
- if (!isset($parentList[$sid])) {
- continue;
- }
- if (!isset($parentList[$sid]['children'])) {
- $parentList[$sid]['children'] = [];
- }
- $parentList[$sid]['children'][] = [
- 'id' => $relation['id'],
- 'name' => $relation['name'],
- ];
- }
- return array_values($parentList);
- }
- /**
- * 图片上传
- * 不同类型放到不同目录,返回格式也会不同
- */
- public function actionUploadImg()
- {
- $upType = '';
- $maxSize = 3;
- if (!empty($_FILES['follow'])) {
- $upType = 'follow';
- $upArr = $_FILES['follow'];
- } elseif (!empty($_FILES['editor'])) {
- $upType = 'editor';
- $upArr = $_FILES['editor'];
- } elseif (!empty($_FILES['avatar'])) {
- $upType = 'avatar';
- $upArr = $_FILES['avatar'];
- $maxSize = 0.3;
- } elseif (!empty($_FILES['canteen'])) {
- $maxSize = 5;
- $upType = 'canteen';
- $upArr = $_FILES['canteen'];
- } else {
- Helper::error('上传有误');
- }
- if ($upArr['size'] > $maxSize * 1024 * 1024) {
- Helper::error("图片大小不能超过{$maxSize}M");
- }
- $type = strtolower($upArr['type']);
- if (!Helper::hasAnyString($type, ['png', 'jpeg', 'jpg'])) {
- Helper::error('图片格式不正确 ' . $type);
- }
- $ext = strtolower(pathinfo($upArr['name'], PATHINFO_EXTENSION));
- $upPath = "zqcrm/{$upType}/" . date('Ymd') . '/' . Helper::getRandomString(16) . '.' . $ext;
- $res = Helper::imageUpload($upArr['tmp_name'], $upPath);
- if (empty($res['code']) || $res['code'] != 200) {
- Helper::error($res['msg'] ?? '上传出错');
- }
- if ($upType == 'avatar') {
- $info = DB::getInfoById('useradmin', $this->getUserId());
- Helper::imageDelete($info['avatar']);
- DB::updateById('useradmin', ['avatar' => $upPath], $this->getUserId());
- }
- if ($upType == 'editor') {
- exit(json_encode([
- 'errno' => 0,
- 'data' => [
- 'url' => Helper::getImageUrl($upPath),
- ],
- ]));
- } else {
- Helper::ok(['name' => $upPath, 'url' => Helper::getImageUrl($upPath)]);
- }
- }
- public function actionDeleteImg()
- {
- $path = Helper::getPostString('path');
- if (empty($path)) {
- Helper::error('参数错误');
- }
- Helper::dealCommonResult(Helper::imageDelete($path));
- }
- public function actionChangePassword()
- {
- $old = Helper::getPostString('password');
- $new = Helper::getPostString('newPassword');
- $new1 = Helper::getPostString('confirmPassword');
- if (!$old || !$new) {
- Helper::error('参数错误');
- }
- if ( $new != $new1){
- Helper::error('新密码不一致');
- }
- $info = DB::getInfoById('useradmin', $this->getUserId());
- if (!$info) {
- Helper::error('用户未找到');
- }
- if (md5($old) != $info['password']) {
- Helper::error('旧密码错误');
- }
- DB::updateById('useradmin', ['password' => md5($new)], $this->getUserId());
- Helper::ok();
- }
- public function actionEditUser()
- {
- $info = [
- 'username' => Helper::getPostString('username'),
- 'phone' => Helper::getPostString('phone'),
- 'email' => Helper::getPostString('email'),
- 'descr' => Helper::getPostString('descr'),
- 'sex' => Helper::getPostInt('sex'),
- ];
- if (!Helper::checkEmptyKey($info, ['username', 'phone'])) {
- Helper::error('用户名称和手机号不能为空');
- }
- DB::updateById('useradmin', $info, $this->getUserId());
- Helper::ok();
- }
- /**
- * Logs out the current user and redirect to homepage.
- */
- public function actionLogout()
- {
- $token = $_SERVER['HTTP_AUTHORIZATION']?? '';
- if ($token) {
- RedisInstance::getInstance()->delete('user_token:'.$token);
- }
- Helper::ok();
- }
- }
|