手把手教你用Go开发客户端软件(使用Go + HTML)
<html resizeable>
<head>
<style src="home://views/" />
<meta charSet="utf-8" />
</head>
<body>
<div class="layout">
<div class="aside">
<h1 class="soft-title"><a href="home://views/">谷歌<br/>推送助手</a></h1>
<div class="aside-menus">
<a href="home://views/" class="menu-item active">推送任务</a>
<a href="home://views/" class="menu-item">使用教程</a>
</div>
</div>
<div class="container">
<div class="task-head">
<button #newTask>新建任务</button>
</div>
<table class="task-list" #taskList>
<colgroup>
<col width="30%">
<col width="15%">
<col width="15%">
<col width="15%">
<col width="30%">
</colgroup>
<thead>
<tr>
<th>站点域名</th>
<th>URL数量</th>
<th>已推送/每日推送</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">加载中</td>
</tr>
</tbody>
</table>
</div>
<form class="control-form" #taslForm>
<div class="form-header">
<a class="form-close" #resultClose>关闭</a>
<h3>创建/编辑任务</h3>
</div>
<div class="form-content">
<div class="form-item">
<div class="form-label">网址或Sitemap地址:</div>
<div class="input-block">
<input(domain) class="layui-input" type="text" placeholder="http://或https://开头的网站地址或Sitemap地址" />
<div class="text-muted">说明:如果填写了Sitemap地址,将自动获取Sitemap中的所有URL推送,<br/>否则将抓取推送网址下的所有链接。</div>
</div>
</div>
<div class="form-item">
<div class="form-label">选择AccountJson:</div>
<div class="input-block text-left">
<div>
<button #selectAccountJson>选择.json文件</button>
<span #accountJson></span>
</div>
<div class="text-muted">说明:需要上传谷歌账号的json文件,用于授权。</div>
</div>
</div>
<div class="form-item">
<div class="form-label">每天推送数量:</div>
<div class="input-block">
<input(daily_num) class="layui-input" type="text" placeholder="默认200" />
<div class="text-muted">说明:请根据你的接口限制,填写每天推送的量。</div>
</div>
</div>
<div>
<button type="default" #formClose>返回</button>
<button type="default" #taskSubmit>提交</button>
</div>
</div>
</form>
<div class="result-list" #resultList>
<div class="form-header">
<a class="form-close" #resultClose>关闭</a>
<h3>查看结果</h3>
</div>
<div class="form-content">
<table>
<colgroup>
<col width="40%">
<col width="60%">
</colgroup>
<tbody>
<tr>
<td>网站网站</td>
<td #resultDomain></td>
</tr>
<tr>
<td>每日推送数量</td>
<td #resultDailyNum>0条</td>
</tr>
<tr>
<td>执行状态</td>
<td #resultStatus>waiting</td>
</tr>
<tr>
<td>已发现URL</td>
<td #resultUrlCount>0条</td>
</tr>
<tr>
<td>已推送</td>
<td #resultDailyFinished>0条</td>
</tr>
<tr>
<td>推送结果</td>
<td class="text-left" #resultResult>
/* <div><span></span><span>失败</span></div> */
</td>
</tr>
<tr>
<td></td>
<td>
<div>
<span class="pate-item">页码:<span #resultPage>1</span>/<span #resultTotalPage>1</span></span>
<button #resultPrev>上一页</button>
<button #resultNext>下一页</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
<script type="text/tiscript">
function syncTasks() {
let res = view.getIndexingTasks()
let result = JSON.parse(res)
// 重置 #taskList
let tb = $(#taskList>tbody)
tb.html = ""
if (!result) {
return;
}
for (let i = 0; i < result.length; i++) {
let task = result[i];
let tr = new Element(#tr)
tr.append(new Element(#td, task.domain))
tr.append(new Element(#td, task.url_count + ""))
tr.append(new Element(#td, task.daily_finished + "/" + task.daily_num))
tr.append(new Element(#td, task.status + ""))
let td = new Element(#td)
td.@#class = "control-btns"
td.attributes["id"] = "task-" + task.id
addControlBtn(td, "结果", "task-result")
if (task.status == "running") {
addControlBtn(td, "停止", "task-stop")
} else {
addControlBtn(td, "启动", "task-start")
}
if (task.status != "running") {
addControlBtn(td, "编辑", "task-edit")
addControlBtn(td, "删除", "task-delete")
}
tr.append(td)
tb.append(tr)
}
}
function addControlBtn(el, str, cls) {
let bt = new Element(#button, str)
bt.@#class = cls
el.append(bt)
}
self.on("click",".task-start", function() {
let id = this.$p(td).attributes['id'].replace("task-", "")
let result = view.startGoogleIndexing(id)
//(#alert, result || "启动成功");
});
self.on("click",".task-stop", function() {
let id = this.$p(td).attributes['id'].replace("task-", "")
let result = view.stopGoogleIndexing(id)
//(#alert, result || "停止成功");
});
self.on("click",".task-edit", function() {
let id = this.$p(td).attributes['id'].replace("task-", "")
showEditWindow(id)
});
self.on("click",".task-result", function() {
let id = this.$p(td).attributes['id'].replace("task-", "")
stdout.println(this.$p(td).attributes['id'])
showResultWindow(id, 1)
});
self.on("click",".task-delete", function() {
let id = this.$p(td).attributes['id'].replace("task-", "")
let result = view.deleteGoogleIndexing(id)
//(#alert, result || "删除成功");
});
// 新建任务
event click $(#newTask){
showEditWindow("-1")
}
function showEditWindow(id) {
let res = view.getIndexingTask(id);
let result = JSON.parse(res) || {};
// 回填表单
$(#taslForm).value=result;
$(#taslForm).@.addClass("active");
}
// 表单
let accountPath = '';
event click $(#selectAccountJson){
let filePath = view.openAccountJson()
self#accountJson.text = filePath
accountPath = filePath;
}
event click $(#formClose){
$(#taslForm).@.removeClass("active");
}
event click $(#taskSubmit){
// 第一步,先保存授权信息
// 第二步,抓取Sitemap
// 第三步,开始推送
let result = view.createGoogleIndexing(accountPath, $(#taslForm).value.domain, $(#taslForm).value.daily_num)
stdout.println(result)
view.msgbox(#alert, result || "保存成功");
if (!result) {
$(#taslForm).@.removeClass("active");
}
// 同步结果
syncTasks();
}
let curId = 0;
let curPage = 1;
let totalPage = 1;
function showResultWindow(id, curp) {
curId = id;
let res = view.getIndexingTask(curId);
let result = JSON.parse(res) || {};
$(#resultList).@.addClass("active");
$(#resultDomain).text = result.domain;
$(#resultDailyNum).text = result.daily_num + "条";
$(#resultStatus).text = result.status;
$(#resultUrlCount).text = result.url_count + "条";
$(#resultDailyFinished).text = "累计:" + result.total_finished + "条" + " / 今日:" + result.daily_finished + "条" + (result.daily_finished >= result.daily_num ? ' / 今日已完成' : '');
let res2 = view.getIndexingUrls(curId, curp)
let result2 = JSON.parse(res2) || {};
$(#resultPage).text = result2.page + "";
$(#resultTotalPage).text = result2.totalPage + "";
curPage = result2.page
totalPage = result2.totalPage
$(#resultResult).html = '';
for (let val in result2.urls) {
$(#resultResult).append("<div class='urls-item'><span class='item-url'>" + val.url + "</span> <span class='item-status' title='"+(val.msg || (val.status == 0 ? '未开始' :''))+"'>" + (val.status == 0 ? '-' : val.status != 200 ? "<span class='status-error'>"+val.status+"</span>" : val.status)+"</span></div>")
}
}
event click $(#resultPrev) {
if(curPage <= 1) {
curPage = 1;
return;
}
curPage = curPage - 1;
showResultWindow(curId, curPage);
}
event click $(#resultNext) {
if(curPage >= totalPage) {
curPage = totalPage;
return;
}
curPage = curPage + 1;
showResultWindow(curId, curPage);
}
event click $(.item-status) {
let title = this.attributes['title'];
if (title) {
view.msgbox(#error, title);
}
}
event click $(#resultClose){
$(#resultList).@.removeClass("active");
$(#taslForm).@.removeClass("active");
}
// 进来的时候先执行一遍
syncTasks();
// 加载tasklist,2秒钟刷新一次
self.timer(2000ms, function() {
syncTasks();
return true;
});
</script>