| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\diy;
- use app\models\Coupon;
- use app\models\DiyPage;
- use app\models\Mch;
- use app\models\Topic;
- use app\models\Goods;
- use app\models\SupplierGoods;
- use app\models\SupplierSetting;
- use yii\base\Model;
- class DiyTemplateForm extends Model
- {
- public $page_id;
- public static $topicList;
- private $couponList;
- private $integralCouponList;
- public $store_id;
- public function rules()
- {
- return [
- [['page_id'], 'integer']
- ];
- }
- public function detail()
- {
- // if (!$this->validate()) {
- // return $this->getErrorResponse();
- // }
- // $diy_page_cache_name = md5('diy_page_cache_name_' . $this->page_id . '_' . $this->store_id);
- // $cache = \Yii::$app->cache->get($diy_page_cache_name);
- // if ($cache) {
- // return $cache;
- // }
- $actModalList = [];
- if ($this->page_id > 0) {
- $page = DiyPage::findOne(['id' => $this->page_id, 'is_delete' => 0, 'status' => 1, 'store_id' => $this->store_id]);
- } else {
- $page = DiyPage::findOne(['is_delete' => 0, 'status' => 1, 'is_index' => 1, 'store_id' => $this->store_id]);
- }
- if (!$page) {
- return [
- 'code' => 1,
- 'msg' => '页面不存在'
- ];
- }
- $detail = $page->getTemplate()->asArray()->one();
- if ($detail) {
- $detail['template'] = ($detail['template'] == 'null' || !$detail['template']) ? [] : json_decode($detail['template'], true);
- DiyGoods::getTemplate($detail['template']);
- $timeAll = [];
- foreach ($detail['template'] as $index => &$item) {
- switch ($item['type']) {
- case 'topic':
- // 专题
- $newTopicList = [];
- if ($item['param']['style'] == 0) {
- $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';
- $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';
- $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';
- $topicList = self::getTopic();
- $newItem = [];
- foreach ($topicList as $key => $topic) {
- $newItem[] = $topic;
- if (count($newItem) > 0 && (count($newItem) == intval($item['param']['count']) || $key == count($topicList) - 1)) {
- $newTopicList[] = $newItem;
- $newItem = [];
- }
- }
- } else {
- $item['param']['cat_index'] = 0;
- $list = [];
- foreach ($item['param']['list'] as $key => $value) {
- if ($value['cat_id'] && $value['goods_style'] != 2) {
- $res = DiyGoods::getGoods('topic', [], $value['cat_id'], 10, true);
- $goodsList = $res['goods_list'];
- if ($value['goods_style'] == 1) {
- array_splice($goodsList, $value['goods_num']);
- }
- $value['goods_list'] = $goodsList;
- }
- //显示、隐藏 begin
- foreach ($value['goods_list'] as $k => $v) {
- $topic_info = Topic::findOne(['id' => $v['id'],'store_id' => \Yii::$app->store->id, 'is_delete' => 0]);
- if($topic_info->is_show == 1){
- unset($value['goods_list'][$k]);
- }
- }
- //显示、隐藏 end
- if (count($value['goods_list']) > 0) {
- $list[] = $value;
- }
- }
- $item['param']['list'] = $list;
- }
- $item['param']['topic_list'] = $newTopicList;
- break;
- case 'video':
- $res = \app\modules\client\models\v1\diy\GetInfo::getVideoInfo($item['param']['url']);
- if ($res && $res['code'] == 0) {
- $item['param']['url'] = $res['url'];
- }
- $item['param']['id'] = $index;
- break;
- case 'coupon':
- $item['param']['coupon_list'] = Coupon::getList();
- break;
- case 'banner':
- foreach ($item['param']['list'] as &$value) {
- if ($value['open_type'] == 'wxapp') {
- $res = self::getUrl($value['page_url']);
- $value['appId'] = $res[2];
- $value['path'] = urldecode($res[4]);
- }
- }
- unset($value);
- $item['param']['banner_list'] = $item['param']['list'];
- break;
- case 'nav':
- $nav_arr = [];
- $nav = [];
- foreach ($item['param']['list'] as $k => &$v) {
- if ($k % ($item['param']['count'] * $item['param']['col']) == 0) {
- if (count($nav) > 0) {
- $nav_arr[] = $nav;
- }
- $nav = [];
- }
- $nav[] = $v;
- }
- if (count($nav) > 0) {
- $nav_arr[] = $nav;
- }
- $item['param']['nav_list'] = $nav_arr;
- $item['param']['is_slide'] = $item['param']['is_slide'] ? 'true' : 'false';
- break;
- case 'rubik':
- foreach ($item['param']['list'] as &$value) {
- if (isset($value['open_type']) && $value['open_type'] == 'wxapp') {
- $res = self::getUrl($value['url']);
- $value['appId'] = $res[2];
- $value['path'] = urldecode($res[4]);
- }
- }
- unset($value);
- $item['param']['nav_list'] = $item['param']['list'];
- break;
- case 'link':
- if (isset($item['param']['open_type']) && $item['param']['open_type'] == 'wxapp') {
- $res = self::getUrl($item['param']['url']);
- $item['param']['appId'] = $res[2];
- $item['param']['path'] = urldecode($res[4]);
- }
- break;
- case 'goods':
- $param = &$item['param'];
- $list = [];
- foreach ($param['list'] as $key => &$value) {
- if ($value['cat_id'] && $param['list'][$key]['goods_style'] != 2) {
- $res = DiyGoods::getGoods('goods', [], $param['list'][$key]['cat_id'], 30, true);
- $goodsList = $res['goods_list'];
- if ($param['list'][$key]['goods_style'] == 1) {
- array_splice($goodsList, $param['list'][$key]['goods_num']);
- }
- $value['goods_list'] = $goodsList;
- }
- if (count($value['goods_list']) > 0) {
- $list[] = $value;
- }
- }
- $param['list'] = $list;
- break;
- case 'recommend_goods':
- $param = &$item['param'];
- $list = [];
- foreach ($param['list'] as $key => &$value) {
- $ids = [];
- foreach ($value['goods_list'] as $r_goods) {
- $ids[] = $r_goods['id'];
- }
- $res = DiyGoods::getGoods('goods', $ids, 0, 30, true);
- $value['goods_list'] = $res['goods_list'];
- if (count($value['goods_list']) > 0) {
- $list[] = $value;
- }
- }
- $param['list'] = $list;
- break;
- case 'time':
- $data_1 = explode('T',$item['param']['date_start']);
- $data_2 = explode('.',$data_1[1]);
- $item['param']['date_start'] = $data_1[0].' '.$data_2[0];
- $data_1 = explode('T',$item['param']['date_end']);
- $data_2 = explode('.',$data_1[1]);
- $item['param']['date_end'] = $data_1[0].' '.$data_2[0];
- $startTime = strtotime($item['param']['date_start']) - time();
- $endTime = strtotime($item['param']['date_end']) - time();
- $item['param']['start_time'] = ($startTime) <= 0 ? 0 : ($startTime);
- $item['param']['end_time'] = ($endTime) <= 0 ? 0 : ($endTime);
- $timeAll['time_' . $index] = $item;
- break;
- case 'modal':
- $item['param']['page_id'] = $page->id;
- $newList = [];
- foreach ($item['param']['list'] as &$value) {
- if ($value['open_type'] == 'wxapp') {
- $res = self::getUrl($value['url']);
- $value['appId'] = $res[2];
- $value['path'] = urldecode($res[4]);
- }
- if ($value['pic_url']) {
- $newList[] = $value;
- }
- }
- $item['param']['list'] = $newList;
- unset($value);
- $actModalList[] = $item['param'];
- break;
- case 'miaosha':
- $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '马上秒';
- $item['param']['type_name'] = '秒杀';
- $newList = [];
- foreach ($item['param']['list'] as &$value) {
- $goodsList = [];
- foreach ($value['goods_list'] as $k => $v) {
- $startTime = strtotime($v['open_date'] . ' ' . $v['start_time'] . ':00:00');
- if ($startTime >= time()) {
- $v['is_start'] = 0;
- $v['time'] = $startTime - time();
- $v['time_end'] = $startTime - time() + 3600;
- $v['time_content'] = $item['param']['list_style'] == 1 ? "活动未开始" : "距开始仅剩";
- } else if ($startTime <= time() && $startTime + 3600 >= time()) {
- $v['is_start'] = 1;
- $v['time'] = $startTime - time() + 3600;
- $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
- } else {
- $v['is_start'] = 1;
- $v['time'] = 0;
- $v['time_content'] = "已结束";
- continue;
- }
- $goodsList[] = $v;
- }
- if (count($goodsList) > 0) {
- $value['goods_list'] = $goodsList;
- $newList[] = $value;
- }
- }
- $item['param']['list'] = $newList;
- unset($value);
- if (count($item['param']['list']) > 0) {
- $timeAll['miaosha_' . $index] = $item;
- }
- break;
- case 'pintuan':
- $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '去拼团';
- $item['param']['type_name'] = '拼团';
- if (count($item['param']['list']) > 0) {
- $timeAll['pintuan_' . $index] = $item;
- }
- break;
- case 'book':
- $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '预约';
- break;
- case 'bargain':
- $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '去参与';
- $item['param']['type_name'] = '砍价';
- $newList = [];
- foreach ($item['param']['list'] as &$value) {
- $goodsList = [];
- foreach ($value['goods_list'] as $k => $v) {
- $startTime = $v['begin_time'];
- $endTime = $v['end_time'];
- if ($startTime >= time()) {
- $v['is_start'] = 0;
- $v['time'] = $startTime - time();
- $v['time_end'] = $endTime - time();
- $v['time_content'] = $item['param']['list_style'] == 1 ? "活动未开始" : "距开始仅剩";
- } else if ($startTime <= time() && $endTime >= time()) {
- $v['is_start'] = 1;
- $v['time'] = $endTime - time();
- $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
- } else {
- $v['is_start'] = 1;
- $v['time'] = 0;
- $v['time_content'] = "已结束";
- continue;
- }
- $goodsList[] = $v;
- }
- if (count($goodsList) > 0) {
- $value['goods_list'] = $goodsList;
- $newList[] = $value;
- }
- }
- $item['param']['list'] = $newList;
- unset($value);
- if (count($item['param']['list']) > 0) {
- $timeAll['bargain_' . $index] = $item;
- }
- break;
- case 'lottery':
- $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : $item['param']['buy_default'];
- $item['param']['type_name'] = '抽奖';
- $newList = [];
- foreach ($item['param']['list'] as &$value) {
- $goodsList = [];
- foreach ($value['goods_list'] as $k => $v) {
- $startTime = $v['begin_time'];
- $endTime = $v['end_time'];
- if ($startTime >= time()) {
- $v['is_start'] = 0;
- $v['time'] = $startTime - time();
- $v['time_end'] = $endTime - time();
- $v['time_content'] = "距开始仅剩";
- } else if ($startTime <= time() && $endTime >= time()) {
- $v['is_start'] = 1;
- $v['time'] = $endTime - time();
- $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
- } else {
- $v['is_start'] = 1;
- $v['time'] = 0;
- $v['time_content'] = "已结束";
- continue;
- }
- $goodsList[] = $v;
- }
- if (count($goodsList) > 0) {
- $value['goods_list'] = $goodsList;
- $newList[] = $value;
- }
- }
- $item['param']['list'] = $newList;
- $item['param']['list_style'] = $item['param']['list_type'];
- unset($value);
- if (count($item['param']['list']) > 0) {
- $timeAll['lottery_' . $index] = $item;
- }
- break;
- case 'mch':
- $param = &$item['param'];
- $mchList = [];
- foreach ($item['param']['list'] as $key => &$value) {
- if ($param['is_goods']) {
- if ($value['mch_id'] && $param['list'][$key]['goods_style'] != 2) {
- $res = DiyGoods::getGoods('goods', [], 0, 10, false, 1, $param['list'][$key]['mch_id']);
- $goods_list = $res['goods_list'];
- if ($param['list'][$key]['goods_style'] == 1) {
- array_splice($goods_list, $param['list'][$key]['goods_num']);
- }
- $value['goods_list'] = $goods_list;
- }
- }
- $mchList[] = $value;
- }
- unset($value);
- $item['param']['list'] = $mchList;
- break;
- case 'integral':
- $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '立即兑换';
- if ($item['param']['is_coupon'] == 1) {
- $item['param']['coupon_list'] = $this->getIntegralCouponList();
- }
- break;
- case 'float':
- $store = [
- 'option' => [
- 'quick_map' => [
- 'status' => $item['param']['quick_map_status'],
- 'icon' => $item['param']['icon'],
- 'address' => $item['param']['address'],
- 'lal' => $item['param']['lal'],
- ],
- 'web_service_status' => $item['param']['web_service_status'],
- 'web_service_url' => $item['param']['web_service_url'],
- 'web_service' => $item['param']['web_service'],
- 'wxapp' => [
- 'status' => $item['param']['wxapp_status'],
- 'appid' => $item['param']['appid'],
- 'path' => $item['param']['path'],
- 'pic_url' => $item['param']['pic_url'],
- ]
- ],
- 'dial' => $item['param']['dial'],
- 'dial_pic' => $item['param']['dial_pic'],
- 'contact_tel' => $item['param']['dial_tel'],
- 'show_customer_service' => $item['param']['show_customer_service'],
- 'service' => $item['param']['service'],
- ];
- $setnavi = [
- 'type' => $item['param']['cat_style'],
- 'home_img' => $item['param']['home_img'],
- ];
- $click_pic = [
- 'open' => $item['param']['open'],
- 'close' => $item['param']['close']
- ];
- $item['param']['store'] = $store;
- $item['param']['setnavi'] = $setnavi;
- $item['param']['click_pic'] = $click_pic;
- break;
- default:
- break;
- }
- }
- unset($item);
- $return = [
- 'code' => 0,
- 'msg' => '请求成功',
- 'data' => [
- 'template' => $detail['template'],
- 'act_modal_list' => $actModalList,
- 'info' => $page->title,
- 'page_id' => $page->id,
- 'status' => 'diy',
- 'time_all' => $timeAll
- ]
- ];
- // \Yii::$app->cache->set($diy_page_cache_name, $return);
- return $return;
- }
- }
- /**
- * 获取整点秒杀数据
- * @param $num
- */
- public function getHourlyMiaosha($num)
- {
- $date = date('Y-m-d', time());
- $time = date('H', time());
- $query = MiaoshaGoods::find()->alias('mg')->where([
- 'mg.store_id' => $this->store->id, 'mg.is_delete' => 0
- ])->joinWith('msGoods g')->andWhere(['g.is_delete' => 0, 'g.status' => 1]);
- $query->andWhere([
- 'and',
- ['mg.open_date' => $date],
- ['mg.start_time' => $time],
- ]);
- $list = $query->all();
- $goodsList = [];
- /* @var MiaoshaGoods[] $list */
- foreach ($list as $key => $item) {
- /* @var MsGoods $msGoods */
- $msGoods = $item->msGoods;
- $price = $msGoods->original_price;
- $attr = json_decode($item['attr'], true);
- foreach ($attr as &$value) {
- $value['num'] = $value['miaosh_num'];
- $value['price'] = $value['miaosh_price'];
- if ($price == 0 || ($price > 0 && round($price, 2) >= round($value['miaosha_price'], 2))) {
- $price = $value['miaosha_price'];
- }
- }
- unset($value);
- $goodsList[] = [
- 'attr' => $attr,
- 'attr_group' => json_decode(json_encode($msGoods->getAttrGroupList()), true),
- 'id' => $item->id,
- 'goods_id' => $msGoods->id,
- 'pic_url' => $msGoods->cover_pic,
- 'cover_pic' => $msGoods->cover_pic,
- 'price' => round($price, 2) ? round($price, 2) : 0,
- 'original_price' => $msGoods->original_price,
- 'name' => $msGoods->name,
- 'page_url' => '/pages/miaosha/details/details?id=' . $item->id,
- 'use_attr' => $msGoods->use_attr,
- 'open_date' => $item->open_date,
- 'start_time' => $item->start_time,
- 'is_negotiable' => 0,
- ];
- }
- $miaosha = Miaosha::findOne([
- 'store_id' => $this->store->id,
- 'mch_id' => 0,
- ]);
- $miaosha_open_date = json_decode($miaosha->open_time, true);
- // 获取下一场
- $next_date = $date;
- $is_next = false;
- if ($miaosha_open_date && $time == end($miaosha_open_date)) {
- $is_next = true;
- $next_date = date('Y-m-d', strtotime("+1 day"));
- }
- $next_query = MiaoshaGoods::find()->where([
- 'store_id' => $this->store->id,
- 'is_delete' => 0,
- 'open_date' => $next_date,
- 'mch_id' => 0,
- ]);
- if (!$is_next) {
- $next_query->andWhere([
- '>', 'start_time', $time
- ]);
- }
- $next = $next_query->orderBy('start_time ASC')->one();
- $next_time = null;
- if ($next) {
- $next_time = strtotime($next->open_date . ' ' . $next->start_time .':00:00');
- }
- return [
- 'goods_list' => $goodsList,
- 'next' => $next_time,
- ];
- }
- public static function getTopic()
- {
- $topicList = self::$topicList;
- if ($topicList) {
- return $topicList;
- }
- $topicList = Topic::find()->where(['store_id' => get_store_id(), 'is_delete' => 0,'is_show' => 1])
- ->orderBy('sort ASC,created_at DESC')->limit(6)->select('id,title')->asArray()->all();
- self::$topicList = $topicList;
- return $topicList;
- }
- public static function getUrl($url)
- {
- preg_match('/^[^\?+]\?([\w|\W]+)=([\w|\W]*?)&([\w|\W]+)=([\w|\W]*?)$/', $url, $res);
- return $res;
- }
- private function getIntegralCouponList()
- {
- if ($this->integralCouponList) {
- return $this->integralCouponList;
- }
- $integralCouponList = Coupon::find()->where(['store_id' => $this->store->id, 'is_delete' => 0, 'is_integral' => 2])
- ->orderBy('sort ASC')->asArray()->all();
- foreach ($integralCouponList as &$value) {
- $value['receive_content'] = "立即兑换";
- }
- $this->integralCouponList = $integralCouponList;
- return $integralCouponList;
- }
- }
|