User.php 17.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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
<?php

namespace Home;

use LibModels\Web\Home\UserData;
use LibModels\Web\Product\SearchData;
use LibModels\Web\Home\AddressData;
use Configs\ChannelConfig;
use WebPlugin\Helpers;
use WebPlugin\Images;
use LibModels\Web\Home\MessageData;
use WebPlugin\Cache;

/**
 * 个人中心-用户相关业务逻辑处理
 */
class UserModel
{

    /**
     * 个人中心-面包屑
     * name 当前停留面包屑名称
     * nav 个人中心 之后的面包屑
     */
    public static function getCenterCrumb($name, $nav = array())
    {
        $retNav = array(
            array(
                'href' => SITE_MAIN,
                'name' => 'YOHO!BUY 有货首页'
            ),
            array(
                'href' => Helpers::url('/home'),
                'name' => '个人中心'
            )
        );
        if ($nav) {
            $retNav[] = $nav;
        }
        $retNav[] = array(
            'name' => $name
        );

        return $retNav;
    }

    public static function getCustomerServiceSwitch() {
        return USE_CACHE && Cache::getBy('zookeeper:/pc/clientService/new') === 'true' ? 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409' : '/service/client';
    }
    /**
     * 个人中心-左侧导航
     * @param string $activeName 当前选中导航的名称
     * @param string $uid 当前用户
     */
    public static function getCenterLeftNav($activeName = '', $uid = 0)
    {
        $nav = array(
            array(
                'title' => '交易管理',
                'subNav' => array(
                    array(
                        'name' => '我的订单',
                        'href' => Helpers::url('/home/orders')
                    ),
                    array(
                        'name' => '我的收藏',
                        'href' => Helpers::url('/home/favorite')
                    ),
                    array(
                        'name' => '我的有货币',
                        'href' => Helpers::url('/home/currency')
                    ),
                    array(
                        'name' => '我的红包',
                        'href' => Helpers::url('/home/redenvelopes')
                    ),
                    array(
                        'name' => '我的优惠券',
                        'href' => Helpers::url('/home/coupons')
                    ),
                    array(
                        'name' => '我的VIP',
                        'href' => Helpers::url('/home/vip')
                    )
                )
            ),
            array(
                'title' => '服务中心',
                'subNav' => array(
                    array(
                        'name' => '我的退/换货',
                        'href' => Helpers::url('/home/returns')
                    ),
                    array(
                        'name' => '我的咨询',
                        'href' => Helpers::url('/home/consult')
                    ),
                    array(
                        'name' => '我的评论',
                        'href' => Helpers::url('/home/comment')
                    ),
//                    array(
//                        'name' => '我的投诉',
//                        'href' => Helpers::url('/home/complaints')
//                    ),
                    array(
                        'name' => '我的信息',
                        'count' => self::unredTotal($uid), //获取未读消息数量
                        'href' => Helpers::url('/home/message')
                    ),
                    array(
                        'name' => '在线客服',
                        'href' => self::getCustomerServiceSwitch(),
                        'isBlank'=>true
                    )
                )
            ),
            array(
                'title' => '个人信息管理',
                'subNav' => array(
                    array(
                        'name' => '编辑个人资料',
                        'href' => Helpers::url('/home/user')
                    ),
                    array(
                        'name' => '账号安全',
                        'href' => Helpers::url('/home/account')
                    ),
                    array(
                        'name' => '地址管理',
                        'href' => Helpers::url('/home/address')
                    ),
                    array(
                        'name' => '兑换礼品卡',
                        'href' => Helpers::url('/home/gift')
                    ),
                )
            )
        );
        //选中项
        if ($activeName) {
            foreach ($nav as $key => $value) {
                $subNav = $value['subNav'];
                foreach ($subNav as $sk => $sv) {
                    if ($activeName == $sv['name']) {
                        $nav[$key]['subNav'][$sk]['active'] = true;
                    }
                }
            }
        }
        return $nav;
    }

    /**
     * 个人中心-个人基础信息
     */
    public static function getUserInfo($uid)
    {
        $userInfo = UserData::getUserInfo($uid);
//        print_r($userInfo); exit;
        // 学生认证
        $res['is_student'] = isset($userInfo['data']['vip_info']['is_student']) ? $userInfo['data']['vip_info']['is_student'] : 0;
        //头像
        $res['headIco'] = empty($userInfo['data']['head_ico']) ? ChannelConfig::$headDefaultImgIco : Images::getImageUrl($userInfo['data']['head_ico'], 100, 100, 2);
        $res['email'] = isset($userInfo['data']['email']) ? $userInfo['data']['email'] : '';
        $res['nickname'] = isset($userInfo['data']['nickname']) ? $userInfo['data']['nickname'] : '';
        $res['username'] = isset($userInfo['data']['username']) ? $userInfo['data']['username'] : '';
        $res['gender'] = isset($userInfo['data']['gender']) ? $userInfo['data']['gender'] : 3;
        $birthday = isset($userInfo['data']['birthday']) ? $userInfo['data']['birthday'] : '';
        $res['birthday'] = $birthday;
        $res['profession'] = isset($userInfo['data']['profession']) ? $userInfo['data']['profession'] : 0;
        $res['income'] = isset($userInfo['data']['income']) ? $userInfo['data']['income'] : 0;
        //生日年-月-日
        $year = '';
        $month = '';
        $day = '';
        if ($birthday) {
            $birthArr = explode("-", $birthday);
            $year = $birthArr[0];
            $month = $birthArr[1];
            $day = $birthArr[2];
        }
        $calArr = self::getCalendar($year, $month, $day);
        $res['handleYear'] = $calArr['handleYear'];
        $res['handleMonth'] = $calArr['handleMonth'];
        $res['handleDay'] = $calArr['handleDay'];
        //性别
        $gender = ChannelConfig::$genderArr;
        foreach ($gender as $gk => $gv) {
            if ($gv['value'] == $res['gender']) {
                $gender[$gk]['isChecked'] = true;
                break;
            }
        }
        $res['handleGender'] = $gender;
        //职业
        $profession = ChannelConfig::$professionArr;
        foreach ($profession as $pk => $pv) {
            if ($res['profession'] == $pv['value']) {
                $profession[$pk]['isChecked'] = true;
                break;
            }
        }
        $res['handleProfession'] = $profession;
        //收入
        $income = ChannelConfig::$incomeArr;
        foreach ($income as $ik => $iv) {
            if ($res['income'] == $iv['value']) {
                $income[$ik]['isChecked'] = true;
                break;
            }
        }
        $res['handleIncome'] = $income;
        return $res;
    }

    /**
     * 取用户头像
     * @param type $uid
     * @return boolean
     */
    public static function getUserHeadImg($uid)
    {
        $userInfo = UserData::getUserInfo($uid);
        //头像
        return empty($userInfo['data']['head_ico']) ? ChannelConfig::$headDefaultImgIco : Images::getImageUrl($userInfo['data']['head_ico'], 100, 100, 2);
    }

    /**
     * 个人中心-联系信息
     */
    public static function getUserContactInfo($uid)
    {
        $userInfo = UserData::getUserContactInfo($uid);
        $res['phone'] = isset($userInfo['data']['phone']) ? $userInfo['data']['phone'] : '';
        $res['mobile'] = isset($userInfo['data']['mobile']) ? $userInfo['data']['mobile'] : '';
        $res['areaCode'] = isset($userInfo['data']['area_code']) ? $userInfo['data']['area_code'] : '';
        $res['qq'] = isset($userInfo['data']['qq']) ? $userInfo['data']['qq'] : '';
        $res['zipCode'] = isset($userInfo['data']['zip_code']) ? $userInfo['data']['zip_code'] : '';
        $res['fullAddress'] = isset($userInfo['data']['full_address']) ? $userInfo['data']['full_address'] : '';
        $provinceArr = array();
        $cityArr = array();
        $areaArr = array();
        if ($res['areaCode']) {
            $provinceCode = substr($res['areaCode'], 0, 2);
            $cityCode = substr($res['areaCode'], 0, 4);
            $areaCode = $res['areaCode'];
            $provinceArr = self::getProviceCityInfo();
            foreach ($provinceArr as $prok => $prov) {
                if ($prov['value'] == $provinceCode) {
                    $provinceArr[$prok]['isChecked'] = true;
                    break;
                }
            }
            $cityArr = self::getProviceCityInfo($provinceCode);
            foreach ($cityArr as $ck => $cv) {
                if ($cv['value'] == $cityCode) {
                    $cityArr[$ck]['isChecked'] = true;
                    break;
                }
            }
            $areaArr = self::getProviceCityInfo($cityCode);
            foreach ($areaArr as $areak => $areav) {
                if ($areav['value'] == $areaCode) {
                    $areaArr[$areak]['isChecked'] = true;
                    break;
                }
            }
        }
        $res['handleProvince'] = $provinceArr;
        $res['handleCity'] = $cityArr;
        $res['handleArea'] = $areaArr;
        return $res;
    }

    /**
     * 个人中心-用户习惯
     */
    public static function getUserHabitsInfo($uid)
    {
        $userInfo = UserData::getUserHabitsInfo($uid);
        $dressHabits = ChannelConfig::$dressHabits;
        $shoppingHabits = ChannelConfig::$shoppingHabits;
        if (isset($userInfo['data']['shopping']) && $userInfo['data']['shopping']) {
            foreach ($shoppingHabits as $shk => $shv) {
                if ($shv['value'] == $userInfo['data']['shopping']) {
                    $shoppingHabits[$shk]['isChecked'] = true;
                    break;
                }
            }
        }
        if (isset($userInfo['data']['dress']) && $userInfo['data']['dress']) {
            $dressIds = explode(",", $userInfo['data']['dress']);
            foreach ($dressHabits as $dhk => $dhv) {
                if (in_array($dhv['value'], $dressIds)) {
                    $dressHabits[$dhk]['isChecked'] = true;
                }
            }
        }
        $res['shopping'] = $shoppingHabits;
        $res['dress'] = $dressHabits;
        return $res;
    }

    /**
     * 个人中心-喜爱的品牌
     * 
     */
    public static function getUserLikeBrandInfo($uid)
    {
        $brandInfo = SearchData::getBrandData();
        $res['hotBrands'] = self::getHotBrands($brandInfo);
        $userInfo = UserData::getUserLikeBrand($uid);
        $res['likeBrandStr'] = empty($userInfo['data']['brand']) ? '' : $userInfo['data']['brand'];
        $favBrands = array();
        if ($res['likeBrandStr']) {
            $brandArr = explode(",", $res['likeBrandStr']);
            foreach ($brandArr as $brandId) {
                foreach ($brandInfo['data'] as $bv) {
                    if ($bv['id'] == $brandId) {
                        $favBrands[] = array(
                            'name' => $bv['brand_name'],
                            'id' => 'brand-' . $bv['id']
                        );
                    }
                }
            }
        }
        $res['favBrands'] = $favBrands;
        return $res;
    }

    /**
     * 获取热销品牌
     * @param type $i 个数
     * @return type
     */
    public static function getHotBrands($brandInfo, $i = 20)
    {
        //取前20个热销品牌
        $hotBrands = array();
        if ($brandInfo['data']) {
            foreach ($brandInfo['data'] as $value) {
                if ($value['is_hot'] == 'Y') {
                    $hotBrands[] = array(
                        'id' => 'pp-' . $value['id'],
                        'name' => 'pp[]',
                        'value' => $value['id'],
                        'text' => $value['brand_name']
                    );
                    $i--;
                }
                if ($i <= 0) {
                    break;
                }
            }
        }
        return $hotBrands;
    }

    /**
     * 联动取地区信息
     */
    public static function getProviceCityInfo($parentId = 0)
    {
        $addressInfo = AddressData::getProviceCityInfo($parentId);
        $res = array();
        $res[0]['value'] = '0';
        if (strlen($parentId) == 2) {
            $defaultText = '请选择省份';
        }
        elseif (strlen($parentId) == 4) {
            $defaultText = '请选择市';
        }
        else {
            $defaultText = '请选择区县';
        }
        $res[0]['text'] = $defaultText;
        if ($addressInfo['data']) {
            foreach ($addressInfo['data'] as $ak => $av) {
                $res[] = array(
                    'value' => $av['id'],
                    'text' => $av['caption'],
                );
            }
        }
        return $res;
    }

    /**
     * 检查brandName是否在品牌库中
     * @param type $brandName
     */
    public static function isBrandName($brandName)
    {
        $ret['flag'] = false;
        do {
            if (!$brandName) {
                break;
            }
            $brandInfo = SearchData::getBrandData();
            if ($brandInfo['data']) {
                foreach ($brandInfo['data'] as $value) {
                    if ($value['brand_name'] == $brandName) {
                        $ret['flag'] = true;
                        $ret['data']['brandName'] = $brandName;
                        $ret['data']['brandId'] = $value['id'];
                        break;
                    }
                }
            }
        }
        while (false);
        return $ret;
    }

    protected static function getCalendar($year = '', $month = '', $day = '')
    {
        $nowYear = date('Y');
        $year = intval($year);
        $month = intval($month);
        $day = intval($day);
        $yearArr[0]['value'] = 0;
        $yearArr[0]['text'] = '年';
        for ($i = 1950; $i <= $nowYear; $i++) {
            $yearArr[] = array('value' => $i, 'text' => $i);
        }
        if ($year) {
            foreach ($yearArr as $yk => $yv) {
                if ($yv['value'] == $year) {
                    $yearArr[$yk]['isChecked'] = true;
                    break;
                }
            }
        }

        $monthArr[0]['value'] = 0;
        $monthArr[0]['text'] = '月';
        for ($j = 1; $j <= 12; $j++) {
            $monthArr[] = array('value' => $j, 'text' => $j);
        }
        if ($month) {
            foreach ($monthArr as $mk => $mv) {
                if ($mv['value'] == $month) {
                    $monthArr[$mk]['isChecked'] = true;
                    break;
                }
            }
        }

        $dayArr[0]['value'] = 0;
        $dayArr[0]['text'] = '日';
        if ($day) {
            $dayLimit = self::daysInMonth($year, $month);
            for ($z = 1; $z <= $dayLimit; $z++) {
                $dayArr[] = array('value' => $z, 'text' => $z);
            }
            foreach ($dayArr as $dk => $dv) {
                if ($dv['value'] == $day) {
                    $dayArr[$dk]['isChecked'] = true;
                    break;
                }
            }
        }

        $res['handleYear'] = $yearArr;
        $res['handleMonth'] = $monthArr;
        $res['handleDay'] = $dayArr;
        return $res;
    }

    protected static function daysInMonth($year, $month)
    {
        $year = intval($year);
        $month = intval($month);
        return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
    }

    public static function unredTotal($uid)
    {
        $number = 0;
        $data = MessageData::unredTotal($uid);
        if (isset($data['data']['total'])) {
            $number = intval($data['data']['total']);
        }
        return $number;
    }

    /**
     * 学生认证入口开关,zookeeper中配置
     */
    public static function getStudentEntryStatus() {
        return USE_CACHE && Cache::getBy('zookeeper:/pc/user/removeStudentIdentification') === 'true' ? true : false;
    }
}