<?php /** * signature.php * 用于生成参数权限签名的小工具。 * 比如:JSSDK 需要用到的签名 signature 或 采用类似算法的签名计算 * @author Deepseath * @version $Id$ */ // 以下代码仅为完成功能而写,代码逻辑性可能不严谨,有洁癖的可以完善一下,跪拜先! $signature = ''; $json = ''; $string = ''; if (!empty($_POST)) { $params = []; $arrays = []; // print_r($_POST); $jsonParams = []; foreach ($_POST['key'] as $_key => $_val) { if (!$_val) { continue; } if ($_val == 'timestamp' && isset($_POST['timestamp'])) { $_POST['value'][$_key] = time(); } $params[] = [ 'key' => $_val, 'value' => $_POST['value'][$_key] ]; $arrays[$_val] = $_POST['value'][$_key]; $jsonParams[$_val] = $_POST['value'][$_key]; } if (isset($arrays['signature'])) { unset($arrays['signature']); } ksort($arrays); // 参数数组 $arr = []; foreach ($arrays as $_key => $_value) { $arr[] = strtolower($_key) . '=' . $_value; } $string = implode('&', $arr); // 根据请求的参数计算的签名值 $signature = sha1($string); $jsonParams['signature'] = $signature; $json = json_encode($jsonParams, JSON_UNESCAPED_UNICODE); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>权限签名(sha1)获取</title> <meta name="author" content="Deepseath@vchangyi.com"> <style type="text/css"> table { width: 100% } .result th { width: 280px; text-align: right } .key { width: 280px } .value { text-align: left } input { width: 99% } textarea { width: 99%; height: 70px } input.timestamp { width: auto !important } button, label { cursor: pointer } button { width: 280px } </style> </head> <body> <table class="result"> <tr> <th>字符串:</th> <td><input type="text" id="string" value="<?php echo htmlspecialchars($string);?>" /></td> </tr> <tr> <th>生成的签名:</th> <td><input type="text" id="signature" value="<?php echo $signature; ?>" /></td> </tr> <tr> <th>生成的请求参数(JSON):</th> <td><textarea id="params"><?php echo $json;?></textarea></td> </tr> </table> <form method="post"> <button type="submit">点击这里将下面的参数提交生成签名字符串</button> <label><input type="checkbox" class="timestamp" name="timestamp" value="1" <?php if (isset($_POST['timestamp'])) { ?> checked="checked" <?php }?> /> 强制将参数“timestamp”设置为当前时间戳(勾选后,timestamp 参数每次提交都是最新的时间戳)</label> <table class="params"> <thead> <tr> <th class="key">参数名</th> <th class="value">参数对应的值</th> </tr> </thead> <tbody> <?php if (empty($params)) { $params[] = [ 'key' => 'ticket', 'value' => '6cf450071cd51538fb850fe899caa6be' ]; $params[] = [ 'key' => 'timestamp', 'value' => time() ]; $params[] = [ 'key' => 'noncestr', 'value' => mt_rand(100000000, 999999999) ]; $params[] = [ 'key' => 'requester', 'value' => 'crm' ]; } for ($i = 0; $i < 30; $i ++) { ?> <tr> <td><input type="text" name="key[<?php echo $i;?>]" value="<?php echo isset($params[$i]) ? htmlspecialchars($params[$i]['key']) : '';?>" /></td> <td><input type="text" name="value[<?php echo $i; ?>]" value="<?php echo isset($params[$i]) ? htmlspecialchars($params[$i]['value']) : '';?>" /></td> </tr> <?php } ?> </table> </form> </body> </html>