IndexController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /*
  3. * @Descripttion: 推三返一控制器
  4. * @copyright: Copyright (c) 2021 广东七件事集团
  5. * @Author: lun
  6. * @Date: 2021-12-28 21:23:39
  7. */
  8. namespace addons\Rebate\api\modules\v1\controllers;
  9. use addons\Rebate\common\models\Rebate;
  10. use addons\Rebate\common\models\RebateBuy;
  11. use addons\Rebate\common\models\RebateRecommendNum;
  12. use addons\Rebate\common\models\RebateRewardLog;
  13. use addons\Rebate\common\models\RebateShare;
  14. use addons\Rebate\common\services\SettingService;
  15. use api\controllers\OnAuthController;
  16. use app\plugins\rebate\models\RebateAd;
  17. use common\enums\AddonsEnum;
  18. use common\enums\OrderStatusEnum;
  19. use common\enums\StatusEnum;
  20. use common\globals\GlobalInvoke;
  21. use common\logic\common\AdLogic;
  22. use common\logic\common\GoodsLogic;
  23. use common\models\order\Order;
  24. use common\models\user\User;
  25. use common\models\goods\Goods;
  26. use Yii;
  27. use yii\base\DynamicModel;
  28. use yii\db\Expression;
  29. use yii\helpers\Json;
  30. use function Clue\StreamFilter\fun;
  31. class IndexController extends OnAuthController
  32. {
  33. public $modelClass = '';
  34. public $enableCsrfValidation = false;
  35. /**
  36. * 不用进行登录验证的方法
  37. *
  38. * 例如: ['index', 'update', 'create', 'view', 'delete']
  39. * 默认全部需要验证
  40. *
  41. * @var array
  42. */
  43. protected $authOptional = ['index','detail'];
  44. const SEARCH_TYPE_ALL = 'all';
  45. const SEARCH_TYPE_ACHIEVED = 'achieved';
  46. const SEARCH_TYPE_UNACHIEVED = 'unachieved';
  47. const SEARCH_TYPE_EXPIRED = 'expired';
  48. /**
  49. * 活动列表
  50. *
  51. */
  52. public function actionIndex()
  53. {
  54. $settingService = new SettingService(['mall_id' => $this->mall_id]);
  55. if (Yii::$app->request->isGet) {
  56. // $params = Yii::$app->request->get();
  57. // $time = time();
  58. // $where = [];
  59. // $where[] = ['=', 'mall_id', $this->mall_id];
  60. // $where[] = ['<=', 'start_at', $time];
  61. // $where[] = ['>=', 'end_at', $time];
  62. // $where[] = ['=', 'status', StatusEnum::ENABLED];
  63. // $order = 'created_at desc';
  64. // $select = "id,item_id,created_at";
  65. // $limit = $params['limit'] ?? 20;
  66. // $with = ['goods' => function($query){
  67. // $query->select("id,goods_name,price,cover_pic");
  68. // }];
  69. // $params = compact('where','order','select','with','limit');
  70. // $list = Rebate::listPage($params, false, true);
  71. //获取全部商品
  72. $all_goods_list = Rebate::getRebateAllGoodsId($this->mall_id);
  73. //用商品ID进行分页
  74. $goods_ids = array_column($all_goods_list, 'item_id');
  75. $goods_ids = array_unique($goods_ids);
  76. $limit = $params['limit'] ?? 20;
  77. $order = '';
  78. if ($goods_ids) {
  79. // 生成 FIELD() 排序条件
  80. $order = new \yii\db\Expression("FIELD(id, " . implode(',', $goods_ids) . ")");
  81. }
  82. $where = [];
  83. $where[] = ['=', 'mall_id', $this->mall_id];
  84. $where[] = ['in', 'id', $goods_ids];
  85. $select = "id,goods_name,price,cover_pic";
  86. $params = compact('where','select', 'order','limit');
  87. $list = Goods::listPage($params, false, true);
  88. foreach ($list['list'] as &$item) {
  89. $item['goods'] = $item;
  90. }
  91. // 获取广告
  92. $ad = AdLogic::getList($this->mall_id, AddonsEnum::ADDONS_NAME_REBATE);
  93. // 获取插件名称
  94. $addons_name = $settingService->getSetting()['addons_name'];
  95. return $this->success('获取成功', compact('list','ad', 'addons_name'));
  96. }
  97. }
  98. /**
  99. * @Author: lun
  100. * @Date: 2022-01-01
  101. * @Time: 17:42
  102. * @Note:我的活动接口
  103. * @return \yii\web\Response
  104. */
  105. public function actionMyActivity()
  106. {
  107. if (Yii::$app->request->isGet) {
  108. $params = Yii::$app->request->get();
  109. $user_id = $this->user_id;
  110. $now_time = time();
  111. $where = [];
  112. $where[] = ['=', 'mall_id', $this->mall_id];
  113. $where[] = ['=', 'user_id', $user_id];
  114. $where[] = ['=', 'status', StatusEnum::ENABLED];
  115. $order = 'created_at desc';
  116. $select = "id,r_id,share_num,pay_num";
  117. $limit = $params['limit'] ?? 20;
  118. $with = ['activity' => function($query){
  119. $query->select("id,title,start_at,end_at,item_type,item_id,item_info,status,is_freeze_first_reward");
  120. }];
  121. $params = compact('where','order','select','with','limit');
  122. $list = RebateRecommendNum::listPage($params, false, true);
  123. if($list) {
  124. foreach($list['list'] as $key => &$item) {
  125. $item['goods'] = Json::decode($item['activity']['item_info'], true);
  126. $item['recommend_num'] = RebateBuy::GetRecommendNumCount($this->mall_id, $this->user_id, $item['r_id'], $item['order_no']);
  127. // 判断是否已结束
  128. $activity = $item['activity'];
  129. if($activity['status'] == 1 && $activity['end_at'] <= $now_time) $item['activity']['status'] = 2;
  130. }
  131. }
  132. return $this->success('获取成功', compact('list','now_time'));
  133. }
  134. }
  135. /**
  136. * @Author: lun
  137. * @Date: 2022-01-01
  138. * @Time: 17:42
  139. * @Note:邀请人数列表接口
  140. * @return \yii\web\Response
  141. */
  142. public function actionInviteNum()
  143. {
  144. if (Yii::$app->request->isPost) {
  145. $params = Yii::$app->request->post();
  146. // 验证
  147. $res = Yii::$app->services->validate->validate($params, [
  148. [['r_id'], 'required'],
  149. [['r_id'], 'integer'],
  150. ]);
  151. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  152. $user_id = $this->user_id;
  153. $where = [];
  154. $where[] = ['=', 'mall_id', $this->mall_id];
  155. $where[] = ['=', 'r_id', $params['r_id']];
  156. $where[] = ['=', 'pid', $user_id];
  157. $where[] = ['=', 'status', StatusEnum::ENABLED];
  158. $order = 'created_at desc';
  159. $select = "id,user_id,created_at";
  160. $limit = $params['limit'] ?? 20;
  161. // 关联查询用户
  162. $with = ['user' => function($query){
  163. $query->select("id,nickname,avatar_url");
  164. }];
  165. $params = compact('where','order','select','with','limit');
  166. $list = RebateShare::listPage($params, false, true);
  167. return $this->success('获取成功', $list);
  168. }
  169. }
  170. /**
  171. * @Author: lun
  172. * @Date: 2022-01-01
  173. * @Time: 17:42
  174. * @Note:推广下单数量列表接口
  175. * @return \yii\web\Response
  176. */
  177. public function actionPayNum()
  178. {
  179. if (Yii::$app->request->isPost) {
  180. $params = Yii::$app->request->post();
  181. // 验证
  182. $res = Yii::$app->services->validate->validate($params, [
  183. [['r_id'], 'required'],
  184. [['r_id'], 'integer'],
  185. [['type'], 'string'],
  186. ]);
  187. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  188. $user_id = $this->user_id;
  189. $where = [];
  190. $where[] = ['=', 'mall_id', $this->mall_id];
  191. $where[] = ['=', 'r_id', $params['r_id']];
  192. $where[] = ['=', 'pid', $user_id];
  193. $where[] = ['=', 'status', StatusEnum::ENABLED];
  194. $order = 'created_at desc';
  195. $select = "id,user_id,order_no,pay_at,pay_money,goods_info,order_settlement_status,refund_id";
  196. $limit = $params['limit'] ?? 20;
  197. if (!empty($params['type'])) {
  198. switch ($params['type']) {
  199. case self::SEARCH_TYPE_ACHIEVED:
  200. $order = sprintf('settlement_at desc, %s', $order);
  201. $where[] = ['=', 'order_settlement_status', StatusEnum::ENABLED];
  202. break;
  203. case self::SEARCH_TYPE_UNACHIEVED:
  204. $where[] = ['=', 'order_settlement_status', StatusEnum::DISABLED];
  205. $where[] = ['=', 'refund_id', StatusEnum::DISABLED];
  206. break;
  207. case self::SEARCH_TYPE_EXPIRED:
  208. $order = sprintf('refund_at desc, %s', $order);
  209. $where[] = ['=', 'order_settlement_status', StatusEnum::DISABLED];
  210. $where[] = ['>', 'refund_id', StatusEnum::DISABLED];
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. // 关联查询用户
  217. $with = ['user' => function($query){
  218. $query->select("id,nickname,avatar_url");
  219. }];
  220. $params = compact('where','order','select','with','limit');
  221. $list = RebateBuy::listPage($params, false, true);
  222. $orderNoArr = array_column($list['list'], 'order_no');
  223. $orderList = Order::find()->where(['order_no' => $orderNoArr])->select('id,order_no,order_status')->asArray()->indexBy('order_no')->all();
  224. if($list['list']) {
  225. foreach ($list['list'] as $key => $item) {
  226. $list['list'][$key]['goods_info'] = Json::decode($item['goods_info'], true);
  227. // 兼容虚拟订单问题,虚拟订单状态当作已完成
  228. $list['list'][$key]['order'] = $orderList[$item['order_no']] ?? ['id' => 0, 'order_no' => $item['order_no'], 'order_status' => OrderStatusEnum::ACCOMPLISH];
  229. }
  230. }
  231. return $this->success('获取成功', $list);
  232. }
  233. }
  234. public function actionGetTabOption()
  235. {
  236. $rid = $this->request->get('rid', 0);
  237. if ($rid && is_numeric($rid)) {
  238. $select = "COUNT(*) AS order_total," .
  239. "COUNT(IF(order_settlement_status = 1, 1, NULL)) AS achieved_total," .
  240. "COUNT(IF(order_settlement_status = 0 AND refund_id = 0, 1, NULL)) AS unachieved_total," .
  241. "COUNT(IF(order_settlement_status = 0 AND refund_id > 0, 1, NULL)) AS expired_total";
  242. $params = [
  243. 'select' => new Expression($select),
  244. 'where' => [
  245. [
  246. 'mall_id' => $this->mall_id,
  247. 'status' => StatusEnum::ENABLED,
  248. 'r_id' => $rid,
  249. 'pid' => $this->user_id
  250. ]
  251. ]
  252. ];
  253. $find = RebateBuy::find();
  254. RebateBuy::paramPack($params, $find);
  255. $statistics = $find->asArray()->one();
  256. }
  257. $this->success('success', [
  258. [
  259. 'name' => sprintf('全部(%d)', $statistics['order_total'] ?? 0),
  260. 'search_type' => self::SEARCH_TYPE_ALL,
  261. ],
  262. [
  263. 'name' => sprintf('已达标(%d)', $statistics['achieved_total'] ?? 0),
  264. 'search_type' => self::SEARCH_TYPE_ACHIEVED,
  265. ],
  266. [
  267. 'name' => sprintf('未达标(%d)', $statistics['unachieved_total'] ?? 0),
  268. 'search_type' => self::SEARCH_TYPE_UNACHIEVED,
  269. ],
  270. [
  271. 'name' => sprintf('已失效(%d)', $statistics['expired_total'] ?? 0),
  272. 'search_type' => self::SEARCH_TYPE_EXPIRED,
  273. ],
  274. ]);
  275. }
  276. /**
  277. * 商品详情
  278. * @Author: lun
  279. * @DateTime: 2022/1/18 0018 19:20
  280. * @Copyright: copyright (c) 2021 广东七件事集团
  281. * @return mixed|void
  282. */
  283. public function actionDetail()
  284. {
  285. $params = Yii::$app->request->get();
  286. $res = Yii::$app->services->validate->validate($params, [
  287. [['goods_id','pid'], 'integer'],
  288. ]);
  289. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  290. // 判断是否是分享
  291. $parent_id = $this->user['parent_id'] ?? 0;
  292. $headers = Yii::$app->request->headers;
  293. if (!empty($params['pid']) && $parent_id == $headers['x-parent-id']) {
  294. // 判断是否符合分享条件,并保存数据
  295. RebateShare::checkShare($this->mall_id, $params['goods_id'], $this->user_id, $parent_id);
  296. }
  297. // 根据商品id获取商品详情信息
  298. $goods = GoodsLogic::GetDetail($this->mall_id, $params, $this->user);
  299. if ($goods == false) return $this->error("获取失败," . GoodsLogic::getStaticError());
  300. // 智能表单
  301. $smart_form_template = GlobalInvoke::exec(GlobalInvoke::GOODS_NEED_SMARTFORM,
  302. $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'goods_id' => $params['goods_id']]);
  303. if (!empty($smart_form_template)) $goods = array_merge($goods, $smart_form_template);
  304. // 商品详情公共调用
  305. $invoke_data = GlobalInvoke::exec(GlobalInvoke::API_GOODS_DETAIL, $this->mall_id,
  306. ['mall_id' => $this->mall_id, 'goods' => $goods, 'user' => $this->user]);
  307. if (!empty($invoke_data['goods'])) $goods = $invoke_data['goods'];
  308. return $this->success('获取成功', $goods);
  309. }
  310. /**
  311. * 活动规则接口
  312. *
  313. * @return void
  314. */
  315. public function actionExplain()
  316. {
  317. $settingService = new SettingService(['mall_id' => $this->mall_id]);
  318. if (Yii::$app->request->isGet) {
  319. $explain = $settingService->getSetting()['explain'];
  320. return $this->success('获取成功', $explain);
  321. }
  322. }
  323. /**
  324. * 奖励明细列表
  325. *
  326. * @return void
  327. */
  328. public function actionRewardLog()
  329. {
  330. if (Yii::$app->request->isPost) {
  331. $params = Yii::$app->request->post();
  332. $res = Yii::$app->services->validate->validate($params, [
  333. [['r_id'], 'required'],
  334. [['r_id'], 'integer'],
  335. ]);
  336. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  337. $user_id = $this->user_id;
  338. $where = [];
  339. $where[] = ['=', 'mall_id', $this->mall_id];
  340. $where[] = ['=', 'r_id', $params['r_id']];
  341. $where[] = ['=', 'user_id', $user_id];
  342. $where[] = ['=', 'status', StatusEnum::ENABLED];
  343. $order = 'created_at desc';
  344. $select = "reward_title,content,created_at,is_frozen";
  345. $limit = $params['limit'] ?? 20;
  346. $params = compact('where','order','select','limit');
  347. $list = RebateRewardLog::listPage($params, false, true);
  348. return $this->success('获取成功', $list);
  349. }
  350. }
  351. }