AccountController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use backend\models\AdminRole;
  4. use backend\controllers\AdminController;
  5. use common\enums\AppEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\Auth;
  8. use common\models\backend\Admin;
  9. use backend\models\Role;
  10. use common\models\rbac\AuthRole;
  11. use Yii;
  12. use yii\filters\AccessControl;
  13. /**
  14. *
  15. * 账号管理
  16. * @Author bing
  17. * @DateTime 2021-08-16 13:12:44
  18. * @copyright: Copyright (c) 2020 广东七件事集团
  19. */
  20. class AccountController extends AdminController
  21. {
  22. protected $member_id;
  23. /**
  24. * @var Member
  25. */
  26. protected $member;
  27. /**
  28. * @throws \yii\base\InvalidConfigException
  29. */
  30. public function init()
  31. {
  32. parent::init();
  33. }
  34. /**
  35. * 首页
  36. *
  37. * @return mixed
  38. */
  39. public function actionIndex()
  40. {
  41. $request = Yii::$app->request;
  42. if ($request->isAjax) {
  43. $params = array('where' => [['status' => StatusEnum::ENABLED], ['mall_id' => 0]], 'order' => 'id asc', 'limit' => $request->get('limit', 12));
  44. $page_data = Admin::listPage($params, false);
  45. if (!empty($page_data['list'])) {
  46. $id_arr = array_column($page_data['list'], 'id');
  47. $admin_role_list = AdminRole::find()->where(['in', 'admin_id', $id_arr])->all();
  48. $role_id_arr = $admin_role_arr = [];
  49. foreach ($admin_role_list as $v) {
  50. $admin_role_arr[$v['admin_id']] = $v['role_id'];
  51. $role_id_arr[] = $v['role_id'];
  52. }
  53. $role_list = AuthRole::find()->where(['in', 'id', $role_id_arr])->all();
  54. $role_arr = array_column($role_list, 'title', 'id');
  55. foreach ($page_data['list'] as $k => $v) {
  56. $page_data['list'][$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
  57. if (!empty($admin_role_arr[$v['id']])) {
  58. $page_data['list'][$k]['role'] = !empty($role_arr[$admin_role_arr[$v['id']]]) ? $role_arr[$admin_role_arr[$v['id']]] : '';
  59. }
  60. }
  61. }
  62. return $this->success('操作成功', $page_data);
  63. }
  64. return $this->render($this->action->id);
  65. }
  66. /**
  67. * 获取后台账号信息
  68. * @return mixed|string|void
  69. */
  70. public function actionUser()
  71. {
  72. $request = Yii::$app->request;
  73. if ($request->isAjax) {
  74. $identity['admin_type'] = $this->admin->admin_type;
  75. $identity['id'] = $this->admin->id;
  76. $identity['mall_num'] = $this->admin->mall_num;
  77. $identity['mch_id'] = $this->admin->mch_id;
  78. $identity['username'] = $this->admin->username;
  79. $data['identity'] = $identity;
  80. return $this->success('操作成功', ['data' => $data]);
  81. }
  82. return $this->render($this->action->id);
  83. }
  84. /**
  85. * 我的账号
  86. * @return mixed|string|void
  87. */
  88. public function actionInfo()
  89. {
  90. $request = Yii::$app->request;
  91. if ($request->isAjax) {
  92. $data['account'] = $this->admin->account;
  93. $data['username'] = $this->admin->username;
  94. return $this->success('操作成功', ['data' => $data]);
  95. }
  96. return $this->render($this->action->id);
  97. }
  98. /**
  99. * 编辑
  100. * @param null $keyword
  101. * @param int $is_recycle
  102. * @return mixed|void
  103. */
  104. public function actionEdit()
  105. {
  106. $request = Yii::$app->request;
  107. if ($request->isAjax) {
  108. if ($request->isGet) {
  109. $id = Yii::$app->request->get('id', '');
  110. $admin = Admin::getOne([['id'=>$id],['status' => StatusEnum::ENABLED],['mall_id'=>0]],true);
  111. $admin['expired_at'] = $admin['expired_at'] >0 ? date('Y-m-d H:i:s',$admin['expired_at']):$admin['expired_at'];
  112. $cond = [
  113. ['mall_id' => 0],
  114. ['app_id' => AppEnum::BACKEND],
  115. ['status' => [StatusEnum::ENABLED]]
  116. ];
  117. $role_list = AuthRole::getRole($cond, ['child'], true);
  118. $data['data'] = [];
  119. if (!empty($admin)) {
  120. $admin_role = AdminRole::findOne(['admin_id' => $admin['id']]);
  121. if (!empty($admin_role)) {
  122. $admin['admin_role_id'] = !empty($admin_role->id) ? $admin_role->id : '';
  123. $role = AuthRole::findOne($admin_role->role_id);
  124. $admin['role'] = !empty($role->title) ? $role->title : '';
  125. $admin['role_id'] = !empty($role->id) ? $role->id : '';
  126. }
  127. $data['data'] = $admin;
  128. }
  129. $data['role_list'] = $role_list ?? [];
  130. return $this->success('操作成功', $data);
  131. }
  132. try {
  133. if ($request->isPost) {
  134. $params = Yii::$app->request->post();
  135. $res = Yii::$app->services->validate->validate($params, [
  136. [['username', 'account','mobile'], 'required'],
  137. [['id', 'admin_role_id', 'role_id','expired_at','mall_num', 'mall_id', 'store_id', 'mch_id', 'remark', 'admin_type', 'password'], 'safe'],
  138. ['username', function ($attribute) use ($params) {
  139. // $where[] = ['mall_id' => 0];
  140. $where[] = ['status' => StatusEnum::ENABLED];
  141. $where[] = ['username' => trim($this->$attribute)];
  142. if (!empty($params['id'])) $where[] = ['<>', 'id', $params['id']];
  143. if (Admin::getOne($where)) $this->addError($attribute, '登录账号已存在');
  144. }],
  145. [
  146. 'mobile', function ($attribute) use ($params) {
  147. $where=[];
  148. $where[] = ['status' => StatusEnum::ENABLED];
  149. $where[] = ['mobile' => trim($this->$attribute)];
  150. if (!empty($params['id'])) $where[] = ['<>', 'id', $params['id']];
  151. if (Admin::getOne($where)) $this->addError($attribute, '手机号已存在');
  152. }]
  153. ]);
  154. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  155. $params['mall_id'] = 0;
  156. $params['mch_id'] = 0;
  157. $params['store_id'] = 0;
  158. $params['mall_num'] = empty($params['mall_num'])?9999:$params['mall_num'];
  159. $params['expired_at'] = empty($params['expired_at'])?0:$params['expired_at'];
  160. $params['remark'] = $params['remark']??'';
  161. $admin = Admin::setData($params);
  162. if ($admin === false) return $this->error(Admin::getStaticError());
  163. return $this->success('操作成功');
  164. }
  165. } catch (\Exception $e) {
  166. return $this->error($e->getMessage(), []);
  167. }
  168. }
  169. }
  170. /**
  171. * 修改密码
  172. * @return mixed|void
  173. */
  174. public function actionUpdatePassword()
  175. {
  176. $request = Yii::$app->request;
  177. try {
  178. if ($request->isPost) {
  179. $params = Yii::$app->request->post();
  180. $res = Yii::$app->services->validate->validate($params, [
  181. [['password'], 'required'],
  182. ]);
  183. $params['mall_id'] = 0;
  184. $params['id'] = $this->admin->id;
  185. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  186. $admin = Admin::setData($params);
  187. if ($admin === false) {
  188. return $this->error(Admin::getStaticError());
  189. }
  190. // 获取跳转登录地址
  191. $suffix = '';
  192. if ($this->admin->admin_type == Admin::ADMIN_TYPE_SUPER) $suffix = '&login_type=' . Yii::$app->params['login_type'];
  193. $redirect_url = Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
  194. Yii::$app->user->logout();
  195. return $this->success('操作成功', $redirect_url . $suffix);
  196. }
  197. } catch (\Exception $e) {
  198. return $this->error($e->getMessage(), []);
  199. }
  200. }
  201. /**
  202. * 删除
  203. * @param null $keyword
  204. * @param int $is_recycle
  205. * @return mixed|void
  206. */
  207. public function actionDel()
  208. {
  209. $request = Yii::$app->request;
  210. if ($request->isAjax) {
  211. if ($request->isGet) {
  212. $params = Yii::$app->request->get();
  213. $condition['id'] = $params['id'];
  214. $condition['mall_id'] = 0;
  215. $condition['status'] = StatusEnum::ENABLED;
  216. $res = Admin::del($condition);
  217. if ($res === false) return $this->error(AdminRole::getStaticError());
  218. return $this->success('操作成功');
  219. }
  220. }
  221. }
  222. }