CommonController.php 11 KB

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