CommonController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * 只需要登入,无需检测权限的公共方法
  4. * 前端直接请求的话必须添加header Authorization: userStore.accessToken
  5. */
  6. class CommonController extends Controller
  7. {
  8. // 首页统计
  9. public function actionStat()
  10. {
  11. $ret = [];
  12. $today = date('Y-m-d');
  13. $cri = DbCriteria::simpleCompare([])
  14. ->addBetweenCondition('create_date', $today, $today . ' 23:59:59')
  15. ->setSelect('count(id) as num');
  16. // 今日新增校方跟进记录
  17. $ret[] = [
  18. 'num' => DB::getScalerWithCriteria('school_follow', $cri),
  19. 'des' => '今日新增校方跟进记录',
  20. 'detail_path' => '/school/follow'
  21. ];
  22. // 今日新增食堂跟进记录
  23. $ret[] = [
  24. 'num' => DB::getScalerWithCriteria('canteen_follow', $cri),
  25. 'des' => '今日新增食堂跟进记录',
  26. 'detail_path' => '/canteen/follow'
  27. ];
  28. // 今日新增餐饮公司跟进记录
  29. $ret[] = [
  30. 'num' => DB::getScalerWithCriteria('company_follow', $cri),
  31. 'des' => '今日新增餐饮公司跟进记录',
  32. 'detail_path' => '/company/follow'
  33. ];
  34. $cri->addCondition('is_del = 0');
  35. // 今日新增校方关系
  36. $ret[] = [
  37. 'num' => DB::getScalerWithCriteria('school_contact', $cri),
  38. 'des' => '今日新增校方关系',
  39. 'detail_path' => '/school/relation'
  40. ];
  41. // 今日新增餐饮公司关系
  42. $ret[] = [
  43. 'num' => DB::getScalerWithCriteria('company_contact', $cri),
  44. 'des' => '今日新增餐饮公司关系',
  45. 'detail_path' => '/company/relation'
  46. ];
  47. Helper::ok($ret);
  48. }
  49. /**
  50. * 统一获取下拉列表: 主要是减少前端请求数量,以及方便统一的操作及权限管理
  51. * @return void
  52. * @throws CException
  53. */
  54. public function actionGetSelectList()
  55. {
  56. $types = Helper::getArrParam($_POST, 'types', Helper::PARAM_KEY_TYPE['array_string']);
  57. $ret = [];
  58. foreach ($types as $type) {
  59. switch ($type) {
  60. case 'user':
  61. $cri = DbCriteria::simpleCompare(['id' => '!=1', 'status' => 1])
  62. ->setSelect('id, username as name');
  63. $data = DB::getListWithCriteria('useradmin', $cri);
  64. $ret['user'] = $data['records']??[];
  65. break;
  66. case 'role':
  67. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  68. $data = DB::getListWithCriteria('role', $cri);
  69. $ret['role'] = $data['records']??[];
  70. break;
  71. case 'school':
  72. $cri = DbCriteria::simpleCompare(['is_del' => 0, 'id' => $this->getSchoolFilter()])
  73. ->setSelect('id, name');
  74. $data = DB::getListWithCriteria('school', $cri);
  75. $ret['school'] = $data['records']??[];
  76. break;
  77. case 'canteen':
  78. $cri = DbCriteria::simpleCompare(['is_del' => 0, 'school_id' => $this->getSchoolFilter()])
  79. ->setSelect('id, name');
  80. $data = DB::getListWithCriteria('canteen', $cri);
  81. $ret['canteen'] = $data['records']??[];
  82. break;
  83. case 'company':
  84. $cri = DbCriteria::simpleCompare(['is_del' => 0, 'id' => $this->getCompanyFilter()])
  85. ->setSelect('id, name');
  86. $data = DB::getListWithCriteria('company', $cri);
  87. $ret['company'] = $data['records']??[];
  88. break;
  89. case 'school_canteen':
  90. $filter = ['is_del' => 0, 'id' => $this->getSchoolFilter()];
  91. $company_id = Helper::getPostInt('company_id');
  92. $exclude_canteen_ids = [];
  93. if ($company_id) {
  94. $exclude_canteen_ids = Helper::arrayColumn(DB::getListWithCriteria('company_canteen_relation', DbCriteria::simpleCompare(['company_id' => '!=' . $company_id])), 'canteen_id');
  95. }
  96. $cri = DbCriteria::simpleCompare($filter)->setSelect('id, name');
  97. $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id');
  98. if (empty($schools)) {
  99. $ret['school_canteen'] = [];
  100. break;
  101. }
  102. $cri1 = DbCriteria::simpleCompare(['is_del' => 0, 'school_id' => array_keys($schools)])
  103. ->setSelect('id, name, school_id as parent_id')
  104. ->addNotInCondition('id', $exclude_canteen_ids);
  105. $relations = DB::getListWithCriteria('canteen', $cri1);
  106. $ret['school_canteen'] = $this->formatRelationList($relations['records'], $schools);
  107. break;
  108. case 'school_relation':
  109. $filter = ['is_del' => 0, 'id' => $this->getSchoolFilter()];
  110. $cri = DbCriteria::simpleCompare($filter)->setSelect('id, name');
  111. $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id');
  112. if (empty($schools)) {
  113. $ret['school_relation'] = [];
  114. break;
  115. }
  116. $cri1 = DbCriteria::simpleCompare(['is_del' => 0, 'school_id' => array_keys($schools)])->setSelect('id, name, school_id as parent_id');
  117. $relations = DB::getListWithCriteria('school_contact', $cri1);
  118. $ret['school_relation'] = $this->formatRelationList($relations['records'], $schools);
  119. break;
  120. case 'company_relation':
  121. $filter = ['is_del' => 0, 'id' => $this->getCompanyFilter()];
  122. $cri = DbCriteria::simpleCompare($filter)->setSelect('id, name');
  123. $compamnys = Helper::arrayColumn(DB::getListWithCriteria('company', $cri), null, 'id');
  124. if (empty($compamnys)) {
  125. $ret['company_relation'] = [];
  126. break;
  127. }
  128. $cri1 = DbCriteria::simpleCompare(['is_del' => 0, 'company_id' => array_keys($compamnys)])->setSelect('id, name, company_id as parent_id');
  129. $relations = DB::getListWithCriteria('company_contact', $cri1);
  130. $ret['company_relation'] = $this->formatRelationList($relations['records'], $compamnys);
  131. break;
  132. default:
  133. Helper::error('参数错误');
  134. break;
  135. }
  136. }
  137. Helper::ok($ret);
  138. }
  139. private function formatRelationList($list, $parentList)
  140. {
  141. foreach ($list as $relation) {
  142. $sid = $relation['parent_id'];
  143. if (!isset($parentList[$sid])) {
  144. continue;
  145. }
  146. if (!isset($parentList[$sid]['children'])) {
  147. $parentList[$sid]['children'] = [];
  148. }
  149. $parentList[$sid]['children'][] = [
  150. 'id' => $relation['id'],
  151. 'name' => $relation['name'],
  152. ];
  153. }
  154. return array_values($parentList);
  155. }
  156. /**
  157. * 图片上传
  158. * 不同类型放到不同目录,返回格式也会不同
  159. */
  160. public function actionUploadImg()
  161. {
  162. $upType = '';
  163. $maxSize = 3;
  164. if (!empty($_FILES['follow'])) {
  165. $upType = 'follow';
  166. $upArr = $_FILES['follow'];
  167. } elseif (!empty($_FILES['editor'])) {
  168. $upType = 'editor';
  169. $upArr = $_FILES['editor'];
  170. } elseif (!empty($_FILES['avatar'])) {
  171. $upType = 'avatar';
  172. $upArr = $_FILES['avatar'];
  173. $maxSize = 0.3;
  174. } elseif (!empty($_FILES['canteen'])) {
  175. $maxSize = 5;
  176. $upType = 'canteen';
  177. $upArr = $_FILES['canteen'];
  178. } else {
  179. Helper::error('上传有误');
  180. }
  181. if ($upArr['size'] > $maxSize * 1024 * 1024) {
  182. Helper::error("图片大小不能超过{$maxSize}M");
  183. }
  184. $type = strtolower($upArr['type']);
  185. if (!Helper::hasAnyString($type, ['png', 'jpeg', 'jpg'])) {
  186. Helper::error('图片格式不正确 ' . $type);
  187. }
  188. $ext = strtolower(pathinfo($upArr['name'], PATHINFO_EXTENSION));
  189. $upPath = "zqcrm/{$upType}/" . date('Ymd') . '/' . Helper::getRandomString(16) . '.' . $ext;
  190. $res = Helper::imageUpload($upArr['tmp_name'], $upPath);
  191. if (empty($res['code']) || $res['code'] != 200) {
  192. Helper::error($res['msg'] ?? '上传出错');
  193. }
  194. if ($upType == 'avatar') {
  195. $info = DB::getInfoById('useradmin', $this->getUserId());
  196. Helper::imageDelete($info['avatar']);
  197. DB::updateById('useradmin', ['avatar' => $upPath], $this->getUserId());
  198. }
  199. if ($upType == 'editor') {
  200. exit(json_encode([
  201. 'errno' => 0,
  202. 'data' => [
  203. 'url' => Helper::getImageUrl($upPath),
  204. ],
  205. ]));
  206. } else {
  207. Helper::ok(['name' => $upPath, 'url' => Helper::getImageUrl($upPath)]);
  208. }
  209. }
  210. public function actionDeleteImg()
  211. {
  212. $path = Helper::getPostString('path');
  213. if (empty($path)) {
  214. Helper::error('参数错误');
  215. }
  216. Helper::dealCommonResult(Helper::imageDelete($path));
  217. }
  218. public function actionChangePassword()
  219. {
  220. $old = Helper::getPostString('password');
  221. $new = Helper::getPostString('newPassword');
  222. $new1 = Helper::getPostString('confirmPassword');
  223. if (!$old || !$new) {
  224. Helper::error('参数错误');
  225. }
  226. if ( $new != $new1){
  227. Helper::error('新密码不一致');
  228. }
  229. $info = DB::getInfoById('useradmin', $this->getUserId());
  230. if (!$info) {
  231. Helper::error('用户未找到');
  232. }
  233. if (md5($old) != $info['password']) {
  234. Helper::error('旧密码错误');
  235. }
  236. DB::updateById('useradmin', ['password' => md5($new)], $this->getUserId());
  237. Helper::ok();
  238. }
  239. public function actionEditUser()
  240. {
  241. $info = [
  242. 'username' => Helper::getPostString('username'),
  243. 'phone' => Helper::getPostString('phone'),
  244. 'email' => Helper::getPostString('email'),
  245. 'descr' => Helper::getPostString('descr'),
  246. 'sex' => Helper::getPostInt('sex'),
  247. ];
  248. if (!Helper::checkEmptyKey($info, ['username', 'phone'])) {
  249. Helper::error('用户名称和手机号不能为空');
  250. }
  251. DB::updateById('useradmin', $info, $this->getUserId());
  252. Helper::ok();
  253. }
  254. /**
  255. * Logs out the current user and redirect to homepage.
  256. */
  257. public function actionLogout()
  258. {
  259. $token = $_SERVER['HTTP_AUTHORIZATION']?? '';
  260. if ($token) {
  261. RedisInstance::getInstance()->delete('user_token:'.$token);
  262. }
  263. Helper::ok();
  264. }
  265. }