GoodsController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\food\controllers;
  8. use app\plugins\food\models\FoodGoods;
  9. use app\plugins\food\models\FoodCat;
  10. use app\utils\Alipay\AlipayProfit;
  11. use app\utils\Image;
  12. use yii\helpers\Json;
  13. class GoodsController extends BaseController
  14. {
  15. public function actionList()
  16. {
  17. $name = get_params('name');
  18. $cat_id = get_params('cat_id', 0);
  19. $status = get_params('status', -1);
  20. $query = FoodGoods::find()
  21. ->alias('fg')
  22. ->leftJoin(['fc' => FoodCat:: tableName()], 'fc.id = fg.cat_id')
  23. ->where([
  24. 'fg.is_delete' => 0,
  25. 'fg.store_id' => get_store_id()
  26. ]);
  27. if (!empty($name)) {
  28. $query->andWhere(['like', 'fg.name', $name]);
  29. }
  30. if ($cat_id > 0) {
  31. $query->andWhere(['cat_id' => $cat_id]);
  32. }
  33. if ($status > -1) {
  34. $query->andWhere(['status' => $status]);
  35. }
  36. $query->select('fg.*, fc.name as cat_name')->orderBy(['sort' => SORT_DESC]);
  37. $list = pagination_make($query);
  38. foreach ($list['list'] as &$item) {
  39. if (!empty($item['cover_pic'])) {
  40. $item['cover_pic'] = json_decode($item['cover_pic'], true);
  41. $item['attr'] = json_decode($item['attr'], true);
  42. }
  43. if (empty($item['attr'])) {
  44. $item['attr'] = '';
  45. }
  46. }
  47. return [
  48. 'code' => 0,
  49. 'msg' => 'success',
  50. 'data' => [
  51. 'data' => $list['list'],
  52. 'pageNo' => $list['pageNo'],
  53. 'totalCount' => $list['totalCount'],
  54. ],
  55. ];
  56. }
  57. public function actionChangeStatus()
  58. {
  59. $id = post_params('id');
  60. $type = post_params('type', 'status');
  61. $cat = FoodGoods::findOne($id);
  62. $cat->{$type} = $cat->{$type} === 1 ? 0 : 1;
  63. if ($cat->save()) {
  64. return [
  65. 'code' => 0,
  66. 'msg' => '修改成功'
  67. ];
  68. }
  69. return [
  70. 'code' => 1,
  71. 'msg' => '修改失败',
  72. ];
  73. }
  74. public function actionDel()
  75. {
  76. $id = post_params('id');
  77. $cat = FoodGoods::findOne($id);
  78. $cat->is_delete = 1;
  79. if ($cat->save()) {
  80. return [
  81. 'code' => 0,
  82. 'msg' => '删除成功'
  83. ];
  84. }
  85. return [
  86. 'code' => 1,
  87. 'msg' => '删除失败',
  88. ];
  89. }
  90. public function actionEdit()
  91. {
  92. try {
  93. $data = all_params();
  94. if (empty($data['cover_pic'])) {
  95. return [
  96. 'code' => 1,
  97. 'msg' => '请上传封面图',
  98. ];
  99. }
  100. if ($data['id'] > 0) {
  101. $goods = FoodGoods::findOne($data['id']);
  102. if (!$goods) {
  103. return [
  104. 'code' => 1,
  105. 'msg' => '商品不存在',
  106. ];
  107. }
  108. } else {
  109. $goods = new FoodGoods();
  110. }
  111. $goods->store_id = get_store_id();
  112. $goods->name = $data['name'];
  113. $goods->subtitle = $data['subtitle'];
  114. $goods->price = $data['price'];
  115. $goods->original_price = $data['original_price'];
  116. $goods->detail = $data['detail'];
  117. $goods->cat_id = $data['cat_id'];
  118. $goods->status = (int)$data['status'];
  119. $goods->is_recommend = (int)$data['is_recommend'];
  120. $goods->sort = $data['sort'];
  121. $goods->virtual_sales = $data['virtual_sales'];
  122. $goods->cover_pic = json_encode($data['cover_pic']);
  123. if ($data['is_use_attr'] && !empty($data['attr'])) {
  124. $goods->attr = json_encode($data['attr']);
  125. } else {
  126. $goods->attr = '';
  127. }
  128. if ($goods->save()) {
  129. // $pic = $data['cover_pic'][0]['url'];
  130. // 上传物料
  131. // list($width, $height, $type, $attr) = getimagesize($pic);
  132. // if ($width < 750) {
  133. // $result = Image::image_resize($pic, \Yii::$app->basePath . '/runtime/image/foods.png', '750px', '600px');
  134. // }
  135. // Todo 暂时屏蔽支付宝物料上传
  136. // $res = AlipayProfit::file_upload($pic);
  137. // \Yii::warning(['<========= 创建商品图片物料 ==========>', $pic, $res]);
  138. // if ($res['code'] == 10000 && $res['msg'] == 'Success') {
  139. // $goods->material_id = $res['material_id'];
  140. // if (!$goods->save()) {
  141. // \Yii::warning(['<========= 创建商品图片物料入库失败 ==========>', $goods->errors]);
  142. // }
  143. // }
  144. return [
  145. 'code' => 0,
  146. 'msg' => '保存成功'
  147. ];
  148. } else {
  149. return [
  150. 'code' => 1,
  151. 'error' => $goods->errors[0],
  152. 'msg' => '保存失败',
  153. ];
  154. }
  155. } catch (\Throwable $throwable) {
  156. return [
  157. 'code' => 1,
  158. 'msg' => '保存失败',
  159. 'error' => $throwable->getMessage(),
  160. ];
  161. }
  162. }
  163. public function actionBatch()
  164. {
  165. try {
  166. $ids = post_params('ids', []);
  167. $action = post_params('action');
  168. if (empty($ids) || empty($action)) {
  169. return [
  170. 'code' => 1,
  171. 'msg' => '缺少参数',
  172. ];
  173. }
  174. $actions = [
  175. 'status_up', // 批量上架
  176. 'status_down', // 批量下架
  177. 'recommend', // 批量推荐
  178. 'un_recommend', // 批量取消推荐
  179. 'delete', // 批量删除
  180. ];
  181. if (in_array($action, $actions) === false) {
  182. return [
  183. 'code' => 1,
  184. 'msg' => '参数不正确',
  185. ];
  186. }
  187. if ($action == 'status_up') {
  188. }
  189. switch ($action) {
  190. case 'status_up':
  191. FoodGoods::updateAll(['status' => 1], ['id' => $ids]);
  192. break;
  193. case 'status_down':
  194. FoodGoods::updateAll(['status' => 0], ['id' => $ids]);
  195. break;
  196. case 'recommend':
  197. FoodGoods::updateAll(['is_recommend' => 1], ['id' => $ids]);
  198. break;
  199. case 'un_recommend':
  200. FoodGoods::updateAll(['is_recommend' => 0], ['id' => $ids]);
  201. break;
  202. case 'delete':
  203. FoodGoods::updateAll(['is_delete' => 1], ['id' => $ids]);
  204. break;
  205. }
  206. return [
  207. 'code' => 0,
  208. 'msg' => '操作成功',
  209. ];
  210. } catch (\Throwable $throwable) {
  211. return [
  212. 'code' => 1,
  213. 'msg' => '操作失败',
  214. 'error' => $throwable->getMessage(),
  215. ];
  216. }
  217. }
  218. public function actionNewList() {
  219. $store_id = get_store_id();
  220. // 分类
  221. $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();
  222. if (empty($cats)) {
  223. return [
  224. 'code' => 1,
  225. 'msg' => '分类未找到'
  226. ];
  227. }
  228. foreach ($cats as $key => &$cat) {
  229. $goods_list = FoodGoods::find()->where(['store_id' => $store_id, 'cat_id' => $cat['id'], 'is_delete' => 0, 'status' => 1])
  230. ->select('id, name, cover_pic as goods_pic, subtitle as desc, price, original_price, attr, virtual_sales as sales')->orderBy('sort desc')->asArray()->all();
  231. if (empty($goods_list)) {
  232. // 如果当前分类下无商品,不作展示
  233. unset($cats[$key]);
  234. continue;
  235. }
  236. foreach ($goods_list as $key => $goods) {
  237. $goods_list[$key]['goods_pic'] = Json::decode($goods['goods_pic'])[0]['url'];
  238. $goods_list[$key]['attr'] = Json::decode($goods['attr']);
  239. }
  240. $cat['list'] = $goods_list;
  241. }
  242. sort($cats);
  243. return [
  244. 'code' => 0,
  245. 'msg' => 'success',
  246. 'data' => $cats
  247. ];
  248. }
  249. }