Blame view

yohobuy/m.yohobuy.com/application/models/Product/List.php 11 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php

namespace Product;

use Configs\CacheConfig;
use LibModels\Wap\Category\ClassData;
use LibModels\Wap\Category\BrandData;
use Plugin\DataProcess\ListProcess;
use Plugin\Helpers;
use Plugin\Cache;

/**
 * 商品列表相关的模板数据模型
 * 
 * @name ListModel
 * @package models/Product
 * @copyright yoho.inc
 * @version 1.0 (2015-10-27 16:27:54)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class ListModel
{
23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    /**
     * 获取品类商品列表数据
     * 
     * @param array $condition 条件
     * @return array | false
     */
    public static function getClassData($condition)
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_PRODUCT_INDEX;
            if (!empty($condition)) {
                $key .= http_build_query($condition, null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口查询数据
hf authored
47
        $listData = ClassData::filterClassData($condition);
48 49
        // 处理返回的数据
        if (isset($listData['code']) && $listData['code'] === 200) {
50
            $result = ListProcess::getListData($listData['data'], false);
51 52 53 54 55 56 57 58 59
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
60
                Cache::set($key, $result, 1800); // 缓存30分钟
61 62 63 64 65
            }
        }

        return $result;
    }
66
67
    /**
68
     * 获取品牌信息
69
     * 
70 71 72
     * @param int $id 唯一的ID
     * @param int $uid 用户ID
     * @param string $title 网站标题
73 74
     * @return array
     */
75
    public static function getBrandIntro($id, $uid, &$title)
76 77
    {
        $result = array();
78
79 80 81
        // 获取品牌介绍信息, 有缓存1小时
        $introData = BrandData::getBrandIntro($id, $uid);
        if (isset($introData['data']['brand_intro'])) {
hf authored
82 83
            $result['id'] = $id;
            $result['intro'] = $introData['data']['brand_intro'];
84
            $result['collected'] = (isset($introData['data']['is_favorite']) && $introData['data']['is_favorite'] == 'Y') ? true : false;
85 86 87 88 89
            // 顶部导航的标题
            $title = isset($introData['data']['brand_name']) ? $introData['data']['brand_name'] : '';
        }

        // 获取品牌banner的数据, 有缓存1小时
hf authored
90
        $bannerData = BrandData::getBrandBanner($id);
91
        if (isset($bannerData['data']['banner'])) {
92
            $result['banner'] = Helpers::getImageUrl($bannerData['data']['banner'], 640, 150);
93 94
        }
95 96 97 98 99 100 101 102 103 104 105 106 107 108
        return $result;
    }

    /**
     * 获取品牌商品列表数据
     * 
     * @param array $condition 条件参数
     * @param string $title 网站标题
     * @return array
     */
    public static function getBrandData($condition, &$title)
    {
        $result = array();
109 110 111 112 113 114
        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_PRODUCT_BRAND;
            if (!empty($condition)) {
                $key .= http_build_query($condition, null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
115 116
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
117 118 119 120 121
                return $result;
            }
        }

        // 调用接口查询数据
hf authored
122
        $listData = BrandData::filterBrandData($condition);
123 124
        // 处理返回的数据
        if (isset($listData['code']) && $listData['code'] === 200) {
125
            $result = ListProcess::getListData($listData['data'], false);
126 127
            if (!empty($listData['data']['brand'])) {
                $result['brandWay'] = array(
hf authored
128
                    'url' => 'http://' . $listData['data']['brand']['brand_domain'] . SUB_DOMAIN,
hf authored
129 130
                    'thumb' => Helpers::getImageUrl($listData['data']['brand']['brand_ico'], 75, 40),
                    'name' => $listData['data']['brand']['brand_name']
131
                );
hf authored
132
                $title = $listData['data']['brand']['brand_name'];
133
            }
134 135 136 137
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
138 139
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
140 141 142
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
143
                Cache::set($key, $result, 1800); // 缓存30分钟
144 145
            }
        }
