GoodsController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace addons\ScoreMall\api\modules\v1\controllers;
  3. use addons\ScoreMall\common\enums\ScoreMallEnum;
  4. use addons\ScoreMall\common\models\ScoreMallExchangeRecord;
  5. use addons\ScoreMall\common\models\ScoreMallGoods;
  6. use common\enums\AddonsEnum;
  7. use common\enums\StatusEnum;
  8. use common\logic\common\GoodsLogic;
  9. use common\models\common\AddonsMarketingSetting;
  10. use Yii;
  11. use api\controllers\OnAuthController;
  12. use common\models\goods\Goods;
  13. use yii\helpers\Json;
  14. /**
  15. * 商品控制器
  16. *
  17. * Class GoodsController
  18. * @package addons\ScoreMall\api\controllers
  19. */
  20. class GoodsController extends OnAuthController
  21. {
  22. public $modelClass = '';
  23. /**
  24. * 不用进行登录验证的方法
  25. *
  26. * 例如: ['index', 'update', 'create', 'view', 'delete']
  27. * 默认全部需要验证
  28. *
  29. * @var array
  30. */
  31. protected $authOptional = ['index', 'list', 'detail'];
  32. /**
  33. * @desc:积分商城首页
  34. * @Author: hua
  35. * @Date: 2021/12/20
  36. * @Time: 16:00
  37. * @Copyright: copyright (c) 2021 广东七件事集团
  38. * @return mixed|void
  39. */
  40. public function actionIndex()
  41. {
  42. $data = AddonsMarketingSetting::getOne([['mall_id' => $this->mall_id], ['addons_name' => ScoreMallEnum::ADDONS_NAME], ['status' => StatusEnum::ENABLED]]);
  43. if ($data == false) return $this->error(AddonsMarketingSetting::getStaticError());
  44. $content = json_decode($data['content'], true);
  45. if (!empty($content)) {
  46. if(empty($content['3']['data']['listType'])){
  47. $active_data = array_pop($content);
  48. $goods_id = [];
  49. if(!empty($active_data['data']['selGoodsList'])){
  50. foreach ($active_data['data']['selGoodsList'] as $item) {
  51. $goods_id[]=$item['params']['id']??'';
  52. }
  53. }
  54. $where[] = ['mall_id' => $this->mall_id];
  55. $where[] = ['goods_id' => $goods_id];
  56. $where[] = ['status' => StatusEnum::ENABLED];
  57. $order = 'id desc';
  58. $index = 'goods_id';
  59. $select = "id,goods_id,cate_id,score,money,type,created_at";
  60. $with = [
  61. 'goods' => function($query){
  62. $query->select("id,goods_name,price,cover_pic,subtitle");
  63. },
  64. 'attr' => function($query){
  65. $query->select("goods_id,goods_attr_id,score,money,deduction_limit,price");
  66. }];
  67. $params = compact('where', 'order', 'select', 'with','index');
  68. $list = ScoreMallGoods::lists($params,true);
  69. $exchange_price_arr = [];
  70. if(!empty($list)){
  71. $score_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score');
  72. foreach ($list as $item){
  73. $exchange_price = ScoreMallGoods::getExchangePrice($item['type'], $item['attr'][0], $score_config, 1, $this->user_id);
  74. $exchange_price_arr[$item['goods_id']] = $exchange_price['exchange_price'];
  75. }
  76. }
  77. if(!empty($active_data['data']['selGoodsList'])) {
  78. foreach ($active_data['data']['selGoodsList'] as &$item) {
  79. if (isset($item['params']['id']) && isset($exchange_price_arr[$item['params']['id']])) {
  80. $item['params']['exchange_price'] = $exchange_price_arr[$item['params']['id']];
  81. }
  82. }
  83. }
  84. $content[] = $active_data;
  85. $data['content'] = json_encode($content);
  86. }
  87. }
  88. return $this->success('操作成功', $data);
  89. }
  90. /**
  91. * 积分商品列表
  92. * @Author: lun
  93. * @DateTime: 2022/1/7 0007 11:43
  94. * @Copyright: copyright (c) 2021 广东七件事集团
  95. * @return string
  96. */
  97. public function actionList()
  98. {
  99. $params = \Yii::$app->request->get();
  100. $res = \Yii::$app->services->validate->validate($params, [
  101. [['cate_id','page','limit'], 'integer'],
  102. [['ids'], 'string'],
  103. ]);
  104. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  105. $where = [];
  106. // 根据积分商品id查询
  107. if (!empty($params['ids'])) {
  108. $where[] = ['in', 'id', Json::decode($params['ids'], true)];
  109. }
  110. $where[] = ['=', 'mall_id', $this->mall_id];
  111. $where[] = ['=', 'status', StatusEnum::ENABLED];
  112. // 根据分类搜索
  113. if (!empty($params['cate_id'])) {
  114. $where[] = ['=', 'cate_id', $params['cate_id']];
  115. }
  116. $with = ['goods' => function($query){
  117. $query->select("id,goods_name,price,cover_pic,original_price");
  118. }];
  119. $order = 'created_at desc';
  120. $select = ['goods_id','score','money','type'];
  121. $params = array('select' => $select,'where' => $where, 'order' => $order, 'with' => $with, 'limit' => $params['limit']);
  122. $list = ScoreMallGoods::listPage($params, false, true);
  123. if (empty($list)) return $this->success('获取成功');
  124. // 获取积分配置
  125. $score_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score');
  126. // 计算积分
  127. foreach ($list['list'] as &$item) {
  128. if ($item['type'] == 3) {
  129. $item['score_deduct'] = '';
  130. } else {
  131. $score_info = ScoreMallGoods::getExchangePrice($item['type'], $item, $score_config);
  132. $item['score_deduct'] = $score_info['exchange_price'];
  133. }
  134. unset($item['score'], $item['money']);
  135. }
  136. $list = ScoreMallGoods::getScoreMallExtraFields($this->mall_id, $list);
  137. return $this->success('获取成功', $list);
  138. }
  139. /**
  140. * 积分商品详情
  141. * @Author: lun
  142. * @DateTime: 2022/1/7 0007 11:43
  143. * @Copyright: copyright (c) 2021 广东七件事集团
  144. * @return string
  145. */
  146. public function actionDetail()
  147. {
  148. $params = \Yii::$app->request->get();
  149. $res = \Yii::$app->services->validate->validate($params, [
  150. [['goods_id'], 'integer'],
  151. [['scene'], 'string'],
  152. [['addonsFields'], 'safe']
  153. ]);
  154. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  155. // 判断该商品是否存在于积分商品中
  156. $score_goods = ScoreMallGoods::getOne([['goods_id' => $params['goods_id'], 'mall_id' => $this->mall_id]],true, ['attr' => function($query){
  157. $query->select("goods_id,goods_attr_id,score,money,deduction_limit,price");
  158. }],false, "goods_id,type,money,score");
  159. if (empty($score_goods)) return $this->error("该商品并非积分商品");
  160. // 根据商品id获取商品详情信息
  161. $goods = GoodsLogic::GetDetail($this->mall_id, $params, $this->user);
  162. if (empty($goods['attr'])) return $this->error('商品规格不存在或已删除');
  163. $goods['member_price'] > 0 && $goods['member_price'] = 0;
  164. $goods['original_price'] = $goods['price']; //积分商城商品划线价:修改成售价
  165. if ($params['addonsFields']) {
  166. $params['addonsFields'] = is_string($params['addonsFields']) ? explode(',', $params['addonsFields']) : $params['addonsFields'];
  167. $goods = Goods::getAddonsGoodsSetting($this->mall_id, $params['addonsFields'], $goods, 'detail');
  168. }
  169. // 获取积分配置
  170. $score_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score');
  171. // 增加积分商品兑换价
  172. $score_info = ScoreMallGoods::getExchangePrice($score_goods['type'], $score_goods['attr'][0], $score_config, 1, $this->user_id ?? 0);
  173. $prefix = '兑换价:';
  174. $goods['score_deduct'] = $prefix . $score_info['exchange_price'];
  175. $goods['marketing_type'] = AddonsEnum::ADDONS_NAME_SCORE_MALL;
  176. // 商品的规格修改
  177. $score_attr = array_column($score_goods['attr'], null,'goods_attr_id');
  178. foreach ($goods['attr'] as &$attr) {
  179. $score_info = ScoreMallGoods::getExchangePrice($score_goods['type'], $score_attr[$attr['id']], $score_config, 1, $this->user_id ?? 0);
  180. $attr['score_deduct'] = $prefix . $score_info['exchange_price'];
  181. }
  182. return $this->success('获取成功', $goods);
  183. }
  184. /**
  185. * 兑换记录
  186. * @Author: lun
  187. * @DateTime: 2022/1/12 0012 18:58
  188. * @Copyright: copyright (c) 2021 广东七件事集团
  189. * @return mixed|void
  190. */
  191. public function actionExchangeLog()
  192. {
  193. $params = Yii::$app->request->post();
  194. $res = Yii::$app->services->validate->validate($params, [
  195. [['page','limit'], 'integer'],
  196. ]);
  197. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  198. $where = [];
  199. $where[] = ['=', 'mall_id', $this->mall_id];
  200. $where[] = ['=', 'user_id', $this->user_id];
  201. $where[] = ['=', 'status', StatusEnum::ENABLED];
  202. $order = 'created_at desc';
  203. $select = ['goods_id','goods','exchange_price','num','order_no','order_id','created_at'];
  204. $params = array('select' => $select, 'where' => $where, 'order' => $order, 'limit' => $params['limit']);
  205. $list = ScoreMallExchangeRecord::listPage($params, false, true);
  206. if (empty($list)) return $this->success('获取成功');
  207. return $this->success('获取成功', $list);
  208. }
  209. }