* complete exportMiniProgram.
This commit is contained in:
+16
-1
@@ -489,14 +489,29 @@ class ai extends control
|
||||
$this->display();
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a mini program as zip file.
|
||||
*
|
||||
* @param string $appID
|
||||
* @return void
|
||||
*/
|
||||
public function exportMiniProgram($appID)
|
||||
{
|
||||
$php = $this->ai->createZtAppPhp($appID);
|
||||
$zip = $this->ai->createZtAppZip($php);
|
||||
|
||||
header('Content-Type: application/zip');
|
||||
header('Content-Transfer-Encoding: binary');
|
||||
header('Content-Disposition: attachment; filename="' . basename($zip) . '"');
|
||||
header('Content-Length: ' . filesize($zip));
|
||||
readfile($zip);
|
||||
unlink($zip);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function importMiniProgram($appID)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,3 +60,46 @@ function unpublishMiniProgram()
|
||||
{
|
||||
window.location.href = createLink('ai', 'unpublishMiniProgram', `appID=${miniProgramID}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export mini program data.
|
||||
* @param {Event} event
|
||||
*/
|
||||
function exportMiniProgram(event)
|
||||
{
|
||||
const $target = $(event.target);
|
||||
const $tr = $target.closest('tr');
|
||||
const id = $tr.find('.c-id').text();
|
||||
const name = $tr.find('.c-name').text();
|
||||
|
||||
$target.closest('button').attr('disabled', 'disabled');
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', createLink('ai', 'exportMiniProgram', `appID=${id}`, 'json'), true);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.setRequestHeader('Accept', 'application/zip');
|
||||
|
||||
xhr.onload = function()
|
||||
{
|
||||
if(xhr.status === 200)
|
||||
{
|
||||
const blob = xhr.response;
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const downloadLink = document.createElement('a');
|
||||
downloadLink.href = url;
|
||||
downloadLink.download = `${name}.ztapp.zip`;
|
||||
downloadLink.click();
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
$target.closest('button').removeAttr('disabled');
|
||||
};
|
||||
|
||||
xhr.onerror = function()
|
||||
{
|
||||
$target.closest('button').removeAttr('disabled');
|
||||
};
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
@@ -49,3 +49,42 @@ function unpublishMiniProgram()
|
||||
{
|
||||
window.location.href = createLink('ai', 'unpublishMiniProgram', `appID=${miniProgramID}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export mini program data.
|
||||
* @param {Event} event
|
||||
*/
|
||||
function exportMiniProgram(event)
|
||||
{
|
||||
const $target = $(event.target);
|
||||
$target.closest('button').attr('disabled', 'disabled');
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', createLink('ai', 'exportMiniProgram', `appID=${miniProgramID}`, 'json'), true);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.setRequestHeader('Accept', 'application/zip');
|
||||
|
||||
xhr.onload = function()
|
||||
{
|
||||
if(xhr.status === 200)
|
||||
{
|
||||
const blob = xhr.response;
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const downloadLink = document.createElement('a');
|
||||
downloadLink.href = url;
|
||||
downloadLink.download = `${miniProgramName}.ztapp.zip`;
|
||||
downloadLink.click();
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
$target.closest('button').removeAttr('disabled');
|
||||
};
|
||||
|
||||
xhr.onerror = function()
|
||||
{
|
||||
$target.closest('button').removeAttr('disabled');
|
||||
};
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
@@ -899,6 +899,59 @@ class aiModel extends model
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a mini program php data file.
|
||||
*
|
||||
* @param string $appID
|
||||
* @return string
|
||||
*/
|
||||
public function createZtAppPhp($appID)
|
||||
{
|
||||
$miniProgram = $this->getMiniProgramByID($appID);
|
||||
$fields = $this->getMiniProgramFields($appID);
|
||||
unset($miniProgram->id);
|
||||
unset($miniProgram->category);
|
||||
unset($miniProgram->createdBy);
|
||||
unset($miniProgram->createdDate);
|
||||
unset($miniProgram->editedBy);
|
||||
unset($miniProgram->editedDate);
|
||||
unset($miniProgram->publishedDate);
|
||||
$miniProgram->published = '0';
|
||||
$miniProgram->fields = array();
|
||||
|
||||
foreach($fields as $field)
|
||||
{
|
||||
unset($field->id);
|
||||
unset($field->appID);
|
||||
$miniProgram->fields[] = $field;
|
||||
}
|
||||
|
||||
$appJson = json_encode($miniProgram);
|
||||
$content = <<<APP
|
||||
<?php
|
||||
|
||||
\$ztApp = '$appJson';
|
||||
APP;
|
||||
$file = $this->app->getAppRoot() . "tmp/{$miniProgram->name}.ztapp.php";
|
||||
file_put_contents($file, $content);
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create mini program zip.
|
||||
*
|
||||
* @param string $file
|
||||
* @return string
|
||||
*/
|
||||
public function createZtAppZip($file)
|
||||
{
|
||||
$this->app->loadClass('pclzip', true);
|
||||
$zipPath = substr($file, 0, -3) . 'zip';
|
||||
$zip = new pclzip($zipPath);
|
||||
$zip->create($file, PCLZIP_OPT_REMOVE_ALL_PATH);
|
||||
return $zipPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save mini program fields.
|
||||
*
|
||||
|
||||
@@ -29,11 +29,16 @@ $finalList = array_merge($categoryList, $lang->ai->miniPrograms->allCategories);
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php if(common::hasPriv('ai', 'createMiniProgram')): ?>
|
||||
<div class="btn-toolbar pull-right">
|
||||
<div class="btn-toolbar pull-right">
|
||||
<?php if(common::hasPriv('ai', 'importMiniProgram')): ?>
|
||||
<button class="btn btn-primary">
|
||||
<?= $lang->ai->import; ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
<?php if(common::hasPriv('ai', 'createMiniProgram')): ?>
|
||||
<?= html::a($this->createLink('ai', 'createMiniProgram'), "<i class='icon icon-plus'></i> " . $lang->ai->miniPrograms->create, '', "class='btn btn-primary'"); ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="modal fade" id="disable-miniprogram">
|
||||
<div class="modal-dialog" style="width: 480px;">
|
||||
@@ -159,9 +164,9 @@ $finalList = array_merge($categoryList, $lang->ai->miniPrograms->allCategories);
|
||||
<i class="icon-ban-circle text-primary"></i>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
<?php if(common::hasPriv('ai', 'deleteMiniProgram')): ?>
|
||||
<button class="btn" onclick="openDeleteDialog(event)" title="<?= $lang->ai->prompts->action->delete; ?>"<?= $isPublished ? ' disabled' : ''; ?>>
|
||||
<i class="icon-trash text-primary"></i>
|
||||
<?php if(common::hasPriv('ai', 'exportMiniProgram')): ?>
|
||||
<button class="btn" onclick="exportMiniProgram(event)" title="<?= $lang->ai->export; ?>"<?= $isPublished ? '' : ' disabled'; ?>>
|
||||
<i class="icon-upload-file text-primary"></i>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
|
||||
@@ -11,7 +11,10 @@
|
||||
*/
|
||||
?>
|
||||
<?php include '../../common/view/header.html.php'; ?>
|
||||
<?php js::set('miniProgramID', $miniProgram->id); ?>
|
||||
<?php
|
||||
js::set('miniProgramID', $miniProgram->id);
|
||||
js::set('miniProgramName', $miniProgram->name);
|
||||
?>
|
||||
<div class="modal fade" id="disable-miniprogram">
|
||||
<div class="modal-dialog" style="width: 480px;">
|
||||
<div class="modal-content">
|
||||
@@ -73,11 +76,16 @@
|
||||
<?php if ($miniProgram->deleted) echo "<span class='label label-danger'>{$lang->ai->prompts->deleted}</span>"; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php if(common::hasPriv('ai', 'createMiniProgram')): ?>
|
||||
<div class="btn-toolbar pull-right">
|
||||
<div class="btn-toolbar pull-right">
|
||||
<?php if(common::hasPriv('ai', 'exportMiniProgram') && $miniProgram->published === '1'): ?>
|
||||
<button class="btn btn-primary" onclick="exportMiniProgram(event)" title="<?= $lang->ai->export; ?>">
|
||||
<?= $lang->ai->export; ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
<?php if(common::hasPriv('ai', 'createMiniProgram')): ?>
|
||||
<?= html::a($this->createLink('ai', 'createMiniProgram'), "<i class='icon icon-plus'></i> " . $lang->ai->miniPrograms->create, '', "class='btn btn-primary'"); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="mainContent" class="main-row">
|
||||
|
||||
Reference in New Issue
Block a user