Browse Source

重新考虑规划商品逻辑和文件逻辑,将临时文件保存到文件中的方法,从商品逻辑文件转移到文件逻辑文件中

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
30e02bca7c
  1. 61
      backend/logic/file/FileManager.php
  2. 60
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php

61
backend/logic/file/FileManager.php

@ -3,6 +3,7 @@
namespace backend\logic\file;
use common\models\ars\File;
use common\models\ars\TemFile;
class FileManager
@ -48,4 +49,64 @@ class FileManager
}
return -1;
}
/**
* @param $imgIdStr
* @param $ownId
* @param $ownType
* @return array
* 根据临时文件id(字符串)将临时文件保存在文件中
*/
public function saveTemFileToFile($temFIleIdStr, $ownId, $ownType)
{
if(!$temFIleIdStr || !$ownId) {
return ['status' => false, 'info' => '参数错误'];
}
$img_id_arr = explode(',', $temFIleIdStr);
$i = 1;
$first_file_id = 0;
foreach ($img_id_arr as $key => $value) {
$tem_file = TemFile::findOne($key);
if(!$tem_file) {
return ['status' => false, 'info' => '存在查找不到的文件'];
}
$res = self::saveNewFile($tem_file, $ownId, $ownType);
if(!$res['status']) {
return ['status' => false, 'info' => '存在文件保存失败'];
}
if($i == 1) {
$first_file_id = $res['file_id'];
}
$i++;
}
return ['status' => true, 'info' => '保存成功', 'first_file_id' => $first_file_id];
}
/**
* @param $temFile
* @param $ownId
* @param $ownType
* @return bool
* 创建新的文件
*/
private function saveNewFile($temFile, $ownId, $ownType)
{
$new_file = new File();
$new_file->name = $temFile->name;
$new_file->type = $temFile->type;
$new_file->own_id = $ownId;
$new_file->own_type = $ownType;
$new_file->alias = $temFile->alias;
$new_file->path = $temFile->path;
if($new_file->save()) {
return ['status' => true, 'file_id' => $new_file->id];
} else {
return ['status' => false];
}
}
}

60
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -3,68 +3,8 @@
namespace antgoods\goods\logic\goods;
use common\models\ars\TemFile;
use common\models\ars\File;
class GoodsManager
{
/**
* @param $imgIdStr
* @param $ownId
* @param $ownType
* @return array
* 将临时文件保存在文件中
*/
public function saveTemFileToFile($imgIdStr, $ownId, $ownType)
{
if(!$imgIdStr || !$ownId) {
return ['status' => false, 'info' => '参数错误'];
}
$img_id_arr = explode(',', $imgIdStr);
$i = 1;
$first_file_id = 0;
foreach ($img_id_arr as $key => $value) {
$tem_file = TemFile::findOne($key);
if(!$tem_file) {
return ['status' => false, 'info' => '存在查找不到的文件'];
}
$res = self::saveNewFile($tem_file, $ownId, $ownType);
if(!$res['status']) {
return ['status' => false, 'info' => '存在文件保存失败'];
}
if($i == 1) {
$first_file_id = $res['file_id'];
}
$i++;
}
return ['status' => true, 'info' => '保存成功', 'first_file_id' => $first_file_id];
}
/**
* @param $temFile
* @param $ownId
* @param $ownType
* @return bool
* 创建新的文件
*/
private function saveNewFile($temFile, $ownId, $ownType)
{
$new_file = new File();
$new_file->name = $temFile->name;
$new_file->type = $temFile->type;
$new_file->own_id = $ownId;
$new_file->own_type = $ownType;
$new_file->alias = $temFile->alias;
$new_file->path = $temFile->path;
if($new_file->save()) {
return ['status' => true, 'file_id' => $new_file->id];
} else {
return ['status' => false];
}
}
}
Loading…
Cancel
Save