62d73041
xu
app-wx
|
1
2
|
<?php namespace app\wx\models;
|
32926e46
xu
app-wx(v0.1.0 bui...
|
3
|
use Yii;
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
4
5
|
use yii\base\Exception;
use yii\log\Logger;
|
62d73041
xu
app-wx
|
6
|
use yii\web\IdentityInterface;
|
32926e46
xu
app-wx(v0.1.0 bui...
|
7
|
use app\wx\models\User as UserModel;
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
8
9
10
11
12
13
|
use common\helpers\ImageManager;
use domain\user\UserProfile;
use domain\user\User;
use domain\PhoneCodeHelper;
use domain\system\SmsMessage;
use stdClass;
|
62d73041
xu
app-wx
|
14
15
|
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
16
|
* Class User
|
62d73041
xu
app-wx
|
17
18
|
* @package app\wx\models
*/
|
32926e46
xu
app-wx(v0.1.0 bui...
|
19
|
class UserIdentity extends UserModel
|
62d73041
xu
app-wx
|
20
|
{
|
62d73041
xu
app-wx
|
21
22
23
|
static $id = null;
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
24
25
26
27
|
* Finds an identity by the given ID.
*
* @param string|int $id the ID to be looked for
* @return IdentityInterface|null the identity object that matches the given ID.
|
62d73041
xu
app-wx
|
28
29
30
|
*/
public static function findIdentity($id)
{
|
32926e46
xu
app-wx(v0.1.0 bui...
|
31
|
return static::findOne($id);
|
62d73041
xu
app-wx
|
32
33
34
|
}
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
35
36
37
38
|
* Finds an identity by the given token.
*
* @param string $token the token to be looked for
* @return IdentityInterface|null the identity object that matches the given token.
|
62d73041
xu
app-wx
|
39
40
41
|
*/
public static function findIdentityByAccessToken($token, $type = null)
{
|
32926e46
xu
app-wx(v0.1.0 bui...
|
42
|
return static::findOne(['access_token' => $token]);
|
62d73041
xu
app-wx
|
43
44
45
|
}
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
46
|
* @inheritdoc
|
62d73041
xu
app-wx
|
47
48
49
50
51
52
53
|
*/
public function getId()
{
return self::getIdFromSession();
}
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
54
|
* @inheritdoc
|
62d73041
xu
app-wx
|
55
56
57
|
*/
public function getAuthKey()
{
|
32926e46
xu
app-wx(v0.1.0 bui...
|
58
|
return '';
|
62d73041
xu
app-wx
|
59
60
61
|
}
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
62
|
* @inheritdoc
|
62d73041
xu
app-wx
|
63
64
65
66
67
68
69
|
*/
public function validateAuthKey($authKey)
{
return true;
}
/**
|
32926e46
xu
app-wx(v0.1.0 bui...
|
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return true ;//Yii::$app->security->validatePassword($password, $this->password_hash);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
|
62d73041
xu
app-wx
|
99
100
101
102
103
|
* @return mixed|null
*/
protected static function getIdFromSession()
{
if (null === self::$id){
|
32926e46
xu
app-wx(v0.1.0 bui...
|
104
|
$user = Yii::$app->getUser();
|
62d73041
xu
app-wx
|
105
106
107
108
109
110
111
|
$session = Yii::$app->getSession();
$id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($user->idParam) : null;
self::$id = $id;
}
return self::$id;
}
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* ----------- 以下用于登录和 ------------*/
/**
* @param $mobile
* @return stdClass
*/
public static function login($mobile)
{
$e = new stdClass();
$e->success = false;
$e->message = '';
$where = ['mobile' => $mobile];
$userEntity = UserModel::findOne($where);
if (empty($userEntity)) {
$e->message = '该手机未注册, 请先注册了再来登录';
return $e;
}
if (User::STATUS_APPROVE != $userEntity->status) {
$phone = Yii::$app->params['SERVICE_PHONE'];
$e->message = '该账号还在审核中,请联系'.$phone;
return $e;
}
//$userEntity 必须是 app\wx\models\User
if(Yii::$app->getUser()->login($userEntity, 7000)) {
PhoneCodeHelper::removeLoginCode($mobile);
$e->message = '登录成功';
$e->success = true;
} else {
$e->message = '登录失败';
}
return $e;
}
/**
* 获取登录验证码
* @param $mobile
* @return stdClass
*/
public static function getLoginVCode($mobile)
{
$e = new stdClass();
$e->success = false;
$e->code = '';
$userInfo = UserModel::findOne(['mobile' => $mobile]);
if (empty($userInfo)) {
$e->message = '登录失败,该手机未注册';
return $e;
}
$code = PhoneCodeHelper::getLoginCode($mobile);
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
162
|
$testPhones = Yii::$app->params['testLoginPhones'];
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
163
164
165
|
$sms = new SmsMessage();
if ($code) {
// 发送短信
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
166
167
168
|
if (!in_array($mobile, $testPhones)) {
$sms->sendLoginCode($mobile, $code);
}
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
169
170
171
172
|
$e->message = '您的登录码已经发送,请注意查收短信!';
} else {
$code = PhoneCodeHelper::setLoginCode($mobile);
// 发送短信
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
173
174
175
|
if (!in_array($mobile, $testPhones)) {
$sms->sendLoginCode($mobile, $code);
}
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
176
177
|
$e->message = '您的登录码已经发送,请注意查收短信!';
}
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
178
179
180
181
182
|
if (!in_array($mobile, $testPhones)) {
$e->code = '';
} else {
$e->code = $code;
}
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
183
|
$e->success = true;
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
return $e;
}
/**
* @param $mobile
* @param $rData
* @return stdClass
*/
public static function register($mobile, $rData)
{
$e = new stdClass();
$e->success = false;
// 检查车厂名称是否注册了
// 检查手机号码是否注册了
$userMobile = UserModel::findOne(['mobile' => $mobile]);
if ($userMobile) {
if (User::STATUS_APPROVE != $userMobile->status) {
$phone = Yii::$app->params['SERVICE_PHONE'];
$e->message = '该账号还在审核中,请联系'.$phone;
} else {
$e->message = '该手机号已经注册过维修厂,请更换其他手机号';
}
return $e;
}
$name = $rData['name'];
$licensePic = $rData['licensePic'];
$uData = [
'mobile' => $mobile,
'name' => $name,
'user_name' => $mobile
];
$tran = Yii::$app->db->beginTransaction();
try {
$userEntity = User::create($uData);
$pData = [
'user_id' => $userEntity->id,
'emergency_contact' => $rData['emergencyContact'],
'emergency_person' => $rData['emergencyPerson'],
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
225
|
'address' => $rData['address']
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
];
$userProfile = UserProfile::create($pData);
PhoneCodeHelper::removeRegisterCode($mobile);
$userUUId = $userEntity->uuid;
$licensePicImage = ImageManager::mvUploadImage($licensePic, $userUUId);
$headPicImage = ImageManager::mvUploadImage($rData['headPic'], $userUUId);
$techChargePicImage = ImageManager::mvUploadImage($rData['techChargePic'], $userUUId);
$QAChargePicImage = ImageManager::mvUploadImage($rData['QAChargePic'], $userUUId);
$userProfile->license_pic = $licensePicImage[0].$licensePicImage[1];
$userProfile->factory_head_pic = $headPicImage[0].$headPicImage[1];
$userProfile->tech_charge_pic = $techChargePicImage[0].$techChargePicImage[1];
$userProfile->qa_charge_pic = $QAChargePicImage[0].$QAChargePicImage[1];
$userProfile->save();
$tran->commit();
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
241
242
|
// 因为注册完成之后要审核,必须把已经登录的退出去
Yii::$app->user->logout();
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
$e->success = true;
$e->message = '注册完成,账号审核通过后即可使用';
} catch (Exception $ex) {
$tran->rollBack();
Yii::getLogger()->log($ex->getMessage(), Logger::LEVEL_ERROR);
$e->message = '注册失败'.$ex->getMessage();
}
return $e;
}
/**
* @param $mobile
* @return stdClass
*/
public static function getRegisterVCode($mobile)
{
$e = new stdClass();
$e->success = false;
$e->message = '';
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
263
|
$e->code = '';
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
264
265
266
267
268
269
270
271
272
273
274
275
276
|
$userModel = UserModel::findOne(['mobile' => $mobile]);
$sms = new SmsMessage();
if ($userModel) {
$e->message = '该手机号已注册过';
return $e;
}
$phoneCode = PhoneCodeHelper::getRegisterCode($mobile);
if ($phoneCode) {
$code = $phoneCode;
//发送短信
$sms->sendRegCode($mobile, $code);
$e->message = '您的注册码已经发送,请注意查收短信!';
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
277
|
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
278
279
280
281
282
|
} else {
$code = PhoneCodeHelper::setRegisterCode($mobile);
//发送短信
$sms->sendRegCode($mobile, $code);
$e->message = '您的注册码已经发送,请注意查收短信!';
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
283
|
}
|
1de3211f
xu
app-wx(v0.1.0 bui...
|
284
285
|
$e->code = '';
$e->success = true;
|
afd2f743
xu
app-ht(v0.0.1 bui...
|
286
287
288
|
return $e;
}
|
62d73041
xu
app-wx
|
289
|
}
|