Files
xk-api-yii/admin/models/RegisterForm.php

244 lines
8.8 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
namespace admin\models;
use common\core\BaseModel;
use common\enums\UserRoleEnum;
use common\helpers\FuncHelper;
use common\models\oldDb\CashAccount;
use common\models\oldDb\Log;
use common\validators\MobileValidator;
use yii\db\Exception;
use yii\helpers\Json;
class RegisterForm extends BaseModel
{
public $username;
public $mobile;
public $password;
public $rememberMe;
public $drugstore;
public $role;
public $store_id;
public $bank_user_name;
public $bank_card;
public $bank_name;
public $bank_account_type;
public $bank_no;
public $province_id;
public $city_id;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'mobile', 'password', 'role'], 'required'],
['mobile', MobileValidator::class],
['rememberMe', 'boolean'],
[['bank_account_type','province_id','city_id'], 'integer'],
[['bank_user_name', 'bank_card', 'bank_name', 'bank_no'], 'string'],
['store_id', 'required', 'when' => function ($model) {
return $this->role == UserRoleEnum::STORE_ADMIN;
}],
['province_id', 'required', 'when' => function ($model) {
return $this->role == UserRoleEnum::PROVINCE_DAI;
}],
[['province_id','city_id'], 'required', 'when' => function ($model) {
return $this->role == UserRoleEnum::CITY_DAI;
}],
];
}
public function attributeLabels()
{
return [
'username'=>'用户名',
'mobile'=>'电话',
'password'=>'密码',
'role'=>'角色',
'province_id'=>'省份',
'city_id'=>'城市',
'store_id'=>'门店',
'bank_user_name'=>'开户人姓名',
'bank_card'=>'银行卡号',
'bank_name'=>'银行名称',
'bank_account_type'=>'银行账户类型 1对公2对私5存折',
'bank_no'=>'银行联行号bank_account_type=1或5或非62开头的对私银行账户时必选',
];
}
public function register()
{
if (!$this->validate()) {
throw new \yii\base\Exception($this->getErrorMsg());
}
$is_admin = Admin::find()->where([
'mobile' => $this->mobile,
'is_delete' => 0
])->one();
if ($is_admin) throw new \yii\base\Exception('该账号已注册过');
if ($this->role == UserRoleEnum::STORE_ADMIN) {
if (empty($this->store_id)) throw new Exception('门店不能为空');
$store= \common\models\oldDb\Store::find()->where(['id'=>$this->store_id])->one();
if (!$store){
throw new Exception('门店不存在!!');
}
}
if ($this->role == UserRoleEnum::SUPER_ADMIN) {
$SUPER_ADMIN = Admin::find()->where([
'role' => UserRoleEnum::SUPER_ADMIN,
])->one();
if ($SUPER_ADMIN) throw new Exception('超管已经存在');
}
if($this->role == UserRoleEnum::SUPPLY){
$code= FuncHelper::create_invite_code();
}
$t = \Yii::$app->db->beginTransaction();
try {
$model = new Admin();
$model->attributes = $this->attributes;
$model->setPassword($this->password);
$model->role = $this->role;
$model->code = $code??'';
$model->province_id= $this->province_id??$store->province_id;
$model->city_id = $this->city_id??$store->city_id;
$model->store_id = $this->store_id ?? '';
$model->saveOrFail();
if ($this->role == UserRoleEnum::SUPER_ADMIN) {
//可提现账户表
$CashAccount = new \common\models\oldDb\CashAccount();
$CashAccount->user_id = 0;
$CashAccount->user_type = 2;//平台
$CashAccount->total_cash = 0;//累计收益
$CashAccount->able_cash = 0;//账户余额
$CashAccount->frozen_cash = 0;//申请提现冻结的金额
$CashAccount->withdrawn_cash = 0;//已提现金额
$CashAccount->wait_cash = 0;//待结算收益
$CashAccount->charge_cash = 0;//手续费
$CashAccount->last_apply_time = date('Y-m-d H:i:s', time());
$CashAccount->saveOrFail();
$log = new \common\models\oldDb\Log();
$log->admin_id = $model->attributes['uid'];
$log->operate_time = date('Y-m-d H:i:s', time());
$log->type = '用户注册';
$log->mold = 0;//注册操作
$log->content = '用户:' . $this->username . '注册成功,用户ID' . $model->attributes['uid'] . ',手机号:' . $this->mobile;
$log->save();
\Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute();
}
if ($this->role == UserRoleEnum::STORE_ADMIN) {
//可提现账户表
$CashAccount = new CashAccount();
$CashAccount->user_id = $this->store_id;
$CashAccount->user_type = 1;
$CashAccount->total_cash = 0;//累计收益
$CashAccount->able_cash = 0;//账户余额
$CashAccount->frozen_cash = 0;//申请提现冻结的金额
$CashAccount->withdrawn_cash = 0;//已提现金额
$CashAccount->wait_cash = 0;//待结算收益
$CashAccount->charge_cash = 0;//手续费
$CashAccount->last_apply_time = date('Y-m-d H:i:s', time());
$CashAccount->saveOrFail();
$log = new Log();
$log->admin_id = \Yii::$app->user->getId();
$log->operate_time = date('Y-m-d H:i:s', time());
$log->type = '用户注册';
$log->mold = 0;//注册操作
$log->content = '用户:' . $this->username . '注册成功,用户ID' . $model->attributes['uid'] . ',手机号:' . $this->mobile . '诊所ID' . $this->store_id;
$log->save();
\Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute();
}
$token = \common\models\oldDb\AdminAccessToken::createToken($model->id, 'admin');
$t->commit();
return [
'token' => $token,
'user' => Admin::findOne($model->id),
];
} catch (Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
public function EditStore()
{
$admin = \Yii::$app->user->identity;
$Store = \common\models\oldDb\Store::find()->where(['id' => $this->store_id])->one();
if (!$Store) throw new Exception('诊所不存在');
if (!empty($this->bank_account_type)){
if ($this->bank_account_type== 1 || $this->bank_account_type == 5) {
if (empty($this->bank_no)) {
throw new Exception('bank_no不能为空');
}
} else {
if (substr($this->bank_card, 0, 2) != '62') {
if (empty($this->bank_no)) {
throw new Exception('bank_no不能为空');
}
}
}
}
$Store->bank_user_name = $this->bank_user_name;
$Store->bank_card = $this->bank_card;
$Store->bank_name = $this->bank_name;
$Store->bank_account_type = $this->bank_account_type;
$Store->bank_no = $this->bank_no;
$Store->saveOrFail();
return ['编辑成功'];
}
public function EditSuper()
{
$admin = \Yii::$app->user->identity;
$Admin=Admin::find()->where(['uid'=>$admin->getId()])->one();
if (!$Admin) throw new Exception('超管信息不存在');
if (!empty($this->bank_account_type)){
if ($this->bank_account_type== 1 || $this->bank_account_type == 5) {
if (empty($this->bank_no)) {
throw new Exception('bank_no不能为空');
}
} else {
if (substr($this->bank_card, 0, 2) != '62') {
if (empty($this->bank_no)) {
throw new Exception('bank_no不能为空');
}
}
}
}
$Admin->bank_user_name = $this->bank_user_name;
$Admin->bank_card = $this->bank_card;
$Admin->bank_name = $this->bank_name;
$Admin->bank_account_type = $this->bank_account_type;
$Admin->bank_no = $this->bank_no;
$Admin->saveOrFail();
return ['编辑成功'];
}
}