StoreAdmin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace addons\Store\common\models;
  3. use backend\models\AdminRole;
  4. use common\enums\StatusEnum;
  5. use common\models\backend\Admin;
  6. use common\models\base\User;
  7. use common\models\mall\Mall;
  8. use common\models\rbac\AuthAssignment;
  9. use Yii;
  10. use Exception;
  11. /**
  12. * This is the model class for table "qimall_addons_store_admin".
  13. *
  14. * @property int $id
  15. * @property string $username 登录账号
  16. * @property string $account 账号名称
  17. * @property string $password 密码
  18. * @property int $mall_id 商城id
  19. * @property int $store_id 门店id
  20. * @property string $auth_key
  21. * @property string $access_token
  22. * @property int $admin_type 管理员类型1超级管理员2管理员3操作员
  23. * @property int $created_at
  24. * @property int $updated_at
  25. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  26. */
  27. class StoreAdmin extends User
  28. {
  29. const ADMIN_TYPE_SUPER = 1; // 超级管理员
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%addons_store_admin}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'store_id','admin_type', 'created_at', 'updated_at', 'status'], 'integer'],
  44. [['username', 'account'], 'string', 'max' => 50],
  45. [['password'], 'string', 'max' => 255],
  46. [['auth_key', 'access_token'], 'string', 'max' => 128],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'username' => '登录账号',
  57. 'account' => '账号名称',
  58. 'password' => '密码',
  59. 'mall_id' => '商城id',
  60. 'store_id' => '门店id',
  61. 'auth_key' => 'Auth Key',
  62. 'access_token' => 'Access Token',
  63. 'admin_type' => '管理员类型1超级管理员2管理员3操作员',
  64. 'created_at' => 'Created At',
  65. 'updated_at' => 'Updated At',
  66. 'status' => '状态[-1:删除;0:禁用;1启用]',
  67. ];
  68. }
  69. /**YII登录机制实现接口 */
  70. public static function findIdentity($id)
  71. {
  72. return static::findOne($id);
  73. }
  74. public static function findIdentityByAccessToken($token, $type = null)
  75. {
  76. return static::findOne(['access_token' => $token]);
  77. }
  78. public function getId()
  79. {
  80. return $this->id;
  81. }
  82. public function getAuthKey()
  83. {
  84. return $this->auth_key;
  85. }
  86. public function validateAuthKey($authKey)
  87. {
  88. return $this->getAuthKey() === $authKey;
  89. }
  90. public function validatePassword($password)
  91. {
  92. return Yii::$app->security->validatePassword($password, $this->password);
  93. }
  94. /**YII登录机制实现接口 */
  95. /**
  96. * 关联授权角色
  97. *
  98. * @return \yii\db\ActiveQuery
  99. */
  100. public function getAssignment()
  101. {
  102. return $this->hasOne(AuthAssignment::class, ['user_id' => 'id'])
  103. ->where(['app_id' => Yii::$app->id]);
  104. }
  105. public function getMall()
  106. {
  107. return $this->hasMany(Mall::class, ['admin_id' => 'id']);
  108. }
  109. /**
  110. * 管理员登录
  111. * @Author bing
  112. * @DateTime 2021-08-12 19:12:55
  113. * @return void
  114. * @copyright: Copyright (c) 2020 广东七件事集团
  115. */
  116. public static function login($params)
  117. {
  118. try {
  119. $username = $params['username'] ?? '';
  120. $password = $params['password'] ?? '';
  121. $where = ['status' => StatusEnum::ENABLED, 'username' => $username,'mall_id'=>$params['mall_id']];
  122. $admin = StoreAdmin::findOne($where);
  123. if(empty($admin)) throw new Exception('登录账号不存在');
  124. $store = Store::findOne(['shop_mobile'=>$username,'mall_id'=>$params['mall_id'],'status' => StatusEnum::ENABLED]);
  125. if(empty($store)||$store->status==StatusEnum::DELETE) throw new Exception('门店不存在');
  126. if($store->status==StatusEnum::DISABLED) throw new Exception('该门店已被禁用');
  127. if(!empty($store['expire_time'])&&time()>$store['expire_time']){
  128. throw new Exception('您的账号已过期');
  129. }
  130. if($store['is_sync'] && $store['sync_status'] != \addons\Store\common\enums\StoreEnum::SYNC_STATUS_COMPLETE) {
  131. throw new Exception('门店信息正在同步中,请稍后再试');
  132. }
  133. // 密码校验
  134. if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
  135. // 管理员类型处理
  136. $redirect_url = 'store/client/index/index';
  137. Yii::$app->session->set('o2o', $store);
  138. $mall = Mall::findOne($params['mall_id']);
  139. Yii::$app->session->set('mall',$mall);
  140. // 执行登录
  141. $store_login_remember_time = $config_params['store_login_remember_time'] ?? 86400 * 30;
  142. $store_login_no_remember_time = $config_params['store_login_no_remember_time'] ?? 3600 * 8;
  143. if (Yii::$app->store_user->login($admin, $params['is_remember'] ? $store_login_remember_time : $store_login_no_remember_time)) {
  144. return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']];
  145. } else {
  146. throw new \Exception('登录失败');
  147. }
  148. } catch (Exception $e) {
  149. self::setStaticError($e->getMessage());
  150. return false;
  151. }
  152. }
  153. /**
  154. * 添加管理员
  155. * @param array $params
  156. * @return bool|Admin
  157. */
  158. public static function setData(array $params)
  159. {
  160. $tr = \Yii::$app->db->beginTransaction();
  161. try {
  162. if (!empty($params['id'])) {
  163. $where = ['id' => $params['id'], ['status' => StatusEnum::ENABLED]];
  164. if (isset($params['mall_id'])) $where[] = ['mall_id' => $params['mall_id']];
  165. $model = self::findOne($where);
  166. if (empty($model)) throw new Exception('服务不存在或已删除');
  167. } else {
  168. $model = new self();
  169. $model->loadDefaultValues();
  170. }
  171. if(!empty($params['password'])){
  172. $params['password'] = Yii::$app->security->generatePasswordHash($params['password']);
  173. }else{
  174. unset($params['password']);
  175. }
  176. if(!empty($params['expired_at'])) $params['expired_at'] = strtotime($params['expired_at']);
  177. if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage());
  178. if(!empty($params['role_id'])){
  179. $res = AdminRole::setData(['id' => $params['admin_role_id'], 'role_id' => $params['role_id'], 'admin_id' => $model->id]);
  180. if ($res === false) throw new Exception(AdminRole::getStaticError());
  181. }
  182. $tr->commit();
  183. return $model;
  184. } catch (Exception $e) {
  185. $tr->rollBack();
  186. self::$static_error = $e->getMessage();
  187. return false;
  188. }
  189. }
  190. /**
  191. * 检测参数数据
  192. * @Author: 广东七件事 zal
  193. * @Date: 2020-04-08
  194. * @Time: 19:49
  195. * @param $arr
  196. * @param $data
  197. * @throws \Exception
  198. */
  199. private static function checkData($arr, $data)
  200. {
  201. foreach ($arr as $key => $item) {
  202. if (!isset($data[$key])) {
  203. throw new \Exception('请传参数' . $key . '->' . $item);
  204. }
  205. }
  206. }
  207. public static function isSuperAdmin($id)
  208. {
  209. $res = self::findOne($id);
  210. if (empty($res)) return false;
  211. if ($res->admin_type == self::ADMIN_TYPE_SUPER) return true;
  212. return false;
  213. }
  214. public static function del($params){
  215. $tr = \Yii::$app->db->beginTransaction();
  216. try {
  217. $model = self::findOne($params);
  218. $model->status = StatusEnum::DELETE;
  219. if($model->admin_type=='1'&& $model->username=='admin') throw new Exception('超级管理员不允许删除');
  220. $res = $model->save();
  221. if ($res === false) throw new Exception(self::getStaticError());
  222. $res = AdminRole::deleteAll(['admin_id'=>$params['id']]);
  223. if ($res === false) throw new Exception(AdminRole::getStaticError());
  224. $tr->commit();
  225. return $model;
  226. }catch (Exception $e){
  227. $tr->rollBack();
  228. self::$static_error = $e->getMessage();
  229. return false;
  230. }
  231. }
  232. public static function loginByH5($params)
  233. {
  234. try {
  235. $username = $params['username'] ?? '';
  236. $password = $params['password'] ?? '';
  237. $where = ['status' => StatusEnum::ENABLED, 'username' => $username,'mall_id'=>$params['mall_id']];
  238. $admin = StoreAdmin::findOne($where);
  239. if(empty($admin)) throw new Exception('登录账号不存在');
  240. $store = Store::findOne(['id'=>$admin['store_id'],'mall_id'=>$params['mall_id']]);
  241. if(!empty($store['expire_time'])&&time()>$store['expire_time']){
  242. throw new Exception('您的账号已过期');
  243. }
  244. // 密码校验
  245. if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
  246. $condition = [];
  247. $condition[] = ['mall_id' => $params['mall_id']];
  248. $condition[] = ['id' => $store['user_id']];
  249. $where = [];
  250. $where[] = ['mall_id' => $params['mall_id']];
  251. $where[] = ['id' => $store['user_id']];
  252. $where[] = ['status' => StatusEnum::ENABLED];
  253. $user = \common\models\user\User::getUser($where);
  254. if(empty($user)) throw new Exception('门店店长会员不存在');
  255. $token = Yii::$app->services->apiAccessToken->getAccessToken($user, 'store');
  256. return $token;
  257. } catch (Exception $e) {
  258. self::setStaticError($e->getMessage());
  259. return false;
  260. }
  261. }
  262. /**
  263. * 获取门店ID获取管理员
  264. *
  265. * @param integer $mall_id
  266. * @param integer $store_id
  267. * @return static|null
  268. */
  269. public static function getAdminByStoreId(int $mall_id, int $store_id)
  270. {
  271. return static::find()
  272. ->where(compact('mall_id', 'store_id'))
  273. ->one();
  274. }
  275. /**
  276. * 修改密码
  277. *
  278. * @param string $pwd
  279. * @return boolean
  280. */
  281. public function changePwd(string $pwd)
  282. {
  283. $this->password = Yii::$app->security->generatePasswordHash($pwd);
  284. return $this->save();
  285. }
  286. }