<?php
namespace App\Libs\Classes;


/**
 * 签名
 */
class WxSign{
	public $secret_key ;
	public function __construct()
	{
	    $this->secret_key = env("SECRET_KEY");
	}
	
	public function arr_to_url($array,$has_sign=false){
		ksort($array);
		reset($array);			
		$arg  = "";
		while (list ($name, $val) = each ($array)) {
			if($name == 'sign' && !$has_sign) continue;
			if(strpos($name, "_") === 0)
				continue;			
			if(is_array($val))
				$val =join(',',$val);
			if($val==="")
				continue;
			$arg.=$name."=". $val ."&";
		}
		$arg = substr($arg,0,strlen($arg)-1);
		
		return $arg;
	}

    /**
     * 普通对称校验签名
     */
    function get_sign($params) {
    	$url = $this->arr_to_url($params,false);
    	$url = $url.'&key='.$this->secret_key;
    	v('get_zw_notify_sign_url:'.$url);
    	$sign = md5($url);
    	return $sign;
    }
    
    
    /**
     * 得到签名数组
     * return 追加timestamp，sign
     *
     */
    public function get_sign_arr($data,$secret_key){
    	if(!empty($secret_key)) $this->secret_key = $secret_key;
    	$data['timestamp'] = time();
    	$sign = $this->get_sign($data);
    	$data['sign'] = $sign;
    	return $data;
    }
   
}

?>
