Admin.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\db\ActiveRecord;
  9. use yii\web\IdentityInterface;
  10. use yii\behaviors\TimestampBehavior;
  11. /**
  12. * Class Admin
  13. * @package app\modules\common\models
  14. *
  15. * @property integer $id
  16. * @property integer $province_id
  17. * @property integer $city_id
  18. * @property integer $district_id
  19. * @property integer $street_id
  20. * @property integer $area_level
  21. * @property string $access_token
  22. * @property string $created_at
  23. * @property string $updated_at
  24. * @property string $username
  25. * @property string $password
  26. * @property integer $mobile
  27. * @property string $remark
  28. * @property string $email
  29. * @property string $name
  30. * @property string $avatar
  31. * @property integer $is_delete
  32. * @property integer $store_id
  33. * @property string $type
  34. * @property integer $type_id
  35. * @property integer $max_app_count
  36. * @property integer $expire_time
  37. * @property integer $saas_user_id
  38. * @property string $max_price
  39. * @property double $rate
  40. * @property integer $is_enable
  41. * @property integer $advert_push_user_id
  42. * @property string $address
  43. */
  44. class Admin extends ActiveRecord
  45. {
  46. const ADMIN_NORMAL = 0; // 正常
  47. const ADMIN_DELETE = 1; // 已删除
  48. const ADMIN_ENABLE = 1; // 开启
  49. const ADMIN_DISABLE = 0; // 禁用
  50. const ADMIN_TYPE_DEFAULT = 'admin'; // 平台管理员
  51. const ADMIN_TYPE_SUPPLIER = 'supplier'; // 供货商
  52. const ADMIN_TYPE_MCH = 'mch'; // 入驻商
  53. const ADMIN_TYPE_MD = 'md'; // 门店
  54. const ADMIN_TYPE_STORE = 'store'; // 商城
  55. const ADMIN_TYPE_STAFF = 'staff'; // 员工
  56. const ADMIN_TYPE_SAAS_STAFF = 'saas_staff';
  57. const ADMIN_TYPE_FRONT_AGENT = 'front_agent'; // 前置仓
  58. const ADMIN_TYPE_GOODS_AGENT = 'goods_agent'; // 产品代理
  59. const ADMIN_TYPE_BD_AGENT = 'bd_agent'; // 推广代理
  60. const ADMIN_TYPE_AREA_AGENT = 'area_agent'; // 区域代理
  61. const ADMIN_TYPE_MINI_ADMIN = 'mini_admin'; // 小程序管理员
  62. public static function tableName()
  63. {
  64. return '{{%admin}}';
  65. }
  66. public function behaviors()
  67. {
  68. return [
  69. [
  70. // 自动更新创建和更新时间
  71. 'class' => TimestampBehavior::class,
  72. 'value' => time()
  73. ]
  74. ];
  75. }
  76. public function rules()
  77. {
  78. return [
  79. [['is_delete', 'store_id', 'type_id', 'max_app_count', 'expire_time', 'saas_user_id', 'is_enable','advert_push_user_id'], 'integer'],
  80. [['max_price', 'rate'], 'number'],
  81. [['access_token'], 'string', 'max' => 60],
  82. [['email', 'name', 'username', 'password', 'type'], 'string', 'max' => 100],
  83. [['avatar'], 'string', 'max' => 255],
  84. [['email'], 'email'],
  85. [['access_token', 'create_at', 'update_at'], 'safe'],
  86. [['address', 'mobile'], 'string']
  87. ];
  88. }
  89. public function attributeLabels()
  90. {
  91. return [
  92. 'id' => 'ID',
  93. 'province_id' => 'province_id',
  94. 'city_id' => 'city_id',
  95. 'district_id' => 'district_id',
  96. 'area_level' => 'area_level',
  97. 'access_token' => 'access token',
  98. 'create_at' => '创建时间',
  99. 'update_at' => '更新时间',
  100. 'username' => '用户名',
  101. 'password' => '密码',
  102. 'mobile' => '手机号',
  103. 'remark' => '备注',
  104. 'email' => '邮箱',
  105. 'name' => '昵称',
  106. 'avatar' => '头像',
  107. 'is_delete' => '是否已删除',
  108. 'store_id' => 'store_id',
  109. 'type' => '管理员类型',
  110. 'type_id' => 'type id',
  111. 'max_app_count' => '最大创建商城数量',
  112. 'expire_time' => '过期时间',
  113. 'max_price' => '允许设置的最大佣金',
  114. 'rate' => '代理商服务费率',
  115. 'is_enable' => '是否开启',
  116. 'address' => '代理地址'
  117. ];
  118. }
  119. public function beforeSave($insert)
  120. {
  121. // 创建用户自动赋值access_token
  122. if (parent::beforeSave($insert)) {
  123. if ($this->isNewRecord) {
  124. $this->access_token = \Yii::$app->security->generateRandomString();
  125. }
  126. if (empty($this->max_price)) {
  127. $this->max_price = 0.00;
  128. }
  129. if (empty($this->rate)) {
  130. $this->rate = 0.00;
  131. }
  132. $this->mobile = \str_replace(' ', '', $this->mobile);
  133. /*if($this->type == 'store' && $this->saas_user_id > 0){
  134. if($this->isNewRecord){
  135. $is = self::find()->where(['type' => 'store', 'saas_user_id' => $this->saas_user_id, 'is_delete' => 0])->one();
  136. }else{
  137. $is = self::find()->where(['type' => 'store', 'saas_user_id' => $this->saas_user_id, 'is_delete' => 0])->andWhere(['!=', 'id', $this->id])->one();
  138. }
  139. if($is){
  140. $this->addError('mobile', '操作失败,其他管理员['. $is->username .']已绑定此用户!请更换');
  141. return false;
  142. }
  143. }*/
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * @param int|string $id
  150. * @return Admin|IdentityInterface|null
  151. */
  152. public static function findIdentity($id)
  153. {
  154. return static::findOne($id);
  155. }
  156. /**
  157. * @param mixed $token
  158. * @param null $type
  159. * @return Admin|IdentityInterface|null
  160. */
  161. public static function findIdentityByAccessToken($token, $type = null)
  162. {
  163. return static::findOne(['access_token' => $token, 'is_delete' => 0]);
  164. }
  165. /**
  166. * @param $username
  167. */
  168. public static function findByUsername($username)
  169. {
  170. return static::find()->where(['is_delete' => 0])->andWhere(['or',['username' => $username,],['name' => $username]])->one();
  171. }
  172. /**
  173. * @param $email
  174. * @return Admin|null
  175. */
  176. public static function findByEmail($email)
  177. {
  178. return static::findOne(['email' => $email, 'is_delete' => 0]);
  179. }
  180. /**
  181. * @param $mobile
  182. * @return Admin|null
  183. */
  184. public static function findByMobile($mobile)
  185. {
  186. return static::findOne(['mobile' => $mobile, 'is_delete' => 0]);
  187. }
  188. /**
  189. * @return int|string
  190. */
  191. public function getId()
  192. {
  193. return $this->id;
  194. }
  195. /**
  196. * 刷新token
  197. * @return bool
  198. */
  199. public function refreshToken()
  200. {
  201. $this->access_token = \Yii::$app->security->generateRandomString();
  202. return $this->save();
  203. }
  204. /**
  205. * 验证密码
  206. * @param $password
  207. * @return bool
  208. */
  209. public function validatePassword($password)
  210. {
  211. return \Yii::$app->security->validatePassword($password, $this->password);
  212. }
  213. /**
  214. * 获取角色
  215. * @return \yii\db\ActiveQuery
  216. * @throws \yii\base\InvalidConfigException
  217. */
  218. public function getRoles()
  219. {
  220. return $this->hasMany(AuthRole::class, ['id' => 'role_id'])
  221. ->viaTable('{{%admin_role}}', ['admin_id' => 'id']);
  222. }
  223. /**
  224. * 获取平台角色
  225. * @return \yii\db\ActiveQuery
  226. * @throws \yii\base\InvalidConfigException
  227. */
  228. public function getSaasRoles()
  229. {
  230. return $this->hasMany(SaasAuthRole::class, ['id' => 'role_id'])
  231. ->viaTable('{{%saas_admin_role}}', ['admin_id' => 'id']);
  232. }
  233. }