mirror of
https://github.com/ProxyPanel/ProxyPanel.git
synced 2026-04-13 07:59:20 +00:00
1. 彻底放弃向闭源版转型,清理掉一批闭源/烂尾的代码;
2. 重新优化优惠券功能的显示与生成;
2.1 现在用户使用优惠券后,购买页面会有优惠券优惠价格等提示;
2.2 支持管理自己定义优惠券代码,可是用HappyNewYear 这样的码了;
3. 管理员页面各搜索功能的BUG修改;
3.1 部分页面的搜索条件添加,以及对之前代码的烂尾进行补全;
3.2 统一代码检查用isset而非 !isEmpty, 来减少误判;
3.3 对搜索在不同设备下的显示进行了优化;
4. 针对html 和 js 项目的代码规范以及简写;
5. 针对 debug方便;添加了debug工具;
提示:请各位在生产环境下关闭debug模式;
239 lines
8.4 KiB
PHP
239 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Models\Goods;
|
|
use App\Http\Models\GoodsLabel;
|
|
use App\Http\Models\Label;
|
|
use DB;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Log;
|
|
use Redirect;
|
|
use Response;
|
|
use Session;
|
|
|
|
/**
|
|
* 商店控制器
|
|
*
|
|
* Class ShopController
|
|
*
|
|
* @package App\Http\Controllers
|
|
*/
|
|
class ShopController extends Controller
|
|
{
|
|
// 商品列表
|
|
public function goodsList(Request $request)
|
|
{
|
|
$type = $request->input('type');
|
|
$status = $request->input('status');
|
|
|
|
$query = Goods::query();
|
|
|
|
if (isset($type)) {
|
|
$query->where('type', $type);
|
|
}
|
|
|
|
if (isset($status)) {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
$view['goodsList'] = $query->orderBy('id', 'desc')->paginate(10)->appends($request->except('page'));
|
|
|
|
return Response::view('shop.goodsList', $view);
|
|
}
|
|
|
|
// 添加商品
|
|
public function addGoods(Request $request)
|
|
{
|
|
if ($request->isMethod('POST')) {
|
|
$this->validate($request, ['name' => 'required', 'traffic' => 'required_unless:type,3|integer|min:1024|max:10240000|nullable', 'price' => 'required|numeric|min:0', 'type' => 'required', 'days' => 'required|integer',], ['name.required' => '请填入名称', 'traffic.required_unless' => '请填入流量', 'traffic.integer' => '内含流量必须是整数值', 'traffic.min' => '内含流量不能低于1MB', 'traffic.max' => '内含流量不能超过10TB', 'price.required' => '请填入价格', 'price.numeric' => '价格不合法', 'price.min' => '价格最低0', 'type.required' => '请选择类型', 'days.required' => '请填入有效期', 'days.integer' => '有效期不合法',]);
|
|
|
|
// 套餐必须有价格
|
|
if ($request->type == 2 && $request->price <= 0) {
|
|
return Redirect::back()->withInput()->withErrors('套餐价格必须大于0');
|
|
}
|
|
|
|
// 套餐有效天数必须大于30天
|
|
if ($request->type == 2 && $request->days < 1) {
|
|
return Redirect::back()->withInput()->withErrors('套餐有效天数必须不能少于1天');
|
|
}
|
|
|
|
// 商品LOGO
|
|
if ($request->hasFile('logo')) {
|
|
$file = $request->file('logo');
|
|
$fileType = $file->getClientOriginalExtension();
|
|
|
|
// 验证文件合法性
|
|
if (!in_array($fileType, ['jpg', 'png', 'jpeg', 'bmp'])) {
|
|
return Redirect::back()->withInput()->withErrors('LOGO不合法');
|
|
}
|
|
|
|
$logoName = date('YmdHis') . mt_rand(1000, 2000) . '.' . $fileType;
|
|
$move = $file->move(base_path() . '/public/upload/image/', $logoName);
|
|
$logo = $move ? '/upload/image/' . $logoName : '';
|
|
} else {
|
|
$logo = '';
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$goods = new Goods();
|
|
$goods->name = $request->name;
|
|
$goods->info = $request->info;
|
|
$goods->desc = $request->desc;
|
|
$goods->logo = $logo;
|
|
$goods->traffic = $request->traffic;
|
|
$goods->price = round($request->price, 2);
|
|
$goods->type = $request->type;
|
|
$goods->days = $request->days;
|
|
$goods->color = $request->color;
|
|
$goods->sort = $request->sort;
|
|
$goods->is_hot = $request->is_hot;
|
|
$goods->is_limit = $request->is_limit;
|
|
$goods->status = $request->status;
|
|
$goods->save();
|
|
|
|
// 生成SKU
|
|
$goods->sku = 'S0000' . $goods->id;
|
|
$goods->save();
|
|
|
|
// 生成商品标签
|
|
$labels = $request->input('labels');
|
|
if (!empty($labels)) {
|
|
foreach ($labels as $label) {
|
|
$goodsLabel = new GoodsLabel();
|
|
$goodsLabel->goods_id = $goods->id;
|
|
$goodsLabel->label_id = $label;
|
|
$goodsLabel->save();
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return Redirect::back()->with('successMsg', '添加成功');
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
Log::info($e);
|
|
|
|
return Redirect::back()->withInput()->withErrors('添加失败');
|
|
}
|
|
} else {
|
|
$view['label_list'] = Label::query()->orderBy('sort', 'desc')->orderBy('id', 'asc')->get();
|
|
|
|
return Response::view('shop.addGoods', $view);
|
|
}
|
|
}
|
|
|
|
// 编辑商品
|
|
public function editGoods(Request $request, $id)
|
|
{
|
|
if ($request->isMethod('POST')) {
|
|
$name = $request->input('name');
|
|
$info = $request->input('info');
|
|
$desc = $request->input('desc');
|
|
$price = round($request->input('price'), 2);
|
|
$labels = $request->input('labels');
|
|
$color = $request->input('color');
|
|
$sort = $request->input('sort');
|
|
$is_hot = $request->input('is_hot');
|
|
$is_limit = $request->input('is_limit');
|
|
$status = $request->input('status');
|
|
|
|
$goods = Goods::query()->where('id', $id)->first();
|
|
if (!$goods) {
|
|
Session::flash('errorMsg', '商品不存在');
|
|
|
|
return Redirect::back();
|
|
}
|
|
|
|
if (empty($name)) {
|
|
Session::flash('errorMsg', '请填写完整');
|
|
|
|
return Redirect::back()->withInput();
|
|
}
|
|
|
|
// 套餐必须有价格
|
|
if ($goods->type == 2 && $price <= 0) {
|
|
Session::flash('errorMsg', '套餐价格必须大于0');
|
|
|
|
return Redirect::back()->withInput();
|
|
}
|
|
|
|
// 商品LOGO
|
|
$logo = '';
|
|
if ($request->hasFile('logo')) {
|
|
$file = $request->file('logo');
|
|
$fileType = $file->getClientOriginalExtension();
|
|
|
|
// 验证文件合法性
|
|
if (!in_array($fileType, ['jpg', 'png', 'jpeg', 'bmp'])) {
|
|
Session::flash('errorMsg', 'LOGO不合法');
|
|
|
|
return Redirect::back()->withInput();
|
|
}
|
|
|
|
$logoName = date('YmdHis') . mt_rand(1000, 2000) . '.' . $fileType;
|
|
$move = $file->move(base_path() . '/public/upload/image/', $logoName);
|
|
$logo = $move ? '/upload/image/' . $logoName : '';
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$data = ['name' => $name, 'info' => $info, 'desc' => $desc, 'price' => $price * 100, 'sort' => $sort, 'color' => $color, 'is_hot' => $is_hot, 'is_limit' => $is_limit, 'status' => $status];
|
|
|
|
if ($logo) {
|
|
$data['logo'] = $logo;
|
|
}
|
|
|
|
Goods::query()->where('id', $id)->update($data);
|
|
|
|
// 先删除该商品所有的标签
|
|
GoodsLabel::query()->where('goods_id', $id)->delete();
|
|
|
|
// 生成商品标签
|
|
if (!empty($labels)) {
|
|
foreach ($labels as $label) {
|
|
$goodsLabel = new GoodsLabel();
|
|
$goodsLabel->goods_id = $id;
|
|
$goodsLabel->label_id = $label;
|
|
$goodsLabel->save();
|
|
}
|
|
}
|
|
|
|
Session::flash('successMsg', '编辑成功');
|
|
|
|
DB::commit();
|
|
} catch (Exception $e) {
|
|
Session::flash('errorMsg', '编辑失败');
|
|
|
|
DB::rollBack();
|
|
}
|
|
|
|
return Redirect::to('shop/editGoods/' . $id);
|
|
} else {
|
|
$goods = Goods::query()->with(['label'])->where('id', $id)->first();
|
|
if ($goods) {
|
|
$label = [];
|
|
foreach ($goods->label as $vo) {
|
|
$label[] = $vo->label_id;
|
|
}
|
|
$goods->labels = $label;
|
|
}
|
|
|
|
$view['goods'] = $goods;
|
|
$view['label_list'] = Label::query()->orderBy('sort', 'desc')->orderBy('id', 'asc')->get();
|
|
|
|
return Response::view('shop.editGoods', $view);
|
|
}
|
|
}
|
|
|
|
// 删除商品
|
|
public function delGoods(Request $request)
|
|
{
|
|
Goods::query()->where('id', $request->input('id'))->delete();
|
|
|
|
return Response::json(['status' => 'success', 'data' => '', 'message' => '删除成功']);
|
|
}
|
|
}
|