近期公司项目有一个web端对文档编辑的需求偶然看到WPS开放平台做了一个简单的编辑demo
PHP端
public function detail($ids = null){
$data = [
'_w_appid' => '你的APPID',
];
$sing = $this->getSign($data);
$url = 'https://wwo.wps.cn/office/w/' . 文档id . '?_w_appid=' .'你的APPID'. '&_w_signature=' . $sing;
$this->assign('url', $url);
return $this->fetch();
}
function getSign($data){
ksort($data);
$stringA = '';
foreach ($data as $key => $item) {
$stringA .= $key . '=' . $item;
}
$stringSignTemp = $stringA . "_w_secretkey=" . '你的Key';
$stringSignTemp = hash_hmac('sha1', $stringSignTemp, '你的Key', true);
$sign = base64_encode($stringSignTemp);
return urlencode($sign);
}
public function info(){
$header = request()->header();
$id = $header['x-weboffice-file-id'];
$templates = db('你的表')->where(['id' => $id])->find();
$data = [
'file' => [
'id' => $id,
'name' => $templates['name'],
'version' => $templates['edition'],
'size' => $templates['size'],
'creator' => 'id0',
'create_time' => time($templates['creattime']),
'modifier' => "id1000",
'modify_time' => time(),
'download_url' => $templates['url'],
'preview_pages' => 100,
'user_acl' => [
'rename' => 0,
'history' => 0,
'copy'=> 1,
'export'=> 1,
'print' => 1,
],
'watermark' => [
'type' => 0,
'value' => "",
'fillstyle' => "rgba( 192, 192, 192, 0.6 )",
'font' => "bold 20px Serif",
'rotate' => -0.7853982,
'horizontal' => 50,
'vertical' => 100
]
],
'user' => [
'id' => $templates['admin_id'],
'name' => 'wps-'.$templates['admin_id'],
'permission' => "write",
'avatar_url' => ""
]
];
return json_encode($data);
}
public function save()
{
$menus=new Obs;
$header = request()->header();
$id = $header['x-weboffice-file-id'];
$templates = db('你的表')->where(['id' => $id])->find();
$url = $menus->uploads($_FILES);
$arr = array(
'name' => $_FILES['file']['name'],
'size' => $_FILES['file']['size'],
'url' => $url,
'edition' => $templates['edition']+1
);
db('你的表')->where(['id' => $id])->update($arr);
$data = [
'file' => [
'id' => $id,
'name' => $_FILES['file']['name'],
'version' => $templates['edition']+1,
'size' => round($_FILES['file']['size']/8),
'download_url' => $url
]
];
return json_encode($data);
}
前端
<script>
window.onload = function() {
const jssdk = WebOfficeSDK.config({
url: "<?php echo $url ?>",
});
console.log(jssdk.iframe);
jssdk.on('fileOpen', (data) => {
console.log(data);
});
};
</script>
效果图
|