Helpers.php 9.52 KB
<?php

namespace Plugin;

/**
 * 辅助类
 */
class Helpers
{

    /**
     * 根据尺寸获得图片url
     * @param  string  $fileName  文件名
     * @param  integer  $width    图片宽度
     * @param  integer  $height   图片高度
     * @param  integer $mode     模式
     * @return string            图片地址
     */
    public static function getImageUrl($fileName, $width, $height, $mode = 2)
    {
        return strtr($fileName, array('{width}' => $width, '{height}' => $height, '{mode}' => $mode));
    }

    /**
     * 格式化商品信息
     * 
     * @param array $productData 需要格式化的商品数据
     * @return array | false
     */
    public static function formatProduct($productData, $showTags = true)
    {
        // 商品信息有问题,则不显示
        if (!isset($productData['product_skn'])) {
            return false;
        }

        // 市场价和售价一样,则不显示市场价
        if (intval($productData['market_price']) === intval($productData['sales_price'])) {
            $productData['market_price'] = false;
        }

        $result = array();
        $result['id'] = $productData['product_skn'];
        $result['product_id'] = $productData['product_id'];
        $result['thumb'] = self::getImageUrl($productData['default_images'], 235, 314);
        $result['name'] = $productData['product_name'];
        $result['price'] = $productData['market_price'];
        $result['salePrice'] = $productData['sales_price'];
        $result['isFew'] = ($productData['is_soon_sold_out'] === 'Y');
        $result['url'] = ''; // @todo
        
        if ($showTags) {
            $result['tags'] = array();
            $result['tags']['isNew'] = isset($productData['is_new']) && $productData['is_new'] === 'Y'; // 新品
            $result['tags']['isSale'] = isset($productData['is_discount']) && $productData['is_discount'] === 'Y'; // 在售
            $result['tags']['isLimit'] = isset($productData['is_limited']) && $productData['is_limited'] === 'Y'; // 限量
            $result['tags']['isYohood'] = isset($productData['is_yohood']) && $productData['is_yohood'] === 'Y'; // YOHOOD
            $result['tags']['midYear'] = isset($productData['mid-year']) && $productData['mid-year'] === 'Y'; // 年中
            $result['tags']['yearEnd'] = isset($productData['year-end']) && $productData['year-end'] === 'Y'; // 年末
            $result['tags']['isReNew'] = false; // 再到着
            $result['tags']['isNewFestival'] = false; // 新品节
        }

        return $result;
    }
    
    /**
     * 格式化资讯文章
     * 
     * @param array $articleData 需要格式化的资讯数据
     * @param bool $showTag 是否显示左上角标签
     * @param mixed $share 是否显示分享,在APP客户端里嵌入需要传url链接
     * @param bool $showAuthor 控制是否显示作者信息
     * @return array | false
     */
    public static function formatArticle($articleData, $showTag = true, $share = false, $showAuthor = true)
    {        
        // 资讯ID不存在,则不显示
        if (!isset($articleData['id'])) {
            return false;
        }
        
        $result = array();
        $result['id'] = $articleData['id'];
        $result['showTags'] = $showTag;
        $result['img'] = self::getImageUrl($articleData['src'], 640, 640);
        $result['url'] = $articleData['url']; // @todo
        $result['title'] = $articleData['title'];
        $result['text'] = $articleData['intro'];
        $result['publishTime'] = $articleData['publish_time'];
        $result['pageView'] = $articleData['views_num'];
        $result['like'] = array();
        $result['like']['count'] = $articleData['praise_num'];
        $result['like']['isLiked'] = isset($articleData['isPraise']) && $articleData['isPraise'] === 'Y';
        // 收藏
        // $result['collect'] = array();
        // $result['collect']['isCollected'] = isset($articleData['isFavor']) && $articleData['isFavor'] === 'Y';
        $result['share'] = $share;
        
        // 判断是否显示作者信息
        if ($showAuthor) {
            $result['author'] = $articleData['author'];
        }
        
        // 模板中需要的标签标识
        if ($showTag) {
            switch (strval($articleData['category_id'])) {
                case '1': // 话题
                    $result['isTopic'] = true;
                    break;
                case '2': // 搭配
                    $result['isCollocation'] = true;
                    break;
                case '3': // 潮人
                    $result['isFashionMan'] = true;
                    break;
                case '4': // 潮品
                    $result['isFashionGood'] = true;
                    break;
                case '5': // 小贴士
                    $result['isTip'] = true;
                    break;
            }
        }

        return $result;
    }

    /**
     * 生成公开的TOKEN凭证
     * 
     * @param string $string 字符串
     * @return string
     */
    public static function makeToken($string)
    {
        return md5(md5($string . '#@!@#'));
    }

    /**
     * 验证TOKEN凭证
     * 
     * @param string $string 字符串
     * @param string $token 公开访问TOKEN
     * @return bool
     */
    public static function verifyToken($string, $token)
    {
        if ($token === self::makeToken($string)) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 验证手机是否合法
     * 
     * @param int $mobile
     * @return boolean
     */
    public static function verifyMobile($mobile)
    {
        if (empty($mobile)) {
            return false;
        }
        return (bool) preg_match('/^1[3|4|5|8|7][0-9]{9}$/', trim($mobile));
    }

    /**
     * 验证密码是否合法
     * 
     * @param int $password
     * @return boolean
     */
    public static function verifyPassword($password)
    {
        if (empty($password)) {
            return false;
        }
        return (bool) preg_match('/^([a-zA-Z0-9\-\+_!@\#$%\^&\*\(\)\:\;\.=\[\]\\\',\?]){6,20}$/', trim($password));
    }

    /**
     * 验证邮箱是否合法
     * 
     * @param string $email
     * @return boolean
     */
    public static function verifyEmail($email)
    {
        if (empty($email)) {
            return false;
        }
        return !!filter_var($email, FILTER_VALIDATE_EMAIL);
    }

    /**
     * 验证国际手机号是否合法 
     * 
     * @param string $areaMobile
     * @return boolean
     */
    public static function verifyAreaMobile($areaMobile)
    {
        if (empty($areaMobile)) {
            return false;
        }
        if (!strpos($areaMobile, '-')) {
            return self::areaMobielVerify($areaMobile);
        } else {
            $mobileData = explode('-', $areaMobile);
            if (count($mobileData) != 2) {
                return false;
            }
        }
        return self::areaMobielVerify($mobileData[1], $mobileData[0]);
    }

    /**
     * 各国手机号规则
     */
    private static function areaMobielVerify($mobile, $area = 86)
    {
        $verify = array(
            86 => array(
                'name' => '中国',
                'match' => (bool) preg_match('/^1[3|4|5|8|7][0-9]{9}$/', trim($mobile)),
            ),
            852 => array(
                'name' => '中国香港',
                'match' => (bool) preg_match('/^[9|6|5][0-9]{7}$/', trim($mobile)),
            ),
            853 => array(
                'name' => '中国澳门',
                'match' => (bool) preg_match('/^[0-9]{8}$/', trim($mobile)),
            ),
            886 => array(
                'name' => '中国台湾',
                'match' => (bool) preg_match('/^[0-9]{10}$/', trim($mobile)),
            ),
            65 => array(
                'name' => '新加坡',
                'match' => (bool) preg_match('/^[9|8][0-9]{7}$/', trim($mobile)),
            ),
            60 => array(
                'name' => '马来西亚',
                'match' => (bool) preg_match('/^1[1|2|3|4|6|7|9][0-9]{8}$/', trim($mobile)),
            ),
            1 => array(
                'name' => '加拿大&美国',
                'match' => (bool) preg_match('/^[0-9]{10}$/', trim($mobile)),
            ),
            82 => array(
                'name' => '韩国',
                'match' => (bool) preg_match('/^01[0-9]{9}$/', trim($mobile)),
            ),
            44 => array(
                'name' => '英国',
                'match' => (bool) preg_match('/^7[7|8|9][0-9]{8}$/', trim($mobile)),
            ),
            81 => array(
                'name' => '日本',
                'match' => (bool) preg_match('/^0[9|8|7][0-9]{9}$/', trim($mobile)),
            ),
            61 => array(
                'name' => '澳大利亚',
                'match' => (bool) preg_match('/^[0-9]{11}$/', trim($mobile)),
            ),
        );
        if (isset($verify[$area])) {
            return $verify[$area]['match'];
        }
        return false;
    }

    /**
     * 获取商品的ICON
     * 
     * @param int $type
     * @return array
     */
    public static function getProductIcon($type) 
    {
        static $icons = array(
            1 => 'cloth',
            3 => 'pants',
            4 => 'dress',
            6 => 'shoe',
            7 => 'bag',
            10 => 'lamp',
            241 => 'headset',
            8 => 'watch',
            360 => 'swim-suit',
            308 => 'under'
        );
        $type = intval($type);
        
        return isset($icons[$type]) ? $icons[$type] : '';
    }
    
}