UserExtra.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use phpDocumentor\Reflection\Types\False_;
  6. use PHPUnit\Util\Exception;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_user_extra".
  10. *
  11. *
  12. * @property int $id
  13. * @property int $user_id
  14. * @property int $sex 性别:1:男;2:女
  15. * @property string $id_card 身份证
  16. * @property string $wechat 微信号
  17. * @property string $birthday 出生年月日
  18. * @property string $province 省
  19. * @property string $city 市
  20. * @property string $area 区
  21. * @property string $town 镇
  22. * @property string $remark 备注
  23. * @property int $created_at 创建时间
  24. * @property int $updated_at 更新时间
  25. * @property int $fans_count 更新时间
  26. * @property int $follow_count 更新时间
  27. * @property string|null $personal_home_bg 个人主页背景
  28. * @property string|null $personal_signature 个人主页个性签名
  29. * @property float $target_weight 个人主页个性签名
  30. * @property float $body_weight 个人主页个性签名
  31. * @property float $body_height 个人主页个性签名
  32. */
  33. class UserExtra extends BaseActiveRecord
  34. {
  35. const SEX_ARR = [
  36. 1 => '男',
  37. 2 => '女'
  38. ];
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%user_extra}}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['user_id'], 'required'],
  53. [['user_id', 'sex', 'created_at', 'updated_at','follow_count','fans_count'], 'integer'],
  54. [['id_card'], 'string', 'max' => 30],
  55. [['province', 'city', 'area', 'remark', 'personal_home_bg', 'personal_signature', 'town'], 'string', 'max' => 255],
  56. [['birthday'], 'string', 'max' => 50],
  57. [['target_weight', 'body_weight', 'body_height'], 'number'],
  58. [['wechat'], 'safe'],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'user_id' => 'User ID',
  69. 'sex' => '性别:1:男;2:女',
  70. 'id_card' => '身份证',
  71. 'wechat' => '微信号',
  72. 'birthday' => '出生年月日',
  73. 'province' => '省',
  74. 'city' => '市',
  75. 'area' => '区',
  76. 'town' => '镇',
  77. 'remark' => '备注',
  78. 'created_at' => '创建时间',
  79. 'updated_at' => '更新时间',
  80. 'personal_home_bg' => '个人主页背景',
  81. 'personal_signature' => '个人主页个性签名',
  82. 'follow_count' => '关注人数',
  83. 'fans_count' => '粉丝人数',
  84. ];
  85. }
  86. /**
  87. * 关联用户表
  88. * @Author: lun
  89. * @DateTime: 2021/12/22 0022 10:32
  90. * @Copyright: copyright (c) 2021 广东七件事集团
  91. * @return \yii\db\ActiveQuery
  92. */
  93. public function getUser()
  94. {
  95. return $this->hasOne(User::class, ['id' => 'user_id']);
  96. }
  97. /**
  98. * 保存数据
  99. * @param array $params
  100. * @return bool|UserExtra
  101. */
  102. public static function setData(array $params)
  103. {
  104. try {
  105. if (!empty($params['user_id'])) {
  106. $model = self::findOne(['user_id' => $params['user_id']]);
  107. if (empty($model)) {
  108. $model = new self();
  109. $model->loadDefaultValues();
  110. }
  111. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  112. return $model;
  113. }
  114. } catch (Exception $e) {
  115. self::$static_error = $e->getMessage();
  116. return false;
  117. }
  118. }
  119. /**
  120. * 获取额外参数
  121. * @Author: lun
  122. * @DateTime: 2021/12/3 0003 10:00
  123. * @Copyright: copyright (c) 2021 广东七件事集团
  124. * @param $user_id
  125. * @return array|bool
  126. */
  127. public static function getUserExtra($user_id)
  128. {
  129. try {
  130. $field = "sex,id_card,birthday,wechat,province,city,area,town,personal_home_bg,personal_signature";
  131. $model = self::find()->select($field)->where(['user_id' => $user_id])->one();
  132. if (empty($model)) {
  133. $model = new self();
  134. $model->user_id = $user_id;
  135. $model->loadDefaultValues();
  136. if (!$model->save()) throw new Exception($model->getErrorMessage());
  137. }
  138. return $model->toArray();
  139. } catch (Exception $e) {
  140. self::setStaticError($e->getMessage());
  141. return false;
  142. }
  143. }
  144. }