dlh_test_server/app/api/logic/LocalStoreGoodsLogic.php

157 lines
4.7 KiB
PHP
Raw Normal View History

2024-08-22 13:59:35 +08:00
<?php
namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\localstore\LocalStoreGoods;
use app\common\model\localstore\LocalStoreGoodsPic;
2024-08-22 17:20:33 +08:00
use app\common\enum\LocalStoreFileEnum;
use think\facade\Config;
2024-08-22 13:59:35 +08:00
class LocalStoreGoodsLogic extends Logic
{
2024-08-23 10:33:31 +08:00
private static $SHELVES = 1;
2024-08-23 09:31:23 +08:00
// 获取商品列表
2024-08-22 14:11:04 +08:00
public static function lists($get)
{
$list = LocalStoreGoods::with('pic')
->field('id, lstore_id, title, price')
2024-08-22 14:11:04 +08:00
->where([
'user_id' => $get['user_id'],
'lstore_id' => $get['lstore_id'],
'state' => $get['type'],
'delete_time' => 0,
2024-08-22 14:11:04 +08:00
])
->order('id', 'desc')
2024-08-22 14:11:04 +08:00
->page($get['page_no'], $get['page_size'])
->select()
->toArray();
2024-08-22 14:11:04 +08:00
$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) {
2024-08-23 10:44:34 +08:00
$v1['url'] = Config::get('aliyunoss.domain') . $v1['url'];
}
2024-08-22 17:20:33 +08:00
}
2024-08-22 14:11:04 +08:00
$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']
];
2024-08-22 14:11:04 +08:00
return $result;
}
2024-08-23 09:31:23 +08:00
// 添加商品
2024-08-23 10:10:57 +08:00
public static function saveGoods($form, $user_id)
2024-08-22 13:59:35 +08:00
{
try {
2024-08-23 10:33:31 +08:00
$len = strlen(Config::get('aliyunoss.domain'));
2024-08-22 13:59:35 +08:00
2024-08-23 10:33:31 +08:00
if ($form['type'] === 'add') {
$id = LocalStoreGoods::insertGetId([
'user_id' => $user_id,
'lstore_id' => $form['lstore_id'],
'title' => $form['title'],
'price' => $form['price'],
2024-08-23 10:34:41 +08:00
'desc' => isset($form['desc']) ?? '',
'details' => isset($form['details']) ?? '',
2024-08-23 10:33:31 +08:00
'state' => self::$SHELVES,
'create_time' => time(),
]);
2024-08-23 10:52:18 +08:00
// 处理图片
$data = [];
foreach ($form['url'] as $k => &$v) {
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = substr($v, $len, strlen($v));
}
2024-08-23 10:33:31 +08:00
} else if ($form['type'] === 'edit') {
LocalStoreGoods::where(['id' => $form['id']])->update([
'title' => $form['title'],
'price' => $form['price'],
2024-08-23 10:48:33 +08:00
'desc' => isset($form['desc']) ?? '',
'details' => isset($form['details']) ?? '',
2024-08-23 10:33:31 +08:00
'update_time' => time(),
]);
LocalStoreGoodsPic::where(['id' => $form['id']])->delete();
2024-08-22 13:59:35 +08:00
2024-08-23 10:52:18 +08:00
// 处理图片
$data = [];
foreach ($form['url'] as $k => &$v) {
$data[$k]['lsg_id'] = $form['id'];
$data[$k]['url'] = substr($v['url'], $len, strlen($v['url']));
}
2024-08-23 10:10:57 +08:00
}
2024-08-23 10:52:55 +08:00
2024-08-23 10:10:57 +08:00
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) {
2024-08-22 13:59:35 +08:00
$data[$k]['lsg_id'] = $id;
$data[$k]['url'] = basename($v);
}
LocalStoreGoodsPic::insertAll($data);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
2024-08-23 09:31:23 +08:00
// 获取商品详情
public static function details($get)
{
2024-08-23 10:02:53 +08:00
$data = LocalStoreGoods::with('pic')
2024-08-23 09:31:23 +08:00
->where([
'id' => $get['id'],
'user_id' => $get['user_id'],
'lstore_id' => $get['lstore_id'],
'delete_time' => 0,
2024-08-23 09:36:21 +08:00
])->find()->toArray();
2024-08-23 10:02:53 +08:00
if ($data) {
foreach ($data['pic'] as &$v) {
2024-08-23 10:43:36 +08:00
$v['url'] = Config::get('aliyunoss.domain') . $v['url'];
2024-08-23 10:02:53 +08:00
}
return $data;
}
return false;
2024-08-23 09:31:23 +08:00
}
2024-08-22 13:59:35 +08:00
}