alias('sa')->where(['sa.store_id' => get_store_id(), 'sa.is_delete' => 0, 'a.is_delete' => 0]); $query->leftJoin(['a' => Admin::tableName()], 'a.type = "mini_admin" AND a.type_id = sa.id'); if ($this->status !== null && (int)$this->status !== -1) { $query->andWhere(['sa.status' => $this->status]); } if ($this->username) { $query->andWhere(['LIKE', 'sa.username', $this->username]); } $query->select('sa.id, sa.username, sa.status, sa.created_at')->orderBy('sa.id desc'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['status'] *= 1; } $link_list = AdminPickLink::getLink(); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], 'link' => $link_list ], ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getInfo() { try { $id = $this->id; $store_admin = StoreAdmin::findOne($id); $saas_user = null; if (!empty($store_admin->saas_id)) { $saas_user = SaasUser::findOne(['id' => $store_admin->saas_id, 'is_delete' => 0]); } $data = [ "id" => (int)$store_admin->id ?? 0, "status" => (int)$store_admin->status ?? 0, "username" => $store_admin->username ?? '', "password" => $id ? '' : ($store_admin->password ?? ''), "rules" => AdminPickLink::getLink(), "store_rules" => !empty($store_admin->rules) ? explode(',', $store_admin->rules) : [], 'saas_user_name' => $saas_user->name ?? '', 'saas_id' => $saas_user->id ?? 0 ]; return [ 'code' => 0, 'msg' => '获取成功', 'data' => $data ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function save() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $username = trim($this->username); $password = trim($this->password); $status = (int)$this->status; $rules = $this->store_rules; $saas_id = $this->saas_id; if (empty($username)) { throw new \Exception('请输入登录账户'); } if (empty($password) && empty($id)) { throw new \Exception('请输入登录密码'); } $other_admin = StoreAdmin::findOne(['username' => $username, 'is_delete' => 0]); if ($other_admin) {//用户名不可重复 if (($id && intval($other_admin->id) !== intval($id)) || !$id) { throw new \Exception("用户名已经存在,不可重复"); } } if (!empty($saas_id)) { $other_admin = StoreAdmin::findOne(['saas_id' => $saas_id, 'is_delete' => 0]); if ($other_admin) {//当前用户已经绑定其他店铺管理员 if (($id && intval($other_admin->id) !== intval($id)) || !$id) { throw new \Exception("当前用户已经绑定其他店铺管理员"); } } } $form = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0]) ?: new StoreAdmin(); $form->username = $username; if (!$id) { $form->password = $password; } $form->store_id = get_store_id(); $form->status = $status; $form->rules = $rules; $form->saas_id = $saas_id; if (!$form->save()) { throw new \Exception(json_encode($form->errors)); } $admin = Admin::findOne(['username' => $this->username, 'is_delete' => 0]); if ($admin) { if ($id) { if (!($admin->type === Admin::ADMIN_TYPE_MINI_ADMIN && $admin->type_id == $form->id)) { throw new \Exception('用户名已经存在'); } } else { throw new \Exception('用户名已经存在'); } } $admin = Admin::findOne(['type' => 'mini_admin', 'is_delete' => 0, 'type_id' => $form->id]) ?: new Admin(); $admin->username = $this->username; $admin->type = 'mini_admin'; $admin->type_id = $form->id; $admin->saas_user_id = 0; if (!$id) { $admin->password = \Yii::$app->security->generatePasswordHash($password); } if (!$admin->save()) { throw new \Exception(json_encode($admin->errors)); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setPassword() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $password = $this->password; $store_admin = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => get_store_id()]); if ($store_admin) { $store_admin->password = \Yii::$app->security->generatePasswordHash($password); if (!$store_admin->save()) { throw new \Exception(json_encode($store_admin->errors)); } $admin = Admin::findOne(['type' => 'mini_admin', 'is_delete' => 0, 'type_id' => $store_admin->id]); $admin->password = \Yii::$app->security->generatePasswordHash($password); if (!$admin->save()) { throw new \Exception(json_encode($admin->errors)); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } throw new \Exception('用户不存在'); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setStatus() { try { $id = $this->id; $status = (int)$this->status; if (!in_array($status, [0, 1])) { throw new \Exception('用户不存在'); } $store_admin = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => get_store_id()]); if ($store_admin) { $store_admin->status = $status; if (!$store_admin->save()) { throw new \Exception(json_encode($store_admin->errors)); } return [ 'code' => 0, 'msg' => '操作成功' ]; } throw new \Exception('用户不存在'); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function del() { $t = \Yii::$app->db->beginTransaction(); try { $id = $this->id; $status = (int)$this->status; if (!in_array($status, [0, 1])) { throw new \Exception('用户不存在'); } $store_admin = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => get_store_id()]); if ($store_admin) { $store_admin->is_delete = 1; if (!$store_admin->save()) { throw new \Exception(json_encode($store_admin->errors)); } $admin = Admin::findOne(['username' => $store_admin->username, 'is_delete' => 0, 'type' => 'mini_admin']); if($admin){ $admin->is_delete = 1; if (!$admin->save()) { throw new \Exception(json_encode($admin->errors)); } } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } throw new \Exception('用户不存在'); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }