<?php


namespace App\Http\helper;


class ApiSign
{
        static $serectKey = 'd03cb659cbf9192dcd066272249f8412';

    // 签名
    public static function sign($data)
    {
        self::$serectKey=env('API_SIGN',self::$serectKey);
        foreach ($data as $key => &$value) {
            if (empty($value) && $value !== '0') {
                unset($data[$key]);
            }
        }
        unset($value);

        if (!is_array($data)) {
            return false;
        }

        ksort($data);

        $sign_str = '';
        foreach ($data as $k => $v) {
            $sign_str .= $k . '=' . $v . '&';
        }
        $sign_str .= 'key=' . self::$serectKey;
        $sign = md5($sign_str);
        return $sign;
    }

    public static function newSign($data)
    {
        self::$serectKey=env('API_SIGN',self::$serectKey);
        foreach ($data as $key => &$value) {
            if ($key == 'userName' || $key == 'userEmail' || $key == 'userPhone' || $key == 'rand_str') {
                unset($data[$key]);
            }
            if (empty($value) && $value !== '0') {
                unset($data[$key]);
            }
        }
        unset($value);

        if (!is_array($data)) {
            return false;
        }

        ksort($data);

        $sign_str = '';
        foreach ($data as $k => $v) {
            $sign_str .= $k . '=' . $v . '&';
        }
        $sign_str .= 'key=' . self::$serectKey;
        $sign = md5($sign_str);
        return $sign;
    }
}
