MchDiyTemplateForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\MchDiyTemplate;
  9. use app\utils\GetInfo;
  10. use yii\base\Model;
  11. class MchDiyTemplateForm extends Model
  12. {
  13. public $template_id;
  14. public $mch_id;
  15. public $page;
  16. public function rules()
  17. {
  18. return [
  19. [['template_id', 'mch_id'], 'integer']
  20. ];
  21. }
  22. public function detail()
  23. {
  24. if (!$this->validate()) {
  25. return $this->getErrorResponse();
  26. }
  27. $detail = MchDiyTemplate::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $this->template_id])->one();
  28. if (!$detail) {
  29. return [
  30. 'code' => 1,
  31. 'msg' => '模板不存在',
  32. ];
  33. }
  34. if ($detail) {
  35. $is_waimai = 0;
  36. $temp = ($detail['template'] == 'null' || !$detail['template']) ? [] : json_decode($detail['template'], true);
  37. MchDiyGoods::getTemplate($temp, $this->mch_id, get_store_id());
  38. $timeAll = [];
  39. foreach ($temp as $index => &$item) {
  40. switch ($item['type']) {
  41. case 'video':
  42. $res = GetInfo::getVideoInfo($item['param']['url']);
  43. if ($res && $res['code'] == 0) {
  44. $item['param']['url'] = $res['url'];
  45. }
  46. $item['param']['id'] = $index;
  47. break;
  48. case 'banner':
  49. foreach ($item['param']['list'] as &$value) {
  50. if ($value['open_type'] == 'wxapp') {
  51. $res = self::getUrl($value['page_url']);
  52. $value['appId'] = $res[2];
  53. $value['path'] = urldecode($res[4]);
  54. }
  55. }
  56. unset($value);
  57. $item['param']['banner_list'] = $item['param']['list'];
  58. break;
  59. case 'nav':
  60. $nav_arr = [];
  61. $nav = [];
  62. foreach ($item['param']['list'] as $k => &$v) {
  63. if ($k % ($item['param']['count'] * $item['param']['col']) == 0) {
  64. if (count($nav) > 0) {
  65. $nav_arr[] = $nav;
  66. }
  67. $nav = [];
  68. }
  69. $nav[] = $v;
  70. }
  71. if (count($nav) > 0) {
  72. $nav_arr[] = $nav;
  73. }
  74. $item['param']['nav_list'] = $nav_arr;
  75. $item['param']['is_slide'] = $item['param']['is_slide'] ? 'true' : 'false';
  76. break;
  77. case 'rubik':
  78. foreach ($item['param']['list'] as &$value) {
  79. if (isset($value['open_type']) && $value['open_type'] == 'wxapp') {
  80. $res = self::getUrl($value['url']);
  81. $value['appId'] = $res[2];
  82. $value['path'] = urldecode($res[4]);
  83. } else {
  84. if (strpos($value['url'], '?') === false) {
  85. $value['url'] = $value['url'] . '?mch_id=' . $this->mch_id;
  86. } else {
  87. $value['url'] = $value['url'] . '&mch_id=' . $this->mch_id;
  88. }
  89. }
  90. }
  91. unset($value);
  92. $item['param']['nav_list'] = $item['param']['list'];
  93. break;
  94. case 'link':
  95. if (isset($item['param']['open_type']) && $item['param']['open_type'] == 'wxapp') {
  96. $res = self::getUrl($item['param']['url']);
  97. $item['param']['appId'] = $res[2];
  98. $item['param']['path'] = urldecode($res[4]);
  99. } else {
  100. if (strpos($item['param']['url'], '?') === false) {
  101. $item['param']['url'] = $item['param']['url'] . '?mch_id=' . $this->mch_id;
  102. } else {
  103. $item['param']['url'] = $item['param']['url'] . '&mch_id=' . $this->mch_id;
  104. }
  105. }
  106. break;
  107. case 'goods':
  108. $param = &$item['param'];
  109. $list = [];
  110. foreach ($param['list'] as $key => &$value) {
  111. if ($value['cat_id'] && $param['list'][$key]['goods_style'] != 2) {
  112. $res = MchDiyGoods::getGoods($this->store, 'goods', [], $param['list'][$key]['cat_id'], 30, true);
  113. $goodsList = $res['goods_list'];
  114. if ($param['list'][$key]['goods_style'] == 1) {
  115. array_splice($goodsList, $param['list'][$key]['goods_num']);
  116. }
  117. $value['goods_list'] = $goodsList;
  118. }
  119. if (count($value['goods_list']) > 0) {
  120. $list[] = $value;
  121. }
  122. }
  123. $param['list'] = $list;
  124. break;
  125. case 'miaosha':
  126. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '马上秒';
  127. $item['param']['type_name'] = '秒杀';
  128. $newList = [];
  129. foreach ($item['param']['list'] as &$value) {
  130. $goodsList = [];
  131. foreach ($value['goods_list'] as $k => $v) {
  132. $startTime = strtotime($v['open_date'] . ' ' . $v['start_time'] . ':00:00');
  133. if ($startTime >= time()) {
  134. $v['is_start'] = 0;
  135. $v['time'] = $startTime - time();
  136. $v['time_end'] = $startTime - time() + 3600;
  137. $v['time_content'] = $item['param']['list_style'] == 1 ? "活动未开始" : "距开始仅剩";
  138. } else if ($startTime <= time() && $startTime + 3600 >= time()) {
  139. $v['is_start'] = 1;
  140. $v['time'] = $startTime - time() + 3600;
  141. $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
  142. } else {
  143. $v['is_start'] = 1;
  144. $v['time'] = 0;
  145. $v['time_content'] = "已结束";
  146. continue;
  147. }
  148. $goodsList[] = $v;
  149. }
  150. if (count($goodsList) > 0) {
  151. $value['goods_list'] = $goodsList;
  152. $newList[] = $value;
  153. }
  154. }
  155. $item['param']['list'] = $newList;
  156. unset($value);
  157. if (count($item['param']['list']) > 0) {
  158. $timeAll['miaosha_' . $index] = $item;
  159. }
  160. break;
  161. case 'pintuan':
  162. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '去拼团';
  163. $item['param']['type_name'] = '拼团';
  164. if (count($item['param']['list']) > 0) {
  165. $timeAll['pintuan_' . $index] = $item;
  166. }
  167. break;
  168. case 'book':
  169. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '预约';
  170. break;
  171. case 'bargain':
  172. $item['param']['buy_content'] = $item['param']['buy_content'] ? $item['param']['buy_content'] : '去参与';
  173. $item['param']['type_name'] = '砍价';
  174. $newList = [];
  175. foreach ($item['param']['list'] as &$value) {
  176. $goodsList = [];
  177. foreach ($value['goods_list'] as $k => $v) {
  178. $startTime = $v['begin_time'];
  179. $endTime = $v['end_time'];
  180. if ($startTime >= time()) {
  181. $v['is_start'] = 0;
  182. $v['time'] = $startTime - time();
  183. $v['time_end'] = $endTime - time();
  184. $v['time_content'] = $item['param']['list_style'] == 1 ? "活动未开始" : "距开始仅剩";
  185. } else if ($startTime <= time() && $endTime >= time()) {
  186. $v['is_start'] = 1;
  187. $v['time'] = $endTime - time();
  188. $v['time_content'] = $item['param']['list_style'] == 1 ? "仅剩" : "距结束仅剩";
  189. } else {
  190. $v['is_start'] = 1;
  191. $v['time'] = 0;
  192. $v['time_content'] = "已结束";
  193. continue;
  194. }
  195. $goodsList[] = $v;
  196. }
  197. if (count($goodsList) > 0) {
  198. $value['goods_list'] = $goodsList;
  199. $newList[] = $value;
  200. }
  201. }
  202. $item['param']['list'] = $newList;
  203. unset($value);
  204. if (count($item['param']['list']) > 0) {
  205. $timeAll['bargain_' . $index] = $item;
  206. }
  207. break;
  208. case 'waimai':
  209. $param = &$item['param'];
  210. $list = [];
  211. foreach ($param['list'] as $key => &$value) {
  212. if ($value['cat_id'] && $param['list'][$key]['goods_style'] != 2) {
  213. $res = MchDiyGoods::getGoods($this->store, 'waimai', [], $param['list'][$key]['cat_id'], 30, true);
  214. $goodsList = $res['goods_list'];
  215. if ($param['list'][$key]['goods_style'] == 1) {
  216. array_splice($goodsList, $param['list'][$key]['goods_num']);
  217. }
  218. $value['goods_list'] = $goodsList;
  219. }
  220. if (count($value['goods_list']) > 0) {
  221. $list[] = $value;
  222. }
  223. $param['list'][$key]['cat_goods_num'] = 0;
  224. }
  225. $param['list'] = $list;
  226. $is_waimai =1;
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. unset($item);
  233. return [
  234. 'code' => 0,
  235. 'msg' => '请求成功',
  236. 'data' => [
  237. 'template' => $temp,
  238. 'act_modal_list' => [],
  239. 'info' => $this->page->title,
  240. 'page_id' => $this->page->id,
  241. 'status' => 'diy',
  242. 'time_all' => $timeAll,
  243. 'is_waimai' =>$is_waimai,
  244. ]
  245. ];
  246. }
  247. }
  248. public static function getUrl($url)
  249. {
  250. preg_match('/^[^\?+]\?([\w|\W]+)=([\w|\W]*?)&([\w|\W]+)=([\w|\W]*?)$/', $url, $res);
  251. return $res;
  252. }
  253. }