| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace app\modules\admin\models;
- use app\constants\AdminPickLink;
- use app\models\Admin;
- use app\models\SaasUser;
- use app\models\StoreAdmin;
- use yii\base\Model;
- class StoreAdminForm extends Model
- {
- public $id;
- public $status;
- public $username;
- public $password;
- public $store_rules;
- public $saas_id;
- public function rules()
- {
- return [
- [['id', 'status', 'saas_id'], 'integer'],
- [['username', 'password', 'store_rules'], 'string'],
- ]; // TODO: Change the autogenerated stub
- }
- public function getAdminList() {
- try {
- $query = StoreAdmin::find()->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()
- ];
- }
- }
- }
|