CommonController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * 不同类型放到不同目录,返回格式也会不同
  51. */
  52. public function actionUploadImg()
  53. {
  54. $upType = '';
  55. $maxSize = 3;
  56. if (!empty($_FILES['follow'])) {
  57. $upType = 'follow';
  58. $upArr = $_FILES['follow'];
  59. } elseif (!empty($_FILES['editor'])) {
  60. $upType = 'editor';
  61. $upArr = $_FILES['editor'];
  62. } elseif (!empty($_FILES['avatar'])) {
  63. $upType = 'avatar';
  64. $upArr = $_FILES['avatar'];
  65. $maxSize = 0.3;
  66. } elseif (!empty($_FILES['canteen'])) {
  67. $upType = 'canteen';
  68. $upArr = $_FILES['canteen'];
  69. } else {
  70. Helper::error('上传有误');
  71. }
  72. $type = strtolower($upArr['type']);
  73. if (!Helper::hasAnyString($type, ['png', 'jpeg', 'jpg'])) {
  74. Helper::error('图片格式不正确 ' . $type);
  75. }
  76. if ($upArr['size'] > $maxSize * 1024 * 1024) {
  77. Helper::error("图片大小不能超过{$maxSize}M");
  78. }
  79. $ext = strtolower(pathinfo($upArr['name'], PATHINFO_EXTENSION));
  80. $upPath = "zqcrm/{$upType}/" . date('Ymd') . '/' . Helper::getRandomString(16) . '.' . $ext;
  81. $res = Helper::imageUpload($upArr['tmp_name'], $upPath);
  82. if (empty($res['code']) || $res['code'] != 200) {
  83. Helper::error($res['msg'] ?? '上传出错');
  84. }
  85. if ($upType == 'avatar') {
  86. $info = DB::getInfoById('useradmin', \Yii::app()->user->_id);
  87. Helper::imageDelete($info['avatar']);
  88. DB::updateById('useradmin', ['avatar' => $upPath], \Yii::app()->user->_id);
  89. }
  90. if ($upType == 'editor') {
  91. exit(json_encode([
  92. 'errno' => 0,
  93. 'data' => [
  94. 'url' => Helper::getImageUrl($upPath),
  95. ],
  96. ]));
  97. } else {
  98. Helper::ok(['name' => $upPath, 'url' => Helper::getImageUrl($upPath)]);
  99. }
  100. }
  101. public function actionDeleteImg()
  102. {
  103. $path = Helper::getPostString('path');
  104. if (empty($path)) {
  105. Helper::error('参数错误');
  106. }
  107. Helper::dealCommonResult(Helper::imageDelete($path));
  108. }
  109. public function actionChangePassword()
  110. {
  111. $old = Helper::getPostString('password');
  112. $new = Helper::getPostString('newPassword');
  113. $new1 = Helper::getPostString('confirmPassword');
  114. if (!$old || !$new) {
  115. Helper::error('参数错误');
  116. }
  117. if ( $new != $new1){
  118. Helper::error('新密码不一致');
  119. }
  120. $info = DB::getInfoById('useradmin', \Yii::app()->user->_id);
  121. if (!$info) {
  122. Helper::error('用户未找到');
  123. }
  124. if (md5($old) != $info['password']) {
  125. Helper::error('旧密码错误');
  126. }
  127. DB::updateById('useradmin', ['password' => md5($new)], \Yii::app()->user->_id);
  128. Helper::ok();
  129. }
  130. public function actionEditUser()
  131. {
  132. $info = [
  133. 'username' => Helper::getPostString('username'),
  134. 'phone' => Helper::getPostString('phone'),
  135. 'email' => Helper::getPostString('email'),
  136. 'descr' => Helper::getPostString('descr'),
  137. 'sex' => Helper::getPostInt('sex'),
  138. ];
  139. if (!Helper::checkEmptyKey($info, ['username', 'phone', 'email'])) {
  140. Helper::error('参数错误');
  141. }
  142. DB::updateById('useradmin', $info, \Yii::app()->user->_id);
  143. Helper::ok();
  144. }
  145. /**
  146. * Logs out the current user and redirect to homepage.
  147. */
  148. public function actionLogout()
  149. {
  150. Yii::app()->user->logout();
  151. Helper::ok();
  152. }
  153. }