152 lines
4.2 KiB
PHP
152 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2020-12-2
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
|
|
namespace app\modules\api\models;
|
|
|
|
use function AlibabaCloud\Client\value;
|
|
use app\models\Coach;
|
|
use app\models\Goods;
|
|
use app\models\Store;
|
|
use app\models\User;
|
|
use app\components\auth\AToken;
|
|
use app\components\EncryptHelper;
|
|
use app\models\UserInformation;
|
|
use app\models\UserOauth;
|
|
use app\modules\api\components\ApiHelper;
|
|
use yii\data\Pagination;
|
|
|
|
|
|
class UserModifyForm extends ApiModel
|
|
{
|
|
|
|
public $cx_mch_id;
|
|
// public $user_id;
|
|
public $real_name;
|
|
public $mobile_phone;
|
|
public $gender;
|
|
public $is_view;
|
|
public $openid;
|
|
public $token_type;
|
|
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
|
|
[['openid'], 'string'],
|
|
[['real_name'], 'required'],
|
|
[['gender'], 'required'],
|
|
[['is_view'], 'required'],
|
|
[['mobile_phone'], 'required'],
|
|
[['mobile_phone'], 'filter', 'filter' => 'trim'],
|
|
[['mobile_phone'], 'match', 'pattern' => '/^[1][34578][0-9]{9}$/'],
|
|
|
|
];
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'mobile_phone' => '手机号',
|
|
'real_name' => '姓名',
|
|
'gender' => '性别',
|
|
'is_view' => '鞋码',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 会员注册
|
|
*/
|
|
public function modify_user()
|
|
{
|
|
if (!$this->validate()) {
|
|
return $this->getModelError();
|
|
}
|
|
|
|
$form = new LoginForm();
|
|
|
|
//有绑定信息 去登陆
|
|
$user = User::findOne(['mobile_phone' => $this->mobile_phone]);
|
|
if (!empty($user->mobile_phone) && !empty($user->is_view)) {
|
|
$form = new LoginForm();
|
|
$form->cx_mch_id = 0;
|
|
$form->username = $user->username;
|
|
$form->password = '123456';
|
|
$form->token_type = 3;
|
|
return $form->login($user);
|
|
}
|
|
|
|
//没有信息 去绑定
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
try {
|
|
|
|
$username = User::generateUsername();
|
|
$password = '123456';
|
|
$password_encryption = \Yii::$app->security->generatePasswordHash($password);
|
|
|
|
|
|
$user = new User();
|
|
$user->cx_mch_id = 0;
|
|
$user->real_name = $this->real_name;
|
|
$user->mobile_phone = $this->mobile_phone;
|
|
$user->gender = $this->gender;
|
|
$user->is_view = $this->is_view;
|
|
$user->mobile_prefix = '86';
|
|
$user->nickname = $this->real_name;
|
|
$user->avatar_url = User::DEFAULT_AVATAR_URL;
|
|
$user->access_token = \Yii::$app->security->generateRandomString();
|
|
$user->type = User::TYPE_USER;
|
|
$user->username = $username;
|
|
$user->password = $password_encryption;
|
|
$user->auth_key = \Yii::$app->security->generateRandomString();;
|
|
$res = $user->save();
|
|
|
|
if ($this->openid) {
|
|
$user_outh = UserOauth::findOne(['openid' => $this->openid]);
|
|
if ($user_outh == null) {
|
|
$t->rollBack();
|
|
return ['code' => 1, 'msg' => '请重新进行微信授权',];
|
|
}
|
|
if ($user_outh->user_id == 0) {
|
|
$user_outh->user_id = $user->id;
|
|
$res1 = $user_outh->save();
|
|
if (!$res1) {
|
|
$t->rollBack();
|
|
return $this->getModelError($user_outh);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$res) {
|
|
$t->rollBack();
|
|
return $this->getModelError($user);
|
|
}
|
|
$t->commit();
|
|
$form->username = $username;
|
|
$form->password = $password;
|
|
$form->token_type = 3;
|
|
$data = $form->login($user);
|
|
return $data;
|
|
} catch (\Exception $e) {
|
|
$t->rollBack();
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '失败',
|
|
'data' => [
|
|
'msg' => $e->getMessage(),
|
|
'line' => $e->getLine(),
|
|
'file' => $e->getFile(),
|
|
],
|
|
];
|
|
}
|
|
}
|
|
} |