special_helper.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/16
  6. * Time: 下午2:07
  7. */
  8. //将乱七八糟的格式输出,整合成统一的格式输出
  9. class brands_special
  10. {
  11. const publish_brands_special = 56;
  12. const local_brands_special = 50;
  13. const test_brands_special = 23;
  14. static private $stBImgContianers;
  15. public function brands_special()
  16. {
  17. if(self::$stBImgContianers == null) {
  18. self::$stBImgContianers = [];
  19. $this->init();
  20. }
  21. }
  22. public function info($brand_id)
  23. {
  24. if(array_key_exists($brand_id,self::$stBImgContianers)) {
  25. return self::$stBImgContianers[$brand_id];
  26. } else {
  27. return false;
  28. }
  29. }
  30. private function init()
  31. {
  32. $special_id = self::special_id();
  33. if($special_id == false) return false;
  34. $helper = new special_helper($special_id);
  35. $blocks = $helper->format($goods_ids);
  36. foreach ($blocks as $block)
  37. {
  38. $items = $block['items'];
  39. if(empty($items) || is_array($items) == false) continue;
  40. foreach ($items as $item)
  41. {
  42. $brand_id = intval($item['data']);
  43. $data = $item;
  44. $data['scale'] = $block['scale'];
  45. self::$stBImgContianers[$brand_id] = $data;
  46. }
  47. if(!empty($items) && is_array($items)) {
  48. }
  49. }
  50. }
  51. private static function special_id()
  52. {
  53. if (is_pushoms()) {
  54. return self::publish_brands_special;
  55. }
  56. elseif(is_localdebug()) {
  57. return self::local_brands_special;
  58. }
  59. else {
  60. return self::test_brands_special;
  61. }
  62. }
  63. }
  64. class special_helper
  65. {
  66. private $special_id;
  67. private $filter_data;
  68. private $goods_ids;
  69. public function __construct($special_id)
  70. {
  71. $this->special_id = $special_id;
  72. $this->read_cache();
  73. }
  74. public static function format_brand_sale($brand_id,$can_click)
  75. {
  76. if($brand_id <= 0) return array();
  77. $helper = new brands_special();
  78. $info = $helper->info($brand_id);
  79. if($info == false) return array();
  80. $result['item_title'] = '';
  81. $result['item_type'] = 'home1';
  82. $result['scale'] = $info['scale'];
  83. $item['image'] = $info['image'];
  84. $item['type'] = $info['type'];
  85. if($can_click == true) {
  86. $item['data'] = $info['data'];
  87. } else {
  88. $item['data'] = 0;
  89. }
  90. $item['title'] = $info['title'];
  91. $result['items'][] = $item;
  92. return $result;
  93. }
  94. public static function format_grid_brands($brands)
  95. {
  96. $result = [];
  97. $result['item_title'] = '';
  98. $result['item_type'] = 'home_grid';
  99. $result['scale'] = 3;
  100. $items = [];
  101. foreach ($brands as $brand) {
  102. $item['image'] = $brand['brand_logo'];
  103. $item['type'] = 'brand';
  104. $item['data'] = $brand['brand_id'];
  105. $item['title'] = $brand['brand_name'];
  106. $items[] = $item;
  107. }
  108. $result['items'] = $items;
  109. return $result;
  110. }
  111. public static function format_brand($brands)
  112. {
  113. if(empty($brands)) return array();
  114. if(count($brands) == 1)
  115. {
  116. $brand_id = intval($brands[0]['data']);
  117. return self::format_brand_sale($brand_id,true);
  118. }
  119. else
  120. {
  121. $result = [];
  122. $result['item_title'] = '';
  123. $result['item_type'] = 'homewords';
  124. foreach ($brands as $val) {
  125. $item['image'] = "";
  126. $item['type'] = $val['type'];
  127. $item['data'] = strval($val['data']);
  128. $item['title'] = $val['title'];
  129. $result['items'][] = $item;
  130. }
  131. return $result;
  132. }
  133. }
  134. public static function format_category($cats,$title = '')
  135. {
  136. if(empty($cats)) return array();
  137. $result = [];
  138. $result['item_title'] = $title;
  139. $result['item_type'] = 'homewords';
  140. foreach ($cats as $val) {
  141. $item['image'] = "";
  142. $item['type'] = $val['type'];
  143. $item['data'] = strval($val['data']);
  144. $item['title'] = $val['title'];
  145. $result['items'][] = $item;
  146. }
  147. return $result;
  148. }
  149. public static function format_keyword($words,$title='')
  150. {
  151. $result = [];
  152. $result['item_title'] = $title;
  153. $result['item_type'] = 'homewords';
  154. foreach ($words as $word) {
  155. $item['image'] = "";
  156. $item['type'] = 'keyword';
  157. $item['data'] = $word;
  158. $item['title'] = $word;
  159. $result['items'][] = $item;
  160. }
  161. return $result;
  162. }
  163. public static function format_bonus($bonuses,$title='')
  164. {
  165. $result = [];
  166. $result['item_title'] = $title;
  167. $result['item_type'] = 'home_bonus';
  168. foreach ($bonuses as $bonus) {
  169. $item['image'] = "";
  170. $item['type'] = 'bonus';
  171. $item['data'] = $bonus['bonus_sn'];
  172. $item['title'] = '';
  173. $result['items'][] = $item;
  174. }
  175. return $result;
  176. }
  177. public static function format_type($type_infos,$title='')
  178. {
  179. $result = [];
  180. $result['item_title'] = $title;
  181. $result['item_type'] = 'home_type';
  182. foreach ($type_infos as $bonus) {
  183. $item['image'] = "";
  184. $item['type'] = 'bonus_type';
  185. $item['data'] = $bonus['type_sn'];
  186. $item['title'] = '';
  187. $result['items'][] = $item;
  188. }
  189. return $result;
  190. }
  191. public static function format_goods($goods_ids,$title)
  192. {
  193. $result = [];
  194. $result['item_title'] = empty($title) ? '' : $title;
  195. $result['item_type'] = 'home_goods';
  196. foreach ($goods_ids as $goods_id) {
  197. $item['image'] = "";
  198. $item['type'] = "goods";
  199. $item['data'] = strval($goods_id);
  200. $result['items'][] = $item;
  201. }
  202. return $result;
  203. }
  204. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  205. public function format(&$goods_ids)
  206. {
  207. $goods_ids = $this->goods_ids;
  208. return $this->filter_data;
  209. }
  210. private function read_cache()
  211. {
  212. $mod_special = Model('mb_special');
  213. $specials = $mod_special->getMbSpecialItemUsableListByID($this->special_id);
  214. // unset($specials['mobile_filter_data'],$specials['mobile_goods_ids']);
  215. //
  216. // $this->filter_data = $this->filter($specials,$goods_ids);
  217. // $this->goods_ids = $goods_ids;
  218. //
  219. // $specials['mobile_filter_data'] = $this->filter_data;
  220. // $specials['mobile_goods_ids'] = $goods_ids;
  221. if(array_key_exists("mobile_filter_data",$specials)) {
  222. $this->filter_data = $specials['mobile_filter_data'];
  223. $this->goods_ids = $specials['mobile_goods_ids'];
  224. } else {
  225. $this->filter_data = $this->filter($specials,$goods_ids);
  226. $this->goods_ids = $goods_ids;
  227. $specials['mobile_filter_data'] = $this->filter_data;
  228. $specials['mobile_goods_ids'] = $goods_ids;
  229. $cache = array('special' => serialize($specials));
  230. wcache($this->special_id, $cache, 'mb_special');
  231. }
  232. }
  233. private function filter($specials,&$goods_ids)
  234. {
  235. $blocks = [];
  236. $goods_ids = [];
  237. foreach($specials as $key => $special)
  238. {
  239. $ids = [];
  240. $block = $this->filter_item($special,$ids,$scale);
  241. if($scale != false && !empty($block)) {
  242. $blocks[] = $block;
  243. $goods_ids = array_merge($goods_ids,$ids);
  244. }
  245. }
  246. $goods_ids = array_unique($goods_ids);
  247. return $blocks;
  248. }
  249. private function filter_item($special,&$goods_ids,&$scale)
  250. {
  251. $scale = false;
  252. $result = [];
  253. foreach($special as $key => $val)
  254. {
  255. $block = [];
  256. $block['item_title'] = empty($val['title']) ? '' : $val['title'];
  257. $block['item_type'] = $key;
  258. if($key == 'home1') {
  259. $block['item_title'] = empty($val['home1_title']) ? '' : $val['home1_title'];
  260. $items = $this->filter_home1($val,$goods_ids,$scale);
  261. }
  262. elseif($key == 'home2') {
  263. $items = $this->filter_home2($val,$goods_ids,$scale);
  264. }
  265. elseif($key == 'home3') {
  266. $items = $val['item'];
  267. $items = $this->filter_home3($items,$goods_ids,$scale);
  268. }
  269. elseif($key == 'home4') {
  270. $items = $this->filter_home4($val,$goods_ids,$scale);
  271. }
  272. elseif($key == 'goods')
  273. {
  274. $block['item_type'] = 'home_goods';
  275. $items = $val['item'];
  276. $items = $this->filter_goods($items,$goods_ids,$scale);
  277. }
  278. elseif($key == 'home5')
  279. {
  280. $items = $val['item'];
  281. $items = $this->filter_adv($items,$goods_ids,$scale);
  282. }
  283. elseif($key == 'adv_list')
  284. {
  285. $items = $val['item'];
  286. $items = $this->filter_adv($items,$goods_ids,$scale);
  287. }
  288. else {
  289. continue;
  290. }
  291. if($items == false) {
  292. continue;
  293. } else {
  294. $this->decode($items);
  295. $block['items'] = $items;
  296. $block['scale'] = $scale;
  297. $result = $block;
  298. }
  299. }
  300. return $result;
  301. }
  302. /**
  303. * 处理type=url 时的&字符转义问题
  304. * @param array $data
  305. * @return array|bool
  306. */
  307. private function decode(&$items)
  308. {
  309. foreach($items as &$item)
  310. {
  311. if ($item['type'] == 'url' || $item['type'] == 'innerurl') {
  312. $item['data'] = htmlspecialchars_decode($item['data']);
  313. }
  314. }
  315. }
  316. private function scale($image)
  317. {
  318. $image = trim($image);
  319. if(empty($image)) {
  320. Log::record("special scale error {$image}",Log::ERR);
  321. return false;
  322. }
  323. $ret = getimagesize($image,$info);
  324. if($ret != false) {
  325. $scale = floatval($ret[0]) / floatval($ret[1]);
  326. return $scale;
  327. } else {
  328. Log::record("special scale getimagesize error {$image}",Log::ERR);
  329. return false;
  330. }
  331. }
  332. private function safe_string($str)
  333. {
  334. if($str == null || empty($str)) {
  335. return "";
  336. }
  337. else {
  338. return $str;
  339. }
  340. }
  341. private function check_goods($ids)
  342. {
  343. if(empty($ids)) return true;
  344. $ids = array_unique($ids);
  345. $mod = Model('goods');
  346. $goods = $mod->getGoodsOnlineList(array("goods_id" => array("in",$ids)));
  347. $gids = [];
  348. foreach ($goods as $item) {
  349. $gids[] = intval($item['goods_id']);
  350. }
  351. if(count($ids) == count($gids)) {
  352. return true;
  353. } else {
  354. return false;
  355. }
  356. }
  357. private function filter_home1($val,&$goods_ids,&$scale)
  358. {
  359. $items = [];
  360. $item['image'] = $val['image'];
  361. $item['type'] = $val['type'];
  362. $item['data'] = $val['data'];
  363. $item['title'] = $this->safe_string($val['title']);
  364. $scale = $this->scale($item['image']);
  365. $ids = [];
  366. if($item['type'] == 'goods') {
  367. $ids[] = intval($item['data']);
  368. }
  369. if(self::check_goods($ids)) {
  370. $goods_ids = array_merge($goods_ids,$ids);
  371. $items[] = $item;
  372. return $items;
  373. } else {
  374. return false;
  375. }
  376. }
  377. private function filter_home2($val,&$goods_ids,&$scale)
  378. {
  379. $items = [];
  380. {
  381. $item['image'] = $val['square_image'];
  382. $item['type'] = $val['square_type'];
  383. $item['data'] = $val['square_data'];
  384. $item['title'] = $this->safe_string($val['square_title']);
  385. $items[] = $item;
  386. }
  387. {
  388. $item['image'] = $val['rectangle1_image'];
  389. $item['type'] = $val['rectangle1_type'];
  390. $item['data'] = $val['rectangle1_data'];
  391. $item['title'] = $this->safe_string($val['rectangle1_title']);
  392. $items[] = $item;
  393. $scale = $this->scale($item['image']);
  394. }
  395. {
  396. $item['image'] = $val['rectangle2_image'];
  397. $item['type'] = $val['rectangle2_type'];
  398. $item['data'] = $val['rectangle2_data'];
  399. $item['title'] = $this->safe_string($val['rectangle2_title']);
  400. $items[] = $item;
  401. }
  402. $ids = [];
  403. foreach ($items as $item)
  404. {
  405. if($item['type'] == 'goods') {
  406. $ids[] = intval($item['data']);
  407. }
  408. }
  409. if(self::check_goods($ids)) {
  410. $goods_ids = array_merge($goods_ids,$ids);
  411. return $items;
  412. } else {
  413. Log::record("check_goods error {$ids}",Log::ERR);
  414. return false;
  415. }
  416. }
  417. private function filter_home3($items,&$goods_ids,&$scale)
  418. {
  419. $result = [];
  420. $has_set = false;
  421. foreach($items as $item)
  422. {
  423. if($has_set == false) {
  424. $scale = $this->scale($item['image']);
  425. if($scale != false) {
  426. $scale = $scale * 2;
  427. }
  428. $has_set = true;
  429. }
  430. if($item['type'] == 'goods')
  431. {
  432. $goods_id = intval($item['data']);
  433. if(self::check_goods(array($goods_id))) {
  434. array_push($goods_ids,$goods_id);
  435. $result[] = $item;
  436. } else {
  437. continue;
  438. }
  439. }
  440. else {
  441. $result[] = $item;
  442. }
  443. }
  444. if(empty($result)) {
  445. return false;
  446. } else {
  447. return $result;
  448. }
  449. }
  450. private function filter_home4($val,&$goods_ids,&$scale)
  451. {
  452. $items = [];
  453. {
  454. $item['image'] = $val['rectangle1_image'];
  455. $item['type'] = $val['rectangle1_type'];
  456. $item['data'] = $val['rectangle1_data'];
  457. $item['title'] = $this->safe_string($val['rectangle1_title']);
  458. $items[] = $item;
  459. }
  460. {
  461. $item['image'] = $val['rectangle2_image'];
  462. $item['type'] = $val['rectangle2_type'];
  463. $item['data'] = $val['rectangle2_data'];
  464. $item['title'] = $this->safe_string($val['rectangle2_title']);
  465. $scale = $this->scale($item['image']);
  466. $items[] = $item;
  467. }
  468. {
  469. $item['image'] = $val['square_image'];
  470. $item['type'] = $val['square_type'];
  471. $item['data'] = $val['square_data'];
  472. $item['title'] = $this->safe_string($val['square_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. return false;
  487. }
  488. }
  489. private function filter_goods($items,&$goods_ids,&$scale)
  490. {
  491. $scale = 1;
  492. $result = [];
  493. foreach($items as $val)
  494. {
  495. $item['data'] = $val['goods_id'];
  496. $item['type'] = 'goods';
  497. $item['image'] = $val['goods_image'];
  498. $item['title'] = $val['goods_name'];
  499. $goods_id = intval($val['goods_id']);
  500. if(self::check_goods(array($goods_id))) {
  501. array_push($goods_ids,$goods_id);
  502. $result[] = $item;
  503. } else {
  504. continue;
  505. }
  506. }
  507. if(empty($result)) {
  508. return false;
  509. } else {
  510. return $result;
  511. }
  512. }
  513. private function filter_adv($items,&$goods_ids,&$scale)
  514. {
  515. $result = [];
  516. $has_set = false;
  517. foreach($items as $item)
  518. {
  519. $val['image'] = $item['image'];
  520. $val['type'] = $item['type'];
  521. $val['data'] = $item['data'];
  522. $val['title'] = $this->safe_string($item['title']);
  523. if($has_set == false) {
  524. $scale = $this->scale($item['image']);
  525. $has_set = true;
  526. }
  527. if($item['type'] == 'goods')
  528. {
  529. $goods_id = intval($item['data']);
  530. if(self::check_goods(array($goods_id))) {
  531. array_push($goods_ids,$goods_id);
  532. $result[] = $val;
  533. } else {
  534. continue;
  535. }
  536. }
  537. else {
  538. $result[] = $val;
  539. }
  540. }
  541. return $result;
  542. }
  543. }