话不多说,上代码。
session_key在微信用户登录的时候应该都自动保存了,这里不做session_key的获取演示了
$data = [
'openid'=>$openid,//当前观看用户的openid
'src_appid'=>env('weixin.appid'),//提审方小程序的appid
'drama_id'=>strval($drama_id]),//提审的剧目id
'serial_list'=>[
['start_serial_no'=>1,//剧集编号表示起始剧集编号,从1开始
'end_serial_no'=>4,//结束剧集编号包含此集
'status'=>1 //上面的起始和结束区间内剧集是否可播放。// 1-可播放,2-不可播放
],
['start_serial_no'=>5
'end_serial_no'=>30,
'status'=>2
],
];,
'data_expire_at'=>time()+7200//数据有效期,时间戳(秒)。到时间过期不可用,建议当前时间戳+2小时
];
$dataJson = json_encode($data,JSON_UNESCAPED_UNICODE);
$key = mb_substr($userInfo['session_key'],0,16,'UTF-8');
return $this->encryptionAes($key,$dataJson);
private function encryptionAes($key,$data){
$aes = new AES($key);
return $aes->encrypt($data);
}
Aes加密类
class Aes
{
private $key;
private $iv;
public function __construct($key, $iv="") {
$this->key = $key;
$this->iv = $iv;
}
public function encrypt($data) {
$encrypted = openssl_encrypt($data, 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
return base64_encode($encrypted);
}
public function decrypt($encryptedData) {
$decrypted = openssl_decrypt(base64_decode($encryptedData), 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
return $decrypted;
}
public function encryptCBC($data) {
$encrypted = openssl_encrypt($data, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
return base64_encode($encrypted);
}
public function decryptCBC($encryptedData) {
$decrypted = openssl_decrypt(base64_decode($encryptedData), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
return $decrypted;
}
}