| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\db\ActiveRecord;
- use yii\web\IdentityInterface;
- use yii\behaviors\TimestampBehavior;
- /**
- * Class Admin
- * @package app\modules\common\models
- *
- * @property integer $id
- * @property integer $province_id
- * @property integer $city_id
- * @property integer $district_id
- * @property integer $street_id
- * @property integer $area_level
- * @property string $access_token
- * @property string $created_at
- * @property string $updated_at
- * @property string $username
- * @property string $password
- * @property integer $mobile
- * @property string $remark
- * @property string $email
- * @property string $name
- * @property string $avatar
- * @property integer $is_delete
- * @property integer $store_id
- * @property string $type
- * @property integer $type_id
- * @property integer $max_app_count
- * @property integer $expire_time
- * @property integer $saas_user_id
- * @property string $max_price
- * @property double $rate
- * @property integer $is_enable
- * @property integer $advert_push_user_id
- * @property string $address
- */
- class Admin extends ActiveRecord
- {
- const ADMIN_NORMAL = 0; // 正常
- const ADMIN_DELETE = 1; // 已删除
- const ADMIN_ENABLE = 1; // 开启
- const ADMIN_DISABLE = 0; // 禁用
- const ADMIN_TYPE_DEFAULT = 'admin'; // 平台管理员
- const ADMIN_TYPE_SUPPLIER = 'supplier'; // 供货商
- const ADMIN_TYPE_MCH = 'mch'; // 入驻商
- const ADMIN_TYPE_MD = 'md'; // 门店
- const ADMIN_TYPE_STORE = 'store'; // 商城
- const ADMIN_TYPE_STAFF = 'staff'; // 员工
- const ADMIN_TYPE_SAAS_STAFF = 'saas_staff';
- const ADMIN_TYPE_FRONT_AGENT = 'front_agent'; // 前置仓
- const ADMIN_TYPE_GOODS_AGENT = 'goods_agent'; // 产品代理
- const ADMIN_TYPE_BD_AGENT = 'bd_agent'; // 推广代理
- const ADMIN_TYPE_AREA_AGENT = 'area_agent'; // 区域代理
- const ADMIN_TYPE_MINI_ADMIN = 'mini_admin'; // 小程序管理员
- public static function tableName()
- {
- return '{{%admin}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- public function rules()
- {
- return [
- [['is_delete', 'store_id', 'type_id', 'max_app_count', 'expire_time', 'saas_user_id', 'is_enable','advert_push_user_id'], 'integer'],
- [['max_price', 'rate'], 'number'],
- [['access_token'], 'string', 'max' => 60],
- [['email', 'name', 'username', 'password', 'type'], 'string', 'max' => 100],
- [['avatar'], 'string', 'max' => 255],
- [['email'], 'email'],
- [['access_token', 'create_at', 'update_at'], 'safe'],
- [['address', 'mobile'], 'string']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'province_id' => 'province_id',
- 'city_id' => 'city_id',
- 'district_id' => 'district_id',
- 'area_level' => 'area_level',
- 'access_token' => 'access token',
- 'create_at' => '创建时间',
- 'update_at' => '更新时间',
- 'username' => '用户名',
- 'password' => '密码',
- 'mobile' => '手机号',
- 'remark' => '备注',
- 'email' => '邮箱',
- 'name' => '昵称',
- 'avatar' => '头像',
- 'is_delete' => '是否已删除',
- 'store_id' => 'store_id',
- 'type' => '管理员类型',
- 'type_id' => 'type id',
- 'max_app_count' => '最大创建商城数量',
- 'expire_time' => '过期时间',
- 'max_price' => '允许设置的最大佣金',
- 'rate' => '代理商服务费率',
- 'is_enable' => '是否开启',
- 'address' => '代理地址'
- ];
- }
- public function beforeSave($insert)
- {
- // 创建用户自动赋值access_token
- if (parent::beforeSave($insert)) {
- if ($this->isNewRecord) {
- $this->access_token = \Yii::$app->security->generateRandomString();
- }
- if (empty($this->max_price)) {
- $this->max_price = 0.00;
- }
- if (empty($this->rate)) {
- $this->rate = 0.00;
- }
- $this->mobile = \str_replace(' ', '', $this->mobile);
- /*if($this->type == 'store' && $this->saas_user_id > 0){
- if($this->isNewRecord){
- $is = self::find()->where(['type' => 'store', 'saas_user_id' => $this->saas_user_id, 'is_delete' => 0])->one();
- }else{
- $is = self::find()->where(['type' => 'store', 'saas_user_id' => $this->saas_user_id, 'is_delete' => 0])->andWhere(['!=', 'id', $this->id])->one();
- }
- if($is){
- $this->addError('mobile', '操作失败,其他管理员['. $is->username .']已绑定此用户!请更换');
- return false;
- }
- }*/
- return true;
- }
- return false;
- }
- /**
- * @param int|string $id
- * @return Admin|IdentityInterface|null
- */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- /**
- * @param mixed $token
- * @param null $type
- * @return Admin|IdentityInterface|null
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return static::findOne(['access_token' => $token, 'is_delete' => 0]);
- }
- /**
- * @param $username
- */
- public static function findByUsername($username)
- {
- return static::find()->where(['is_delete' => 0])->andWhere(['or',['username' => $username,],['name' => $username]])->one();
- }
- /**
- * @param $email
- * @return Admin|null
- */
- public static function findByEmail($email)
- {
- return static::findOne(['email' => $email, 'is_delete' => 0]);
- }
- /**
- * @param $mobile
- * @return Admin|null
- */
- public static function findByMobile($mobile)
- {
- return static::findOne(['mobile' => $mobile, 'is_delete' => 0]);
- }
- /**
- * @return int|string
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * 刷新token
- * @return bool
- */
- public function refreshToken()
- {
- $this->access_token = \Yii::$app->security->generateRandomString();
- return $this->save();
- }
- /**
- * 验证密码
- * @param $password
- * @return bool
- */
- public function validatePassword($password)
- {
- return \Yii::$app->security->validatePassword($password, $this->password);
- }
- /**
- * 获取角色
- * @return \yii\db\ActiveQuery
- * @throws \yii\base\InvalidConfigException
- */
- public function getRoles()
- {
- return $this->hasMany(AuthRole::class, ['id' => 'role_id'])
- ->viaTable('{{%admin_role}}', ['admin_id' => 'id']);
- }
- /**
- * 获取平台角色
- * @return \yii\db\ActiveQuery
- * @throws \yii\base\InvalidConfigException
- */
- public function getSaasRoles()
- {
- return $this->hasMany(SaasAuthRole::class, ['id' => 'role_id'])
- ->viaTable('{{%saas_admin_role}}', ['admin_id' => 'id']);
- }
- }
|