diff --git a/demo/sync/font.php b/demo/sync/font.php index 882b1da..39e0379 100644 --- a/demo/sync/font.php +++ b/demo/sync/font.php @@ -18,7 +18,7 @@ * "status": 0 * "data": { * "fontName": "fonteditor", // 字体名称 - * "hasNew": 1, // 如果有新数据则标记为true, 同时设置fontType, timestamp, ttf字段 + * "hasNew": 1, // 如果有新数据则标记为1, 同时设置fontType, timestamp, ttf字段 * "timestamp": 12345678, // 新纪录时间戳,unix timestamp 精确到毫秒 * "fontType": "ttf", // 新纪录类型 * "ttf": base64str // 新纪录的base64字体数据 @@ -29,7 +29,7 @@ * GET参数: * action: 当前动作,默认为`push`推送数据 * POST参数: - * callbackUrl: post回调函数地址,通过302调转到回调地址,通知编辑器 + * callbackUrl: post回调函数地址,通过302跳转到回调地址,通知编辑器 * fontName: 字体名称 * fontType: 字体类型,多个类型用`,`隔开 * encode: 编码格式,默认`base64` @@ -55,7 +55,8 @@ * @author mengke01(kekee000@gmail.com) */ -define('SYNC_FILE', __DIR__ . '/list.md'); +define('SYNC_FILE', __DIR__ . '/list.md'); // 同步的文件 + /** @@ -78,21 +79,6 @@ function jsonp($status, $data = null, $statusInfo = null) { echo "$callback($json)"; } - -/** - * base64字符串转字节后写入到文件 - * - * @param string $base64Str base64字符串 - * @param string $outputfile 输出文件地址 - * @return 写入的字节数或者false - */ -function writeFile($base64Str, $outputfile) { - $ifp = fopen($outputfile, "wb"); - $ret = fwrite($ifp, base64_decode( $base64Str)); - fclose($ifp); - return $ret; -} - /** * 获取毫秒计数的unix 时间戳 * @@ -102,6 +88,33 @@ function getTimestamp() { return intval(microtime(true) * 1000); } +/** + * base64字符串转字节后写入到文件 + * + * @param string $base64Str base64字符串 + * @param string $file 输出文件地址 + * @return 写入的字节数或者false + */ +function writeBase64File($base64Str, $file) { + $fileHandle = fopen($file, "wb"); + $ret = fwrite($fileHandle, base64_decode($base64Str)); + fclose($fileHandle); + return $ret; +} + +/** + * 读取文件到base64字符串 + * + * @param string $file 输出文件地址 + * @return base64字符串 + */ +function readBase64File($file) { + $fileHandle = fopen($file, 'rb'); + $fontBuffer = fread($fileHandle, filesize($file)); + fclose($fileHandle); + return base64_encode($fontBuffer); +} + /** * 获取同步的记录 * @@ -111,7 +124,9 @@ function getSyncRecord() { if (file_exists(SYNC_FILE)) { $text = file_get_contents(SYNC_FILE); $json = json_decode($text, true); - return empty($json) ? array() : $json; + if(!empty($json)) { + return $json; + } } return array(); } @@ -119,8 +134,6 @@ function getSyncRecord() { /** * 保存同步的记录 - * - * @return array */ function saveSyncRecord($data) { file_put_contents(SYNC_FILE, json_encode($data)); @@ -141,7 +154,7 @@ function doPush() { foreach (explode(',', $fontType) as $type) { if (!empty($_POST[$type])) { - writeFile($_POST[$type], "${fontName}.${type}"); + writeBase64File($_POST[$type], "${fontName}.${type}"); $ret[] = $type; } } @@ -156,7 +169,7 @@ function doPush() { ); saveSyncRecord($recordList); - // 回调地址 + // 根据回调地址进行返回结果 if (!empty($_POST['callbackUrl'])) { $data = array( 'status' => 0, @@ -167,7 +180,6 @@ function doPush() { ) ); $url = $_POST['callbackUrl'] . '&data=' . urlencode(json_encode($data)); - echo $url; header('Location: ' . $url); } } @@ -201,10 +213,7 @@ function doPull() { 'timestamp' => $record['timestamp'], 'fontType' => $fontType, ); - $fileHandle = fopen($fontFile, 'r'); - $fontBuffer = fread($fileHandle, filesize($fontFile)); - fclose($fileHandle); - $data[$fontType] = base64_encode($fontBuffer); + $data[$fontType] = readBase64File($fontFile); jsonp(0, $data); return; } @@ -217,6 +226,7 @@ function doPull() { jsonp(0, $data); } + // 设置当前操作的帐号 if (empty($_COOKIE['FONT_USER'])) { $user = md5($_SERVER["REMOTE_ADDR"] . 'FONT_USER'); @@ -224,7 +234,6 @@ if (empty($_COOKIE['FONT_USER'])) { setcookie('FONT_USER', $user, time() + 315360000); // 10年不过期 } - // 入口 $action = $_GET['action']; if ($action == 'pull') { diff --git a/demo/sync/list.md b/demo/sync/list.md index 4325c1f..b9ba506 100644 --- a/demo/sync/list.md +++ b/demo/sync/list.md @@ -1 +1 @@ -{"cicons":{"user":"4ea8e455b2c68b92496d783039b08f2b","timestamp":1458308116871,"fontType":"ttf"}} \ No newline at end of file +{"cicons":{"user":"3e60a8b0712c80d976fb3e917c658d4f","timestamp":1458308845195,"fontType":"ttf"}} \ No newline at end of file diff --git a/src/fonteditor/controller/actions.js b/src/fonteditor/controller/actions.js index a49d9b2..fe3aea2 100644 --- a/src/fonteditor/controller/actions.js +++ b/src/fonteditor/controller/actions.js @@ -130,6 +130,7 @@ define( 'save': function () { if (program.ttfManager.get()) { + // 已经保存过的项目 var projectId = program.data.projectId; if (projectId) { program.project.update(projectId, program.ttfManager.get()) @@ -145,6 +146,7 @@ define( }); } + // 未保存的项目 else { var name = program.ttfManager.get().name.fontFamily || ''; if ((name = window.prompt(i18n.lang.msg_input_proj_name, name))) { diff --git a/src/fonteditor/widget/sync.js b/src/fonteditor/widget/sync.js index 119030b..a968bc3 100644 --- a/src/fonteditor/widget/sync.js +++ b/src/fonteditor/widget/sync.js @@ -81,7 +81,6 @@ define( setSyncStatus(syncConfig.url, 0x2); // 不支持拉取,但有可能支持推送,这里也处理为成功 if (reason.status === 200) { - resolver.resolve({}); } else { @@ -165,6 +164,13 @@ define( }); } + /** + * 更新推送时间戳,在下一次推送的时候发送时间戳 + * + * @param {string} projectId 项目编号 + * @param {number} timestamp 当前记录时间戳 + * @param {Object} syncConfig 同步配置 + */ function updateSyncTimestamp(projectId, timestamp, syncConfig) { if (!syncConfig) { syncConfig = project.getConfig(projectId).sync; @@ -196,7 +202,7 @@ define( var resolver = new Resolver(); checkSync(syncConfig).then(function (data) { - if (data.hasNew && confirm('字体`' + data.fontName + '`有新版本,是否同步新版本?')) { + if (data.hasNew && window.confirm('字体`' + data.fontName + '`有新版本,是否同步新版本?')) { if (data.fontType === DEFAULT_FONTTYPE && data[data.fontType]) { // 解析后台传送过来的ttf字形 var ttfBuffer = new Int8Array(base642bytes(data[data.fontType])).buffer; @@ -218,7 +224,7 @@ define( } return; } - + // 获取当前推送的ttf,如果没有,则从本地存储中获取 if (!ttf) { project.get(projectId).then(function (data) { doSync(syncConfig, data, resolver); @@ -237,7 +243,7 @@ define( }); var promise = resolver.promise(); - // 更新当前的时间戳 + // 更新当前的时间戳,这里包含两个来源,拉取新纪录的时间戳和推送成功后的时间戳 promise.then(function (data) { if (data.timestamp) { updateSyncTimestamp(projectId, data.timestamp, syncConfig);