123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/6/16
- * Time: 下午2:07
- */
- //将乱七八糟的格式输出,整合成统一的格式输出
- require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
- class brands_special
- {
- const publish_brands_special = 56;
- const local_brands_special = 50;
- const test_brands_special = 56;
- static private $stBImgContianers;
- public function brands_special()
- {
- if(self::$stBImgContianers == null) {
- self::$stBImgContianers = [];
- $this->init();
- }
- }
- public function info($brand_id)
- {
- if(isset(self::$stBImgContianers[$brand_id])) {
- return self::$stBImgContianers[$brand_id];
- } else {
- return false;
- }
- }
- private function init()
- {
- $special_id = self::special_id();
- if($special_id == false) return false;
- $helper = new special_formater($special_id);
- $blocks = $helper->format($goods_ids);
- foreach ($blocks as $block)
- {
- $items = $block['items'];
- if(empty($items) || is_array($items) == false) continue;
- foreach ($items as $item)
- {
- $brand_id = intval($item['data']);
- $data = $item;
- $data['scale'] = $block['scale'];
- self::$stBImgContianers[$brand_id] = $data;
- }
- if(!empty($items) && is_array($items)) {
- }
- }
- }
- private static function special_id()
- {
- if (is_pushoms()) {
- return self::publish_brands_special;
- }
- elseif(is_localdebug()) {
- return self::local_brands_special;
- }
- else {
- return self::test_brands_special;
- }
- }
- }
- class special_formater
- {
- const def_item_bg_color = '#F2F2F2';
- const def_item_bg_type = 'color';
- const def_divider_bg_img= '/mobile/defimg/divider_bg.png';
- private $special_id;
- private $filter_data;
- private $goods_ids;
- private $old_version;
- public function __construct($special_id,$old_version = false)
- {
- $this->special_id = $special_id;
- $this->old_version = $old_version;
- $this->read_cache();
- }
- public static function def_divider()
- {
- static $block = [];
- if(empty($block))
- {
- $block['item_type'] = 'divider';
- $block['item_title'] = '';
- $block['bg_type'] = 'image';
- $image = RESOURCE_SITE_URL . self::def_divider_bg_img;
- $block['bg_data'] = $image;
- $block['has_margin'] = false;
- $block['scale'] = self::scale($image);
- }
- return $block;
- }
- public static function format_brand_sale($brand_id,$can_click)
- {
- if($brand_id <= 0) return array();
- $helper = new brands_special();
- $info = $helper->info($brand_id);
- if($info == false) return array();
- $result['item_title'] = '';
- $result['item_type'] = 'home1';
- $result['scale'] = $info['scale'];
- $item['image'] = $info['image'];
- $item['type'] = $info['type'];
- if($can_click == true) {
- $item['data'] = $info['data'];
- } else {
- $item['data'] = 0;
- }
- $item['title'] = $info['title'];
- $result['items'][] = $item;
- return $result;
- }
- public static function format_grid_brands($brands)
- {
- $result = [];
- $result['item_title'] = '';
- $result['item_type'] = 'home_grid';
- $result['scale'] = 3;
- $items = [];
- foreach ($brands as $brand) {
- $item['image'] = $brand['brand_logo'];
- $item['type'] = 'brand';
- $item['data'] = $brand['brand_id'];
- $item['title'] = $brand['brand_name'];
- $items[] = $item;
- }
- $result['items'] = $items;
- return $result;
- }
- public static function format_comments($comments)
- {
- $blocks = [];
- foreach ($comments as $val)
- {
- $block = [];
- $block['item_title'] = '';
- $block['item_type'] = 'home_comment';
- $block['scale'] = 1;
- $item['image'] = '';
- $item['type'] = 'comment';
- $item['data'] = intval($val['geval_id']);
- $item['title'] = $val['geval_goodsname'];
- $items[] = $item;
- $block['items'] = $items;
- $blocks[] = $block;
- }
- return $blocks;
- }
- public static function format_brand($brands)
- {
- if(empty($brands)) return array();
- if(count($brands) == 1)
- {
- $brand_id = intval($brands[0]['data']);
- return self::format_brand_sale($brand_id,true);
- }
- else
- {
- $result = [];
- $result['item_title'] = '';
- $result['item_type'] = 'homewords';
- foreach ($brands as $val) {
- $item['image'] = "";
- $item['type'] = $val['type'];
- $item['data'] = strval($val['data']);
- $item['title'] = $val['title'];
- $result['items'][] = $item;
- }
- return $result;
- }
- }
- public static function format_category($cats,$title = '')
- {
- if(empty($cats)) return array();
- $result = [];
- $result['item_title'] = $title;
- $result['item_type'] = 'homewords';
- foreach ($cats as $val) {
- $item['image'] = "";
- $item['type'] = $val['type'];
- $item['data'] = strval($val['data']);
- $item['title'] = $val['title'];
- $result['items'][] = $item;
- }
- return $result;
- }
- public static function format_keyword($words,$title='')
- {
- $result = [];
- $result['item_title'] = $title;
- $result['item_type'] = 'homewords';
- foreach ($words as $word) {
- $item['image'] = "";
- $item['type'] = 'keyword';
- $item['data'] = $word;
- $item['title'] = $word;
- $result['items'][] = $item;
- }
- return $result;
- }
- public static function format_bonus($bonuses,$title='')
- {
- $result = [];
- $result['item_title'] = $title;
- $result['item_type'] = 'home_bonus';
- foreach ($bonuses as $bonus) {
- $item['image'] = "";
- $item['type'] = 'bonus';
- $item['data'] = $bonus['bonus_sn'];
- $item['title'] = '';
- $result['items'][] = $item;
- }
- return $result;
- }
- public static function format_type($type_infos,$title='')
- {
- $result = [];
- $result['item_title'] = $title;
- $result['item_type'] = 'home_type';
- foreach ($type_infos as $type) {
- $item['image'] = "";
- $item['type'] = 'bonus_type';
- $item['data'] = $type['type_sn'];
- $item['title'] = '';
- $result['items'][] = $item;
- }
- return $result;
- }
- public static function format_goods($goods_ids,$title,$sort_summary)
- {
- $result = [];
- $result['item_title'] = empty($title) ? '' : $title;
- $result['item_type'] = 'home_goods';
- foreach ($goods_ids as $goods_id)
- {
- $summary = $sort_summary[$goods_id];
- $item['image'] = $summary['goods_image_url'];
- $item['show_type'] = "goods";
- $item['show_data'] = strval($goods_id);
- $item['type'] = "goods";
- $item['data'] = strval($goods_id);
- $item['title'] = $summary['goods_mobile_name'];
- $result['items'][] = $item;
- }
- return $result;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public function format(&$goods_ids)
- {
- $goods_ids = $this->goods_ids;
- return $this->filter_data;
- }
- private function read_cache()
- {
- $mod_special = Model('mb_special');
- $specials = $mod_special->getMbSpecialItemUsableListByID($this->special_id);
- if(empty($specials)) {
- Log::record("getMbSpecialItemUsableListByID is empty",Log::ERR);
- }
- if(isset($specials["mobile_filter_data"])) {
- $this->filter_data = $specials['mobile_filter_data'];
- $this->goods_ids = $specials['mobile_goods_ids'];
- } else {
- $this->filter_data = $this->filter($specials['blocks'],$goods_ids);
- $this->goods_ids = $goods_ids;
- $specials['mobile_filter_data'] = $this->filter_data;
- $specials['mobile_goods_ids'] = $goods_ids;
- $cache = array('special' => serialize($specials));
- wcache($this->special_id, $cache, 'mb_special');
- }
- }
- private function filter($specials,&$goods_ids)
- {
- $blocks = [];
- $goods_ids = [];
- foreach($specials as $special)
- {
- $ids = [];
- $block = $this->filter_item($special,$ids,$scale);
- if($scale != false && !empty($block)) {
- $blocks[] = $block;
- $goods_ids = array_merge($goods_ids,$ids);
- }
- }
- $goods_ids = array_unique($goods_ids);
- return $blocks;
- }
- private function filter_item($special,&$goods_ids,&$scale)
- {
- $block = [];
- $block['item_title'] = empty($special['title']) ? '' : $special['title'];
- $block['item_type'] = $special['item_type'];
- $block['bg_image'] = $special['bg_image'];
- $block['bg_type'] = $special['bg_type'];
- $block['bg_data'] = $special['bg_data'];
- $block['has_margin'] = $special['has_margin'];
- $block_scale = false;
- $this->format_property($block,$block_scale);
- $key = $special['item_type'];
- if ($key == 'divider') {
- $scale = $block_scale;
- $block['scale'] = $scale;
- return $block;
- }
- $org_items = $special['items'];
- if($key != 'divider' && empty($org_items)) {
- return false;
- }
- $scale = false;
- if($key == 'adv_list' || $key == 'home3' || $key == 'home5' || $key =='home6' || $key == 'home7' || $key == 'goods')
- {
- $items = $this->filter_normal($key,$org_items,$goods_ids,$scale);
- if($key == 'goods') {
- $block['item_type'] = 'home_goods';
- }
- elseif($key == 'home3') {
- if($this->old_version == false) $scale = $block_scale;
- }
- elseif($key == 'home6') {
- $block['item_type'] = 'horizon';
- $scale = $block_scale;
- }
- elseif($key == 'home7') {
- $block['item_type'] = 'horizon_goods';
- $scale = $block_scale;
- }
- }
- elseif($key == 'home1') {
- $items = $this->filter_home1($org_items,$goods_ids,$scale);
- }
- elseif($key == 'home2') {
- $items = $this->filter_home2($org_items,$goods_ids,$scale);
- }
- elseif($key == 'home4') {
- $items = $this->filter_home4($org_items,$goods_ids,$scale);
- }
- else {
- return false;
- }
- if($items == false && $key != 'divider') {
- return false;
- }
- $this->decode($items);
- foreach ($items as &$item)
- {
- if($this->image_wh($item['image'],$w,$h)) {
- $item['width'] = $w;
- $item['height'] = $h;
- }
- }
- $block['items'] = $items;
- if($block_scale) {
- $block['scale'] = $scale;
- }
- else {
- $block['scale'] = $scale;
- }
- return $block;
- }
- /**
- * 处理type=url 时的&字符转义问题
- * @param array $data
- * @return array|bool
- */
- private function decode(&$items)
- {
- foreach($items as &$item)
- {
- static $action_types = array('url','innerurl','video','appjump');
- static $show_types = array('webview','video');
- if(in_array($item['type'],$action_types)){
- $item['data'] = htmlspecialchars_decode($item['data']);
- }
- if(in_array($item['show_type'],$show_types)){
- $item['show_data'] = htmlspecialchars_decode($item['show_data']);
- }
- }
- }
- public static function scale($image)
- {
- $image = trim($image);
- if(empty($image)) {
- Log::record("special scale error {$image}",Log::ERR);
- return false;
- }
- $ret = util::imgsize($image);
- if($ret != false) {
- $scale = doubleval($ret[0]) / doubleval($ret[1]);
- return $scale;
- } else {
- Log::record("special scale getimagesize error {$image}",Log::ERR);
- return false;
- }
- }
- private function safe_string($str)
- {
- if($str == null || empty($str)) {
- return "";
- }
- else {
- return $str;
- }
- }
- private function check_goods($ids)
- {
- if(empty($ids)) return true;
- $ids = array_unique($ids);
- $mod = Model('goods');
- $goods = $mod->getGoodsOnlineList(array("goods_id" => array("in",$ids)));
- $gids = [];
- foreach ($goods as $item) {
- $gids[] = intval($item['goods_id']);
- }
- if(count($ids) == count($gids)) {
- return true;
- } else {
- return false;
- }
- }
- private function filter_home1($val,&$goods_ids,&$scale)
- {
- $items = [];
- $item['image'] = $val['image'];
- $item['type'] = $val['type'];
- $item['data'] = $val['data'];
- $item['show_type'] = $val['show_type'];
- $item['show_data'] = $val['show_data'];
- $item['title'] = $this->safe_string($val['title']);
- $scale = self::scale($item['image']);
- $ids = [];
- if($item['type'] == 'goods') {
- $ids[] = intval($item['data']);
- }
- if(self::check_goods($ids)) {
- $goods_ids = array_merge($goods_ids,$ids);
- $items[] = $item;
- return $items;
- } else {
- return false;
- }
- }
- private function filter_home2($val,&$goods_ids,&$scale)
- {
- $items = [];
- {
- $item['image'] = $val['square_image'];
- $item['type'] = $val['square_type'];
- $item['data'] = $val['square_data'];
- $item['show_type'] = $val['square_show_type'];
- $item['show_data'] = $val['square_show_data'];
- $item['title'] = $this->safe_string($val['square_title']);
- $items[] = $item;
- }
- {
- $item['image'] = $val['rectangle1_image'];
- $item['type'] = $val['rectangle1_type'];
- $item['data'] = $val['rectangle1_data'];
- $item['show_type'] = $val['rectangle1_show_type'];
- $item['show_data'] = $val['rectangle1_show_data'];
- $item['title'] = $this->safe_string($val['rectangle1_title']);
- $items[] = $item;
- $scale = self::scale($item['image']);
- }
- {
- $item['image'] = $val['rectangle2_image'];
- $item['type'] = $val['rectangle2_type'];
- $item['data'] = $val['rectangle2_data'];
- $item['show_type'] = $val['rectangle2_show_type'];
- $item['show_data'] = $val['rectangle2_show_data'];
- $item['title'] = $this->safe_string($val['rectangle2_title']);
- $items[] = $item;
- }
- $ids = [];
- foreach ($items as $item)
- {
- if($item['type'] == 'goods') {
- $ids[] = intval($item['data']);
- }
- }
- if(self::check_goods($ids)) {
- $goods_ids = array_merge($goods_ids,$ids);
- return $items;
- } else {
- Log::record("check_goods error {$ids}",Log::ERR);
- return false;
- }
- }
- private function filter_home4($val,&$goods_ids,&$scale)
- {
- $items = [];
- {
- $item['image'] = $val['rectangle1_image'];
- $item['type'] = $val['rectangle1_type'];
- $item['data'] = $val['rectangle1_data'];
- $item['show_type'] = $val['rectangle1_show_type'];
- $item['show_data'] = $val['rectangle1_show_data'];
- $item['title'] = $this->safe_string($val['rectangle1_title']);
- $items[] = $item;
- }
- {
- $item['image'] = $val['rectangle2_image'];
- $item['type'] = $val['rectangle2_type'];
- $item['data'] = $val['rectangle2_data'];
- $item['show_type'] = $val['rectangle2_show_type'];
- $item['show_data'] = $val['rectangle2_show_data'];
- $item['title'] = $this->safe_string($val['rectangle2_title']);
- $scale = self::scale($item['image']);
- $items[] = $item;
- }
- {
- $item['image'] = $val['square_image'];
- $item['type'] = $val['square_type'];
- $item['data'] = $val['square_data'];
- $item['show_type'] = $val['square_show_type'];
- $item['show_data'] = $val['square_show_data'];
- $item['title'] = $this->safe_string($val['square_title']);
- $items[] = $item;
- }
- $ids = [];
- foreach ($items as $item)
- {
- if($item['type'] == 'goods') {
- $ids[] = intval($item['data']);
- }
- }
- if(self::check_goods($ids)) {
- $goods_ids = array_merge($goods_ids,$ids);
- return $items;
- } else {
- return false;
- }
- }
- private function image_wh($image,&$width,&$height)
- {
- $ret = util::imgsize($image);
- if ($ret != false) {
- $width = $ret[0];
- $height = $ret[1];
- return true;
- } else {
- Log::record("cannt find image imgurl={$image}",Log::ERR);
- return false;
- }
- }
- private function format_property(&$block,&$block_scale)
- {
- $image = $block['bg_image'];
- if(empty($image))
- {
- if($block['item_type'] == 'divider') {
- $image = RESOURCE_SITE_URL . self::def_divider_bg_img;
- }
- }
- else {
- $image = getMbSpecialImageUrl($image);
- }
- $block['bg_image'] = $image;
- if(!empty($image)) {
- $block_scale = self::scale($image);
- } else {
- $block_scale = false;
- }
- if($block['bg_type'] == 'image')
- {
- if($block_scale != false) {
- $block['bg_data'] = $image;
- } else {
- $block['bg_data'] = '';
- }
- }
- else
- {
- if(empty($block['bg_data'])) {
- $block['bg_data'] = self::def_item_bg_color;
- }
- }
- unset($block['bg_image']);
- }
- private function filter_normal($block_type,$items, &$goods_ids, &$scale)
- {
- $result = [];
- $has_set = false;
- foreach($items as $item)
- {
- $item['title'] = $this->safe_string($item['title']);
- if($has_set == false) {
- $scale = self::scale($item['image']);
- $has_set = true;
- }
- if($item['type'] == 'goods')
- {
- $goods_id = intval($item['data']);
- if(self::check_goods(array($goods_id))) {
- array_push($goods_ids,$goods_id);
- $result[] = $item;
- } else {
- Log::record("block_type={$block_type} cannt find goods ,goods_id={$goods_id}",Log::ERR);
- continue;
- }
- }
- else {
- $result[] = $item;
- }
- }
- if($has_set == true)
- {
- if($block_type == 'home3') {
- $scale = $scale * 2;
- }
- elseif($block_type == 'goods') {
- $scale = 1;
- }
- }
- if(empty($result)) {
- return false;
- } else {
- return $result;
- }
- }
- }
- class special_manager
- {
- private static $stInstance = null;
- private $mContents;
- public function __construct()
- {
- $this->mContents = [];
- }
- public static function instance() {
- if(self::$stInstance == null) {
- self::$stInstance = new special_manager();
- }
- return self::$stInstance;
- }
- public function special($special_id,&$goods_ids)
- {
- if(StatesHelper::fetch_state('special')) {
- $this->mContents = [];
- }
- $special_id = intval($special_id);
- if(array_key_exists($special_id,$this->mContents)) {
- $data = $this->mContents[$special_id]['data'];
- $goods_ids = $this->mContents[$special_id]['gids'];
- return $data;
- }
- else
- {
- $formater = new special_formater($special_id,$this->old_version($special_id));
- $data = $formater->format($goods_ids);
- $this->mContents[$special_id]['data'] = $data;
- $this->mContents[$special_id]['gids'] = $goods_ids;
- return $data;
- }
- }
- private function old_version($special_id)
- {
- global $config;
- if($_SESSION['is_lasted']) return false;
- return in_array($special_id,$config['old_specials']);
- }
- }
|