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

187 lines
6.2 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年9月13日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\auth;
use app\components\EncryptHelper;
use app\models\auth\Role;
use app\models\auth\RoleUser;
use app\models\auth\RolePermission;
use app\models\common\CommonUserEditForm;
use app\models\User;
use app\models\SysAdmin;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonRoleUserEditForm extends Model
{
public $username;
public $password;
public $nickname;
public $type;
public $role_ids;
public $model;
public $cx_mch_id;
public $creator_user_id;
public $mobile_phone;
public function rules()
{
return [
[['username', 'password', 'nickname'], 'trim'],
[['username', 'password', 'nickname'], 'string'],
[['password'], 'string', 'min' => 8],
[['cx_mch_id', 'creator_user_id', 'type'], 'integer'],
[['role_ids', 'model'], 'safe'],
[['role_ids', 'model', 'nickname', 'creator_user_id', 'type','mobile_phone'], 'required'],
[['username', 'password','mobile_phone'], 'required', 'on' => 'add'],
];
}
public function attributeLabels()
{
return [
'cx_mch_id' => '平台商户ID',
'username' => '用户名',
'password' => '密码',
'nickname' => '昵称',
'mobile_phone' => '手机号',
];
}
public function save()
{
if(!$this->validate()){
return $this->getModelError();
}
$t = \Yii::$app->db->beginTransaction();
$form = new CommonUserEditForm();
$find_ = User::findOne([
'username' => $this->username,
]);
if(!empty($find_)){
if(!empty($this->model) && $this->model->id == $find_->id){
}else{
return $this->apiReturnError("用户名已存在,请更换");
}
}
if($this->model->isNewRecord){
$form->scenario = 'add';
// $form->type = $this->type;
$form->avatar_url = User::DEFAULT_AVATAR_URL;
$form->access_token = \Yii::$app->security->generateRandomString();
$form->is_modify_un = 0;
$user = User::findOne(['mobile_phone' => $this->mobile_phone,'is_delete' => 0]);
$name_msg = "";
if($user != null){
if($user->type != 0){
if($user->type == '2'){
$name_msg = '门店人员';
}elseif ($user->type == '6'){
$name_msg = '员工';
}elseif ($user->type == '7'){
$name_msg = 'BOSS账号';
}elseif ($user->type == '8'){
$name_msg = '总部管理员';
}
return $this->apiReturnError($this->mobile_phone.'已成为'.$name_msg.'请更换手机号');
}else{
$this->model = $user;
}
}
}
$role = Role::findOne(['id' => $this->role_ids,'is_delete' => 0]);
if($role == null){
return $this->apiReturnError("选择角色不存在");
}
if($role->name == 'BOSS账号'){
$this->type = User::TYPE_BOSS_STAFF;
}elseif ($role->name == '总部管理员'){
$this->type = User::TYPE_ADMIN_STAFF;
}
$form->type = $this->type;
$form->cx_mch_id = $this->cx_mch_id;
$form->mobile_phone = $this->mobile_phone;
$form->model = $this->model;
if(!empty($this->password)){
$form->password = \Yii::$app->security->generatePasswordHash($this->password);
}
$form->username = $this->username;
$form->nickname = $this->nickname;
$res = $form->save();
if($res['code'] != 0){
$t->rollBack();
return $this->apiReturnError($res['msg']);
}
$user_id = $res['data']['user_id'];
//保存角色
$res = $this->saveUserRole($user_id, $this->role_ids, $this->cx_mch_id);
if($res['code'] != 0){
$t->rollBack();
return $this->apiReturnError($res['msg']);
}
//保存系统管理员
$res = $this->saveSysAdmin($user_id, $this->creator_user_id, $this->cx_mch_id);
if($res['code'] != 0){
$t->rollBack();
return $this->apiReturnError($res['msg']);
}
$t->commit();
return $this->apiReturnSuccess("保存成功");
}
private function saveUserRole($user_id, $role_ids, $cx_mch_id = 0)
{
if(empty($this->role_ids)){
return $this->apiReturnError("角色不能为空");
}
RoleUser::updateAll(['is_delete' => 1], ['is_delete' => 0, 'user_id' => $user_id, 'cx_mch_id' => $cx_mch_id]);
$role_user = RoleUser::findOne(['user_id' => $user_id, 'role_id' => $role_ids]);
if($role_user == null){
$role_user = new RoleUser();
$role_user->cx_mch_id = $cx_mch_id;
$role_user->user_id = $user_id;
$role_user->role_id = $role_ids;
}
$role_user->is_delete = 0;
if(!$role_user->save()){
return $this->getModelError($role_user);
}
return $this->apiReturnSuccess();
}
private function saveSysAdmin($user_id,$creator_user_id, $cx_mch_id)
{
$sys_admin = SysAdmin::findOne(['user_id' => $user_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0]);
if($sys_admin == null){
$sys_admin = new SysAdmin();
$sys_admin->user_id = $user_id;
$sys_admin->cx_mch_id = $cx_mch_id;
$sys_admin->creator_user_id = $creator_user_id;
}
$sys_admin->is_delete = 0;
if(!$sys_admin->save()){
return $this->getModelError($sys_admin);
}
return $this->apiReturnSuccess();
}
}