AccountController.php 9.1 KB

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