| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace backend\modules\mall\controllers;
- use backend\models\AdminRole;
- use backend\controllers\MallController;
- use common\enums\AppEnum;
- use common\enums\StatusEnum;
- use common\models\backend\Admin;
- use common\models\rbac\AuthRole;
- use Yii;
- use common\models\mall\Mall;
- /**
- * 账号管理
- *
- * @Author bing
- * @DateTime 2021-08-16 13:12:44
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class AccountController extends MallController
- {
- protected $member_id;
- protected $member;
- /**
- * @throws \yii\base\InvalidConfigException
- */
- public function init()
- {
- parent::init();
- }
- /**
- * 首页
- *
- * @return mixed
- */
- public function actionIndex()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- $params = array('where' => [['status' => StatusEnum::ENABLED], ['mall_id' => $this->mall_id]], 'order' => 'id desc', 'limit' => $request->get('limit', 12));
- $page_data = Admin::listPage($params, false);
- if (!empty($page_data['list'])) {
- $id_arr = array_column($page_data['list'], 'id');
- $admin_role_list = AdminRole::find()->where(['in', 'admin_id', $id_arr])->all();
- $role_id_arr = $admin_role_arr = [];
- foreach ($admin_role_list as $v) {
- $admin_role_arr[$v['admin_id']] = $v['role_id'];
- $role_id_arr[] = $v['role_id'];
- }
- $cond = [
- ['mall_id' => $this->mall_id],
- ['app_id' => AppEnum::BACKEND],
- ['status' => StatusEnum::ENABLED]
- ];
- $role_list = AuthRole::getRole($cond);
- $role_arr = array_column($role_list, 'title', 'id');
- foreach ($page_data['list'] as $k => $v) {
- $page_data['list'][$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
- if (!empty($admin_role_arr[$v['id']])) {
- $page_data['list'][$k]['role'] = !empty($role_arr[$admin_role_arr[$v['id']]]) ? $role_arr[$admin_role_arr[$v['id']]] : '';
- }
- }
- }
- $mall = Mall::find()->where(['id'=>$this->mall_id])->one();
- $url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login&mall_sign='.$mall['mall_sign']);
- $page_data['url'] = urldecode($url);
- return $this->success('操作成功', $page_data);
- }
- return $this->render($this->action->id);
- }
- /**
- * 获取后台账号信息
- * @return mixed|string|void
- */
- public function actionUser()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- $identity['admin_type'] = $this->admin->admin_type;
- $identity['id'] = $this->admin->id;
- $identity['mall_num'] = $this->admin->mall_num;
- $identity['mch_id'] = $this->admin->mch_id;
- $identity['username'] = $this->admin->username;
- $data['identity'] = $identity;
- return $this->success('操作成功', ['data' => $data]);
- }
- return $this->render($this->action->id);
- }
- /**
- * 我的账号
- * @return mixed|string|void
- */
- public function actionInfo()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- $data['account'] = $this->admin->account;
- $data['username'] = $this->admin->username;
- return $this->success('操作成功', ['data' => $data]);
- }
- return $this->render($this->action->id);
- }
- /**
- * 编辑
- * @param null $keyword
- * @param int $is_recycle
- * @return mixed|void
- */
- public function actionEdit()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $id = Yii::$app->request->get('id', '');
- $where[] = ['and',
- ['id' => $id],
- ['status' => StatusEnum::ENABLED],
- ['mall_id' => $this->mall_id]
- ];
- $admin = Admin::getOne($where, true);
- $cond = [
- ['mall_id' => $this->mall_id],
- ['app_id' => AppEnum::BACKEND],
- ['is_default'=>0],
- ['status' => StatusEnum::ENABLED]
- ];
- $role_list = AuthRole::getRole($cond);
- $data['data'] = [];
- if (!empty($admin)) {
- $admin_role = AdminRole::findOne(['admin_id' => $admin['id']]);
- if (!empty($admin_role)) {
- $admin['admin_role_id'] = !empty($admin_role->id) ? $admin_role->id : '';
- $role = AuthRole::findOne(['id' => $admin_role->role_id, 'mall_id' => $this->mall_id]);
- $admin['role'] = !empty($role->title) ? $role->title : '';
- $admin['role_id'] = !empty($role->id) ? $role->id : '';
- }
- $data['data'] = $admin;
- }
- $data['role_list'] = $role_list ?? [];
- return $this->success('操作成功', $data);
- }
- try {
- if ($request->isPost) {
- $mall_id = $this->mall_id;
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['username', 'account','mobile'], 'required'],
- [['id', 'admin_role_id', 'role_id', 'mall_id', 'store_id', 'mch_id', 'remark', 'admin_type', 'password'], 'safe'],
- ['username', function ($attribute) use ($mall_id, $params) {
- // $where[] = ['mall_id' => $mall_id];
- $where[] = ['username' => trim($this->$attribute)];
- $where[] = ['status' => StatusEnum::ENABLED];
- if (!empty($params['id'])) $where[] = ['<>', 'id', $params['id']];
- if (Admin::getOne($where)) $this->addError($attribute, '登录账号已存在');
- }],
- [
- 'mobile', function ($attribute) use ($params) {
- $where=[];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['mobile' => trim($this->$attribute)];
- if (!empty($params['id'])) $where[] = ['<>', 'id', $params['id']];
- if (Admin::getOne($where)) $this->addError($attribute, '手机号已存在');
- }]
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['remark'] = $params['remark']??'';
- $params['mall_id'] = $this->mall_id;
- $params['mch_id'] = 0;//预留
- $params['store_id'] = 0;//预留
- $params['admin_type'] = Admin::ADMIN_TYPE_OPERATE;
- $admin = Admin::setData($params);
- if ($admin === false) return $this->error(Admin::getStaticError());
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- }
- /**
- * 修改密码
- * @return mixed|void
- */
- public function actionUpdatePassword()
- {
- $request = Yii::$app->request;
- try {
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['password'], 'required'],
- ]);
- $params['id'] = $this->admin->id;
- $params['mall_id'] = $this->mall_id;
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $admin = Admin::setData($params);
- if ($admin === false) {
- return $this->error(Admin::getStaticError());
- }
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- /**
- * 删除
- * @param null $keyword
- * @param int $is_recycle
- * @return mixed|void
- */
- public function actionDel()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = Yii::$app->request->get();
- $condition['id'] = $params['id'];
- $condition['mall_id'] = $this->mall_id;
- $condition['status'] = StatusEnum::ENABLED;
- $res = Admin::del($condition);
- if ($res === false) return $this->error(AdminRole::getStaticError());
- return $this->success('操作成功');
- }
- }
- }
- }
|