PicController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace business\controllers;
  3. use biz\goods\services\CategoryService;
  4. use biz\goods\services\PicCategoryService;
  5. use biz\goods\services\PicService;
  6. use Yii;
  7. use common\components\util;
  8. class PicController extends BaseController
  9. {
  10. //图库列表 shish 2019.12.7
  11. public function actionList()
  12. {
  13. $get = Yii::$app->request->get();
  14. $where = [];
  15. $where['merchantId'] = $this->merchantId;
  16. $categoryId = isset($get['categoryId']) && is_numeric($get['categoryId']) ? $get['categoryId'] : -1;
  17. if ($categoryId != -1) {
  18. $where['categoryId'] = $get['categoryId'];
  19. }
  20. $list = PicCategoryService::getPicList($where);
  21. util::success($list);
  22. }
  23. //上传图片 shish 2019.12.7
  24. public function actionAdd()
  25. {
  26. $img = '/uploads/201905/25/jpg/19052591518_12358.jpg';
  27. $prefix = Yii::$app->params['imgUrl'];
  28. $url = $prefix . $img;
  29. $merchantId = Yii::$app->params['merchantId'];
  30. $time = time();
  31. $name = '未命名';
  32. $data = ['img' => $img, 'merchantId' => $merchantId, 'name' => $name, 'addTime' => $time];
  33. $pic = PicService::add($data);
  34. util::success(['id' => $pic['id'], 'url' => $url, 'shortUrl' => $img]);
  35. }
  36. //备注图片 shish 2019.12.8
  37. public function actionRemark()
  38. {
  39. $post = Yii::$app->request->post();
  40. $picIdList = isset($post['picIdList']) && !empty($post['picIdList']) ? $post['picIdList'] : [];
  41. unset($post['picIdList']);
  42. if (empty($picIdList)) {
  43. util::fail('没有图片需要备注');
  44. }
  45. $categoryIdList = isset($post['categoryIdList']) && !empty($post['categoryIdList']) ? $post['categoryIdList'] : [];
  46. unset($post['categoryIdList']);
  47. $merchantHas = CategoryService::getCategoryIdList($this->merchantId);
  48. $diff = array_diff($categoryIdList, $merchantHas);
  49. if (!empty($diff)) {
  50. util::fail('只能设置自己的分类');
  51. }
  52. $picList = PicService::getByIds($picIdList);
  53. $add = [];
  54. foreach ($picList as $picKey => $picVal) {
  55. if ($picVal['merchantId'] != $this->merchantId) {
  56. util::fail('只能设置自己的图片');
  57. }
  58. $currentId = $picVal['id'];
  59. PicService::updateById($currentId, $post);
  60. foreach ($categoryIdList as $categoryId) {
  61. $add[] = ['categoryId' => $categoryId, 'picId' => $currentId, 'merchantId' => $this->merchantId];
  62. }
  63. PicCategoryService::deleteByCondition(['picId' => $currentId]);
  64. }
  65. PicCategoryService::batchAdd($add);
  66. util::ok('备注成功');
  67. }
  68. }