146
147 148 149 150
        return $result;
    }

    /**
hf authored
151 152 153 154 155 156 157 158 159 160 161
     * 根据品牌ID获取品牌LOGO
     * 
     * @param int $id 品牌ID
     * @param string $title 品牌标题
     * @return array | false
     */
    public static function getBrandLogoByIds($id, &$title)
    {
        $result = false;

        if (USE_CACHE) {
162
            $key = CacheConfig::KEY_ACTION_PRODUCT_BRAND_LOGO . strval($id);
hf authored
163 164 165 166 167 168 169 170
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口查询数据
171 172 173
        $brandLogo = BrandData::getBrandLogo($id);
        var_dump($brandLogo);
        exit;
hf authored
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        // 处理返回的数据
        if (isset($brandLogo['data'][0])) {
            $result = array(
                'url' => Helpers::url('', null, $brandLogo['data'][0]['brand_domain']),
                'thumb' => Helpers::getImageUrl($brandLogo['data'][0]['brand_ico'], 75, 40),
                'name' => $brandLogo['data'][0]['brand_name'],
            );
            $title = $result['name'];
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
191
                Cache::set($key, $result); // 缓存1小时
hf authored
192 193 194 195 196
            }
        }

        return $result;
    }
197 198

    /**
hf authored
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
     * 根据品牌ID获取品牌LOGO
     * 
     * @param int $id 品牌ID
     * @param string $title 品牌标题
     * @return array | false
     */
    public static function getBrandLogoByDomain($domain, &$title)
    {
        $result = false;

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_PRODUCT_BRAND_LOGO_DOMAIN . strval($domain);
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口查询数据
hf authored
219
        $brandLogo = BrandData::getBrandLogoByDomain($domain);
hf authored
220 221 222
        // 处理返回的数据
        if (isset($brandLogo['data'])) {
            $result = array(
223
                'id' => $brandLogo['data']['id'],
hf authored
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                'url' => Helpers::url('', null, $brandLogo['data']['brand_domain']),
                'thumb' => Helpers::getImageUrl($brandLogo['data']['brand_ico'], 75, 40),
                'name' => $brandLogo['data']['brand_name'],
            );
            $title = $result['name'];
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result); // 缓存1小时
            }
        }

        return $result;
    }
hf authored
244 245

    /**
246 247 248
     * 获取所有的品牌名称列表
     * 
     * @return array(
249
     *    品牌ID => 品牌域名(domain)
250 251 252 253 254 255 256
     * )
     */
    public static function getAllBrandDomains()
    {
        $result = array();

        if (USE_CACHE) {
257
            $key = CacheConfig::KEY_ACTION_PRODUCT_BRAND_DOMAINS;
258 259 260 261 262 263 264 265 266 267 268 269
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        $brand = BrandData::getBrandsData(null);
        /* 按字母'A-Z'分组的品牌列表 */
        if (!empty($brand['data']['brands'])) {
            foreach ($brand['data']['brands'] as $value) {
                foreach ($value as $row) {
270
                    $result[$row['id']] = $row['brand_domain'];
271 272 273 274 275 276
                }
            }
        }
        // 更多关联的品牌
        if (!empty($brand['data']['morebrands'])) {
            foreach ($brand['data']['morebrands'] as $row) {
277
                $result[$row['id']] = $row['brand_domain'];
278 279 280 281 282 283 284 285 286 287 288
            }
        }
        $brand = array();

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                Cache::set($key, $result);
            }
        }

        return $result;
    }

    /**
     * 获取所有的品牌名称列表
     * 
     * @return array(
     *    品牌域名(domain) => 品牌名称(name)
     * )
     */
    public static function getAllBrandNames()
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_PRODUCT_BRAND_NAMES;
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        $brand = BrandData::getBrandsData(null);
        /* 按字母'A-Z'分组的品牌列表 */
        if (!empty($brand['data']['brands'])) {
            foreach ($brand['data']['brands'] as $value) {
                foreach ($value as $row) {
                    $result[$row['brand_domain']] = strtolower($row['brand_name']);
                }
            }
        }
        // 更多关联的品牌
        if (!empty($brand['data']['morebrands'])) {
            foreach ($brand['data']['morebrands'] as $row) {
                $result[$row['brand_domain']] = strtolower($row['brand_name']);
            }
        }
        $brand = array();

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result);
341 342 343 344 345 346 347
            }
        }

        return $result;
    }

}