cxfoot/modules/store/models/storeUser/StoreUserEditForm.php
2023-10-24 14:54:18 +08:00

294 lines
10 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 2020-11-5
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\store\models\storeUser;
use app\components\EncryptHelper;
use app\models\Admin;
use app\models\auth\RoleUser;
use app\models\common\CommonUserEditForm;
use app\models\Store;
use app\models\StoreUser;
use app\models\SysAdmin;
use app\models\User;
use app\modules\store\models\AdminModel;
use yii\data\Pagination;
class StoreUserEditForm extends AdminModel
{
public $cx_mch_id;
public $creator_user_id;
public $model;
public $user_type;
public $username;
public $password;
public $mobile_phone;
public $real_name;
public $store_id;
public $status;
public $user_id;
public $created_at;
public $updated_at;
public $is_delete;
public $deleted_at;
public function rules()
{
return [
[['cx_mch_id','user_type','status', 'created_at', 'updated_at', 'is_delete', 'deleted_at','user_id','mobile_phone','store_id','creator_user_id'], 'integer'],
[['role_ids', 'model'], 'safe'],
[['username'], 'string', 'max' => 64],
[['password'], 'string', 'max' => 255],
[['user_type', 'mobile_phone', 'real_name','store_id'], 'required'],
];
}
public function attributeLabels()
{
return [
'user_type' => '门店名称',
'username' => '登录账号(后台)',
'password' => '登录密码(后台)',
'mobile_phone' => '手机号',
'real_name' => '真实姓名',
'status' => '人员状态',
'created_at' => '添加时间',
'updated_at' => '修改时间',
'is_delete' => '是否删除0=否1=是',
'deleted_at' => '删除时间',
'store_id' => '所属门店',
];
}
public function edit()
{
if(!$this->validate()){
return $this->getModelError();
}
$t = \Yii::$app->db->beginTransaction();
$is_meta = false;
if($this->model->isNewRecord){
$this->model->is_delete = 0;
$this->model->deleted_at = 0;
$this->model->created_at = time();
$is_meta = true;
if(empty($this->username) && $this->user_type != '2'){
return $this->apiReturnError('请设置登录账号(后台)');
}
if(empty($this->password) && $this->user_type != '2'){
return $this->apiReturnError('请设置登录密码(后台)');
}
}
$meta = self::saveMeta($is_meta);
if($meta['code'] != 0){
$t->rollBack();
return $meta;
}
$this->model->updated_at = time();
$this->model->user_id = $this->user_id;
$this->model->store_id = $this->store_id;
$this->model->username = $this->username;
if(!empty($this->password)){
$this->model->password = \Yii::$app->security->generatePasswordHash($this->password);
}
$this->model->user_type = $this->user_type;
$this->model->status = $this->status;
if(!$this->model->save()){
$t->rollBack();
return $this->getModelError($this->model);
}
if($this->user_type != '2' && $this->user_type != '3'){
//保存系统管理员权限
$res = $this->saveSysAdmin($this->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 saveMeta($is_meta)
{
if($is_meta){
//保存user
$s_u = self::saveUser();
if($s_u['code'] != 0){
return $s_u;
}
}else{
//根据门店id找到上次对应的user_id
$s_u = StoreUser::findOne(['id' => $this->model->id,'is_delete' => 0,'user_type' => $this->user_type]);
if($s_u == null){
return ['code' => 1,'msg' => '此人员权限异常'];
}
$s_s_u_id = $s_u->user_id;
$this->user_id = $s_u->user_id;
//根据user_id获取用户数据
$user = User::findOne(['id' => $s_s_u_id,'is_delete' => 0]);
if($user == null){
return ['code' => 1,'msg' => '权限数据不存在于此人员'];
}
//上次用户的手机号与本次是否一致(区分是否同一人管理)
$encrypted_mobile = EncryptHelper::encryptMobilePhone($this->mobile_phone);
if($user->mobile_phone != $encrypted_mobile){
//不是同一手机号则查找本次手机号是否存在用户,不存在则创建
$user_s = User::findOne(['mobile_phone' => $encrypted_mobile,'is_delete' => 0]);
if($user_s == null){
$s_u = self::saveUser();
if($s_u['code'] != 0){
return $s_u;
}
$user_id = $s_u['data']['user_id'];
}else{
$user_id = $user_s->id;
}
}else{
$user_id = $user->id;
}
if($s_s_u_id != $user_id){
//上次手机号与本次不一致,若原管理员不为其他门店管理员则删除权限
$last_exists = StoreUser::find()->where(['user_id' => $s_s_u_id,'is_delete' => 0,'user_type' => $this->user_type])
->andWhere(['!=','store_id',$this->model->id])->exists();
if(!$last_exists && $this->user_type != '2' && $this->user_type != '3'){
$admin = \Yii::$app->db->createCommand()->update(Admin::tableName(), ['is_delete' => 1], ['user_id' => $s_s_u_id,'is_delete' => 0])->execute();
if(!$admin){
return ['code' => 1,'msg' => '重置权限失败'];
}
}
$this->user_id = $user_id;
}else{
$user->real_name = $this->real_name;
if(!$user->save()){
return ['code' => 1,'msg' => '姓名修改失败'];
}
}
}
return ['code' => 0,'msg' => '操作成功'];
}
//保存user获取user_id
private function saveUser()
{
$encrypted_mobile = EncryptHelper::encryptMobilePhone($this->mobile_phone);
$user = User::findOne(['mobile_phone' => $encrypted_mobile,'is_delete' => 0]);
if($user != null && in_array($user->type,[1,2,6,7,8])){
return $this->apiReturnError('此手机号已拥有其他身份,请更换手机号');
}
$username = empty($user) ? '' : $user->username;
if($user == null)
$user = new User();
$username = User::generateUsername();
$form = new CommonUserEditForm();
$form->scenario = 'store_add';
$form->model = $user;
$form->cx_mch_id = 0;
$form->username = $username;
$form->password = \Yii::$app->security->generatePasswordHash($this->password);
$form->real_name = $this->real_name;
$form->nickname = $username;
$form->avatar_url = User::DEFAULT_AVATAR_URL;
$form->access_token = \Yii::$app->security->generateRandomString();
$form->is_modify_un = 1;
$form->type = User::TYPE_STORE;
$form->mobile_phone = $this->mobile_phone;
$form->mobile_prefix = '86';
$res = $form->save();
if($res['code'] != 0){
return $res;
}
$this->user_id = $res['data']['user_id'];
return $this->apiReturnSuccess('ok',['user_id' => $res['data']['user_id']]);
}
//保存store_user根据store_id获取user_id
private function saveStoreUser()
{
$form = StoreUser::findOne(['store_id' => $this->model->id,'is_delete' => 0,'user_type' => 1]);
if($form == null)
$store_user = StoreUser::find()->where(['username' => $this->username,'is_delete' => 0])->exists();
if($store_user)
return ['code' => 1,'msg' => '登录账号已被占用,请更换'];
$form = new StoreUser();
$form->store_id = $this->model->id;
$form->created_at = time();
$form->is_delete = 0;
$form->deleted_at = 0;
$form->status = 0;
$form->user_type = 1;
$form->user_id = $this->user_id;
$form->username = $this->username;
$form->password = \Yii::$app->security->generatePasswordHash($this->password);
$form->updated_at = time();
if(!$form->save())
return $this->getModelError($form);
return ['code' => 0,'msg' => 'ok','data' => ['user_id' => $form->user_id]];
}
private function saveUserRole($user_id, $role_ids, $cx_mch_id = 0)
{
if(empty($role_ids)){
return $this->apiReturnError("角色不能为空");
}
RoleUser::updateAll(['is_delete' => 1], ['is_delete' => 0, 'user_id' => $user_id, 'cx_mch_id' => $cx_mch_id]);
foreach ($role_ids as $role_id){
$role_user = RoleUser::findOne(['user_id' => $user_id, 'role_id' => $role_id]);
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_id;
}
$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();
}
}