Helpers.php 6.54 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 = 1)
    {
        return strtr($fileName, array('{width}' => $width, '{height}' => $height, '{mode}' => $mode));
    }

    /**
     * 格式化商品信息
     * 
     * @param array $productData 需要格式化的商品数据
     * @return array | false
     */
    public static function formatProduct($productData)
    {
        // 商品信息有问题,则不显示
        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'] = Helpers::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
        $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;
    }

    /**
     * 生成公开的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;
    }

}