dggjimai/application/admin/controller/GoodsController.php

151 lines
4.8 KiB
PHP
Executable File

<?php
/**
* 商品控制器
*/
namespace app\admin\controller;
use think\Request;
use app\common\model\Goods;
use app\common\model\MallImage;
use app\common\model\GoodsCategory;
use app\common\model\Brand;
use app\common\model\GoodsAttrCategory;
use app\common\validate\GoodsValidate;
class GoodsController extends Controller
{
//列表
public function index(Request $request, Goods $model)
{
$param = $request->param();
$model = $model->with('goods_category')->scope('where', $param);
$data = $model->paginate($this->admin['per_page'], false, ['query' => $request->get()]);
//关键词,排序等赋值
$this->assign($request->get());
$this->assign([
'data' => $data,
'page' => $data->render(),
'total' => $data->total(),
]);
return $this->fetch();
}
//添加
public function add(Request $request, Goods $model,MallImage $mimode, GoodsValidate $validate)
{
if ($request->isPost()) {
$param = $request->param();
$validate_result = $validate->scene('add')->check($param);
if (!$validate_result) {
return error($validate->getError());
}
$param['detail'] = $request->param(false)['detail'];
// 启动事务
$model->startTrans();
try{
$model::create($param);
$goods_id=$model->getLastInsID();
goods_imgs_upload($goods_id,1,$param['ori_img'],$param['thumb_img'],$param['ori_fm_img'],$param['thumb_img_f']);
// 提交事务
$model->commit();
}catch(\Exception $e){
// 回滚事务
$model->rollback();
return error($e->getMessage());
}
$url = URL_BACK;
if(isset($param['_create']) && $param['_create']==1){
$url = URL_RELOAD;
}
return success('添加成功',$url);
}
$this->assign([
'goods_category_list' => $this->getSelectList(new GoodsCategory),
'imgurl_list'=>array(),
]);
return $this->fetch();
}
//修改
public function edit($id, Request $request, Goods $model ,MallImage $mimode, GoodsValidate $validate)
{
$data = $model::get($id);
if ($request->isPost()) {
$param = $request->param();
$validate_result = $validate->scene('edit')->check($param);
if (!$validate_result) {
return error($validate->getError());
}
$param['detail'] = $request->param(false)['detail'];
// 启动事务
$model->startTrans();
try{
$data->save($param);
$MallImage=$mimode->where(array('type'=>1,'union_id'=>$id))->select();
goods_imgs_upload($id,1,$param['ori_img'],$param['thumb_img'],$param['ori_fm_img'],$param['thumb_img_f'],$MallImage);
// 提交事务
$model->commit();
}catch(\Exception $e){
// 回滚事务
$model->rollback();
return error($e->getMessage());
}
return success();
}
$ori_img='';
$thumb_img='';
$imgurl_list=$mimode->field('thumb_image,ori_image,is_cover')->where(array('type'=>1,'union_id'=>$id))->select();
foreach ($imgurl_list as $key => $v) {
$is_cover=$v['is_cover']>0 ? $v['is_cover']:'0';
if ($is_cover == 1) {
$ori_img = $v['ori_image'];
$thumb_img = $v['thumb_image'];
}
}
$this->assign([
'data' => $data,
'goods_category_list' => $this->getSelectList(new GoodsCategory, $data->goods_category_id),
'ori_img'=>$ori_img,
'thumb_img'=>$thumb_img,
'imgurl_list'=>$imgurl_list,
]);
return $this->fetch('add');
}
//删除
public function del($id, Goods $model)
{
if (count($model->noDeletionId) > 0) {
if (is_array($id)) {
if (array_intersect($model->noDeletionId, $id)) {
return error('ID为' . implode(',', $model->noDeletionId) . '的数据无法删除');
}
} else if (in_array($id, $model->noDeletionId)) {
return error('ID为' . $id . '的数据无法删除');
}
}
if ($model->softDelete) {
$result = $model->whereIn('id', $id)->useSoftDelete('delete_time', time())->delete();
} else {
$result = $model->whereIn('id', $id)->delete();
}
return $result ? success('操作成功', URL_RELOAD) : error();
}
}