随机生成16位字符串
static public get_random_str ( integer $length = 16 ) : string | ||
$length | integer | |
리턴 | string | 生成的字符串 |
/** * JS-SDK 获取JS-SDK配置需要的信息 * * @param string $url 可选:调取JS-SDK的页面url,默认为HTTP_REFERER * @param string $type 可选:返回配置信息的格式 json & jsonp, 默认为对象数组 * @param string $$jsonp_callback 可选:使用json的callback名称 * * @return mixed * * Examples: * ``` * $api->get_jsapi_config(); * $api->get_jsapi_config('http://www.baidu.com/'); * ``` * Result: * ``` * { * errcode: 0, * appId: "wx733d7f24bd29224a", * timestamp: 1440073485, * nonceStr: "5Ars5fLaLuPEXSgm", * signature: "7f830aff99ff11fa931cae61b5b932b1f2c8ee10", * url: "http://www.baidu.com/" * } * ``` * * Examples: * ``` * $api->get_jsapi_config('', 'json'); * ``` * Result: * ``` * {"errcode":0,"appId":"wx733d7f24bd29224a","timestamp":1440073708,"nonceStr":"caFkkXnOhVrcq3Ke","signature":"1c6c08ddf6e0e3c0fd33aafcb160a9f67d6b8f94","url":null} * ``` * * Examples: * ``` * $api->get_jsapi_config('', 'jsonp'); * $api->get_jsapi_config('', 'jsonp', 'callback'); * ``` * Result: * ``` * ;jQuery17105012127514928579_1440073858610({"errcode":0,"appId":"wx733d7f24bd29224a","timestamp":1440073875,"nonceStr":"vsGBSM0MMiWeIJFQ","signature":"616005786e404fe0da226a6decc2730624bedbfc","url":null}) * ``` */ public function get_jsapi_config($url = '', $type = '', $jsonp_callback = 'callback') { $jsapi_ticket = $this->get_jsapi_ticket(); $nonce_str = SHA1::get_random_str(); $timestamp = time(); if ($url == '') { $url = $_SERVER['HTTP_REFERER']; } $signature = SHA1::get_jsapi_signature($jsapi_ticket, $nonce_str, $timestamp, $url); if ($signature === false) { $jsapi_config = array('errcode' => -1, 'errmsg' => 'get jsapi signature error.'); } else { $jsapi_config = array('errcode' => 0, 'appId' => $this->appId, 'timestamp' => $timestamp, 'nonceStr' => $nonce_str, 'signature' => $signature, 'url' => $url); } if ($type == 'json' || $type == 'jsonp') { $jsapi_config = json_encode($jsapi_config); if ($type == 'jsonp' && isset($_REQUEST[$jsonp_callback]) && !empty($_REQUEST[$jsonp_callback])) { $jsapi_config = ';' . $_REQUEST[$jsonp_callback] . '(' . $jsapi_config . ')'; } } return $jsapi_config; }
/** * 微信支付 - 获取jsapi支付的参数 * 用于直接填入js函数作为参数 * @param string $prepayId 预生成订单ID * @return string */ public function getWxPayJsApiParameters($prepayId) { // 获取jsapi支付的参数 $input = ['appId' => $this->appId, 'timeStamp' => (string) time(), 'nonceStr' => SHA1::get_random_str(32), 'package' => 'prepay_id=' . $prepayId, 'signType' => 'MD5']; // 签名 $input['paySign'] = SHA1::getSign2($input, 'key=' . $this->key); return json_encode($input); }