100 lines
3.1 KiB
PHP
100 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Models\ProductInformations;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Illuminate\Support\Facades\Redis;
|
||
|
|
|
||
|
|
class ProductInformationController extends Controller
|
||
|
|
{
|
||
|
|
// 新增、更新产品动态
|
||
|
|
public function addInfo(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->id;
|
||
|
|
$title = $request->title;
|
||
|
|
$content = $request->content;
|
||
|
|
$pic = $request->pic;
|
||
|
|
$is_hot = $request->is_hot;
|
||
|
|
if($id) {
|
||
|
|
$information = ProductInformations::find($id);
|
||
|
|
if(!$information) return $this->failure('未查询到改数据');
|
||
|
|
} else {
|
||
|
|
$user_id = $request->user_id;
|
||
|
|
$information = new ProductInformations();
|
||
|
|
}
|
||
|
|
if(isset($user_id)){
|
||
|
|
$information->user_id = $user_id;
|
||
|
|
}
|
||
|
|
if($title)
|
||
|
|
$information->title = $title;
|
||
|
|
if($content)
|
||
|
|
$information->content = $content;
|
||
|
|
if($pic)
|
||
|
|
$information->pic = $pic;
|
||
|
|
if(isset($request->is_hot) && $request->is_hot) {
|
||
|
|
$information->is_hot = 1;
|
||
|
|
} else {
|
||
|
|
$information->is_hot = 0;
|
||
|
|
}
|
||
|
|
$information->save();
|
||
|
|
$pv = Redis::zscore('product_information', $information->id)?:0;
|
||
|
|
return $this->success('ok',['id'=>$information->id]);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error($e->getMessage());
|
||
|
|
return $this->failure('数据错误');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取产品动态列表
|
||
|
|
public function infos(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$infos = ProductInformations::orderBy('id','desc')->paginate();
|
||
|
|
foreach ($infos as $info) {
|
||
|
|
$info->pv = Redis::zscore('product_information',$info->id)?:0;
|
||
|
|
}
|
||
|
|
return $this->success('ok',$infos);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error($e->getMessage());
|
||
|
|
return $this->failure('数据错误');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取产品动态列表
|
||
|
|
public function info(Request $request,$id)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$info = ProductInformations::find($id);
|
||
|
|
if(!$info) {
|
||
|
|
return $this->failure('数据不存在');
|
||
|
|
}
|
||
|
|
$info->pv = Redis::zscore('product_information',$info->id)?:0;
|
||
|
|
return $this->success('ok',$info);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error($e->getMessage());
|
||
|
|
return $this->failure('数据错误');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 删除产品动态
|
||
|
|
public function del(Request $request,$id)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$info = ProductInformations::find($id);
|
||
|
|
if(!$info) {
|
||
|
|
return $this->success('动态已删除');
|
||
|
|
} else {
|
||
|
|
$info->delete();
|
||
|
|
return $this->success('ok');
|
||
|
|
}
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error($e->getMessage());
|
||
|
|
return $this->failure('数据错误');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|