UserController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\BaseUser;
  5. use common\services\AdminLogService;
  6. use common\services\CaseService;
  7. use common\services\UserService;
  8. use Exception;
  9. use frontend\modules\api\components\BaseAdminController;
  10. use Yii;
  11. use yii\base\InvalidConfigException;
  12. use yii\web\Response;
  13. use yii\web\User;
  14. class UserController extends BaseAdminController
  15. {
  16. /**
  17. * 用户列表
  18. * @param int $current
  19. * @param int $page
  20. * @return Response
  21. * @throws InvalidConfigException
  22. */
  23. public function actionList(int $current = 1, int $page = 10)
  24. {
  25. //公共部分
  26. $query = BaseUser::find()->alias("u")
  27. ->join('LEFT JOIN', 'staff s', 's.uid = u.id')
  28. ->where("u.id > 1")
  29. ->andWhere("u.status >= 0")
  30. ->limit($page)
  31. ->offset(($current - 1) * $page);
  32. //排序
  33. $this->initSequence($query, "u.id desc");
  34. //主要筛选条件
  35. $this->addConditionToQueryNew("u.id", "id", $query);
  36. $this->addConditionToQuery("username", $query, true);
  37. if ($this->getParams("name") !== null && $this->getParams("name") !== "") {
  38. $query->andWhere(["like", "s.name", $this->getParams("name")]);
  39. }
  40. if ($this->getParams("department") !== null && $this->getParams("department") !== "") {
  41. $query->andWhere(["like", "s.department", $this->getParams("department")]);
  42. }
  43. if ($this->getParams("employment") !== null && $this->getParams("employment") !== "") {
  44. $query->andWhere(["like", "s.employment", $this->getParams("employment")]);
  45. }
  46. if ($this->getParams("seniority") !== null && $this->getParams("seniority") !== "") {
  47. $query->andWhere(["s.seniority" => $this->getParams("seniority")]);
  48. }
  49. if ($this->getParams("phone") !== null && $this->getParams("phone") !== "") {
  50. $query->andWhere(["like", "s.phone", $this->getParams("phone")]);
  51. }
  52. $this->addConditionToQuery("email", $query, true);
  53. $this->addConditionToQuery("roles", $query, true);
  54. $this->addStartEndConditionToQuery("last_login_time", $query);
  55. $info = $query
  56. ->asArray()
  57. ->select(UserService::getUserSqlSelectInfo())
  58. ->all();
  59. $info = CaseService::setStudyTime($info);
  60. $data['data'] = $info;
  61. //返回
  62. $data['total'] = $query->count();
  63. return $this->asJson($data);
  64. }
  65. /**
  66. * 用户信息
  67. * @param $userId
  68. * @return Response
  69. * @throws Exception
  70. */
  71. public function actionInfo($userId)
  72. {
  73. // 基础校验
  74. /** @var BaseUser $info */
  75. $info = UserService::getQuery()->alias("u")
  76. ->join('LEFT JOIN', 'staff s', 's.uid = u.id')
  77. ->where(["u.id" => $userId])
  78. ->asArray()
  79. ->select(UserService::getUserSqlSelectInfo())
  80. ->one();
  81. // 计算学习案例时长
  82. $info = CaseService::setStudyTime([$info])[0];
  83. $data['data'] = $info;
  84. // 追加头像路径
  85. $imageBasePath = $this->getImageBasePath();
  86. $data['imageBasePath'] = $imageBasePath;
  87. //返回时去除密码
  88. unset($data['data']['password']);
  89. return $this->asJson($data);
  90. }
  91. /**
  92. * 用户添加
  93. * @return Response
  94. * @throws Exception
  95. */
  96. public function actionAdd()
  97. {
  98. UserService::add();
  99. return $this->asJson();
  100. }
  101. /**
  102. * 用户修改
  103. * @param $id
  104. * @return Response
  105. * @throws Exception
  106. */
  107. public function actionUserUpdate($id)
  108. {
  109. UserService::update($id);
  110. return $this->asJson();
  111. }
  112. /**
  113. * 从excel批量导入
  114. * @return Response
  115. * @throws \yii\db\Exception
  116. */
  117. public function actionAddBatch()
  118. {
  119. // 获取传值
  120. $post = Yii::$app->request->post();
  121. $json = $post['json'];
  122. if (count($json) > 10000) {
  123. return $this->asJson([], 1, "批量新增数量过大,请控制在10000条记录之内!"); //通知前端
  124. }
  125. $param = [
  126. 'username',
  127. 'password',
  128. 'name',
  129. 'phone',
  130. 'email',
  131. 'status',
  132. 'roles',
  133. 'department_id',
  134. 'job_id',
  135. 'create_time',
  136. 'update_time',
  137. ];
  138. $paramData = [];
  139. foreach ($json as $key => $value) {
  140. $time = strtotime('now');
  141. if ($key > 0) {
  142. //插入
  143. $paramData[] = [
  144. 'username' => $value[1],
  145. 'password' => md5($value[2]),
  146. 'name' => $value[3],
  147. 'phone' => (string)$value[4],
  148. 'email' => $value[5],
  149. 'status' => $value[6],
  150. 'roles' => (string)$value[7],
  151. 'department_id' => $value[8],
  152. 'job_id' => $value[9],
  153. 'create_time' => date("Y-m-d H:i:s", $time),
  154. 'update_time' => date("Y-m-d H:i:s", $time),
  155. ];
  156. }
  157. }
  158. Yii::$app->db->createCommand()->batchInsert(BaseUser::tableName(), $param, $paramData)->execute();
  159. $data = [];
  160. return $this->asJson($data);
  161. }
  162. /**
  163. * 用户名查重
  164. * @param $category
  165. * @param $value
  166. * @return Response
  167. */
  168. public function actionCheck($category, $value)
  169. {
  170. // 定义返回值
  171. $checkData = [];
  172. if ($category == "username") {
  173. $info = UserService::getQuery()->andWhere(['username' => $value])->one();
  174. if ($info) {
  175. return $this->asJson([], 1, "此用户账号 {$value} 已存在!"); //通知前端
  176. }
  177. } else {
  178. return $this->asJson([], 1, "类型错误!"); //通知前端
  179. }
  180. // 返回
  181. return $this->asJson($checkData);
  182. }
  183. /**
  184. * 修改资料
  185. * @param $password
  186. * @param $origin
  187. * @return Response
  188. * @throws Exception
  189. */
  190. public function actionChangeInfo($password, $origin)
  191. {
  192. // 基础校验
  193. $info = UserService::getBaseUserById($this->uid);
  194. // if ($info->password !== md5($origin)) {
  195. if ($info->password !== $origin) {
  196. throw new AjaxException("原始密码错误!");
  197. }
  198. // if ($info->password === md5($password)) {
  199. if ($info->password === $password) {
  200. throw new AjaxException("新密码与原始密码相同!");
  201. }
  202. $info->password = $password;
  203. // $info->password = md5($password);
  204. if (!$info->save()) {
  205. throw new AjaxException("保存失败!");
  206. }
  207. $data['data'] = [];
  208. return $this->asJson($data);
  209. }
  210. /**
  211. * 返回可以审核案例的用户列表
  212. * @return Response
  213. */
  214. public function actionCanCheckList(): Response
  215. {
  216. $data = UserService::getCanCheckList();
  217. return $this->asJson($data);
  218. }
  219. /**
  220. * 保存头像
  221. * @param null $userId
  222. * @return Response
  223. * @throws Exception
  224. */
  225. public function actionSaveAvatar($userId = null)
  226. {
  227. // 基础校验
  228. $info = UserService::getBaseUserById($userId);
  229. $info->path = $_GET['path'];
  230. if (!$info->save()) {
  231. return $this->asJson([], 1, "保存失败!"); //通知前端
  232. }
  233. $data['data'] = [];
  234. return $this->asJson($data);
  235. }
  236. /**
  237. * 登出
  238. * @return Response
  239. */
  240. public function actionLogout()
  241. {
  242. $model = "";
  243. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  244. AdminLogService::saveLogWithUpdateHistory($origin, $model, 0, "", $this->getAllParams());
  245. return $this->asJson();
  246. }
  247. }