dlh_test_server/app/api/logic/LocalStoreGoodsLogic.php
2024-08-23 10:24:45 +08:00

134 lines
3.9 KiB
PHP

<?php
namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\localstore\LocalStoreGoods;
use app\common\model\localstore\LocalStoreGoodsPic;
use app\common\enum\LocalStoreFileEnum;
use think\facade\Config;
class LocalStoreGoodsLogic extends Logic
{
// 获取商品列表
public static function lists($get)
{
$list = LocalStoreGoods::with('pic')
->field('id, lstore_id, title, price')
->where([
'user_id' => $get['user_id'],
'lstore_id' => $get['lstore_id'],
'state' => $get['type'],
'delete_time' => 0,
])
->order('id', 'desc')
->page($get['page_no'], $get['page_size'])
->select()
->toArray();
$count = LocalStoreGoods::where([
'user_id' => $get['user_id'],
'lstore_id' => $get['lstore_id'],
'state' => $get['lstore_id'],
'delete_time' => 0,
])->count();
foreach ($list as &$v) {
foreach ($v['pic'] as &$v1) {
$v1['url'] = Config::get('aliyunoss.domain') . LocalStoreFileEnum::$APP_UPLOAD_PATH['local_shop_add'] . '/' . $get['user_id'] . '_' . $get['lstore_id'] . '/' . $v1['url'];
}
}
$result = [
'count' => $count,
'lists' => $list,
'more' => is_more($count, $get['page_no'], $get['page_size']),
'page_no' => $get['page_no'],
'page_size' => $get['page_size']
];
return $result;
}
// 添加商品
public static function saveGoods($form, $user_id)
{
try {
$form['user_id'] = $user_id;
$form['create_time'] = time();
$form['state'] = 1;
$pic = $form['url'];
unset($form['url']);
// $id = LocalStoreGoods::insertGetId($form);
$data = [];
$len = strlen(Config::get('aliyunoss.domain'));
foreach ($pic as $k => &$v) {
dump(substr($v, $len, 0));
// $data[$k]['lsg_id'] = $id;
// $data[$k]['url'] = basename($v);
}
die;
LocalStoreGoodsPic::insertAll($data);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function editGoods($form, $user_id)
{
try {
$form['user_id'] = $user_id;
$form['update'] = time();
$form['state'] = 1;
$pic = $form['url'];
unset($form['url']);
$id = LocalStoreGoods::update([
'title' => $form['title'],
'price' => $form['price'],
'desc' => $form['desc'],
'details' => $form['details'],
]);
$data = [];
foreach ($pic as $k => &$v) {
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = basename($v);
}
LocalStoreGoodsPic::insertAll($data);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
// 获取商品详情
public static function details($get)
{
$data = LocalStoreGoods::with('pic')
->where([
'id' => $get['id'],
'user_id' => $get['user_id'],
'lstore_id' => $get['lstore_id'],
'delete_time' => 0,
])->find()->toArray();
if ($data) {
foreach ($data['pic'] as &$v) {
$v['url'] = Config::get('aliyunoss.domain') . LocalStoreFileEnum::$APP_UPLOAD_PATH['local_shop_add'] . '/' . $get['user_id'] . '_' . $get['lstore_id'] . '/' . $v['url'];
}
return $data;
}
return false;
}
}