MchController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace addons\Mch\api\modules\v1\controllers;
  3. use addons\Mch\common\models\Mch;
  4. use addons\Mch\common\models\MchCate;
  5. use common\enums\StatusEnum;
  6. use common\models\goods\GoodsCate;
  7. use common\models\order\OrderComments;
  8. use Yii;
  9. use api\controllers\OnAuthController;
  10. /**
  11. * 默认控制器
  12. *
  13. * Class DefaultController
  14. * @package addons\Store\api\modules\v1\controllers
  15. */
  16. class MchController extends OnAuthController
  17. {
  18. public $modelClass = '';
  19. /**
  20. * 不用进行登录验证的方法
  21. *
  22. * 例如: ['index', 'update', 'create', 'view', 'delete']
  23. * 默认全部需要验证
  24. *
  25. * @var array
  26. */
  27. protected $authOptional = ['index'];
  28. /**
  29. * 首页
  30. *
  31. * @return string
  32. */
  33. public function actionIndex()
  34. {
  35. $params = Yii::$app->request->get();
  36. $mch_id = $params['mch_id'];
  37. if (empty($mch_id)) return $this->error('参数错误');
  38. $mall_id = $this->mall_id;
  39. $mch = Mch::getOne([['id' => $mch_id], ['mall_id' => $mall_id]]);
  40. if (empty($mch)) return $this->error('商户不存在');
  41. $time = time();
  42. $data['store_status'] = '';
  43. if(empty($mch['shop_status'])) $data['store_status'] = '休息中';
  44. if(!empty($data['expire_time'])&&$time>=$data['expire_time'])$data['store_status'] = '休息中';
  45. $data['mch_id'] = $mch['id'];
  46. $data['logo_img'] = $mch['logo_img'];
  47. $data['store_name'] = $mch['store_name'];
  48. $data['store_background'] = $mch['store_background']??'';
  49. $data['praise'] = $mch['praise'];//好评
  50. $data['sales_volume'] = $mch['sales_volume'];
  51. $comment_where = [['mch_id' => $mch_id], ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]];
  52. $order_comment_count = OrderComments::listCount(['where' => $comment_where,]);
  53. $data['order_comment_count'] = $order_comment_count ?? 0;//评论条数
  54. $mch_goods_cate_list = GoodsCate::lists([
  55. 'where' => [
  56. // ['mch_id' => $mch_id],
  57. ['mall_id' => $mall_id],
  58. ['status' => StatusEnum::ENABLED],
  59. ['is_show' => 1],
  60. ['parent_id' => 0],
  61. ],
  62. 'select' => 'id,name',
  63. 'order' => 'sort asc',
  64. ]);
  65. $data['mch_goods_cate_list'] = $mch_goods_cate_list;
  66. return $this->success('成功', $data);
  67. }
  68. //商户列表
  69. public function actionMchList()
  70. {
  71. $params = Yii::$app->request->get();
  72. $sort = $params['sort'] ?? '';
  73. $mall_id = $this->mall_id;
  74. $status = StatusEnum::ENABLED;
  75. $shop_status = 1;
  76. $check_status = 1;
  77. $where[] = ['mall_id' => $mall_id];
  78. $where[] = ['status' => $status];
  79. $where[] = ['shop_status' => $shop_status];
  80. $where[] = ['check_status' => $check_status];
  81. $where[] = ['or', ['>', 'expire_time', time()], ['expire_time' => 0]];
  82. if(!empty($params['mch_id'])) $where[] = ['id' => explode(',',$params['mch_id'])];
  83. if (!empty($params['keyword'])) $where[] = ['like', 'store_name', trim($params['keyword']) . '%', false];
  84. if (!empty($params['cate_id'])) $where[] = ['c_id' => $params['cate_id']];
  85. if (in_array($sort, ['sales_volume', 'praise'])) $params['order'] = "{$sort} desc";
  86. $params['where'] = $where;
  87. $list_page = Mch::listPage($params);
  88. return $this->success('成功', $list_page);
  89. }
  90. public function actionMchCate()
  91. {
  92. $list = MchCate::lists(['where' => ['mall_id' => $this->mall_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED], 'select' => 'id,parent_id,name']);
  93. $list = MchCate::getTreeMenu($list, 0);
  94. return $this->success('ok', compact('list'));
  95. }
  96. public function actionCate()
  97. {
  98. $params = \Yii::$app->request->get();
  99. $cate_id = empty($params['cate_id']) ? 0 : $params['cate_id'];
  100. $params['where'] = [['mall_id' => $this->mall_id, 'parent_id' => $cate_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED]];
  101. $params['select'] = 'id,name,pic,advert_pic,advert_url';
  102. $params['order'] = 'sort desc';
  103. $cate_list = MchCate::listPage($params);
  104. if (false === $cate_list) {
  105. return $this->error(MchCate::getStaticError());
  106. }
  107. if (!empty($cate_list['list'])) {
  108. foreach ($cate_list['list'] as &$lv) {
  109. if (!empty($lv['advert_pic'])) {
  110. $lv['advert_pic'] = json_decode($lv['advert_pic'], true);
  111. }
  112. if (!empty($lv['advert_url'])) {
  113. $lv['advert_url'] = json_decode($lv['advert_url'], true);
  114. }
  115. }
  116. }
  117. $cate = MchCate::find()
  118. ->select('id,name,pic,advert_pic,advert_url')
  119. ->where(['mall_id' => $this->mall_id, 'id' => $cate_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED])
  120. ->asArray()
  121. ->one();
  122. if (!empty($cate['advert_pic'])) {
  123. $cate['advert_pic'] = json_decode($cate['advert_pic'], true);
  124. }
  125. if (!empty($cate['advert_url'])) {
  126. $cate['pic_url'] = json_decode($cate['advert_url'], true);
  127. }
  128. $cate_list['cate_info'] = $cate;
  129. return $this->success('操作成功', $cate_list);
  130. }
  131. }