MchAdmin.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace addons\Mch\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_mch_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 $mch_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 MchAdmin extends User
  28. {
  29. const ADMIN_TYPE_SUPER = 1; // 超级管理员
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%addons_mch_admin}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'mch_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. 'mch_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 = self::findOne($where);
  123. if(empty($admin)) throw new Exception('登录账号不存在');
  124. $mch = Mch::findOne(['shop_mobile'=>$username,'mall_id'=>$params['mall_id'],'status'=>StatusEnum::ENABLED]);
  125. if(empty($mch)||$mch->status==StatusEnum::DELETE) throw new Exception('商户不存在');
  126. if($mch->status==StatusEnum::DISABLED) throw new Exception('该商户已被禁用');
  127. if(!empty($mch['expire_time'])&&time()>$mch['expire_time']){
  128. throw new Exception('您的账号已过期');
  129. }
  130. // 密码校验
  131. if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
  132. // 管理员类型处理
  133. $redirect_url = 'mch/client/index/index';
  134. Yii::$app->session->set('mch', $mch);
  135. $mall = Mall::findOne($params['mall_id']);
  136. Yii::$app->session->set('mall',$mall);
  137. // 执行登录
  138. $mch_login_remember_time = $config_params['mch_login_remember_time'] ?? 86400 * 30;
  139. $mch_login_no_remember_time = $config_params['mch_login_no_remember_time'] ?? 3600 * 8;
  140. if (Yii::$app->mch_user->login($admin, $params['is_remember'] ? $mch_login_remember_time : $mch_login_no_remember_time)) {
  141. return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']];
  142. } else {
  143. throw new \Exception('登录失败');
  144. }
  145. } catch (Exception $e) {
  146. self::setStaticError($e->getMessage());
  147. return false;
  148. }
  149. }
  150. /**
  151. * 添加管理员
  152. * @param array $params
  153. * @return bool|Admin
  154. */
  155. public static function setData(array $params)
  156. {
  157. $tr = \Yii::$app->db->beginTransaction();
  158. try {
  159. if (!empty($params['id'])) {
  160. $where = ['id' => $params['id'], ['status' => StatusEnum::ENABLED]];
  161. if (isset($params['mall_id'])) $where[] = ['mall_id' => $params['mall_id']];
  162. $model = self::findOne($where);
  163. if (empty($model)) throw new Exception('服务不存在或已删除');
  164. } else {
  165. $model = new self();
  166. $model->loadDefaultValues();
  167. }
  168. if(!empty($params['password'])){
  169. $params['password'] = Yii::$app->security->generatePasswordHash($params['password']);
  170. }else{
  171. unset($params['password']);
  172. }
  173. if(!empty($params['expired_at'])) $params['expired_at'] = strtotime($params['expired_at']);
  174. if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage());
  175. if(!empty($params['role_id'])){
  176. $res = AdminRole::setData(['id' => $params['admin_role_id'], 'role_id' => $params['role_id'], 'admin_id' => $model->id]);
  177. if ($res === false) throw new Exception(AdminRole::getStaticError());
  178. }
  179. $tr->commit();
  180. return $model;
  181. } catch (Exception $e) {
  182. $tr->rollBack();
  183. self::$static_error = $e->getMessage();
  184. return false;
  185. }
  186. }
  187. /**
  188. * 检测参数数据
  189. * @Author: 广东七件事 zal
  190. * @Date: 2020-04-08
  191. * @Time: 19:49
  192. * @param $arr
  193. * @param $data
  194. * @throws \Exception
  195. */
  196. private static function checkData($arr, $data)
  197. {
  198. foreach ($arr as $key => $item) {
  199. if (!isset($data[$key])) {
  200. throw new \Exception('请传参数' . $key . '->' . $item);
  201. }
  202. }
  203. }
  204. public static function isSuperAdmin($id)
  205. {
  206. $res = self::findOne($id);
  207. if (empty($res)) return false;
  208. if ($res->admin_type == self::ADMIN_TYPE_SUPER) return true;
  209. return false;
  210. }
  211. public static function del($params){
  212. $tr = \Yii::$app->db->beginTransaction();
  213. try {
  214. $model = self::findOne($params);
  215. $model->status = StatusEnum::DELETE;
  216. if($model->admin_type=='1'&& $model->username=='admin') throw new Exception('超级管理员不允许删除');
  217. $res = $model->save();
  218. if ($res === false) throw new Exception(self::getStaticError());
  219. $res = AdminRole::deleteAll(['admin_id'=>$params['id']]);
  220. if ($res === false) throw new Exception(AdminRole::getStaticError());
  221. $tr->commit();
  222. return $model;
  223. }catch (Exception $e){
  224. $tr->rollBack();
  225. self::$static_error = $e->getMessage();
  226. return false;
  227. }
  228. }
  229. }