public function actionUpload()
 {
     $params = \Yii::$app->request->post();
     $name = $params['name'];
     $filetype = $params['filetype'];
     if (\Yii::$app->request->isPost) {
         $model = new Uploadform();
         $model->setFileTableName($model::TABLE_NAME_SHOP_CONFIG);
         $model->file = UploadedFile::getInstance($model, $name);
         if ($model->file) {
             $data = array();
             $data[$model->formName()]['table_name'] = $model->getFileTableName();
             $data[$model->formName()]['file_type'] = $filetype ?: $model::FILE_TYPE_IMAGE;
             $data[$model->formName()]['file_ext'] = $model->file->getExtension();
             if ($id = $model->create($data)) {
                 $file = $model->file_dir . $model->file_name;
                 $file = \Yii::getAlias($file);
                 $rs = $model->file->saveAs($file);
                 $data = ['file_name' => $model->file_name, 'img_src' => getImageHost() . $model->getDir() . $model->file_name];
                 returnJson(1, '成功!', $data);
             } else {
                 $errors = $model->getFirstErrors();
                 $errorInfo = array_shift($errors);
                 returnJson(0, '失败!' . $errorInfo);
             }
             return;
         }
     }
     returnJson(0, '失败,请选择要上传的文件!');
 }
Ejemplo n.º 2
0
 public static function getIdByFileName($fileName, $tableName)
 {
     $uploadId = 0;
     $data = Uploadform::find()->where(['file_name' => $fileName, 'table_name' => $tableName])->select('upload_id')->one();
     if ($data && ($uploadId = $data['upload_id'])) {
         return $uploadId;
     }
     return $uploadId;
 }
Ejemplo n.º 3
0
 /**
  * //处理表单提交
  * @param $post
  */
 public function postDataHandle($post)
 {
     $valueArr = $post['value'];
     $uploadArr = $post['UploadForm'];
     $invoiceTypeValue = array('type' => $post['invoice_type'], 'rate' => $post['invoice_rate']);
     $uploadForm = new Uploadform();
     $uploadForm->setFileTableName(Uploadform::TABLE_NAME_SHOP_CONFIG);
     //更新上传文件缓存表
     foreach ($uploadArr as $k => $file_name) {
         $column_value = null;
         if ($file_name && ($column_value = array_search($file_name, $valueArr))) {
             $uploadId = Uploadform::getIdByFileName($file_name, Uploadform::TABLE_NAME_SHOP_CONFIG);
             $uploadForm->updateColumnValue($uploadId, $column_value);
         }
     }
     //保存Shop_config表
     foreach ($valueArr as $id => $value) {
         $modle = $this->findOne($id);
         if ($modle->value == $value) {
             continue;
         }
         //图片路径
         if (in_array($value, array_values($uploadArr))) {
             $modle->store_dir = $uploadForm->getDir();
         }
         $modle->value = $value;
         $modle->save();
     }
     //保存税率
     $dataInvoiceType = $modle->findOne(['code' => 'invoice_type']);
     $invoiceTypeValue = serialize($invoiceTypeValue);
     if ($dataInvoiceType->value != $invoiceTypeValue) {
         $dataInvoiceType->value = $invoiceTypeValue;
         $dataInvoiceType->save();
     }
 }