special_helper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 = 50;
  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_localdebug()) {
  54. return self::local_brands_special;
  55. }
  56. elseif (is_pushoms()) {
  57. return self::publish_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_goods($goods_ids,$title)
  164. {
  165. $result = [];
  166. $result['item_title'] = empty($title) ? '' : $title;
  167. $result['item_type'] = 'home_goods';
  168. foreach ($goods_ids as $goods_id) {
  169. $item['image'] = "";
  170. $item['type'] = "goods";
  171. $item['data'] = strval($goods_id);
  172. $result['items'][] = $item;
  173. }
  174. return $result;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. public function format(&$goods_ids)
  178. {
  179. $goods_ids = $this->goods_ids;
  180. return $this->filter_data;
  181. }
  182. private function read_cache()
  183. {
  184. $mod_special = Model('mb_special');
  185. $specials = $mod_special->getMbSpecialItemUsableListByID($this->special_id);
  186. // unset($specials['mobile_filter_data'],$specials['mobile_goods_ids']);
  187. //
  188. // $this->filter_data = $this->filter($specials,$goods_ids);
  189. // $this->goods_ids = $goods_ids;
  190. //
  191. // $specials['mobile_filter_data'] = $this->filter_data;
  192. // $specials['mobile_goods_ids'] = $goods_ids;
  193. if(array_key_exists("mobile_filter_data",$specials)) {
  194. $this->filter_data = $specials['mobile_filter_data'];
  195. $this->goods_ids = $specials['mobile_goods_ids'];
  196. } else {
  197. $this->filter_data = $this->filter($specials,$goods_ids);
  198. $this->goods_ids = $goods_ids;
  199. $specials['mobile_filter_data'] = $this->filter_data;
  200. $specials['mobile_goods_ids'] = $goods_ids;
  201. $cache = array('special' => serialize($specials));
  202. wcache($this->special_id, $cache, 'mb_special');
  203. }
  204. }
  205. private function filter($specials,&$goods_ids)
  206. {
  207. $blocks = [];
  208. $goods_ids = array();
  209. foreach($specials as $key => $special)
  210. {
  211. $block = $this->filter_item($special,$goods_ids,$scale);
  212. if($scale != false && !empty($block)) {
  213. $blocks[] = $block;
  214. }
  215. }
  216. return $blocks;
  217. }
  218. private function filter_item($special,&$goods_ids,&$scale)
  219. {
  220. $scale = false;
  221. $result = [];
  222. foreach($special as $key => $val)
  223. {
  224. $block = [];
  225. $block['item_title'] = empty($val['title']) ? '' : $val['title'];
  226. $block['item_type'] = $key;
  227. if($key == 'home1') {
  228. $items = $this->filter_home1($val,$goods_ids,$scale);
  229. }
  230. elseif($key == 'home2') {
  231. $items = $this->filter_home2($val,$goods_ids,$scale);
  232. }
  233. elseif($key == 'home3') {
  234. $items = $val['item'];
  235. $items = $this->filter_home3($items,$goods_ids,$scale);
  236. }
  237. elseif($key == 'home4') {
  238. $items = $this->filter_home4($val,$goods_ids,$scale);
  239. }
  240. elseif($key == 'goods')
  241. {
  242. $block['item_type'] = 'home_goods';
  243. $items = $val['item'];
  244. $items = $this->filter_goods($items,$goods_ids,$scale);
  245. }
  246. elseif($key == 'home5')
  247. {
  248. $items = $val['item'];
  249. $items = $this->filter_adv($items,$goods_ids,$scale);
  250. }
  251. elseif($key == 'adv_list')
  252. {
  253. $items = $val['item'];
  254. $items = $this->filter_adv($items,$goods_ids,$scale);
  255. }
  256. else {
  257. continue;
  258. }
  259. if($items == false) {
  260. continue;
  261. } else {
  262. $this->decode($items);
  263. $block['items'] = $items;
  264. $block['scale'] = $scale;
  265. $result = $block;
  266. }
  267. }
  268. return $result;
  269. }
  270. /**
  271. * 处理type=url 时的&字符转义问题
  272. * @param array $data
  273. * @return array|bool
  274. */
  275. private function decode(&$items)
  276. {
  277. foreach($items as &$item)
  278. {
  279. if ($item['type'] == 'url' || $item['type'] == 'innerurl') {
  280. $item['data'] = htmlspecialchars_decode($item['data']);
  281. }
  282. }
  283. }
  284. private function scale($image)
  285. {
  286. $image = trim($image);
  287. if(empty($image)) {
  288. return false;
  289. }
  290. $ret = getimagesize($image,$info);
  291. if($ret != false) {
  292. $scale = floatval($ret[0]) / floatval($ret[1]);
  293. return $scale;
  294. } else {
  295. return false;
  296. }
  297. }
  298. private function safe_string($str)
  299. {
  300. if($str == null || empty($str)) {
  301. return "";
  302. }
  303. else {
  304. return $str;
  305. }
  306. }
  307. private function check_goods($ids)
  308. {
  309. if(empty($ids)) return true;
  310. $ids = array_unique($ids);
  311. $mod = Model('goods');
  312. $goods = $mod->getGoodsOnlineList(array("goods_id" => array("in",$ids)));
  313. $gids = [];
  314. foreach ($goods as $item) {
  315. $gids[] = intval($item['goods_id']);
  316. }
  317. if(count($ids) == count($gids)) {
  318. return true;
  319. } else {
  320. return false;
  321. }
  322. }
  323. private function filter_home1($val,&$goods_ids,&$scale)
  324. {
  325. $items = [];
  326. $item['image'] = $val['image'];
  327. $item['type'] = $val['type'];
  328. $item['data'] = $val['data'];
  329. $item['title'] = $this->safe_string($val['title']);
  330. $scale = $this->scale($item['image']);
  331. $ids = [];
  332. if($item['type'] == 'goods') {
  333. $ids[] = intval($item['data']);
  334. }
  335. if(self::check_goods($ids)) {
  336. array_push($goods_ids,$goods_id);
  337. $items[] = $item;
  338. return $items;
  339. } else {
  340. return false;
  341. }
  342. }
  343. private function filter_home2($val,&$goods_ids,&$scale)
  344. {
  345. $items = [];
  346. {
  347. $item['image'] = $val['square_image'];
  348. $item['type'] = $val['square_type'];
  349. $item['data'] = $val['square_data'];
  350. $item['title'] = $this->safe_string($val['square_title']);
  351. $items[] = $item;
  352. }
  353. {
  354. $item['image'] = $val['rectangle1_image'];
  355. $item['type'] = $val['rectangle1_type'];
  356. $item['data'] = $val['rectangle1_data'];
  357. $item['title'] = $this->safe_string($val['rectangle1_title']);
  358. $items[] = $item;
  359. $scale = $this->scale($item['image']);
  360. }
  361. {
  362. $item['image'] = $val['rectangle2_image'];
  363. $item['type'] = $val['rectangle2_type'];
  364. $item['data'] = $val['rectangle2_data'];
  365. $item['title'] = $this->safe_string($val['rectangle2_title']);
  366. $items[] = $item;
  367. }
  368. $ids = [];
  369. foreach ($items as $item)
  370. {
  371. if($item['type'] == 'goods') {
  372. $ids[] = intval($item['data']);
  373. }
  374. }
  375. if(self::check_goods($ids)) {
  376. $goods_ids = array_merge($goods_ids,$ids);
  377. return $items;
  378. } else {
  379. return false;
  380. }
  381. }
  382. private function filter_home3($items,&$goods_ids,&$scale)
  383. {
  384. $result = [];
  385. $has_set = false;
  386. foreach($items as $item)
  387. {
  388. if($has_set == false) {
  389. $scale = $this->scale($item['image']);
  390. if($scale != false) {
  391. $scale = $scale * 2;
  392. }
  393. $has_set = true;
  394. }
  395. if($item['type'] == 'goods')
  396. {
  397. $goods_id = intval($item['data']);
  398. if(self::check_goods(array($goods_id))) {
  399. array_push($goods_ids,$goods_id);
  400. $result[] = $item;
  401. } else {
  402. continue;
  403. }
  404. }
  405. else {
  406. $result[] = $item;
  407. }
  408. }
  409. if(empty($result)) {
  410. return false;
  411. } else {
  412. return $result;
  413. }
  414. }
  415. private function filter_home4($val,&$goods_ids,&$scale)
  416. {
  417. $items = [];
  418. {
  419. $item['image'] = $val['rectangle1_image'];
  420. $item['type'] = $val['rectangle1_type'];
  421. $item['data'] = $val['rectangle1_data'];
  422. $item['title'] = $this->safe_string($val['rectangle1_title']);
  423. $items[] = $item;
  424. }
  425. {
  426. $item['image'] = $val['rectangle2_image'];
  427. $item['type'] = $val['rectangle2_type'];
  428. $item['data'] = $val['rectangle2_data'];
  429. $item['title'] = $this->safe_string($val['rectangle2_title']);
  430. $scale = $this->scale($item['image']);
  431. $items[] = $item;
  432. }
  433. {
  434. $item['image'] = $val['square_image'];
  435. $item['type'] = $val['square_type'];
  436. $item['data'] = $val['square_data'];
  437. $item['title'] = $this->safe_string($val['square_title']);
  438. $items[] = $item;
  439. }
  440. $ids = [];
  441. foreach ($items as $item)
  442. {
  443. if($item['type'] == 'goods') {
  444. $ids[] = intval($item['data']);
  445. }
  446. }
  447. if(self::check_goods($ids)) {
  448. $goods_ids = array_merge($goods_ids,$ids);
  449. return $items;
  450. } else {
  451. return false;
  452. }
  453. }
  454. private function filter_goods($items,&$goods_ids,&$scale)
  455. {
  456. $scale = 1;
  457. $result = [];
  458. foreach($items as $val)
  459. {
  460. $item['data'] = $val['goods_id'];
  461. $item['type'] = 'goods';
  462. $item['image'] = $val['goods_image'];
  463. $item['title'] = $val['goods_name'];
  464. $goods_id = intval($val['goods_id']);
  465. if(self::check_goods(array($goods_id))) {
  466. array_push($goods_ids,$goods_id);
  467. $result[] = $item;
  468. } else {
  469. continue;
  470. }
  471. }
  472. if(empty($result)) {
  473. return false;
  474. } else {
  475. return $result;
  476. }
  477. }
  478. private function filter_adv($items,&$goods_ids,&$scale)
  479. {
  480. $result = [];
  481. $has_set = false;
  482. foreach($items as $item)
  483. {
  484. $val['image'] = $item['image'];
  485. $val['type'] = $item['type'];
  486. $val['data'] = $item['data'];
  487. $val['title'] = $this->safe_string($item['title']);
  488. if($has_set == false) {
  489. $scale = $this->scale($item['image']);
  490. $has_set = true;
  491. }
  492. if($item['type'] == 'goods')
  493. {
  494. $goods_id = intval($item['data']);
  495. if(self::check_goods(array($goods_id))) {
  496. array_push($goods_ids,$goods_id);
  497. $result[] = $val;
  498. } else {
  499. continue;
  500. }
  501. }
  502. else {
  503. $result[] = $val;
  504. }
  505. }
  506. return $result;
  507. }
  508. }