UserIdentity.php
8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
99
100
101
102
103
104
105
106
107
108
109
110
111
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php namespace app\wx\models;
use Yii;
use yii\base\Exception;
use yii\log\Logger;
use yii\web\IdentityInterface;
use app\wx\models\User as UserModel;
use common\helpers\ImageManager;
use domain\user\UserProfile;
use domain\user\User;
use domain\PhoneCodeHelper;
use domain\system\SmsMessage;
use stdClass;
/**
* Class User
* @package app\wx\models
*/
class UserIdentity extends UserModel
{
static $id = null;
/**
* 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.
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* 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.
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
/**
* @inheritdoc
*/
public function getId()
{
return self::getIdFromSession();
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return '';
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return true;
}
/**
* 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();
}
/**
* @return mixed|null
*/
protected static function getIdFromSession()
{
if (null === self::$id){
$user = Yii::$app->getUser();
$session = Yii::$app->getSession();
$id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($user->idParam) : null;
self::$id = $id;
}
return self::$id;
}
/* ----------- 以下用于登录和 ------------*/
/**
* @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);
$testPhones = Yii::$app->params['testLoginPhones'];
$sms = new SmsMessage();
if ($code) {
// 发送短信
if (!in_array($mobile, $testPhones)) {
$sms->sendLoginCode($mobile, $code);
}
$e->message = '您的登录码已经发送,请注意查收短信!';
} else {
$code = PhoneCodeHelper::setLoginCode($mobile);
// 发送短信
if (!in_array($mobile, $testPhones)) {
$sms->sendLoginCode($mobile, $code);
}
$e->message = '您的登录码已经发送,请注意查收短信!';
}
if (!in_array($mobile, $testPhones)) {
$e->code = '';
} else {
$e->code = $code;
}
$e->success = true;
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'],
'address' => $rData['address']
];
$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();
// 因为注册完成之后要审核,必须把已经登录的退出去
Yii::$app->user->logout();
$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 = '';
$e->code = '';
$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 = '您的注册码已经发送,请注意查收短信!';
} else {
$code = PhoneCodeHelper::setRegisterCode($mobile);
//发送短信
$sms->sendRegCode($mobile, $code);
$e->message = '您的注册码已经发送,请注意查收短信!';
}
$e->code = '';
$e->success = true;
return $e;
}
}