Common.php 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
<?php

use Action\WebAction;
use Index\HomeModel;
use Product\SearchModel;
use Shopping\CartModel;
use Configs\WebCacheConfig;
use LibModels\Web\Home\IndexData;
use LibModels\Web\Home\UserData;
use LibModels\Web\Home\CartData;
use WebPlugin\Cache;
use WebPlugin\Images;
use WebPlugin\Helpers;
use Api\Yohobuy;

class CommonController extends WebAction
{

    /**
     * 获取首页资源品牌
     */
    public function getIndexResourceBrandAction()
    {
        $data = array();

        do {
            if (!$this->isAjax()) {
                break;
            }

            $type = $this->get('type');
            if (empty($type)) {
                break;
            }

            // 首页资源品牌,采用内存存储
            $key = WebCacheConfig::KEY_WEB_INDEX_BRANDS_LIST_DATA . '_' . $type;
            // array('logoBrand'=>'','moreBrand'=>'')
            $data = Cache::get($key, 'master');
            if (empty($data)) {//从slave取数据
                $data = Cache::get($key, 'slave');
            }

        } while (false);

        $this->echoJson($data);
    }

    /**
     * 新品上架 接口数据
     *
     * @param string channel 当前频道
     * @param int pageIndex 当前页数
     * @param int pageCount 一页显示个数
     */
    public function getNewArrivalAction()
    {
        $result = array();
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            $channels = array(
                HomeModel::COOKIE_NAME_BOYS,
                HomeModel::COOKIE_NAME_GIRLS,
                HomeModel::COOKIE_NAME_KIDS,
                HomeModel::COOKIE_NAME_LIFESTYLE
            );
            $channel = $this->post('type', '');
            $pageIndex = (int)$this->post('pageIndex', 0);
            $pageCount = (int)$this->post('pageCount', 8);
            if (!in_array($channel, $channels)) {
                break;
            }

            $data = HomeModel::getNewArrival($channel);
            if (empty($data)) {
                break;
            }

            if ($pageIndex < 0) {
                $pageIndex = 0;
            }
            if ($pageCount < 0 || $pageCount > 50) {
                $pageCount = 20;
            }
            $data = array_slice($data, $pageIndex, $pageCount);
            if (empty($data)) {
                break;
            }
            $result = array(
                'code' => 200,
                'goods' => $data
            );
            $data = array();

        } while (false);

