Пример #1
0
 /**
  * 添加管理员
  */
 public function addAdmin()
 {
     try {
         $session = Yii::$app->getSession();
         $now = time();
         $ip = Yii::$app->util->getClientIP();
         $salt = random(6);
         if (!empty($_FILES[self::ADMIN_BASE_INFO]['name']['avatar'])) {
             $pathName = $this->upload('uploads/', $this);
             if (!$pathName) {
                 return ['error' => '图片格式不符'];
             }
         }
         // 手机号和密码不能为空
         if (empty($this->mobile) || empty($this->password)) {
             return ['error' => '手机号和密码不能为空'];
         }
         $util = new Util();
         // 判断手机格式
         if (!$util->regularMobile($this->mobile)) {
             return ['error' => '手机格式不符'];
         }
         // 判断密码格式
         if (!Yii::$app->util->regularPassword($this->password)) {
             return ['error' => '密码格式不符'];
         }
         // 判断邮箱格式
         if (!$util->regularEmail($this->email) && !empty($this->email)) {
             return ['error' => '邮箱格式不符'];
         }
         // 判断部门长度
         if (strlen($this->department) > 30) {
             return ['error' => '部门名称过长'];
         }
         // 判断管理员名格式
         if (!empty($this->adminname) && !Yii::$app->util->regularUserName($this->adminname)) {
             return ['error' => '管理员名格式不符'];
         }
         $this->gender ? $this->gender : ($this->gender = self::ADMIN_GENDER);
         $this->role_id ? $this->role_id : ($this->role_id = self::ADMIN_ROLE_ID);
         $this->status ? $this->status : ($this->status = self::ADMIN_NORMAL_STATUS);
         if (self::getAdminById($session[self::SESSION_KEY_ADMIN]['id'])['role_id'] != 0) {
             $this->role_id = self::getAdminById($session[self::SESSION_KEY_ADMIN]['id'])['role_id'];
         }
         $this->password = md5(md5($this->password) . $salt);
         $this->password_salt = $salt;
         $this->create_ip = $ip;
         $this->create_id = $session[self::SESSION_KEY_ADMIN]['id'];
         $this->create_time = $now;
         $this->update_time = $now;
         return $this->save() ? $this->id : ['error' => '系统错误,请稍后'];
     } catch (Exception $e) {
         return ['error' => '网络繁忙,请稍后'];
     }
 }
Пример #2
0
 /**
  * Updates an existing UserBaseInfo model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $this->layout = 'index';
     $model = $this->findModel($id);
     $session = Yii::$app->getSession();
     if ($model->load(Yii::$app->request->post())) {
         // 判断手机号格式
         $util = new Util();
         // if (!$util->regularMobile($model->mobile)) {
         //     return $this->render('update', [
         //         'model' => $model,
         //         'error' => '手机号格式不正确',
         //     ]);
         // }
         // 判断邮箱格式
         if (!$util->regularEmail($model->email) && !empty($model->email)) {
             return $this->render('update', ['model' => $model, 'error' => '邮箱格式不正确']);
         }
         if (strlen($model->company) > 50) {
             return $this->render('update', ['model' => $model, 'error' => '公司名称过长']);
         }
         if (strlen($model->position) > 90) {
             return $this->render('update', ['model' => $model, 'error' => '就任职位名过长']);
         }
         // if (!empty($model->username) && !Yii::$app->util->regularUserName($model->username)) {
         //     return $this->render('update', [
         //         'model' => $model,
         //         'error' => '用户名格式不符',
         //     ]);
         // }
         $model->update_time = time();
         // 上传图片
         if (!empty($_FILES[UserBaseInfo::USER_BASE_INFO]['name']['avatar'])) {
             $types = ['jpg', 'png', 'gif'];
             $adminPath = "uploads/";
             // 图片储存路径
             $avatar = UploadedFile::getInstance($model, 'avatar');
             $ext = $avatar->getExtension();
             if (!in_array($ext, $types)) {
                 return $this->render('update', ['model' => $model, 'error' => '上传图片格式不符']);
             }
             $adminName = time() . rand(1000, 9999) . "." . $ext;
             $path = abs(crc32($model->mobile) % 500);
             // 多项式加密取绝对值
             $adminPath .= $path . "/";
             if (!file_exists($adminPath)) {
                 mkdir($adminPath, 0777, true);
             }
             $avatar->saveAs($adminPath . $adminName);
             $model->avatar = $adminPath . $adminName;
         } else {
             unset($model->avatar);
         }
         if (empty($model->gender)) {
             $model->gender = UserBaseInfo::USER_GENDER_CONFIDENTIAL;
         }
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('update', ['model' => $model, 'error' => '网络繁忙,操作数据失败请稍后']);
         }
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }