StoreAdminForm.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\constants\AdminPickLink;
  4. use app\models\Admin;
  5. use app\models\SaasUser;
  6. use app\models\StoreAdmin;
  7. use yii\base\Model;
  8. class StoreAdminForm extends Model
  9. {
  10. public $id;
  11. public $status;
  12. public $username;
  13. public $password;
  14. public $store_rules;
  15. public $saas_id;
  16. public function rules()
  17. {
  18. return [
  19. [['id', 'status', 'saas_id'], 'integer'],
  20. [['username', 'password', 'store_rules'], 'string'],
  21. ]; // TODO: Change the autogenerated stub
  22. }
  23. public function getAdminList() {
  24. try {
  25. $query = StoreAdmin::find()->alias('sa')->where(['sa.store_id' => get_store_id(), 'sa.is_delete' => 0, 'a.is_delete' => 0]);
  26. $query->leftJoin(['a' => Admin::tableName()], 'a.type = "mini_admin" AND a.type_id = sa.id');
  27. if ($this->status !== null && (int)$this->status !== -1) {
  28. $query->andWhere(['sa.status' => $this->status]);
  29. }
  30. if ($this->username) {
  31. $query->andWhere(['LIKE', 'sa.username', $this->username]);
  32. }
  33. $query->select('sa.id, sa.username, sa.status, sa.created_at')->orderBy('sa.id desc');
  34. $pagination = pagination_make($query);
  35. foreach ($pagination['list'] as &$item) {
  36. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  37. $item['status'] *= 1;
  38. }
  39. $link_list = AdminPickLink::getLink();
  40. return [
  41. 'code' => 0,
  42. 'msg' => 'success',
  43. 'data' => [
  44. 'data' => $pagination['list'],
  45. 'pageNo' => $pagination['pageNo'],
  46. 'totalCount' => $pagination['totalCount'],
  47. 'link' => $link_list
  48. ],
  49. ];
  50. } catch (\Exception $e) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $e->getMessage()
  54. ];
  55. }
  56. }
  57. public function getInfo() {
  58. try {
  59. $id = $this->id;
  60. $store_admin = StoreAdmin::findOne($id);
  61. $saas_user = null;
  62. if (!empty($store_admin->saas_id)) {
  63. $saas_user = SaasUser::findOne(['id' => $store_admin->saas_id, 'is_delete' => 0]);
  64. }
  65. $data = [
  66. "id" => (int)$store_admin->id ?? 0,
  67. "status" => (int)$store_admin->status ?? 0,
  68. "username" => $store_admin->username ?? '',
  69. "password" => $id ? '' : ($store_admin->password ?? ''),
  70. "rules" => AdminPickLink::getLink(),
  71. "store_rules" => !empty($store_admin->rules) ? explode(',', $store_admin->rules) : [],
  72. 'saas_user_name' => $saas_user->name ?? '',
  73. 'saas_id' => $saas_user->id ?? 0
  74. ];
  75. return [
  76. 'code' => 0,
  77. 'msg' => '获取成功',
  78. 'data' => $data
  79. ];
  80. } catch (\Exception $e) {
  81. return [
  82. 'code' => 1,
  83. 'msg' => $e->getMessage()
  84. ];
  85. }
  86. }
  87. public function save() {
  88. $t = \Yii::$app->db->beginTransaction();
  89. try {
  90. $id = $this->id;
  91. $username = trim($this->username);
  92. $password = trim($this->password);
  93. $status = (int)$this->status;
  94. $rules = $this->store_rules;
  95. $saas_id = $this->saas_id;
  96. if (empty($username)) {
  97. throw new \Exception('请输入登录账户');
  98. }
  99. if (empty($password) && empty($id)) {
  100. throw new \Exception('请输入登录密码');
  101. }
  102. $other_admin = StoreAdmin::findOne(['username' => $username, 'is_delete' => 0]);
  103. if ($other_admin) {//用户名不可重复
  104. if (($id && intval($other_admin->id) !== intval($id)) || !$id) {
  105. throw new \Exception("用户名已经存在,不可重复");
  106. }
  107. }
  108. if (!empty($saas_id)) {
  109. $other_admin = StoreAdmin::findOne(['saas_id' => $saas_id, 'is_delete' => 0]);
  110. if ($other_admin) {//当前用户已经绑定其他店铺管理员
  111. if (($id && intval($other_admin->id) !== intval($id)) || !$id) {
  112. throw new \Exception("当前用户已经绑定其他店铺管理员");
  113. }
  114. }
  115. }
  116. $form = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0]) ?: new StoreAdmin();
  117. $form->username = $username;
  118. if (!$id) {
  119. $form->password = $password;
  120. }
  121. $form->store_id = get_store_id();
  122. $form->status = $status;
  123. $form->rules = $rules;
  124. $form->saas_id = $saas_id;
  125. if (!$form->save()) {
  126. throw new \Exception(json_encode($form->errors));
  127. }
  128. $admin = Admin::findOne(['username' => $this->username, 'is_delete' => 0]);
  129. if ($admin) {
  130. if ($id) {
  131. if (!($admin->type === Admin::ADMIN_TYPE_MINI_ADMIN && $admin->type_id == $form->id)) {
  132. throw new \Exception('用户名已经存在');
  133. }
  134. } else {
  135. throw new \Exception('用户名已经存在');
  136. }
  137. }
  138. $admin = Admin::findOne(['type' => 'mini_admin', 'is_delete' => 0, 'type_id' => $form->id]) ?: new Admin();
  139. $admin->username = $this->username;
  140. $admin->type = 'mini_admin';
  141. $admin->type_id = $form->id;
  142. $admin->saas_user_id = 0;
  143. if (!$id) {
  144. $admin->password = \Yii::$app->security->generatePasswordHash($password);
  145. }
  146. if (!$admin->save()) {
  147. throw new \Exception(json_encode($admin->errors));
  148. }
  149. $t->commit();
  150. return [
  151. 'code' => 0,
  152. 'msg' => '操作成功'
  153. ];
  154. } catch (\Exception $e) {
  155. $t->rollBack();
  156. return [
  157. 'code' => 1,
  158. 'msg' => $e->getMessage()
  159. ];
  160. }
  161. }
  162. public function setPassword() {
  163. $t = \Yii::$app->db->beginTransaction();
  164. try {
  165. $id = $this->id;
  166. $password = $this->password;
  167. $store_admin = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  168. if ($store_admin) {
  169. $store_admin->password = \Yii::$app->security->generatePasswordHash($password);
  170. if (!$store_admin->save()) {
  171. throw new \Exception(json_encode($store_admin->errors));
  172. }
  173. $admin = Admin::findOne(['type' => 'mini_admin', 'is_delete' => 0, 'type_id' => $store_admin->id]);
  174. $admin->password = \Yii::$app->security->generatePasswordHash($password);
  175. if (!$admin->save()) {
  176. throw new \Exception(json_encode($admin->errors));
  177. }
  178. $t->commit();
  179. return [
  180. 'code' => 0,
  181. 'msg' => '操作成功'
  182. ];
  183. }
  184. throw new \Exception('用户不存在');
  185. } catch (\Exception $e) {
  186. $t->rollBack();
  187. return [
  188. 'code' => 1,
  189. 'msg' => $e->getMessage()
  190. ];
  191. }
  192. }
  193. public function setStatus() {
  194. try {
  195. $id = $this->id;
  196. $status = (int)$this->status;
  197. if (!in_array($status, [0, 1])) {
  198. throw new \Exception('用户不存在');
  199. }
  200. $store_admin = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  201. if ($store_admin) {
  202. $store_admin->status = $status;
  203. if (!$store_admin->save()) {
  204. throw new \Exception(json_encode($store_admin->errors));
  205. }
  206. return [
  207. 'code' => 0,
  208. 'msg' => '操作成功'
  209. ];
  210. }
  211. throw new \Exception('用户不存在');
  212. } catch (\Exception $e) {
  213. return [
  214. 'code' => 1,
  215. 'msg' => $e->getMessage()
  216. ];
  217. }
  218. }
  219. public function del() {
  220. $t = \Yii::$app->db->beginTransaction();
  221. try {
  222. $id = $this->id;
  223. $status = (int)$this->status;
  224. if (!in_array($status, [0, 1])) {
  225. throw new \Exception('用户不存在');
  226. }
  227. $store_admin = StoreAdmin::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  228. if ($store_admin) {
  229. $store_admin->is_delete = 1;
  230. if (!$store_admin->save()) {
  231. throw new \Exception(json_encode($store_admin->errors));
  232. }
  233. $admin = Admin::findOne(['username' => $store_admin->username, 'is_delete' => 0, 'type' => 'mini_admin']);
  234. if($admin){
  235. $admin->is_delete = 1;
  236. if (!$admin->save()) {
  237. throw new \Exception(json_encode($admin->errors));
  238. }
  239. }
  240. $t->commit();
  241. return [
  242. 'code' => 0,
  243. 'msg' => '操作成功'
  244. ];
  245. }
  246. throw new \Exception('用户不存在');
  247. } catch (\Exception $e) {
  248. $t->rollBack();
  249. return [
  250. 'code' => 1,
  251. 'msg' => $e->getMessage()
  252. ];
  253. }
  254. }
  255. }