        $this->echoJson($result);
    }

    /**
     * 获取资源位banner
     *
     * @return jsonp
     */
    public function getbannerAction()
    {
        $contentCode = $this->get('content_code', '');
        $callback = $this->get('callback', '');
        $width = $this->get('width', '');
        $height = $this->get('height', '');

        $data = IndexData::getResourceData($contentCode);
        if (empty($data['data'])) {
            return $this->helpJsonCallbackResult($callback, 200, '没有数据', '');
        } else {
            $banner = '';
            if (isset($data['data'][0]['data'])) {
                if ($data['data'][0]['template_name'] == 'single_image') {
                    $banner = current($data['data'][0]['data']);
                } else if ($data['data'][0]['template_name'] == 'single_name_image') {
                    $banner = $data['data'][0]['data'];
                }
                if (!empty($banner)) {
                    if (empty($width) || empty($height)) {
                        $width = 2600; //通栏广告
                        $height = 60;
                    }
                    $banner['src'] = Images::getImageUrl($banner['src'], $width, $height, 2);
                    $banner['url'] = Helpers::getUrlSafe($banner['url']);
                }
            }
            return $this->helpJsonCallbackResult($callback, $data['code'], $data['message'], $banner);
        }
    }

    /**
     * 获取邮件订阅
     *
     * @return jsonp
     */
    public function emailsubscriberAction()
    {
        $callback = $this->get('callback', '');
        $email = $this->get('email', '');
        $uid = intval($this->get('uid', '0'));
        $data = array();
        do {
            if (!Helpers::verifyEmail($email)) {
                break;
            }
            $data = IndexData::emailSubscriber($email, $uid);
            if (isset($data['code']) && $data['code'] === 200) {
                return $this->helpJsonCallbackResult($callback, $data['code'], $data['message'], array('result' => 1));
            }
        } while (false);
        return $this->helpJsonCallbackResult($callback, 403, '订阅失败', '');
    }

    /**
     * 意见反馈
     *
     * @return jsonp
     */
    public function suggestfeedbackAction()
    {
        $callback = $this->get('callback', '');
        $feedback_id = intval($this->get('feedback_id', 0));
        $question_id = intval($this->get('question_id', 0));
        $answer = trim($this->get('answer'));
        $solution = intval($this->get('solution', 0));
        if (!empty($feedback_id) || !empty($question_id) || !empty($answer) || !empty($solution)) {
            $data = IndexData::suggestFeedback($feedback_id, $question_id, $answer, $solution);
            return $this->helpJsonCallbackResult($callback, $data['code'], $data['message'], $data['data']);
        } else {
            return $this->helpJsonCallbackResult($callback, 403, '意见反馈失败');
        }
    }

    /*
     * 简单头部
     */
    public function getSimpleHeaderAction()
    {
        $result = array();
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            //获取用户
            $uid = $this->getUid(true);
            if (!$uid) {
                $isLogin = false;
                $username = '';
            } else {
                $isLogin = true;
                $username = $this->_uname;
            }
            //拼接简单头部
            $time = time();
            $tool = array(
                'favorite' => Helpers::url('/home/favorite?t=' . $time), //我的收藏链接
                'coupon' => Helpers::url('/home/coupons?t=' . $time), //我的优惠券链接
                'order' => Helpers::url('/home/orders?t=' . $time), //订单中心连接
                'help' => Helpers::url('/help'),
            );
            if ($isLogin) {
                $tool['user'] = $username;
                $tool['userCenter'] = Helpers::url('/home?t=' . $time);//用户中心链接
                $tool['logout'] = Helpers::url('/logout.html?t=' . $time); //退出
            } else {
                $tool['login'] = Helpers::url('/signin.html'); //登录链接,已登录不传
                $tool['register'] = Helpers::url('/reg.html'); //注册链接,已登录不传
            }

            $simpleHeader = array(
                'user' => $username,
                'href' => $tool,
                'logo' => array(
                    'img' => '//static.yohobuy.com/newheader/img/logo_e.png',
                    'url' => SITE_MAIN
                ),
            );

            $result = array(
                'code' => 200,
                'data' => $simpleHeader
            );
        } while (false);

        $this->echoJson($result);
    }

    /**
     * 头部调用登陆用户信息
     */
    public function passportAction()
    {
        // 是否调用个人信息数字接口
        $getNumberFlag = true;

        $userInfo = array();
        $callback = $this->get('callback');
        $uid = $this->getUid();
        do {

            if (empty($uid)) {
                break;
            }

            //获取个人信息VIP资料
            $api['profile'] = UserData::getUserProfile($uid, true);
            if ($getNumberFlag) {
                $api['number'] = UserData::getUserInfoNum($uid, true);
            }
            $apiInfo = Yohobuy::getMulti($api);

            //个人信息调用失败返回
            if (!isset($apiInfo['profile']) || empty($apiInfo['profile'])) {
                break;
            }
            $userInfo['result'] = 1;

            //个人资料
            $userInfo['profileName'] = $apiInfo['profile']['profile_name'];
            $userInfo['headIco'] = isset($apiInfo['profile']['head_ico']) && !empty($apiInfo['profile']['head_ico']) ? Helpers::getImageUrl($apiInfo['profile']['head_ico'], 63, 63) : '';
            //VIP信息
            $userInfo['curLevel'] = $apiInfo['profile']['vip_info']['cur_level'];
            $userInfo['curTitle'] = $apiInfo['profile']['vip_info']['title'];
            $userInfo['curYearCost'] = isset($apiInfo['profile']['vip_info']['curYearCost']) ? round($apiInfo['profile']['vip_info']['curYearCost']) : 0;
            $userInfo['nextLevel'] = isset($apiInfo['profile']['vip_info']['next_level']) ? $apiInfo['profile']['vip_info']['next_level'] : 0;
            $userInfo['nextVipTitle'] = isset($apiInfo['profile']['vip_info']['nextVipTitle']) ? $apiInfo['profile']['vip_info']['nextVipTitle'] : '';
            $userInfo['nextVipNeedCost'] = isset($apiInfo['profile']['vip_info']['nextVipNeedCost']) ? round($apiInfo['profile']['vip_info']['nextVipNeedCost']) : 0;

            //升级百分比
            if ($userInfo['nextVipNeedCost']) {
                $userInfo['curYearCostPer'] = round($userInfo['curYearCost'] / $userInfo['nextVipNeedCost'] * 100);
            }

            //待处理订单
            $userInfo['order'] = isset($apiInfo['number']) && !empty($apiInfo['number']) ? $apiInfo['number']['wait_pay_num'] + $apiInfo['number']['wait_cargo_num'] + $apiInfo['number']['send_cargo_num'] : 0;
            //我的收藏
            $userInfo['favorite'] = isset($apiInfo['number']) && !empty($apiInfo['number']) ? $apiInfo['number']['brand_favorite_total'] : 0;
            //我的优惠券
            $userInfo['coupon'] = isset($apiInfo['number']) && !empty($apiInfo['number']) ? $apiInfo['number']['coupon_num'] : 0;
            //我的有货币
            $userInfo['coin'] = isset($apiInfo['number']) && !empty($apiInfo['number']) ? $apiInfo['number']['yoho_coin_num'] : 0;
            //我的退换货
            $userInfo['return'] = isset($apiInfo['number']) && !empty($apiInfo['number']) ? $apiInfo['number']['refund_exchange_num'] : 0;

            //更新购物车cookie信息
            $total = 0;
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = CartModel::shoppingCart($uid, $shoppingKey);
            if (isset($result['main_goods'])) {
                foreach ($result['main_goods'] as $val) {
                    $total += $val['buy_number'];
                }
                // 老站购物车需要的COOKIE
                $this->setCookie('_g', json_encode(array(
                    '_k' => $shoppingKey,
                    '_nac' => $total,
                    '_ac' => 0,
                    '_r' => 1
                )));
            }

        } while (false);

        if (empty($userInfo)) {
            $this->helpJsonCallbackResult($callback, 403, 'User info', '');
        } else {
            $this->helpJsonCallbackResult($callback, 200, 'User info', $userInfo);
        }

    }

    /**
     * 最近浏览记录
     */
    public function recentReviewAction()
    {
        $result = array();
        $callback = $this->get('callback');
        $limit = $this->get('limit');
        do {
            $productInfo = explode(',', $this->getCookie('_browseskn'));
            $skn = '';
            if (empty($productInfo)) {
                break;
            }
            foreach ($productInfo as $val) {
                $sknArray = explode('-', $val);
                $skn[] = $sknArray[0];
            }
            $skn = array_slice($skn, 0, $limit);
            $result = SearchModel::historyProduct($skn, $limit);
        } while (false);
        $this->helpJsonCallbackResult($callback, 200, 'User info', $result);
    }

    /**
     * 购物车
     * @author sefon 2016-4-20 15:15:31
     */
    public function shoppingCartAction()
    {
        $callback = $this->get('callback');
        $uid = $this->getUid(true);
        $shoppingKey = Helpers::getShoppingKeyByCookie();
        $result = CartModel::shoppingCart($uid, $shoppingKey);
        $this->helpJsonCallbackResult($callback, 200, 'shoppingCart', $result);
    }

    /**
     * 删除购物车商品
     */
    public function delCartGoodsAction()
    {
        $productSku = $this->get('product_sku');
        $buyNumber = $this->get('product_num');
        $callback = $this->get('callback');
        $uid = $this->getUid(true);
        $shoppingKey = Helpers::getShoppingKeyByCookie();
        $skuList[$productSku] = intval($buyNumber);
        $result = CartModel::removeFromCart($uid, json_encode($skuList), $shoppingKey);
        if (isset($result['code']) && $result['code'] == 200 && isset($result['total_goods_num'])) {
            $this->helpJsonCallbackResult($callback, 200, $result['message'], array('total_goods_num' => $result['total_goods_num']));
        } else {
            $this->helpJsonCallbackResult($callback, $result['code'], $result['message'], '');
        }
    }

    /**
     * 选择支付时,时间间隔校验插入
     */
    public function addPaymentIntervalAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            if (!$this->isAjax()) {
                break;
            }

            $uid = $this->getUid(TRUE);
            $orderCode = $this->get('orderCode', '');
            $payment = $this->get('payment', 0);

            if (!$uid || !$orderCode || !$payment) {
                break;
            }

            $res = CartData::savePrePayInfo($uid, $orderCode, $payment);
            $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => $res['data']);

        } while (false);

        $this->echoJson($data);
    }
}