| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\food\controllers;
- use app\plugins\food\models\FoodGoods;
- use app\plugins\food\models\FoodCat;
- use app\utils\Alipay\AlipayProfit;
- use app\utils\Image;
- use yii\helpers\Json;
- class GoodsController extends BaseController
- {
- public function actionList()
- {
- $name = get_params('name');
- $cat_id = get_params('cat_id', 0);
- $status = get_params('status', -1);
- $query = FoodGoods::find()
- ->alias('fg')
- ->leftJoin(['fc' => FoodCat:: tableName()], 'fc.id = fg.cat_id')
- ->where([
- 'fg.is_delete' => 0,
- 'fg.store_id' => get_store_id()
- ]);
- if (!empty($name)) {
- $query->andWhere(['like', 'fg.name', $name]);
- }
- if ($cat_id > 0) {
- $query->andWhere(['cat_id' => $cat_id]);
- }
- if ($status > -1) {
- $query->andWhere(['status' => $status]);
- }
- $query->select('fg.*, fc.name as cat_name')->orderBy(['sort' => SORT_DESC]);
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- if (!empty($item['cover_pic'])) {
- $item['cover_pic'] = json_decode($item['cover_pic'], true);
- $item['attr'] = json_decode($item['attr'], true);
- }
- if (empty($item['attr'])) {
- $item['attr'] = '';
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ],
- ];
- }
- public function actionChangeStatus()
- {
- $id = post_params('id');
- $type = post_params('type', 'status');
- $cat = FoodGoods::findOne($id);
- $cat->{$type} = $cat->{$type} === 1 ? 0 : 1;
- if ($cat->save()) {
- return [
- 'code' => 0,
- 'msg' => '修改成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '修改失败',
- ];
- }
- public function actionDel()
- {
- $id = post_params('id');
- $cat = FoodGoods::findOne($id);
- $cat->is_delete = 1;
- if ($cat->save()) {
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '删除失败',
- ];
- }
- public function actionEdit()
- {
- try {
- $data = all_params();
- if (empty($data['cover_pic'])) {
- return [
- 'code' => 1,
- 'msg' => '请上传封面图',
- ];
- }
- if ($data['id'] > 0) {
- $goods = FoodGoods::findOne($data['id']);
- if (!$goods) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在',
- ];
- }
- } else {
- $goods = new FoodGoods();
- }
- $goods->store_id = get_store_id();
- $goods->name = $data['name'];
- $goods->subtitle = $data['subtitle'];
- $goods->price = $data['price'];
- $goods->original_price = $data['original_price'];
- $goods->detail = $data['detail'];
- $goods->cat_id = $data['cat_id'];
- $goods->status = (int)$data['status'];
- $goods->is_recommend = (int)$data['is_recommend'];
- $goods->sort = $data['sort'];
- $goods->virtual_sales = $data['virtual_sales'];
- $goods->cover_pic = json_encode($data['cover_pic']);
- if ($data['is_use_attr'] && !empty($data['attr'])) {
- $goods->attr = json_encode($data['attr']);
- } else {
- $goods->attr = '';
- }
- if ($goods->save()) {
- // $pic = $data['cover_pic'][0]['url'];
- // 上传物料
- // list($width, $height, $type, $attr) = getimagesize($pic);
- // if ($width < 750) {
- // $result = Image::image_resize($pic, \Yii::$app->basePath . '/runtime/image/foods.png', '750px', '600px');
- // }
- // Todo 暂时屏蔽支付宝物料上传
- // $res = AlipayProfit::file_upload($pic);
- // \Yii::warning(['<========= 创建商品图片物料 ==========>', $pic, $res]);
- // if ($res['code'] == 10000 && $res['msg'] == 'Success') {
- // $goods->material_id = $res['material_id'];
- // if (!$goods->save()) {
- // \Yii::warning(['<========= 创建商品图片物料入库失败 ==========>', $goods->errors]);
- // }
- // }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'error' => $goods->errors[0],
- 'msg' => '保存失败',
- ];
- }
- } catch (\Throwable $throwable) {
- return [
- 'code' => 1,
- 'msg' => '保存失败',
- 'error' => $throwable->getMessage(),
- ];
- }
- }
- public function actionBatch()
- {
- try {
- $ids = post_params('ids', []);
- $action = post_params('action');
- if (empty($ids) || empty($action)) {
- return [
- 'code' => 1,
- 'msg' => '缺少参数',
- ];
- }
- $actions = [
- 'status_up', // 批量上架
- 'status_down', // 批量下架
- 'recommend', // 批量推荐
- 'un_recommend', // 批量取消推荐
- 'delete', // 批量删除
- ];
- if (in_array($action, $actions) === false) {
- return [
- 'code' => 1,
- 'msg' => '参数不正确',
- ];
- }
- if ($action == 'status_up') {
- }
- switch ($action) {
- case 'status_up':
- FoodGoods::updateAll(['status' => 1], ['id' => $ids]);
- break;
- case 'status_down':
- FoodGoods::updateAll(['status' => 0], ['id' => $ids]);
- break;
- case 'recommend':
- FoodGoods::updateAll(['is_recommend' => 1], ['id' => $ids]);
- break;
- case 'un_recommend':
- FoodGoods::updateAll(['is_recommend' => 0], ['id' => $ids]);
- break;
- case 'delete':
- FoodGoods::updateAll(['is_delete' => 1], ['id' => $ids]);
- break;
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- } catch (\Throwable $throwable) {
- return [
- 'code' => 1,
- 'msg' => '操作失败',
- 'error' => $throwable->getMessage(),
- ];
- }
- }
- public function actionNewList() {
- $store_id = get_store_id();
- // 分类
- $cats = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])->orderBy('sort desc')->select('id, name as title, desc as subtitle, pic_url as icon')->asArray()->all();
- if (empty($cats)) {
- return [
- 'code' => 1,
- 'msg' => '分类未找到'
- ];
- }
- foreach ($cats as $key => &$cat) {
- $goods_list = FoodGoods::find()->where(['store_id' => $store_id, 'cat_id' => $cat['id'], 'is_delete' => 0, 'status' => 1])
- ->select('id, name, cover_pic as goods_pic, subtitle as desc, price, original_price, attr, virtual_sales as sales')->orderBy('sort desc')->asArray()->all();
- if (empty($goods_list)) {
- // 如果当前分类下无商品,不作展示
- unset($cats[$key]);
- continue;
- }
- foreach ($goods_list as $key => $goods) {
- $goods_list[$key]['goods_pic'] = Json::decode($goods['goods_pic'])[0]['url'];
- $goods_list[$key]['attr'] = Json::decode($goods['attr']);
- }
- $cat['list'] = $goods_list;
- }
- sort($cats);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $cats
- ];
- }
- }
|