cxfoot/modules/api/models/UserModifyForm.php
2023-10-24 14:54:18 +08:00

394 lines
12 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\modules\api\components\ApiHelper;
use yii\data\Pagination;
class UserModifyForm extends ApiModel
{
public $cx_mch_id;
public $user_id;
public $price;
public $plugin_sign = 'member';
public $member_status = 1;
public $real_name;
public $mobile_phone;
public $age;
public $store_id;
public $territory;
public $height;
public $weight;
public $college;
public $ball_age;
public $address;
public $arm_length;
public $glove;
public $palm_size;
public $shoes_size;
public function rules()
{
return [
[['real_name'], 'required', 'on' => 'modify_user'],
// [['height'], 'required', 'on' => 'modify_user'],
// [['weight'], 'required', 'on' => 'modify_user'],
// [['age'], 'required', 'on' => 'modify_user'],
[['store_id'], 'required', 'on' => 'modify_user'],
// [['territory'], 'required', 'on' => 'modify_user'],
[['territory'], 'string', 'max' => 100, 'on' => 'modify_user'],
[['college'], 'required', 'on' => 'modify_user_buy'],
[['ball_age'], 'required', 'on' => 'modify_user_buy'],
[['address'], 'required', 'on' => 'modify_user_buy'],
[['arm_length'], 'required', 'on' => 'modify_user_buy'],
[['glove'], 'required', 'on' => 'modify_user_buy'],
[['palm_size'], 'required', 'on' => 'modify_user_buy'],
[['shoes_size'], 'required', 'on' => 'modify_user_buy'],
[['mobile_phone'], 'filter', 'filter' => 'trim', 'on' => 'user_personal'],
[['mobile_phone'], 'match', 'pattern' => '/^[1][34578][0-9]{9}$/', 'on' => 'user_personal'],
[['height'], 'required', 'on' => 'user_personal'],
[['weight'], 'required', 'on' => 'user_personal'],
[['age'], 'required', 'on' => 'user_personal'],
[['store_id'], 'required', 'on' => 'user_personal'],
[['territory'], 'required', 'on' => 'user_personal'],
[['territory'], 'string', 'max' => 100, 'on' => 'user_personal'],
[['college'], 'required', 'on' => 'user_personal'],
[['ball_age'], 'required', 'on' => 'user_personal'],
[['address'], 'required', 'on' => 'user_personal'],
[['arm_length'], 'required', 'on' => 'user_personal'],
[['glove'], 'required', 'on' => 'user_personal'],
[['palm_size'], 'required', 'on' => 'user_personal'],
[['shoes_size'], 'required', 'on' => 'user_personal'],
];
}
public function scenarios()
{
return [
'modify_user' => ['real_name', 'mobile_phone', 'height', 'weight', 'age', 'store_id', 'territory', 'price', 'member_status'],
'modify_user_buy' => ['college', 'ball_age', 'address', 'arm_length', 'glove', 'palm_size', 'shoes_size'],
'user_personal' => ['real_name', 'mobile_phone', 'height', 'weight', 'age', 'store_id', 'territory'
, 'college', 'ball_age', 'address', 'arm_length', 'glove', 'palm_size', 'shoes_size'],
];
}
/**
* 会员注册
*/
public function modify_user()
{
$userInformationModel = new UserInformation();
$userInformationData = UserInformation::findOne(['user_id' => $this->user_id, 'member_status' => 1]);
//有会员信息 该会员已注册
if ($userInformationData) {
return $this->apiReturnSuccess('会员已注册', $userInformationData);
}
if (!$this->validate()) {
return $this->getModelError();
}
//查user表用户信息 user
$user = $this->verifyUserData();
$t = \Yii::$app->db->beginTransaction();
try {
//user表添加真实姓名跟手机号
if ($this->real_name) {
$user->real_name = $this->real_name;
}
if (!$user->save()) {
$t->rollBack();
return $this->getModelError($user);
}
// 年龄 体重 身高 门店 所属地 学院 球龄 地址 臂长 手掌大小 手套尺寸 鞋码
$data['user_id'] = $this->user_id;
//user_information表添加其他信息
//体重
if ($this->weight) {
$data['weight'] = $this->weight;
}
//身高
if ($this->height) {
$data['height'] = $this->height;
}
//年龄
if ($this->age) {
$data['age'] = $this->age;
}
//门店
if ($this->store_id) {
$data['store_id'] = $this->store_id;
}
//所属地
if ($this->territory) {
$data['territory'] = $this->territory;
}
// todo 测试使用,未接通支付,直接变更为已支付,并且日期添加上
$data['member_status'] = 1;
$data['end_time'] = time() + 60 * 60 * 24 * 300;
$data['begin_time'] = time();
$data['created_at'] = time();
$data['member_status'] = $this->member_status;
$userInformationModel->attributes = $data;
if (!$userInformationModel->save()) {
return $this->getModelError($userInformationModel);
}
$t->commit();
return [
'code' => 0,
'msg' => '注册成功',
];
} catch (\Exception $e) {
$t->rollBack();
return [
'code' => 1,
'msg' => '注册失败',
];
}
}
/**
* 会员注册 填充
* @return array
*/
public function modify_user_buy()
{
if (!$this->validate()) {
return $this->getModelError();
}
$this->verifyUserData();
$userInformationModel = new UserInformation();
$userInformationData = UserInformation::findOne(['user_id' => $this->user_id]);
if (empty($userInformationData['user_id'])) {
return $this->apiReturnError('请先注册会员');
}
//有会员信息 该会员已注册
if ($userInformationData['ball_age']) {
return $this->apiReturnSuccess('会员已注册', $userInformationData, 1);
}
$userInformationData->college = $this->college;
$userInformationData->ball_age = $this->ball_age;
$userInformationData->address = $this->address;
$userInformationData->arm_length = $this->arm_length;
$userInformationData->glove = $this->glove;
$userInformationData->palm_size = $this->palm_size;
$userInformationData->shoes_size = $this->shoes_size;
$userInformationData->updated_at = time();
if (!$userInformationData->save()) {
return $this->getModelError($userInformationData);
}
return [
'code' => 0,
'msg' => '注册成功'
];
}
/** `
* 验证用户
* @return ApiHelper|array|null
*/
public function verifyUserData()
{
$user = ApiHelper::findOneUser($this->user_id, $this->cx_mch_id);
if ($user == null) {
return $this->apiReturnError('用户不存在', [], 1);
}
return $user;
}
/**
* 会员注册用户信息判断
* @return array
*/
public function verifyUserInfo()
{
$user = UserInformation::find()->alias('user_info')
->innerJoin(['user' => User::tableName()], 'user_info.user_id=user.id')
->where(['user_info.user_id' => $this->user_id])
->select('user_info.*,user.real_name,user.mobile_phone')
->asArray()->one();
if ($user['mobile_phone']) {
$user['mobile_phone'] = EncryptHelper::decryptMobilePhone($user['mobile_phone']);
}
if ($user['end_time']) {
$user['end_time'] = date('Y-m-d', $user['end_time']);
}
$user['member_status'] = $user['member_status'] == 0 ? 0 : 1;
$user['fill_status'] = empty($user['ball_age']) ? 0 : 1;
if ($user['store_id']) {
$store = Store::find()->where(['id' => $user['store_id']])->asArray()->one();
$user['store_name'] = $store['name'];
}
return [
'code' => 0,
'msg' => '会员信息',
'data' => $user
];
}
/**
* 会员信息填充
* @param $request
* @return array
*/
public function userPersonal($request)
{
if ($request->isGet) {
return $this->verifyUserInfo();
}
if ($request->isPost) {
if (!$this->validate()) {
return $this->getModelError();
}
$user = $this->verifyUserData();
$userInformationModel = UserInformation::findOne(['user_id' => $this->user_id]);
$data['updated_at'] = time();
if ($userInformationModel == null) {
$userInformationModel = new UserInformation();
$data['created_at'] = time();
}
//开启事务
$t = \Yii::$app->db->beginTransaction();
try {
//user表添加真实姓名跟手机号
if ($this->real_name) {
$user->real_name = $this->real_name;
}
if ($this->mobile_phone) {
// $user->mobile_phone = EncryptHelper::encryptMobilePhone($this->mobile_phone);
}
if (!$user->save())
return $this->getModelError($user);
// 年龄 体重 身高 门店 所属地 学院 球龄 地址 臂长 手掌大小 手套尺寸 鞋码
$data['user_id'] = $this->user_id;
//user_information表添加其他信息
//体重
if ($this->weight) {
$data['weight'] = $this->weight;
}
//身高
if ($this->height) {
$data['height'] = $this->height;
}
//年龄
if ($this->age) {
$data['age'] = $this->age;
}
//门店
if ($this->store_id) {
$data['store_id'] = $this->store_id;
}
//所属地
if ($this->territory) {
$data['territory'] = $this->territory;
}
//学院
if ($this->college) {
$data['college'] = $this->college;
}
//球龄
if ($this->ball_age) {
$data['ball_age'] = $this->ball_age;
}
//地址
if ($this->address) {
$data['address'] = $this->address;
}
//臂长
if ($this->arm_length) {
$data['arm_length'] = $this->arm_length;
}
//手套尺寸
if ($this->glove) {
$data['glove'] = $this->glove;
}
//手掌大小
if ($this->palm_size) {
$data['palm_size'] = $this->palm_size;
}
//鞋子大小
if ($this->shoes_size) {
$data['shoes_size'] = $this->shoes_size;
}
$userInformationModel->attributes = $data;
if (!$userInformationModel->save())
return $this->getModelError($userInformationModel);
$t->commit();
return [
'code' => 0,
'msg' => '成功',
'data' => []
];
} catch (\Exception $ex) {
$t->rollBack();
return [
'code' => 1,
'msg' => '失败',
'data' => null
];
}
}
return $this->apiReturnError('非法请求');
}
}