special_helper.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/16
  6. * Time: 下午2:07
  7. */
  8. //将乱七八糟的格式输出,整合成统一的格式输出
  9. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  10. class brands_special
  11. {
  12. const publish_brands_special = 56;
  13. const local_brands_special = 50;
  14. const test_brands_special = 56;
  15. static private $stBImgContianers;
  16. public function brands_special()
  17. {
  18. if(self::$stBImgContianers == null) {
  19. self::$stBImgContianers = [];
  20. $this->init();
  21. }
  22. }
  23. public function info($brand_id)
  24. {
  25. if(isset(self::$stBImgContianers[$brand_id])) {
  26. return self::$stBImgContianers[$brand_id];
  27. } else {
  28. return false;
  29. }
  30. }
  31. private function init()
  32. {
  33. $special_id = self::special_id();
  34. if($special_id == false) return false;
  35. $helper = new special_formater($special_id);
  36. $blocks = $helper->format($goods_ids);
  37. foreach ($blocks as $block)
  38. {
  39. $items = $block['items'];
  40. if(empty($items) || is_array($items) == false) continue;
  41. foreach ($items as $item)
  42. {
  43. $brand_id = intval($item['data']);
  44. $data = $item;
  45. $data['scale'] = $block['scale'];
  46. self::$stBImgContianers[$brand_id] = $data;
  47. }
  48. if(!empty($items) && is_array($items)) {
  49. }
  50. }
  51. }
  52. private static function special_id()
  53. {
  54. if (is_pushoms()) {
  55. return self::publish_brands_special;
  56. }
  57. elseif(is_localdebug()) {
  58. return self::local_brands_special;
  59. }
  60. else {
  61. return self::test_brands_special;
  62. }
  63. }
  64. }
  65. class special_formater
  66. {
  67. const def_item_bg_color = '#F2F2F2';
  68. const def_item_bg_type = 'color';
  69. const def_divider_bg_img= '/mobile/defimg/divider_bg.png';
  70. private $special_id;
  71. private $filter_data;
  72. private $goods_ids;
  73. private $old_version;
  74. public function __construct($special_id,$old_version = false)
  75. {
  76. $this->special_id = $special_id;
  77. $this->old_version = $old_version;
  78. $this->read_cache();
  79. }
  80. public static function format_brand_sale($brand_id,$can_click)
  81. {
  82. if($brand_id <= 0) return array();
  83. $helper = new brands_special();
  84. $info = $helper->info($brand_id);
  85. if($info == false) return array();
  86. $result['item_title'] = '';
  87. $result['item_type'] = 'home1';
  88. $result['scale'] = $info['scale'];
  89. $item['image'] = $info['image'];
  90. $item['type'] = $info['type'];
  91. if($can_click == true) {
  92. $item['data'] = $info['data'];
  93. } else {
  94. $item['data'] = 0;
  95. }
  96. $item['title'] = $info['title'];
  97. $result['items'][] = $item;
  98. return $result;
  99. }
  100. public static function format_grid_brands($brands)
  101. {
  102. $result = [];
  103. $result['item_title'] = '';
  104. $result['item_type'] = 'home_grid';
  105. $result['scale'] = 3;
  106. $items = [];
  107. foreach ($brands as $brand) {
  108. $item['image'] = $brand['brand_logo'];
  109. $item['type'] = 'brand';
  110. $item['data'] = $brand['brand_id'];
  111. $item['title'] = $brand['brand_name'];
  112. $items[] = $item;
  113. }
  114. $result['items'] = $items;
  115. return $result;
  116. }
  117. public static function format_comments($comments)
  118. {
  119. $blocks = [];
  120. foreach ($comments as $val)
  121. {
  122. $block = [];
  123. $block['item_title'] = '';
  124. $block['item_type'] = 'home_comment';
  125. $block['scale'] = 1;
  126. $item['image'] = '';
  127. $item['type'] = 'comment';
  128. $item['data'] = intval($val['geval_id']);
  129. $item['title'] = $val['geval_goodsname'];
  130. $items[] = $item;
  131. $block['items'] = $items;
  132. $blocks[] = $block;
  133. }
  134. return $blocks;
  135. }
  136. public static function format_brand($brands)
  137. {
  138. if(empty($brands)) return array();
  139. if(count($brands) == 1)
  140. {
  141. $brand_id = intval($brands[0]['data']);
  142. return self::format_brand_sale($brand_id,true);
  143. }
  144. else
  145. {
  146. $result = [];
  147. $result['item_title'] = '';
  148. $result['item_type'] = 'homewords';
  149. foreach ($brands as $val) {
  150. $item['image'] = "";
  151. $item['type'] = $val['type'];
  152. $item['data'] = strval($val['data']);
  153. $item['title'] = $val['title'];
  154. $result['items'][] = $item;
  155. }
  156. return $result;
  157. }
  158. }
  159. public static function format_category($cats,$title = '')
  160. {
  161. if(empty($cats)) return array();
  162. $result = [];
  163. $result['item_title'] = $title;
  164. $result['item_type'] = 'homewords';
  165. foreach ($cats as $val) {
  166. $item['image'] = "";
  167. $item['type'] = $val['type'];
  168. $item['data'] = strval($val['data']);
  169. $item['title'] = $val['title'];
  170. $result['items'][] = $item;
  171. }
  172. return $result;
  173. }
  174. public static function format_keyword($words,$title='')
  175. {
  176. $result = [];
  177. $result['item_title'] = $title;
  178. $result['item_type'] = 'homewords';
  179. foreach ($words as $word) {
  180. $item['image'] = "";
  181. $item['type'] = 'keyword';
  182. $item['data'] = $word;
  183. $item['title'] = $word;
  184. $result['items'][] = $item;
  185. }
  186. return $result;
  187. }
  188. public static function format_bonus($bonuses,$title='')
  189. {
  190. $result = [];
  191. $result['item_title'] = $title;
  192. $result['item_type'] = 'home_bonus';
  193. foreach ($bonuses as $bonus) {
  194. $item['image'] = "";
  195. $item['type'] = 'bonus';
  196. $item['data'] = $bonus['bonus_sn'];
  197. $item['title'] = '';
  198. $result['items'][] = $item;
  199. }
  200. return $result;
  201. }
  202. public static function format_type($type_infos,$title='')
  203. {
  204. $result = [];
  205. $result['item_title'] = $title;
  206. $result['item_type'] = 'home_type';
  207. foreach ($type_infos as $type) {
  208. $item['image'] = "";
  209. $item['type'] = 'bonus_type';
  210. $item['data'] = $type['type_sn'];
  211. $item['title'] = '';
  212. $result['items'][] = $item;
  213. }
  214. return $result;
  215. }
  216. public static function format_goods($goods_ids,$title,$sort_summary)
  217. {
  218. $result = [];
  219. $result['item_title'] = empty($title) ? '' : $title;
  220. $result['item_type'] = 'home_goods';
  221. foreach ($goods_ids as $goods_id)
  222. {
  223. $summary = $sort_summary[$goods_id];
  224. $item['image'] = $summary['goods_image_url'];
  225. $item['show_type'] = "goods";
  226. $item['show_data'] = strval($goods_id);
  227. $item['type'] = "goods";
  228. $item['data'] = strval($goods_id);
  229. $item['title'] = $summary['goods_mobile_name'];
  230. $result['items'][] = $item;
  231. }
  232. return $result;
  233. }
  234. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  235. public function format(&$goods_ids)
  236. {
  237. $goods_ids = $this->goods_ids;
  238. return $this->filter_data;
  239. }
  240. private function read_cache()
  241. {
  242. $mod_special = Model('mb_special');
  243. $specials = $mod_special->getMbSpecialItemUsableListByID($this->special_id);
  244. if(empty($specials)) {
  245. Log::record("getMbSpecialItemUsableListByID is empty",Log::ERR);
  246. }
  247. $this->filter_data = $this->filter($specials,$goods_ids);
  248. $this->goods_ids = $goods_ids;
  249. }
  250. private function filter($specials,&$goods_ids)
  251. {
  252. $blocks = [];
  253. $goods_ids = [];
  254. foreach($specials as $special)
  255. {
  256. $ids = [];
  257. $block = $this->filter_item($special,$ids,$scale);
  258. if($scale != false && !empty($block)) {
  259. $blocks[] = $block;
  260. $goods_ids = array_merge($goods_ids,$ids);
  261. }
  262. }
  263. $goods_ids = array_unique($goods_ids);
  264. return $blocks;
  265. }
  266. private function filter_item($special,&$goods_ids,&$scale)
  267. {
  268. $block = [];
  269. $block['item_title'] = empty($special['title']) ? '' : $special['title'];
  270. $block['item_type'] = $special['item_type'];
  271. $block['bg_image'] = $special['bg_image'];
  272. $block['bg_type'] = $special['bg_type'];
  273. $block['bg_data'] = $special['bg_data'];
  274. $block['has_margin'] = $special['has_margin'];
  275. $block_scale = false;
  276. $this->format_property($block,$block_scale);
  277. $key = $special['item_type'];
  278. if ($key == 'divider') {
  279. $scale = $block_scale;
  280. $block['scale'] = $scale;
  281. return $block;
  282. }
  283. $org_items = $special['items'];
  284. if($key != 'divider' && empty($org_items)) {
  285. return false;
  286. }
  287. $scale = false;
  288. if($key == 'adv_list' || $key == 'home3' || $key == 'home5' || $key =='home6' || $key == 'home7' || $key == 'goods')
  289. {
  290. $items = $this->filter_normal($key,$org_items,$goods_ids,$scale);
  291. if($key == 'goods') {
  292. $block['item_type'] = 'home_goods';
  293. }
  294. elseif($key == 'home3') {
  295. if($this->old_version == false) $scale = $block_scale;
  296. }
  297. elseif($key == 'home6') {
  298. $block['item_type'] = 'horizon';
  299. $scale = $block_scale;
  300. }
  301. elseif($key == 'home7') {
  302. $block['item_type'] = 'horizon_goods';
  303. $scale = $block_scale;
  304. }
  305. }
  306. elseif($key == 'home1') {
  307. $items = $this->filter_home1($org_items,$goods_ids,$scale);
  308. }
  309. elseif($key == 'home2') {
  310. $items = $this->filter_home2($org_items,$goods_ids,$scale);
  311. }
  312. elseif($key == 'home4') {
  313. $items = $this->filter_home4($org_items,$goods_ids,$scale);
  314. }
  315. else {
  316. return false;
  317. }
  318. if($items == false && $key != 'divider') {
  319. return false;
  320. }
  321. $this->decode($items);
  322. foreach ($items as &$item)
  323. {
  324. if($this->image_wh($item['image'],$w,$h)) {
  325. $item['width'] = $w;
  326. $item['height'] = $h;
  327. }
  328. }
  329. $block['items'] = $items;
  330. if($block_scale) {
  331. $block['scale'] = $scale;
  332. }
  333. else {
  334. $block['scale'] = $scale;
  335. }
  336. return $block;
  337. }
  338. /**
  339. * 处理type=url 时的&字符转义问题
  340. * @param array $data
  341. * @return array|bool
  342. */
  343. private function decode(&$items)
  344. {
  345. foreach($items as &$item)
  346. {
  347. static $types = array('url','innerurl','video');
  348. if(in_array($item['type'],$types)){
  349. $item['data'] = htmlspecialchars_decode($item['data']);
  350. }
  351. }
  352. }
  353. private function scale($image)
  354. {
  355. $image = trim($image);
  356. if(empty($image)) {
  357. Log::record("special scale error {$image}",Log::ERR);
  358. return false;
  359. }
  360. $ret = util::imgsize($image);
  361. if($ret != false) {
  362. $scale = doubleval($ret[0]) / doubleval($ret[1]);
  363. return $scale;
  364. } else {
  365. Log::record("special scale getimagesize error {$image}",Log::ERR);
  366. return false;
  367. }
  368. }
  369. private function safe_string($str)
  370. {
  371. if($str == null || empty($str)) {
  372. return "";
  373. }
  374. else {
  375. return $str;
  376. }
  377. }
  378. private function check_goods($ids)
  379. {
  380. if(empty($ids)) return true;
  381. $ids = array_unique($ids);
  382. $mod = Model('goods');
  383. $goods = $mod->getGoodsOnlineList(array("goods_id" => array("in",$ids)));
  384. $gids = [];
  385. foreach ($goods as $item) {
  386. $gids[] = intval($item['goods_id']);
  387. }
  388. if(count($ids) == count($gids)) {
  389. return true;
  390. } else {
  391. return false;
  392. }
  393. }
  394. private function filter_home1($val,&$goods_ids,&$scale)
  395. {
  396. $items = [];
  397. $item['image'] = $val['image'];
  398. $item['type'] = $val['type'];
  399. $item['data'] = $val['data'];
  400. $item['show_type'] = $val['show_type'];
  401. $item['show_data'] = $val['show_data'];
  402. $item['title'] = $this->safe_string($val['title']);
  403. $scale = $this->scale($item['image']);
  404. $ids = [];
  405. if($item['type'] == 'goods') {
  406. $ids[] = intval($item['data']);
  407. }
  408. if(self::check_goods($ids)) {
  409. $goods_ids = array_merge($goods_ids,$ids);
  410. $items[] = $item;
  411. return $items;
  412. } else {
  413. return false;
  414. }
  415. }
  416. private function filter_home2($val,&$goods_ids,&$scale)
  417. {
  418. $items = [];
  419. {
  420. $item['image'] = $val['square_image'];
  421. $item['type'] = $val['square_type'];
  422. $item['data'] = $val['square_data'];
  423. $item['show_type'] = $val['square_show_type'];
  424. $item['show_data'] = $val['square_show_data'];
  425. $item['title'] = $this->safe_string($val['square_title']);
  426. $items[] = $item;
  427. }
  428. {
  429. $item['image'] = $val['rectangle1_image'];
  430. $item['type'] = $val['rectangle1_type'];
  431. $item['data'] = $val['rectangle1_data'];
  432. $item['show_type'] = $val['rectangle1_show_type'];
  433. $item['show_data'] = $val['rectangle1_show_data'];
  434. $item['title'] = $this->safe_string($val['rectangle1_title']);
  435. $items[] = $item;
  436. $scale = $this->scale($item['image']);
  437. }
  438. {
  439. $item['image'] = $val['rectangle2_image'];
  440. $item['type'] = $val['rectangle2_type'];
  441. $item['data'] = $val['rectangle2_data'];
  442. $item['show_type'] = $val['rectangle2_show_type'];
  443. $item['show_data'] = $val['rectangle2_show_data'];
  444. $item['title'] = $this->safe_string($val['rectangle2_title']);
  445. $items[] = $item;
  446. }
  447. $ids = [];
  448. foreach ($items as $item)
  449. {
  450. if($item['type'] == 'goods') {
  451. $ids[] = intval($item['data']);
  452. }
  453. }
  454. if(self::check_goods($ids)) {
  455. $goods_ids = array_merge($goods_ids,$ids);
  456. return $items;
  457. } else {
  458. Log::record("check_goods error {$ids}",Log::ERR);
  459. return false;
  460. }
  461. }
  462. private function filter_home4($val,&$goods_ids,&$scale)
  463. {
  464. $items = [];
  465. {
  466. $item['image'] = $val['rectangle1_image'];
  467. $item['type'] = $val['rectangle1_type'];
  468. $item['data'] = $val['rectangle1_data'];
  469. $item['show_type'] = $val['rectangle1_show_type'];
  470. $item['show_data'] = $val['rectangle1_show_data'];
  471. $item['title'] = $this->safe_string($val['rectangle1_title']);
  472. $items[] = $item;
  473. }
  474. {
  475. $item['image'] = $val['rectangle2_image'];
  476. $item['type'] = $val['rectangle2_type'];
  477. $item['data'] = $val['rectangle2_data'];
  478. $item['show_type'] = $val['rectangle2_show_type'];
  479. $item['show_data'] = $val['rectangle2_show_data'];
  480. $item['title'] = $this->safe_string($val['rectangle2_title']);
  481. $scale = $this->scale($item['image']);
  482. $items[] = $item;
  483. }
  484. {
  485. $item['image'] = $val['square_image'];
  486. $item['type'] = $val['square_type'];
  487. $item['data'] = $val['square_data'];
  488. $item['show_type'] = $val['square_show_type'];
  489. $item['show_data'] = $val['square_show_data'];
  490. $item['title'] = $this->safe_string($val['square_title']);
  491. $items[] = $item;
  492. }
  493. $ids = [];
  494. foreach ($items as $item)
  495. {
  496. if($item['type'] == 'goods') {
  497. $ids[] = intval($item['data']);
  498. }
  499. }
  500. if(self::check_goods($ids)) {
  501. $goods_ids = array_merge($goods_ids,$ids);
  502. return $items;
  503. } else {
  504. return false;
  505. }
  506. }
  507. private function image_wh($image,&$width,&$height)
  508. {
  509. $ret = util::imgsize($image);
  510. if ($ret != false) {
  511. $width = $ret[0];
  512. $height = $ret[1];
  513. return true;
  514. } else {
  515. Log::record("cannt find image imgurl={$image}",Log::ERR);
  516. return false;
  517. }
  518. }
  519. private function format_property(&$block,&$block_scale)
  520. {
  521. $image = $block['bg_image'];
  522. if(empty($image))
  523. {
  524. if($block['item_type'] == 'divider') {
  525. $image = RESOURCE_SITE_URL . self::def_divider_bg_img;
  526. }
  527. }
  528. else {
  529. $image = getMbSpecialImageUrl($image);
  530. }
  531. $block['bg_image'] = $image;
  532. if(!empty($image)) {
  533. $block_scale = $this->scale($image);
  534. } else {
  535. $block_scale = false;
  536. }
  537. if($block['bg_type'] == 'image')
  538. {
  539. if($block_scale != false) {
  540. $block['bg_data'] = $image;
  541. } else {
  542. $block['bg_data'] = '';
  543. }
  544. }
  545. else
  546. {
  547. if(empty($block['bg_data'])) {
  548. $block['bg_data'] = self::def_item_bg_color;
  549. }
  550. }
  551. unset($block['bg_image']);
  552. }
  553. private function filter_normal($block_type,$items, &$goods_ids, &$scale)
  554. {
  555. $result = [];
  556. $has_set = false;
  557. foreach($items as $item)
  558. {
  559. $item['title'] = $this->safe_string($item['title']);
  560. if($has_set == false) {
  561. $scale = $this->scale($item['image']);
  562. $has_set = true;
  563. }
  564. if($item['type'] == 'goods')
  565. {
  566. $goods_id = intval($item['data']);
  567. if(self::check_goods(array($goods_id))) {
  568. array_push($goods_ids,$goods_id);
  569. $result[] = $item;
  570. } else {
  571. Log::record("block_type={$block_type} cannt find goods ,goods_id={$goods_id}",Log::ERR);
  572. continue;
  573. }
  574. }
  575. else {
  576. $result[] = $item;
  577. }
  578. }
  579. if($has_set == true)
  580. {
  581. if($block_type == 'home3') {
  582. $scale = $scale * 2;
  583. }
  584. elseif($block_type == 'goods') {
  585. $scale = 1;
  586. }
  587. }
  588. if(empty($result)) {
  589. return false;
  590. } else {
  591. return $result;
  592. }
  593. }
  594. }
  595. class special_manager
  596. {
  597. private static $stInstance = null;
  598. private $mContents;
  599. public function __construct()
  600. {
  601. $this->mContents = [];
  602. }
  603. public static function instance() {
  604. if(self::$stInstance == null) {
  605. self::$stInstance = new special_manager();
  606. }
  607. return self::$stInstance;
  608. }
  609. public function special($special_id,&$goods_ids)
  610. {
  611. if(StatesHelper::fetch_state('special')) {
  612. $this->mContents = [];
  613. }
  614. $special_id = intval($special_id);
  615. if(array_key_exists($special_id,$this->mContents)) {
  616. $data = $this->mContents[$special_id]['data'];
  617. $goods_ids = $this->mContents[$special_id]['gids'];
  618. return $data;
  619. }
  620. else
  621. {
  622. $formater = new special_formater($special_id,$this->old_version($special_id));
  623. $data = $formater->format($goods_ids);
  624. $this->mContents[$special_id]['data'] = $data;
  625. $this->mContents[$special_id]['gids'] = $goods_ids;
  626. return $data;
  627. }
  628. }
  629. private function old_version($special_id)
  630. {
  631. global $config;
  632. if($_SESSION['is_lasted']) return false;
  633. return in_array($special_id,$config['old_specials']);
  634. }
  635. }