Member.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace common\models\member;
  3. use Yii;
  4. use yii\base\Exception;
  5. use yii\base\InvalidConfigException;
  6. use yii\db\ActiveQuery;
  7. use common\models\base\User;
  8. use common\traits\Tree;
  9. use common\models\rbac\AuthAssignment;
  10. use common\helpers\RegularHelper;
  11. use common\enums\StatusEnum;
  12. use common\enums\MemberTypeEnum;
  13. use common\helpers\HashidsHelper;
  14. use common\helpers\StringHelper;
  15. use common\models\api\AccessToken;
  16. use common\traits\HasOneMerchant;
  17. /**
  18. * This is the model class for table "{{%member}}".
  19. *
  20. * @property int $id
  21. * @property int|null $merchant_id 商户ID
  22. * @property int|null $store_id 店铺ID
  23. * @property string $username 帐号
  24. * @property string $password_hash 密码
  25. * @property string $auth_key 授权令牌
  26. * @property string|null $password_reset_token 密码重置令牌
  27. * @property string|null $mobile_reset_token 手机号码重置令牌
  28. * @property int|null $type 1:会员;2:后台管理员;3:商家管理员
  29. * @property string|null $realname 真实姓名
  30. * @property string|null $nickname 昵称
  31. * @property string|null $head_portrait 头像
  32. * @property int|null $gender 性别[0:未知;1:男;2:女]
  33. * @property string|null $qq qq
  34. * @property string|null $email 邮箱
  35. * @property string|null $birthday 生日
  36. * @property int|null $province_id 省
  37. * @property int|null $city_id 城市
  38. * @property int|null $area_id 地区
  39. * @property string|null $address 默认地址
  40. * @property string|null $mobile 手机号码
  41. * @property string|null $tel_no 电话号码
  42. * @property string|null $bg_image 个人背景图
  43. * @property string|null $description 个人说明
  44. * @property int|null $visit_count 访问次数
  45. * @property int|null $last_time 最后一次登录时间
  46. * @property string|null $last_ip 最后一次登录ip
  47. * @property int|null $role 权限
  48. * @property int|null $current_level 当前级别
  49. * @property int|null $level_expiration_time 等级到期时间
  50. * @property int|null $level_buy_type 1:赠送;2:购买
  51. * @property int|null $pid 上级id
  52. * @property int|null $level 级别
  53. * @property string|null $tree 树
  54. * @property string|null $promoter_code 推广码
  55. * @property int|null $certification_type 认证类型
  56. * @property string|null $source 注册来源
  57. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  58. * @property int|null $created_at 创建时间
  59. * @property int|null $updated_at 修改时间
  60. */
  61. class Member extends User
  62. {
  63. use Tree, HasOneMerchant;
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public static function tableName()
  68. {
  69. return '{{%member}}';
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function rules()
  75. {
  76. return [
  77. [
  78. [
  79. 'merchant_id',
  80. 'store_id',
  81. 'type',
  82. 'gender',
  83. 'province_id',
  84. 'city_id',
  85. 'area_id',
  86. 'visit_count',
  87. 'last_time',
  88. 'role',
  89. 'current_level',
  90. 'level_expiration_time',
  91. 'level_buy_type',
  92. 'pid',
  93. 'level',
  94. 'certification_type',
  95. 'status',
  96. 'created_at',
  97. 'updated_at',
  98. ],
  99. 'integer',
  100. ],
  101. [['birthday'], 'safe'],
  102. [['email'], 'email'],
  103. [['username', 'qq', 'mobile', 'tel_no'], 'string', 'max' => 20],
  104. ['mobile', 'match', 'pattern' => RegularHelper::mobile(), 'message' => '不是一个有效的手机号码'],
  105. [['password_hash', 'password_reset_token', 'mobile_reset_token', 'head_portrait'], 'string', 'max' => 150],
  106. [['auth_key'], 'string', 'max' => 32],
  107. [['realname', 'promoter_code', 'source'], 'string', 'max' => 50],
  108. [['nickname', 'email'], 'string', 'max' => 60],
  109. [['description'], 'string', 'max' => 140],
  110. [['address', 'bg_image'], 'string', 'max' => 200],
  111. [['last_ip'], 'string', 'max' => 40],
  112. [['tree'], 'string', 'max' => 2000],
  113. ];
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function attributeLabels()
  119. {
  120. return [
  121. 'id' => 'ID',
  122. 'username' => '账号',
  123. 'password_hash' => '密码',
  124. 'auth_key' => '授权令牌',
  125. 'password_reset_token' => '密码重置令牌',
  126. 'mobile_reset_token' => '手机号码重置令牌',
  127. 'type' => '管理员类型',
  128. 'nickname' => '昵称',
  129. 'realname' => '真实姓名',
  130. 'head_portrait' => '头像',
  131. 'gender' => '性别',
  132. 'qq' => 'qq',
  133. 'email' => '邮箱',
  134. 'birthday' => '生日',
  135. 'province_id' => '所在省',
  136. 'city_id' => '所在市',
  137. 'area_id' => '所在区',
  138. 'address' => '所在详细地址',
  139. 'mobile' => '手机号码',
  140. 'tel_no' => '家庭号码',
  141. 'bg_image' => '个人背景图',
  142. 'description' => '个人说明',
  143. 'visit_count' => '访问次数',
  144. 'last_time' => '最后一次登录时间',
  145. 'last_ip' => '最后一次登录ip',
  146. 'role' => '权限',
  147. 'current_level' => '当前级别',
  148. 'level_expiration_time' => '等级到期时间',
  149. 'level_buy_type' => '等级类型', // 1:赠送;2:购买
  150. 'pid' => '上级id',
  151. 'level' => '级别',
  152. 'tree' => '树',
  153. 'promoter_code' => '推广码',
  154. 'certification_type' => '认证类型',
  155. 'source' => '注册来源',
  156. 'status' => '状态',
  157. 'created_at' => '创建时间',
  158. 'updated_at' => '修改时间',
  159. ];
  160. }
  161. /**
  162. * 关联授权角色
  163. *
  164. * @return ActiveQuery
  165. */
  166. public function getAssignment()
  167. {
  168. return $this->hasMany(AuthAssignment::class, ['user_id' => 'id']);
  169. }
  170. /**
  171. * 关联账号
  172. */
  173. public function getAccount()
  174. {
  175. return $this->hasOne(Account::class, ['member_id' => 'id']);
  176. }
  177. /**
  178. * 关联级别
  179. */
  180. public function getMemberLevel()
  181. {
  182. return $this->hasOne(Level::class, ['level' => 'current_level'])
  183. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  184. }
  185. /**
  186. * 关联第三方绑定
  187. */
  188. public function getAuth()
  189. {
  190. return $this->hasMany(Auth::class, ['member_id' => 'id'])->where(['status' => StatusEnum::ENABLED]);
  191. }
  192. /**
  193. * 关联标签
  194. *
  195. * @return ActiveQuery
  196. * @throws InvalidConfigException
  197. */
  198. public function getTag()
  199. {
  200. return $this->hasMany(Tag::class, ['id' => 'tag_id'])
  201. ->viaTable(TagMap::tableName(), ['member_id' => 'id'])
  202. ->asArray();
  203. }
  204. /**
  205. * 统计
  206. *
  207. * @return \yii\db\ActiveQuery
  208. */
  209. public function getStat()
  210. {
  211. return $this->hasOne(Stat::class, ['member_id' => 'id']);
  212. }
  213. /**
  214. * @param bool $insert
  215. * @return bool
  216. * @throws Exception
  217. */
  218. public function beforeSave($insert)
  219. {
  220. empty($this->store_id) && $this->store_id = 0;
  221. empty($this->type) && $this->type = MemberTypeEnum::MEMBER;
  222. if ($this->isNewRecord) {
  223. $this->auth_key = Yii::$app->security->generateRandomString();
  224. }
  225. // 处理上下级关系
  226. $this->autoUpdateTree();
  227. return parent::beforeSave($insert);
  228. }
  229. /**
  230. * @param bool $insert
  231. * @param array $changedAttributes
  232. */
  233. public function afterSave($insert, $changedAttributes)
  234. {
  235. if ($insert) {
  236. $account = new Account();
  237. $account->member_id = $this->id;
  238. $account->member_type = $this->type;
  239. $account->merchant_id = $this->merchant_id;
  240. $account->store_id = $this->store_id;
  241. $account->save();
  242. $updateData = [];
  243. empty($this->promoter_code) && $updateData['promoter_code'] = HashidsHelper::encode($this->id);
  244. if (empty($this->nickname) && !empty($this->mobile)) {
  245. $nickname = StringHelper::random(5).'_'.substr($this->mobile, -4);
  246. $this->nickname = $nickname;
  247. $updateData['nickname'] = $nickname;
  248. }
  249. !empty($updateData) && Member::updateAll($updateData, ['id' => $this->id]);
  250. // 统计
  251. $account = new Stat();
  252. $account->member_id = $this->id;
  253. $account->merchant_id = $this->merchant_id;
  254. $account->store_id = $this->store_id;
  255. $account->save();
  256. }
  257. if ($this->status == StatusEnum::DISABLED) {
  258. AccessToken::updateAll(['status' => $this->status], ['member_id' => $this->id]);
  259. // 记录行为
  260. Yii::$app->services->actionLog->create('memberBlacklist', '拉入黑名单');
  261. }
  262. if ($this->status == StatusEnum::DELETE) {
  263. Account::updateAll(['status' => $this->status], ['member_id' => $this->id]);
  264. AccessToken::updateAll(['status' => $this->status], ['member_id' => $this->id]);
  265. // 记录行为
  266. Yii::$app->services->actionLog->create('memberDelete', '删除用户');
  267. }
  268. parent::afterSave($insert, $changedAttributes);
  269. }
  270. }
粤ICP备19079148号