| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace addons\Mch\common\models;
- use backend\models\AdminRole;
- use common\enums\StatusEnum;
- use common\models\backend\Admin;
- use common\models\base\User;
- use common\models\mall\Mall;
- use common\models\rbac\AuthAssignment;
- use Yii;
- use Exception;
- /**
- * This is the model class for table "qimall_addons_mch_admin".
- *
- * @property int $id
- * @property string $username 登录账号
- * @property string $account 账号名称
- * @property string $password 密码
- * @property int $mall_id 商城id
- * @property int $mch_id 多商户id
- * @property string $auth_key
- * @property string $access_token
- * @property int $admin_type 管理员类型1超级管理员2管理员3操作员
- * @property int $created_at
- * @property int $updated_at
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- */
- class MchAdmin extends User
- {
- const ADMIN_TYPE_SUPER = 1; // 超级管理员
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_mch_admin}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'mch_id', 'admin_type', 'created_at', 'updated_at', 'status'], 'integer'],
- [['username', 'account'], 'string', 'max' => 50],
- [['password'], 'string', 'max' => 255],
- [['auth_key', 'access_token'], 'string', 'max' => 128],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'username' => '登录账号',
- 'account' => '账号名称',
- 'password' => '密码',
- 'mall_id' => '商城id',
- 'mch_id' => '多商户id',
- 'auth_key' => 'Auth Key',
- 'access_token' => 'Access Token',
- 'admin_type' => '管理员类型1超级管理员2管理员3操作员',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- ];
- }
- /**YII登录机制实现接口 */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return static::findOne(['access_token' => $token]);
- }
- public function getId()
- {
- return $this->id;
- }
- public function getAuthKey()
- {
- return $this->auth_key;
- }
- public function validateAuthKey($authKey)
- {
- return $this->getAuthKey() === $authKey;
- }
- public function validatePassword($password)
- {
- return Yii::$app->security->validatePassword($password, $this->password);
- }
- /**YII登录机制实现接口 */
- /**
- * 关联授权角色
- *
- * @return \yii\db\ActiveQuery
- */
- public function getAssignment()
- {
- return $this->hasOne(AuthAssignment::class, ['user_id' => 'id'])
- ->where(['app_id' => Yii::$app->id]);
- }
- public function getMall()
- {
- return $this->hasMany(Mall::class, ['admin_id' => 'id']);
- }
- /**
- * 管理员登录
- * @Author bing
- * @DateTime 2021-08-12 19:12:55
- * @return void
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public static function login($params)
- {
- try {
- $username = $params['username'] ?? '';
- $password = $params['password'] ?? '';
- $where = ['status' => StatusEnum::ENABLED, 'username' => $username,'mall_id'=>$params['mall_id']];
- $admin = self::findOne($where);
- if(empty($admin)) throw new Exception('登录账号不存在');
- $mch = Mch::findOne(['shop_mobile'=>$username,'mall_id'=>$params['mall_id'],'status'=>StatusEnum::ENABLED]);
- if(empty($mch)||$mch->status==StatusEnum::DELETE) throw new Exception('商户不存在');
- if($mch->status==StatusEnum::DISABLED) throw new Exception('该商户已被禁用');
- if(!empty($mch['expire_time'])&&time()>$mch['expire_time']){
- throw new Exception('您的账号已过期');
- }
- // 密码校验
- if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误');
- // 管理员类型处理
- $redirect_url = 'mch/client/index/index';
- Yii::$app->session->set('mch', $mch);
- $mall = Mall::findOne($params['mall_id']);
- Yii::$app->session->set('mall',$mall);
- // 执行登录
- $mch_login_remember_time = $config_params['mch_login_remember_time'] ?? 86400 * 30;
- $mch_login_no_remember_time = $config_params['mch_login_no_remember_time'] ?? 3600 * 8;
- if (Yii::$app->mch_user->login($admin, $params['is_remember'] ? $mch_login_remember_time : $mch_login_no_remember_time)) {
- return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']];
- } else {
- throw new \Exception('登录失败');
- }
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 添加管理员
- * @param array $params
- * @return bool|Admin
- */
- public static function setData(array $params)
- {
- $tr = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($params['id'])) {
- $where = ['id' => $params['id'], ['status' => StatusEnum::ENABLED]];
- if (isset($params['mall_id'])) $where[] = ['mall_id' => $params['mall_id']];
- $model = self::findOne($where);
- if (empty($model)) throw new Exception('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if(!empty($params['password'])){
- $params['password'] = Yii::$app->security->generatePasswordHash($params['password']);
- }else{
- unset($params['password']);
- }
- if(!empty($params['expired_at'])) $params['expired_at'] = strtotime($params['expired_at']);
- if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage());
- if(!empty($params['role_id'])){
- $res = AdminRole::setData(['id' => $params['admin_role_id'], 'role_id' => $params['role_id'], 'admin_id' => $model->id]);
- if ($res === false) throw new Exception(AdminRole::getStaticError());
- }
- $tr->commit();
- return $model;
- } catch (Exception $e) {
- $tr->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 检测参数数据
- * @Author: 广东七件事 zal
- * @Date: 2020-04-08
- * @Time: 19:49
- * @param $arr
- * @param $data
- * @throws \Exception
- */
- private static function checkData($arr, $data)
- {
- foreach ($arr as $key => $item) {
- if (!isset($data[$key])) {
- throw new \Exception('请传参数' . $key . '->' . $item);
- }
- }
- }
- public static function isSuperAdmin($id)
- {
- $res = self::findOne($id);
- if (empty($res)) return false;
- if ($res->admin_type == self::ADMIN_TYPE_SUPER) return true;
- return false;
- }
- public static function del($params){
- $tr = \Yii::$app->db->beginTransaction();
- try {
- $model = self::findOne($params);
- $model->status = StatusEnum::DELETE;
- if($model->admin_type=='1'&& $model->username=='admin') throw new Exception('超级管理员不允许删除');
- $res = $model->save();
- if ($res === false) throw new Exception(self::getStaticError());
- $res = AdminRole::deleteAll(['admin_id'=>$params['id']]);
- if ($res === false) throw new Exception(AdminRole::getStaticError());
- $tr->commit();
- return $model;
- }catch (Exception $e){
- $tr->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|