cxfoot/models/common/CommonUserEditForm.php
2023-10-27 14:25:12 +08:00

242 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2021-5-10
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\components\EncryptHelper;
use app\components\FlashStorage;
use app\components\SysErrCode;
use app\components\Validation;
use app\models\User;
use app\models\Model;
class CommonUserEditForm extends Model
{
public $cx_mch_id;
public $username;
public $password;
public $auth_key;
public $access_token;
public $nickname;
public $avatar_url;
public $mobile_phone;
public $mobile_prefix;
public $email;
public $real_name;
public $desc;
public $gender;
public $birthday;
public $status;
public $parent_id;
public $is_modify_un;
public $country_id;
public $city_id;
public $type;
public $store_id;
public $model;
public function rules()
{
return [
[['username', 'password', 'auth_key', 'access_token', 'nickname', 'avatar_url', 'mobile_phone', 'mobile_prefix', 'email', 'real_name', 'desc', ], 'trim'],
[['username', 'password', 'auth_key', 'access_token', 'nickname', 'avatar_url', 'mobile_phone', 'mobile_prefix', 'email', 'real_name', 'desc', ], 'string'],
[['gender', 'birthday', 'status', 'parent_id', 'is_modify_un', 'country_id', 'city_id', 'type','store_id'], 'integer'],
[['model'], 'safe'],
[['model', 'cx_mch_id', ], 'required'],
[['username'], 'string', 'min' => 5],
[['password'], 'string', 'min' => 6],
['username', 'match', 'pattern' => '/^[a-z]\w*$/i', 'message' => '用户名需要以字母开头,由数字和字母组成。'],
[['username', 'nickname', ], 'required', 'on' => 'add'],
[['username', 'nickname', ], 'required', 'on' => 'wxmp_signup'],
[['username', 'nickname', 'real_name', 'mobile_phone', 'password'], 'required', 'on' => 'user_add'],
[['mobile_phone', 'mobile_prefix', 'username', ], 'required', 'on' => 'mobile_signup'],
[['mobile_phone', 'mobile_prefix', ], 'required', 'on' => 'bind_mobile'],
[['username', 'password','mobile_phone'], 'required', 'on' => 'store_add'],
[['mobile_phone', 'mobile_prefix'], 'required', 'on' => 'coach_add'],
[['mobile_phone', 'mobile_prefix'], 'required', 'on' => 'detection_add'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'cx_mch_id' => '平台商户ID',
'username' => '用户名',
'password' => '密码',
'auth_key' => 'AUTH_KEY',
'access_token' => 'ACCESS_TOKEN',
'nickname' => '昵称',
'avatar_url' => '头像',
'mobile_phone' => '手机号',
'mobile_prefix' => '手机号国家代码',
'email' => '邮箱',
'real_name' => '真实姓名',
'desc' => '个性签名',
'gender' => '性别0=女1=男2=保密',
'birthday' => '生日',
'status' => '状态0=正常1=封禁2=挂起3=注销',
'parent_id' => 'PARENT_ID',
'is_modify_un' => '用户名是否可以修改0=否1=是',
'is_delete' => '是否删除0=否1=是',
'country_id' => '国家ID',
'city_id' => '城市ID',
'type' => '用户类型',
];
}
public function save()
{
if(!$this->validate()){
return $this->getModelError();
}
if($this->model->isNewRecord){
$this->model->cx_mch_id = $this->cx_mch_id;
$res = $this->checkUsername($this->username);
if($res['code'] != 0)
return $res;
if($this->password == null)
$this->password = \Yii::$app->security->generateRandomString();
$this->model->password = $this->password;
$this->model->auth_key = \Yii::$app->security->generateRandomString();
$this->model->status = User::STATUS_NORMAL;
$this->model->is_modify_un = $this->is_modify_un == null ? 0 : $this->is_modify_un;
$this->model->is_delete = 0;
}
if($this->username != null)
$this->model->username = $this->username;
if($this->password != null)
$this->model->password = $this->password;
if($this->access_token != null)
$this->model->access_token = $this->access_token;
if($this->nickname != null)
$this->model->nickname = $this->nickname;
if($this->avatar_url != null)
$this->model->avatar_url = $this->avatar_url;
if($this->mobile_prefix != null)
$this->model->mobile_prefix = $this->mobile_prefix;
if(!empty($this->mobile_phone)){
if(!Validation::is_mobile($this->mobile_phone,$this->mobile_prefix)){
return $this->apiReturnError("手机号格式不正确");
}
$encrypted_mobile = EncryptHelper::encryptMobilePhone($this->mobile_phone);
if(!$encrypted_mobile){
return [
'code' => 1,
'msg' => '系统内部错误,错误代码:'.SysErrCode::$encryptedMobilePhoneFailed
];
}
$user_id = $this->model->isNewRecord ? 0 : $this->model->id;
$res = $this->checkMobile($encrypted_mobile, $this->mobile_prefix, $user_id);
if($res['code'] != 0)
return $res;
$this->model->mobile_phone = $encrypted_mobile;
}
if($this->email != null)
$this->model->email = $this->email;
if($this->real_name != null)
$this->model->real_name = $this->real_name;
if($this->desc != null)
$this->model->desc = $this->desc;
if($this->gender != null)
$this->model->gender = $this->gender;
if($this->birthday != null)
$this->model->birthday = $this->birthday;
if($this->parent_id != null)
$this->model->parent_id = $this->parent_id;
if($this->country_id != null)
$this->model->country_id = $this->country_id;
if($this->city_id != null)
$this->model->city_id = $this->city_id;
if($this->type != null)
$this->model->type = $this->type;
$this->model->updated_at = time();
if(!$this->model->save()){
return $this->getModelError($this->model);
}
if($this->model->isNewRecord){
//@TODO 账户初始化
}
return [
'code' => 0,
'msg' => '保存成功',
'data' => [
'user_id' => $this->model->id,
]
];
}
//检测用户名是否已经存在
public function checkUsername($username)
{
$temp = User::findOne([
'username' => $username,
'is_delete' => 0
]);
if($temp != null){
return [
'code' => 1,
'msg' => '用户名已存在'
];
}
return [
'code' => 0,
'msg' => 'ok'
];
}
//检测手机号是否已经存在
public function checkMobile($mobile_phone, $mobile_prefix = "86", $user_id)
{
$temp = User::findOne([
'mobile_phone' => $mobile_phone,
'mobile_prefix' => $mobile_prefix,
'is_delete' => 0
]);
if($temp != null && $temp->id != $user_id){
return [
'code' => 1,
'msg' => '手机号已存在'
];
}
return [
'code' => 0,
'msg' => 'ok'
];
}
}