commit | author | age
|
90c639
|
1 |
<?php |
Z |
2 |
|
|
3 |
namespace app\models; |
|
4 |
|
|
5 |
use Yii; |
|
6 |
use yii\web\IdentityInterface; |
|
7 |
|
|
8 |
/** |
|
9 |
* This is the model class for table "{{%user}}". |
|
10 |
* |
|
11 |
* @property int $id |
|
12 |
* @property int $mall_id |
|
13 |
* @property int $mch_id |
|
14 |
* @property string $username |
|
15 |
* @property string $password |
|
16 |
* @property string $nickname |
|
17 |
* @property string $auth_key |
|
18 |
* @property string $access_token |
|
19 |
* @property string $mobile |
|
20 |
* @property string $unionid |
|
21 |
* @property string $created_at |
|
22 |
* @property string $updated_at |
|
23 |
* @property string $deleted_at |
|
24 |
* @property string $is_delete |
|
25 |
* @property UserIdentity $identity |
|
26 |
* @property UserInfo $userInfo |
|
27 |
* @property Share $share |
|
28 |
* @property User $parent |
|
29 |
* @property AdminInfo $adminInfo |
|
30 |
* @property UserPlatform[] $userPlatform |
|
31 |
*/ |
|
32 |
class User extends ModelActiveRecord implements IdentityInterface |
|
33 |
{ |
|
34 |
const EVENT_REGISTER = 'userRegister'; |
|
35 |
const EVENT_LOGIN = 'userLogin'; |
|
36 |
|
|
37 |
private $xOptions; |
|
38 |
|
|
39 |
/** |
|
40 |
* {@inheritdoc} |
|
41 |
*/ |
|
42 |
public static function tableName() |
|
43 |
{ |
|
44 |
return '{{%user}}'; |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* {@inheritdoc} |
|
49 |
*/ |
|
50 |
public function rules() |
|
51 |
{ |
|
52 |
return [ |
|
53 |
[['mall_id', 'is_delete', 'mch_id'], 'integer'], |
|
54 |
[['username', 'password', 'auth_key', 'access_token'], 'required'], |
|
55 |
[['created_at', 'updated_at', 'deleted_at', 'mobile'], 'safe'], |
|
56 |
[['username', 'unionid'], 'string', 'max' => 64], |
|
57 |
[['password', 'auth_key', 'access_token'], 'string', 'max' => 128], |
|
58 |
[['nickname'], 'string', 'max' => 45], |
|
59 |
]; |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* {@inheritdoc} |
|
64 |
*/ |
|
65 |
public function attributeLabels() |
|
66 |
{ |
|
67 |
return [ |
|
68 |
'id' => 'ID', |
|
69 |
'mall_id' => 'Mall ID', |
|
70 |
'mch_id' => 'Mch ID', |
|
71 |
'username' => 'Username', |
|
72 |
'password' => 'Password', |
|
73 |
'nickname' => 'Nickname', |
|
74 |
'auth_key' => 'Auth Key', |
|
75 |
'access_token' => 'Access Token', |
|
76 |
'unionid' => 'Unionid', |
|
77 |
'created_at' => 'Created At', |
|
78 |
'updated_at' => 'Updated At', |
|
79 |
'deleted_at' => 'Deleted At', |
|
80 |
'is_delete' => 'Is Delete', |
|
81 |
]; |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* 根据给到的ID查询身份。 |
|
86 |
* |
|
87 |
* @param string|integer $id 被查询的ID |
|
88 |
* @return IdentityInterface|null 通过ID匹配到的身份对象 |
|
89 |
*/ |
|
90 |
public static function findIdentity($id) |
|
91 |
{ |
|
92 |
return static::findOne($id); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* 根据 token 查询身份。 |
|
97 |
* |
|
98 |
* @param string $token 被查询的 token |
|
99 |
* @return IdentityInterface|null 通过 token 得到的身份对象 |
|
100 |
*/ |
|
101 |
public static function findIdentityByAccessToken($token, $type = null) |
|
102 |
{ |
|
103 |
return static::findOne(['access_token' => $token]); |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* @return int|string 当前用户ID |
|
108 |
*/ |
|
109 |
public function getId() |
|
110 |
{ |
|
111 |
return $this->id; |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* @return string 当前用户的(cookie)认证密钥 |
|
116 |
*/ |
|
117 |
public function getAuthKey() |
|
118 |
{ |
|
119 |
return $this->auth_key; |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* @param string $authKey |
|
124 |
* @return boolean if auth key is valid for current user |
|
125 |
*/ |
|
126 |
public function validateAuthKey($authKey) |
|
127 |
{ |
|
128 |
return $this->getAuthKey() === $authKey; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* 用户身份 |
|
133 |
* @return \yii\db\ActiveQuery |
|
134 |
*/ |
|
135 |
public function getIdentity() |
|
136 |
{ |
|
137 |
return $this->hasOne(UserIdentity::className(), ['user_id' => 'id']); |
|
138 |
} |
|
139 |
|
|
140 |
public function getAdminInfo() |
|
141 |
{ |
|
142 |
return $this->hasOne(AdminInfo::className(), ['user_id' => 'id']); |
|
143 |
} |
|
144 |
|
|
145 |
public function getRole() |
|
146 |
{ |
|
147 |
return $this->hasMany(AuthRole::className(), ['id' => 'role_id'])->andWhere(['is_delete' => 0]) |
|
148 |
->viaTable(AuthRoleUser::tableName(), ['user_id' => 'id', 'is_delete' => 'is_delete']); |
|
149 |
} |
|
150 |
|
|
151 |
public function getUserInfo() |
|
152 |
{ |
|
153 |
return $this->hasOne(UserInfo::className(), ['user_id' => 'id']); |
|
154 |
} |
|
155 |
|
|
156 |
public function getStore() |
|
157 |
{ |
|
158 |
return $this->hasOne(Store::className(), ['id' => 'store_id']) |
|
159 |
->viaTable(UserInfo::tableName(), ['user_id' => 'id', 'is_delete' => 'is_delete']); |
|
160 |
} |
|
161 |
|
|
162 |
public function getShare() |
|
163 |
{ |
|
164 |
return $this->hasOne(Share::className(), ['user_id' => 'id']); |
|
165 |
} |
|
166 |
|
|
167 |
public function getParent() |
|
168 |
{ |
|
169 |
return $this->hasOne(User::className(), ['id' => 'parent_id']) |
|
170 |
->viaTable(UserInfo::tableName(), ['user_id' => 'id', 'is_delete' => 'is_delete']); |
|
171 |
} |
|
172 |
|
|
173 |
public function getNewParent() |
|
174 |
{ |
|
175 |
return $this->hasOne(User::className(), ['id' => 'parent_id']); |
|
176 |
} |
|
177 |
|
|
178 |
public function getFormId() |
|
179 |
{ |
|
180 |
return $this->hasMany(Formid::className(), ['user_id' => 'id']); |
|
181 |
} |
|
182 |
|
|
183 |
public function getOneFormId() |
|
184 |
{ |
|
185 |
return $this->hasOne(Formid::className(), ['user_id' => 'id']); |
|
186 |
} |
|
187 |
|
|
188 |
public function setShare($val) |
|
189 |
{ |
|
190 |
$this->share = $val; |
|
191 |
} |
|
192 |
|
|
193 |
public function getMall() |
|
194 |
{ |
|
195 |
return $this->hasMany(Mall::className(), ['user_id' => 'id']); |
|
196 |
} |
|
197 |
|
|
198 |
public function getPlatform($platform) |
|
199 |
{ |
|
200 |
switch ($platform) { |
|
201 |
case 'wxapp': |
|
202 |
$value = '微信'; |
|
203 |
break; |
|
204 |
case 'aliapp': |
|
205 |
$value = '支付宝'; |
|
206 |
break; |
|
207 |
case 'ttapp': |
|
208 |
$value = '头条'; |
|
209 |
break; |
|
210 |
case 'bdapp': |
|
211 |
$value = '百度'; |
|
212 |
break; |
|
213 |
default: |
|
214 |
$value = '未知'; |
|
215 |
break; |
|
216 |
} |
|
217 |
|
|
218 |
return $value; |
|
219 |
} |
|
220 |
|
|
221 |
public function getUserPlatform() |
|
222 |
{ |
|
223 |
return $this->hasMany(UserPlatform::className(), ['user_id' => 'id']); |
|
224 |
} |
|
225 |
} |