Grade.php 3.69 KB
<?php

namespace Home;

use Configs\CacheConfig;
use LibModels\Wap\Home\GradeData;
use Plugin\Cache;
use Plugin\Helpers;

/**
 * 会员等级相关数据处理
 */
class GradeModel
{
    /*
     * 获取个人中心-会员等级数据
     */

    public static function getGrade($uid)
    {
        $result = array();

        if (USE_CACHE) {
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get(CacheConfig::KEY_ACTION_HOME_GRADE, 'master');
            if (!empty($result)) {
                return $result;
            }
        }
        //调用接口获取数据
        $data = GradeData::getGradeData($uid);
        if (!empty($data['curVipInfo'])) {
            switch (intval($data['curVipInfo']['curLevel'])) {
                case 0://普通会员
                    $result['vipGrade']['vip0'] = true;
                    break;
                case 1://银卡会员
                    $result['vipGrade']['vip1'] = true;
                    break;
                case 2://金卡会员
                    $result['vipGrade']['vip2'] = true;
                    break;
                case 3://白金会员
                    $result['vipGrade']['vip3'] = true;
                    break;
            }

            //今年总消费
            $result['vipGrade']['costOfThisYear'] = $data['costInfo']['yearTotal'];
            //升级下一等级会员的进度;
            $result['vipGrade']['percent'] = 100 * (round($data['costInfo']['total'] / $data['nextVipInfo']['needCost'], 2));
            //距离升级所需消费金额

            if ($data['curVipInfo']['curLevel'] != 3) {
                $result['vipGrade']['costGap'] = $data['nextVipInfo']['needCost'];
            }
            //消费总计
            $result['vipGrade']['sumCost'] = $data['costInfo']['total'];
            //username 调用获取用户基本信息数据,获得username;
            //$userProfile = GradeData::getUserProfileData($gender,$uid,$channel);
            $result['vipGrade']['name'] = '杨阳';
            //跳转url(会员特权详情)
            $result['vipGrade']['allUrl'] = Helpers::url('/Home/preferential', null);
            //当前vip等级享受的特权
            $result['vipGrade']['privilege'] = $data['curVipInfo']['premiumScop'];
        }

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

        return $result;
    }

    /*
     * 获取个人中心-会员特权详情页
     */

	public static function getPreferential()
	{
		$result = array();

		if (USE_CACHE) {
			// 先尝试获取一级缓存(master), 有数据则直接返回.
			$result = Cache::get(CacheConfig::KEY_ACTION_HOME_PREFERENTIAL, 'master');
			if (!empty($result)) {
				return $result;
			}
		}
		//调取接口获得数据
		$data = GradeData::getPreferentialData();
		if (!empty($data)){
			$result['vipGrade']['privilege'] = $data;
		}


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

		return $result;
	}

}