1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace app\service\mobile_get;
- trait UnicomHelper {
- protected static $ZOP_KEY = [
- 'appCode' => 'appCode',
- 'HMACMD5' => 'HMACMD5',
- 'AES' => 'AES'
- ];
- /**
- * uuid 唯一序列码
- * @param string $prefix
- * @return string
- */
- function uuid($prefix = '')
- {
- $chars = md5(uniqid(mt_rand(), true));
- $uuid = substr($chars, 0, 8) . '-';
- $uuid .= substr($chars, 8, 4) . '-';
- $uuid .= substr($chars, 12, 4) . '-';
- $uuid .= substr($chars, 16, 4) . '-';
- $uuid .= substr($chars, 20, 12);
- return $prefix . $uuid;
- }
- //获取当前时间毫秒
- function msectime()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
- // 时间戳
- function getTimestamp()
- {
- $time = $this->msectime();
- return date('Y-m-d H:i:s.') . substr($time, -3);
- }
- // 生成签名
- function genSign($uuid, $timestamp)
- {
- $dataStr = 'appCode' .self::$ZOP_KEY['appCode'] . 'timestamp' . $timestamp . 'uuid' . $uuid . self::$ZOP_KEY['HMACMD5'];
- return $this->sign(self::$ZOP_KEY['HMACMD5'], $dataStr);
- }
- /**
- * 签名
- * @param string 密钥 联通侧提供的HmacMD5
- * @param string appCode+appCode值+timestamp+timestamp值*+uuid+uuid值+HmacMD5密钥值
- */
- function sign($key, $dataStr)
- {
- return base64_encode(hash_hmac("md5", $dataStr, base64_decode($key), true));
- }
- /**
- *AES加密
- */
- function encrypt($data, $key)
- {
- $data = openssl_encrypt($data, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
- return base64_encode($data);
- }
- function body($data=[]){
- // uuid
- $uuid = $this->uuid();
- // 生成时间戳
- $timestamp = $this->getTimestamp();
- $reqObj = [
- 'head' => [
- 'sign' => $this->genSign($uuid, $timestamp),
- 'timestamp' => $timestamp,
- 'uuid' => $uuid
- ],
- 'body' => $data
- ];
- return $reqObj;
- }
- }
|