CommunityGoodsController.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace addons\Community\backend\controllers;
  3. use addons\Community\common\models\CommunityGoods;
  4. use addons\Community\common\models\CommunityGoodsSetting;
  5. use addons\Community\common\models\CommunityHead;
  6. use common\enums\StatusEnum;
  7. use Yii;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class CommunityGoodsController
  12. * @package addons\Community\backend\controllers
  13. */
  14. class CommunityGoodsController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * 首页
  19. *
  20. * @return string
  21. */
  22. public function actionGetGoodsSetting()
  23. {
  24. if (!\Yii::$app->request->isAjax) {
  25. return $this->render($this->action->id);
  26. }
  27. if (\Yii::$app->request->isGet) {
  28. $params = Yii::$app->request->get();
  29. $goods_id = $params['goods_id'] ?? 0;
  30. $list = [];
  31. $enable = 0;
  32. $enable_refund = 0;
  33. $data = [];
  34. $supported_logistics = [1, 2];
  35. if ($goods_id > 0) {
  36. $setting = CommunityGoodsSetting::getOne([['mall_id' => $this->mall_id], ['goods_id' => $goods_id], ['status' => StatusEnum::ENABLED]], true, [], false, 'enable, supported_logistics, enable_refund');
  37. $enable = $setting ? $setting['enable'] : $enable;
  38. $enable_refund = $setting ? $setting['enable_refund'] : $enable_refund;
  39. $supported_logistics = $setting ? ($setting['supported_logistics'] ? json_decode($setting['supported_logistics'], true) : $supported_logistics) : $supported_logistics;
  40. $param = [
  41. 'where' => [['mall_id' => $this->mall_id], ['goods_id' => $goods_id], ['status' => StatusEnum::ENABLED]],
  42. 'select' => 'community_id'
  43. ];
  44. $community_goods = CommunityGoods::lists($param, true);
  45. $community_ids = array_column($community_goods, 'community_id');
  46. $community_list = CommunityHead::lists([
  47. 'where' => [
  48. ['mall_id' => $this->mall_id],
  49. ['id' => $community_ids],
  50. ['status' => StatusEnum::ENABLED]
  51. ],
  52. 'select' => 'id, province_id, city_id, district_id'
  53. ], true);
  54. // do not give up
  55. foreach ($community_list as $v) {
  56. !isset($data[$v['province_id']]) && $data[$v['province_id']] = ['id' => $v['province_id'], 'children' => []];
  57. !isset($data[$v['province_id']]['children'][$v['city_id']]) && $data[$v['province_id']]['children'][$v['city_id']] = ['id' => $v['city_id'], 'children' => []];
  58. !isset($data[$v['province_id']]['children'][$v['city_id']]['children'][$v['district_id']]) && $data[$v['province_id']]['children'][$v['city_id']]['children'][$v['district_id']] = ['id' => $v['district_id'], 'children' => []];
  59. $data[$v['province_id']]['children'][$v['city_id']]['children'][$v['district_id']]['children'][] = ['id' => $v['id']];
  60. }
  61. $data = array_values($data);
  62. foreach ($data as $k => $v) {
  63. foreach ($v['children'] as $key => $value) {
  64. isset($data[$k]['children'][$key]['children']) && $data[$k]['children'][$key]['children'] = array_values($data[$k]['children'][$key]['children']);
  65. }
  66. isset($data[$k]['children']) && $data[$k]['children'] = array_values($data[$k]['children']);
  67. }
  68. }
  69. $list['enable'] = $enable;
  70. $list['enable_refund'] = $enable_refund;
  71. $list['list'] = $data;
  72. $list['supported_logistics'] = $supported_logistics;
  73. return $this->success('success', $list);
  74. }
  75. }
  76. }