special_helper.php 21 KB

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