DiyTemplateForm.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\diy;
  8. use app\models\Coupon;
  9. use app\models\DiyPage;
  10. use app\models\Mch;
  11. use app\models\Topic;
  12. use app\models\Goods;
  13. use app\models\SupplierGoods;
  14. use app\models\SupplierSetting;
  15. use yii\base\Model;
  16. class DiyTemplateForm extends Model
  17. {
  18. public $page_id;
  19. public static $topicList;
  20. private $couponList;
  21. private $integralCouponList;
  22. public $store_id;
  23. public function rules()
  24. {
  25. return [
  26. [['page_id'], 'integer']
  27. ];
  28. }
  29. public function detail()
  30. {
  31. // if (!$this->validate()) {
  32. // return $this->getErrorResponse();
  33. // }
  34. // $diy_page_cache_name = md5('diy_page_cache_name_' . $this->page_id . '_' . $this->store_id);
  35. // $cache = \Yii::$app->cache->get($diy_page_cache_name);
  36. // if ($cache) {
  37. // return $cache;
  38. // }
  39. $actModalList = [];
  40. if ($this->page_id > 0) {
  41. $page = DiyPage::findOne(['id' => $this->page_id, 'is_delete' => 0, 'status' => 1, 'store_id' => $this->store_id]);
  42. } else {
  43. $page = DiyPage::findOne(['is_delete' => 0, 'status' => 1, 'is_index' => 1, 'store_id' => $this->store_id]);
  44. }
  45. if (!$page) {
  46. return [
  47. 'code' => 1,
  48. 'msg' => '页面不存在'
  49. ];
  50. }
  51. $detail = $page->getTemplate()->asArray()->one();
  52. if ($detail) {
  53. $detail['template'] = ($detail['template'] == 'null' || !$detail['template']) ? [] : json_decode($detail['template'], true);
  54. DiyGoods::getTemplate($detail['template']);
  55. $timeAll = [];
  56. foreach ($detail['template'] as $index => &$item) {
  57. switch ($item['type']) {
  58. case 'topic':
  59. // 专题
  60. $newTopicList = [];
  61. if ($item['param']['style'] == 0) {
  62. $item['param']['logo_1'] = $item['param']['logo_1'] ? $item['param']['logo_1'] : \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/home-page/icon-topic-1.png';
  63. $item['param']['logo_2'] = $item['param']['logo_2'] ? $item['param']['logo_2'] : \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/home-page/icon-topic.png';
  64. $item['param']['heated'] = $item['param']['heated'] ? $item['param']['heated'] : \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/home-page/icon-topic-r.png';
  65. $topicList = self::getTopic();
  66. $newItem = [];
  67. foreach ($topicList as $key => $topic) {
  68. $newItem[] = $topic;
  69. if (count($newItem) > 0 && (count($newItem) == intval($item['param']['count']) || $key == count($topicList) - 1)) {
  70. $newTopicList[] = $newItem;
  71. $newItem = [];
  72. }
  73. }
  74. } else {
  75. $item['param']['cat_index'] = 0;
  76. $list = [];
  77. foreach ($item['param']['list'] as $key => $value) {
  78. if ($value['cat_id'] && $value['goods_style'] != 2) {
  79. $res = DiyGoods::getGoods('topic', [], $value['cat_id'], 10, true);
  80. $goodsList = $res['goods_list'];
  81. if ($value['goods_style'] == 1) {
  82. array_splice($goodsList, $value['goods_num']);
  83. }
  84. $value['goods_list'] = $goodsList;
  85. }
  86. //显示、隐藏 begin
  87. foreach ($value['goods_list'] as $k => $v) {
  88. $topic_info = Topic::findOne(['id' => $v['id'],'store_id' => \Yii::$app->store->id, 'is_delete' => 0]);
  89. if($topic_info->is_show == 1){
  90. unset($value['goods_list'][$k]);
  91. }
  92. }
  93. //显示、隐藏 end
  94. if (count($value['goods_list']) > 0) {
  95. $list[] = $value;
  96. }
  97. }
  98. $item['param']['list'] = $list;
  99. }
  100. $item['param']['topic_list'] = $newTopicList;
  101. break;
  102. case 'video':
  103. $res = \app\modules\client\models\v1\diy\GetInfo::getVideoInfo($item['param']['url']);
  104. if ($res && $res['code'] == 0) {
  105. $item['param']['url'] = $res['url'];
  106. }
  107. $item['param']['id'] = $index;
  108. break;
  109. case 'coupon':
  110. $item['param']['coupon_list'] = Coupon::getList();
  111. break;
  112. case 'banner':
  113. foreach ($item['param']['list'] as &$value) {
  114. if ($value['open_type'] == 'wxapp') {
  115. $res = self::getUrl($value['page_url']);
  116. $value['appId'] = $res[2];
  117. $value['path'] = urldecode($res[4]);
  118. }
  119. }
  120. unset($value);
  121. $item['param']['banner_list'] = $item['param']['list'];
  122. break;
  123. case 'nav':
  124. $nav_arr = [];
  125. $nav = [];
  126. foreach ($item['param']['list'] as $k => &$v) {
  127. if ($k % ($item['param']['count'] * $item['param']['col']) == 0) {
  128. if (count($nav) > 0) {
  129. $nav_arr[] = $nav;
  130. }
  131. $nav = [];
  132. }
  133. $nav[] = $v;
  134. }
  135. if (count($nav) > 0) {
  136. $nav_arr[] = $nav;
  137. }
  138. $item['param']['nav_list'] = $nav_arr;
  139. $item['param']['is_slide'] = $item['param']['is_slide'] ? 'true' : 'false';
  140. break;
  141. case 'rubik':
  142. foreach ($item['param']['list'] as &$value) {
  143. if (isset($value['open_type']) && $value['open_type'] == 'wxapp') {
  144. $res = self::getUrl($value['url']);
  145. $value['appId'] = $res[2];
  146. $value['path'] = urldecode($res[4]);
  147. }
  148. }
  149. unset($value);
  150. $item['param']['nav_list'] = $item['param']['list'];
  151. break;
  152. case 'link':
  153. if (isset($item['param']['open_type']) && $item['param']['open_type'] == 'wxapp') {
  154. $res = self::getUrl($item['param']['url']);
  155. $item['param']['appId'] = $res[2];
  156. $item['param']['path'] = urldecode($res[4]);
  157. }
  158. break;
  159. case 'goods':
  160. $param = &$item['param'];
  161. $list = [];
  162. foreach ($param['list'] as $key => &$value) {
  163. if ($value['cat_id'] && $param['list'][$key]['goods_style'] != 2) {
  164. $res = DiyGoods::getGoods('goods', [], $param['list'][$key]['cat_id'], 30, true);
  165. $goodsList = $res['goods_list'];
  166. if ($param['list'][$key]['goods_style'] == 1) {
  167. array_splice($goodsList, $param['list'][$key]['goods_num']);
  168. }
  169. $value['goods_list'] = $goodsList;
  170. }
  171. if (count($value['goods_list']) > 0) {
  172. $list[] = $value;
  173. }
  174. }
  175. $param['list'] = $list;
  176. break;
  177. case 'recommend_goods':
  178. $param = &$item['param'];
  179. $list = [];
  180. foreach ($param['list'] as $key => &$value) {
  181. $ids = [];
  182. foreach ($value['goods_list'] as $r_goods) {
  183. $ids[] = $r_goods['id'];
  184. }
  185. $res = DiyGoods::getGoods('goods', $ids, 0, 30, true);
  186. $value['goods_list'] = $res['goods_list'];
  187. if (count($value['goods_list']) > 0) {
  188. $list[] = $value;
  189. }
  190. }
  191. $param['list'] = $list;
  192. break;
  193. case 'time':
  194. $data_1 = explode('T',$item['param']['date_start']);
  195. $data_2 = explode('.',$data_1[1]);
  196. $item['param']['date_start'] = $data_1[0].' '.$data_2[0];
  197. $data_1 = explode('T',$item['param']['date_end']);
  198. $data_2 = explode('.',$data_1[1]);
  199. $item['param']['date_end'] = $data_1[0].' '.$data_2[0];
  200. $startTime = strtotime($item['param']['date_start']) - time();
  201. $endTime = strtotime($item['param']['date_end']) - time();
  202. $item['param']['start_time'] = ($startTime) <= 0 ? 0 : ($startTime);
  203. $item['param']['end_time'] = ($endTime) <= 0 ? 0 : ($endTime);
  204. $timeAll['time_' . $index] = $item;
  205. break;
  206. case 'modal':
  207. $item['param']['page_id'] = $page->id;
  208. $newList = [];
  209. foreach ($item['param']['list'] as &$value) {
  210. if ($value['open_type'] == 'wxapp') {
  211. $res = self::getUrl($value['url']);
  212. $value['appId'] = $res[2];
  213. $value['path'] = urldecode($res[4]);
  214. }
  215. if ($value['pic_url']) {
  216. $newList[] = $value;
  217. }
  218. }
  219. $item['param']['list'] = $newList;
  220. unset($value);
  221. $actModalList[] = $item['param'];
  222. break;
  223. case 'miaosha':
  224. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '马上秒';
  225. $item['param']['type_name'] = '秒杀';
  226. $newList = [];
  227. foreach ($item['param']['list'] as &$value) {
  228. $goodsList = [];
  229. foreach ($value['goods_list'] as $k => $v) {
  230. $startTime = strtotime($v['open_date'] . ' ' . $v['start_time'] . ':00:00');
  231. if ($startTime >= time()) {
  232. $v['is_start'] = 0;
  233. $v['time'] = $startTime - time();
  234. $v['time_end'] = $startTime - time() + 3600;
  235. $v['time_content'] = $item['param']['list_style'] == 1 ? "活动未开始" : "距开始仅剩";
  236. } else if ($startTime <= time() && $startTime + 3600 >= time()) {
  237. $v['is_start'] = 1;
  238. $v['time'] = $startTime - time() + 3600;
  239. $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
  240. } else {
  241. $v['is_start'] = 1;
  242. $v['time'] = 0;
  243. $v['time_content'] = "已结束";
  244. continue;
  245. }
  246. $goodsList[] = $v;
  247. }
  248. if (count($goodsList) > 0) {
  249. $value['goods_list'] = $goodsList;
  250. $newList[] = $value;
  251. }
  252. }
  253. $item['param']['list'] = $newList;
  254. unset($value);
  255. if (count($item['param']['list']) > 0) {
  256. $timeAll['miaosha_' . $index] = $item;
  257. }
  258. break;
  259. case 'pintuan':
  260. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '去拼团';
  261. $item['param']['type_name'] = '拼团';
  262. if (count($item['param']['list']) > 0) {
  263. $timeAll['pintuan_' . $index] = $item;
  264. }
  265. break;
  266. case 'book':
  267. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '预约';
  268. break;
  269. case 'bargain':
  270. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '去参与';
  271. $item['param']['type_name'] = '砍价';
  272. $newList = [];
  273. foreach ($item['param']['list'] as &$value) {
  274. $goodsList = [];
  275. foreach ($value['goods_list'] as $k => $v) {
  276. $startTime = $v['begin_time'];
  277. $endTime = $v['end_time'];
  278. if ($startTime >= time()) {
  279. $v['is_start'] = 0;
  280. $v['time'] = $startTime - time();
  281. $v['time_end'] = $endTime - time();
  282. $v['time_content'] = $item['param']['list_style'] == 1 ? "活动未开始" : "距开始仅剩";
  283. } else if ($startTime <= time() && $endTime >= time()) {
  284. $v['is_start'] = 1;
  285. $v['time'] = $endTime - time();
  286. $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
  287. } else {
  288. $v['is_start'] = 1;
  289. $v['time'] = 0;
  290. $v['time_content'] = "已结束";
  291. continue;
  292. }
  293. $goodsList[] = $v;
  294. }
  295. if (count($goodsList) > 0) {
  296. $value['goods_list'] = $goodsList;
  297. $newList[] = $value;
  298. }
  299. }
  300. $item['param']['list'] = $newList;
  301. unset($value);
  302. if (count($item['param']['list']) > 0) {
  303. $timeAll['bargain_' . $index] = $item;
  304. }
  305. break;
  306. case 'lottery':
  307. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : $item['param']['buy_default'];
  308. $item['param']['type_name'] = '抽奖';
  309. $newList = [];
  310. foreach ($item['param']['list'] as &$value) {
  311. $goodsList = [];
  312. foreach ($value['goods_list'] as $k => $v) {
  313. $startTime = $v['begin_time'];
  314. $endTime = $v['end_time'];
  315. if ($startTime >= time()) {
  316. $v['is_start'] = 0;
  317. $v['time'] = $startTime - time();
  318. $v['time_end'] = $endTime - time();
  319. $v['time_content'] = "距开始仅剩";
  320. } else if ($startTime <= time() && $endTime >= time()) {
  321. $v['is_start'] = 1;
  322. $v['time'] = $endTime - time();
  323. $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
  324. } else {
  325. $v['is_start'] = 1;
  326. $v['time'] = 0;
  327. $v['time_content'] = "已结束";
  328. continue;
  329. }
  330. $goodsList[] = $v;
  331. }
  332. if (count($goodsList) > 0) {
  333. $value['goods_list'] = $goodsList;
  334. $newList[] = $value;
  335. }
  336. }
  337. $item['param']['list'] = $newList;
  338. $item['param']['list_style'] = $item['param']['list_type'];
  339. unset($value);
  340. if (count($item['param']['list']) > 0) {
  341. $timeAll['lottery_' . $index] = $item;
  342. }
  343. break;
  344. case 'mch':
  345. $param = &$item['param'];
  346. $mchList = [];
  347. foreach ($item['param']['list'] as $key => &$value) {
  348. if ($param['is_goods']) {
  349. if ($value['mch_id'] && $param['list'][$key]['goods_style'] != 2) {
  350. $res = DiyGoods::getGoods('goods', [], 0, 10, false, 1, $param['list'][$key]['mch_id']);
  351. $goods_list = $res['goods_list'];
  352. if ($param['list'][$key]['goods_style'] == 1) {
  353. array_splice($goods_list, $param['list'][$key]['goods_num']);
  354. }
  355. $value['goods_list'] = $goods_list;
  356. }
  357. }
  358. $mchList[] = $value;
  359. }
  360. unset($value);
  361. $item['param']['list'] = $mchList;
  362. break;
  363. case 'integral':
  364. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '立即兑换';
  365. if ($item['param']['is_coupon'] == 1) {
  366. $item['param']['coupon_list'] = $this->getIntegralCouponList();
  367. }
  368. break;
  369. case 'float':
  370. $store = [
  371. 'option' => [
  372. 'quick_map' => [
  373. 'status' => $item['param']['quick_map_status'],
  374. 'icon' => $item['param']['icon'],
  375. 'address' => $item['param']['address'],
  376. 'lal' => $item['param']['lal'],
  377. ],
  378. 'web_service_status' => $item['param']['web_service_status'],
  379. 'web_service_url' => $item['param']['web_service_url'],
  380. 'web_service' => $item['param']['web_service'],
  381. 'wxapp' => [
  382. 'status' => $item['param']['wxapp_status'],
  383. 'appid' => $item['param']['appid'],
  384. 'path' => $item['param']['path'],
  385. 'pic_url' => $item['param']['pic_url'],
  386. ]
  387. ],
  388. 'dial' => $item['param']['dial'],
  389. 'dial_pic' => $item['param']['dial_pic'],
  390. 'contact_tel' => $item['param']['dial_tel'],
  391. 'show_customer_service' => $item['param']['show_customer_service'],
  392. 'service' => $item['param']['service'],
  393. ];
  394. $setnavi = [
  395. 'type' => $item['param']['cat_style'],
  396. 'home_img' => $item['param']['home_img'],
  397. ];
  398. $click_pic = [
  399. 'open' => $item['param']['open'],
  400. 'close' => $item['param']['close']
  401. ];
  402. $item['param']['store'] = $store;
  403. $item['param']['setnavi'] = $setnavi;
  404. $item['param']['click_pic'] = $click_pic;
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. unset($item);
  411. $return = [
  412. 'code' => 0,
  413. 'msg' => '请求成功',
  414. 'data' => [
  415. 'template' => $detail['template'],
  416. 'act_modal_list' => $actModalList,
  417. 'info' => $page->title,
  418. 'page_id' => $page->id,
  419. 'status' => 'diy',
  420. 'time_all' => $timeAll
  421. ]
  422. ];
  423. // \Yii::$app->cache->set($diy_page_cache_name, $return);
  424. return $return;
  425. }
  426. }
  427. /**
  428. * 获取整点秒杀数据
  429. * @param $num
  430. */
  431. public function getHourlyMiaosha($num)
  432. {
  433. $date = date('Y-m-d', time());
  434. $time = date('H', time());
  435. $query = MiaoshaGoods::find()->alias('mg')->where([
  436. 'mg.store_id' => $this->store->id, 'mg.is_delete' => 0
  437. ])->joinWith('msGoods g')->andWhere(['g.is_delete' => 0, 'g.status' => 1]);
  438. $query->andWhere([
  439. 'and',
  440. ['mg.open_date' => $date],
  441. ['mg.start_time' => $time],
  442. ]);
  443. $list = $query->all();
  444. $goodsList = [];
  445. /* @var MiaoshaGoods[] $list */
  446. foreach ($list as $key => $item) {
  447. /* @var MsGoods $msGoods */
  448. $msGoods = $item->msGoods;
  449. $price = $msGoods->original_price;
  450. $attr = json_decode($item['attr'], true);
  451. foreach ($attr as &$value) {
  452. $value['num'] = $value['miaosh_num'];
  453. $value['price'] = $value['miaosh_price'];
  454. if ($price == 0 || ($price > 0 && round($price, 2) >= round($value['miaosha_price'], 2))) {
  455. $price = $value['miaosha_price'];
  456. }
  457. }
  458. unset($value);
  459. $goodsList[] = [
  460. 'attr' => $attr,
  461. 'attr_group' => json_decode(json_encode($msGoods->getAttrGroupList()), true),
  462. 'id' => $item->id,
  463. 'goods_id' => $msGoods->id,
  464. 'pic_url' => $msGoods->cover_pic,
  465. 'cover_pic' => $msGoods->cover_pic,
  466. 'price' => round($price, 2) ? round($price, 2) : 0,
  467. 'original_price' => $msGoods->original_price,
  468. 'name' => $msGoods->name,
  469. 'page_url' => '/pages/miaosha/details/details?id=' . $item->id,
  470. 'use_attr' => $msGoods->use_attr,
  471. 'open_date' => $item->open_date,
  472. 'start_time' => $item->start_time,
  473. 'is_negotiable' => 0,
  474. ];
  475. }
  476. $miaosha = Miaosha::findOne([
  477. 'store_id' => $this->store->id,
  478. 'mch_id' => 0,
  479. ]);
  480. $miaosha_open_date = json_decode($miaosha->open_time, true);
  481. // 获取下一场
  482. $next_date = $date;
  483. $is_next = false;
  484. if ($miaosha_open_date && $time == end($miaosha_open_date)) {
  485. $is_next = true;
  486. $next_date = date('Y-m-d', strtotime("+1 day"));
  487. }
  488. $next_query = MiaoshaGoods::find()->where([
  489. 'store_id' => $this->store->id,
  490. 'is_delete' => 0,
  491. 'open_date' => $next_date,
  492. 'mch_id' => 0,
  493. ]);
  494. if (!$is_next) {
  495. $next_query->andWhere([
  496. '>', 'start_time', $time
  497. ]);
  498. }
  499. $next = $next_query->orderBy('start_time ASC')->one();
  500. $next_time = null;
  501. if ($next) {
  502. $next_time = strtotime($next->open_date . ' ' . $next->start_time .':00:00');
  503. }
  504. return [
  505. 'goods_list' => $goodsList,
  506. 'next' => $next_time,
  507. ];
  508. }
  509. public static function getTopic()
  510. {
  511. $topicList = self::$topicList;
  512. if ($topicList) {
  513. return $topicList;
  514. }
  515. $topicList = Topic::find()->where(['store_id' => get_store_id(), 'is_delete' => 0,'is_show' => 1])
  516. ->orderBy('sort ASC,created_at DESC')->limit(6)->select('id,title')->asArray()->all();
  517. self::$topicList = $topicList;
  518. return $topicList;
  519. }
  520. public static function getUrl($url)
  521. {
  522. preg_match('/^[^\?+]\?([\w|\W]+)=([\w|\W]*?)&([\w|\W]+)=([\w|\W]*?)$/', $url, $res);
  523. return $res;
  524. }
  525. private function getIntegralCouponList()
  526. {
  527. if ($this->integralCouponList) {
  528. return $this->integralCouponList;
  529. }
  530. $integralCouponList = Coupon::find()->where(['store_id' => $this->store->id, 'is_delete' => 0, 'is_integral' => 2])
  531. ->orderBy('sort ASC')->asArray()->all();
  532. foreach ($integralCouponList as &$value) {
  533. $value['receive_content'] = "立即兑换";
  534. }
  535. $this->integralCouponList = $integralCouponList;
  536. return $integralCouponList;
  537. }
  538. }