-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluJs.go
859 lines (746 loc) · 27.8 KB
/
pluJs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
// pluJs ,生成 Plumemo 前端文件,批量添加轮播图
// Powered By Luckykeeper <[email protected] | https://luckykeeper.site> 2022/11/19
// 考虑 memo 酱换成 momo 酱(MomoTalk)
package main
import (
"crypto/md5"
"database/sql"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/flopp/go-findfont"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/xuri/excelize/v2"
"gopkg.in/ini.v1"
)
// 显示中文
// 设置环境变量 通过go-findfont 寻找simkai.ttf 字体
func init() {
fontPaths := findfont.List()
for _, path := range fontPaths {
if strings.Contains(path, "simkai.ttf") {
fmt.Println(path)
os.Setenv("FYNE_FONT", path) // 设置环境变量 // 取消环境变量 os.Unsetenv("FYNE_FONT")
break
}
}
}
var (
Content *fyne.Container
ContentTitle *widget.Label
ContentTitleBox *fyne.Container
contentBox *fyne.Container
RunTime = 0
LayoutChanged = false
sql_initialize_table = `CREATE TABLE file (url TEXT PRIMARY KEY NOT NULL,fileName TEXT NOT NULL,type TEXT NOT NULL,md5 TEXT NOT NULL);`
)
func main() {
// 先初始化数据库
if exists, _ := PathExists("./pluJs.db"); exists {
log.Println("数据库存在")
} else {
log.Println("创建数据库!")
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
db.Exec(sql_initialize_table) //初始化
}
// App 基本信息
a := app.NewWithID("plujs.luckykeeper.site")
logo, _ := fyne.LoadResourceFromPath("plujs_icon.ico")
a.SetIcon(logo)
makeTray(a)
logLifecycle(a)
w := a.NewWindow("PluJs, A software to generate plumemo frontend JavaScript File | Powered by Luckykeeper | Build 20221119 | Ver 1.0.0")
w.SetMainMenu(makeMenu(a, w))
// 左侧菜单
menu := container.NewVBox(
widget.NewButtonWithIcon("Welcome PluJs!",
theme.HomeIcon(),
welcomeScreen),
widget.NewButtonWithIcon("给momo酱喂食!",
theme.DeleteIcon(),
addImage),
widget.NewButtonWithIcon("momo酱今天吃什么?",
theme.SearchReplaceIcon(),
database),
// 在这里添加换旧、删除功能【给momo酱换道菜】
widget.NewButtonWithIcon("连接momo酱到心智云图数据库!",
theme.StorageIcon(),
couldDatabase),
widget.NewButtonWithIcon("再见,momo酱!",
theme.VisibilityOffIcon(),
func() { fyne.App.Quit(a) }),
)
left := container.New(layout.NewHBoxLayout(), menu, widget.NewSeparator())
go func() {
for {
time.Sleep(time.Millisecond * 500)
if RunTime == 0 {
ContentTitle = widget.NewLabel("雷猴哇~<( ̄︶ ̄)↗[GO!]")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
addImageIcon := canvas.NewImageFromFile("./icon/top.jpg")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(290.75, 500))
Content = container.NewCenter(container.NewVBox(
widget.NewLabelWithStyle("↑Shirasu Azusa, A student from Blue Archive", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
widget.NewLabelWithStyle("Welcome to PluJs, A software to generate plumemo frontend JavaScript File", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
container.NewHBox(
widget.NewHyperlink("Powered By Luckykeeper", parseURL("https://luckykeeper.site/")),
widget.NewLabel("-"),
widget.NewHyperlink("Github", parseURL("https://github.com/luckykeeper/")),
widget.NewLabel("-"),
widget.NewHyperlink("Blog", parseURL("https://luckykeeper.site/")),
),
))
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
}
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
if RunTime == 0 || LayoutChanged {
contentBox.Refresh()
RunTime = 1
LayoutChanged = false
}
// 显示主界面,分别:适应宽度,左侧菜单,分割线,右侧内容
w.SetContent(container.New(layout.NewBorderLayout(nil, nil, left, nil), left, contentBox))
}
}()
// 设置窗口大小
w.Resize(fyne.NewSize(1280, 720))
w.SetFixedSize(true)
// 润!
w.ShowAndRun()
}
// 欢迎界面
func welcomeScreen() {
ContentTitle = widget.NewLabel("雷猴哇~<( ̄︶ ̄)↗[GO!]")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
addImageIcon := canvas.NewImageFromFile("./icon/top.jpg")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(290.75, 500))
Content = container.NewCenter(container.NewVBox(
widget.NewLabelWithStyle("↑Shirasu Azusa, A student from Blue Archive", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
widget.NewLabelWithStyle("Welcome to PluJs, A software to generate plumemo frontend JavaScript File", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
container.NewHBox(
widget.NewHyperlink("Powered By Luckykeeper", parseURL("https://luckykeeper.site/")),
widget.NewLabel("-"),
widget.NewHyperlink("Github", parseURL("https://github.com/luckykeeper/")),
widget.NewLabel("-"),
widget.NewHyperlink("Blog", parseURL("https://luckykeeper.site/")),
),
))
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
LayoutChanged = true
}
// 添加界面
func addImage() {
ContentTitle = widget.NewLabel("momo酱今天吃嘉然!ヾ(≧▽≦*)o")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
// 添加图片小技巧:container 套 container ,注意 layout.NewVBoxLayout (或者 HBox )的时候会按照组件最小大小来排列
// 下面带字的会自动计算最小大小,但是你加的图片不会,所以你需要手动给它一个大小,不然就会被压成一个 1x1 的像素点(乐)
addImageIcon := canvas.NewImageFromFile("./icon/eat.png")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(232.5, 350.8))
Content = container.NewCenter(
container.NewVBox(
widget.NewLabelWithStyle("momo酱今天吃点什么好呢?", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
widget.NewButton("momo酱今天恰_____",
addImageFromTable,
),
widget.NewLabelWithStyle("提示,在 addImage.xlsx 文件中添加外链图片链接", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})),
)
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
LayoutChanged = true
}
// 添加界面->添加
// 说明, addImage.xlsx 文件中图片分三类,一类是只在轮播图中使用的,一类是只在文章图使用的,一类是两边都使用的(共通图)
func addImageFromTable() {
addImageTable, _ := excelize.OpenFile("./addImage.xlsx")
if exists, _ := PathExists("./conflict.xlsx"); exists {
os.Remove("./conflict.xlsx")
}
f := excelize.NewFile()
f.SaveAs("./conflict.xlsx")
fileType := "banner"
checkAndWrite(addImageTable, fileType, "轮播图")
fileType = "article"
checkAndWrite(addImageTable, fileType, "文章图")
fileType = "all"
checkAndWrite(addImageTable, fileType, "共通图")
os.Remove("./addImage.xlsx")
copy("addImage_Template.xlsx", "./addImage.xlsx", 64)
// temp 文件夹一锅端
os.RemoveAll("./temp")
// insertSql := "insert into file (id, fileName,type) values ('" + id + "','" + upFileName + "','" + fileType + "');"
// log.Println("insertSql:", insertSql)
// db.Exec(insertSql)
}
// [DB 路径可写死]
// 检查并写入
func checkAndWrite(addImageTable *excelize.File, fileType, sheet string) {
for line := 2; line >= 2; line++ {
urlRaw1, _ := addImageTable.GetCellValue(sheet, "A"+strconv.Itoa(line))
if urlRaw1 == "" {
line = 0
} else if strings.Contains(urlRaw1, ": http") {
log.Println("Cloudreve!")
fileName := strings.Split(strings.Split(urlRaw1, ": ")[0], ".")[0]
url := strings.Split(urlRaw1, ": ")[1]
log.Println("fileName:", fileName)
log.Println("url:", url)
fileHash := getFileMd5(url, fileName)
log.Println("MD5:", fileHash)
// MD5 存在,导出冲突表
log.Println("_____________")
if DataExists(url, fileName, fileHash) {
exportConflict(url, fileName, fileHash)
} else { // MD5 不存在,写入数据库
writeToSqlite(url, fileName, fileType, fileHash)
}
} else {
log.Println("Direct Link!")
fileName, _ := addImageTable.GetCellValue(sheet, "B"+strconv.Itoa(line))
url := urlRaw1
log.Println("fileName:", fileName)
log.Println("url:", url)
fileHash := getFileMd5(url, fileName)
log.Println("MD5:", fileHash)
log.Println("_____________")
// MD5 存在,导出冲突表
if DataExists(url, fileName, fileHash) {
exportConflict(url, fileName, fileHash)
} else { // MD5 不存在,写入数据库
writeToSqlite(url, fileName, fileType, fileHash)
}
}
}
}
// 获取在线文件的 MD5 值,以便比较
func getFileMd5(url, name string) (hash string) {
if exists, _ := PathExists("./temp"); !exists {
os.Mkdir("./temp", os.ModePerm)
}
// 下载指定 URL 文件,准备计算 MD5
resp, err := http.Get(url)
if err != nil {
log.Println("respErr: ", err)
}
defer resp.Body.Close()
// 创建一个文件用于保存
saveTempFile, _ := os.Create("./temp/" + name)
defer saveTempFile.Close()
// 然后将响应流和文件流对接起来
io.Copy(saveTempFile, resp.Body)
f, _ := os.Open("./temp/" + name)
defer f.Close()
h := md5.New()
io.Copy(h, f)
hash = fmt.Sprintf("%x", h.Sum(nil))
return hash
}
// 判断 MD5 在文件中是否存在,存在输出同 MD5 信息(Excel)
func DataExists(urlNew, fileNameNew, fileHash string) bool {
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
querySql := "select url,filename from file where md5='" + fileHash + "';"
var data, data1 string
queryResult := db.QueryRow(querySql).Scan(&data, &data1)
if queryResult == sql.ErrNoRows {
return false
} else { // 数据库存在同 MD5 文件
return true
}
}
// 冲突文件导出
func exportConflict(urlNew, fileNameNew, fileHash string) {
f, _ := excelize.OpenFile("./conflict.xlsx")
f.NewSheet("冲突表")
// 设置单元格的值
f.SetCellValue("冲突表", "A1", "已存在文件URL")
f.SetCellValue("冲突表", "B1", "已存在文件名称")
f.SetCellValue("冲突表", "C1", "冲突文件URL")
f.SetCellValue("冲突表", "D1", "冲突文件名称")
f.SetColWidth("冲突表", "B", "B", 80)
f.SetColWidth("冲突表", "D", "D", 80)
f.SetColWidth("冲突表", "A", "A", 20)
f.SetColWidth("冲突表", "C", "C", 20)
// 数据库读数据
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
rows, _ := db.Query("select url,fileName from file where md5='" + fileHash + "';")
// 寻找最下一行
Line := 2
for Line >= 2 {
excelContent, _ := f.GetCellValue("冲突表", "A"+strconv.Itoa(Line))
if excelContent == "" {
break
} else {
Line++
}
}
for rows.Next() {
var urlExists string
var fileNameExists string
rows.Scan(&urlExists, &fileNameExists)
f.SetCellValue("冲突表", fmt.Sprintf("A%d", Line), urlExists)
f.SetCellValue("冲突表", fmt.Sprintf("B%d", Line), fileNameExists)
f.SetCellValue("冲突表", fmt.Sprintf("C%d", Line), urlNew)
f.SetCellValue("冲突表", fmt.Sprintf("D%d", Line), fileNameNew)
Line++
}
f.DeleteSheet("Sheet1")
f.SaveAs("./conflict.xlsx")
}
// 写入不冲突数据到数据库
func writeToSqlite(url, fileName, fileType, fileHash string) {
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
insertSql := "insert into file (url, fileName,type,md5) values ('" + url + "','" + fileName + "','" + fileType + "','" + fileHash + "');"
// log.Println("insertSql:", insertSql)
db.Exec(insertSql)
}
// 判断文件是否存在
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// 库存界面
func database() {
ContentTitle = widget.NewLabel("momo酱吃不下了啦!o(*≧▽≦)ツ┏━┓")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
addImageIcon := canvas.NewImageFromFile("./icon/change.jpg")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(265.5, 375.5))
Content = container.NewCenter(container.NewVBox(
widget.NewLabelWithStyle("momo酱吃不下了啦!", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
container.NewHBox(
widget.NewButtonWithIcon("给我恰!",
theme.DownloadIcon(),
generateFrontendMenu,
),
widget.NewButtonWithIcon("塞回去!",
theme.UploadIcon(),
updateFromExcel,
),
)))
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
LayoutChanged = true
}
// 导出前端文件及Excel
func generateFrontendMenu() {
ContentTitle = widget.NewLabel("呜呜呜o(一︿一+)o")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
addImageIcon := canvas.NewImageFromFile("./icon/change.jpg")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(265.5, 375.5))
Content = container.NewCenter(container.NewVBox(
widget.NewLabelWithStyle("呜呜呜o(一︿一+)o", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
widget.NewLabelWithStyle("已导出前端文件及Excel!", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
))
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
LayoutChanged = true
if exists, _ := PathExists("./fileList.xlsx"); exists {
os.Remove("./fileList.xlsx")
}
generateFrontend()
}
// 实行生成前端文件及Excel
func generateFrontend() {
if exists, _ := PathExists("./conflict.xlsx"); exists {
os.Remove("./conflict.xlsx")
} else if exists, _ := PathExists("./fileList.xlsx"); exists {
os.Remove("./fileList.xlsx")
}
// 生成前端文件
frontendBeforeRaw, _ := ioutil.ReadFile("frontendBefore.txt")
frontendBefore := string(frontendBeforeRaw)
frontendAfterRaw, _ := ioutil.ReadFile("frontendAfter.txt")
frontendAfter := string(frontendAfterRaw)
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
// // 查询需要写入的条数
// queryBannerCountSql := "SELECT COUNT(*) FROM (SELECT type FROM file WHERE type='banner');"
// queryArticleCountSql := "SELECT COUNT(*) FROM (SELECT type FROM file WHERE type='article');"
// queryAllCountSql := "SELECT COUNT(*) FROM (SELECT type FROM file WHERE type='all');"
// var queryBannerResult, queryArticleResult, queryAllCountResult int
// db.QueryRow(queryBannerCountSql).Scan(&queryBannerResult)
// db.QueryRow(queryArticleCountSql).Scan(&queryArticleResult)
// db.QueryRow(queryAllCountSql).Scan(&queryAllCountResult)
// bannerNumber := queryBannerResult + queryAllCountResult
// articleNumber := queryArticleResult + queryAllCountResult
// ListImg: 在前,文章随机图
listImgReturnBefore := "ListImg:["
listImgReturnAfter := "],"
ListImgReturnInside := ""
// bannerList: 在后,轮播随机图
bannerImgReturnBefore := "bannerList: ["
bannerImgReturnAfter := "]"
BannerImgReturnInside := ""
// 查询语句
queryAllUrlRows, _ := db.Query("SELECT url FROM file WHERE type='all';")
queryArticleUrlRows, _ := db.Query("SELECT url FROM file WHERE type='article';")
queryBannerUrlRows, _ := db.Query("SELECT url FROM file WHERE type='banner';")
// 生成 ListImg
for queryAllUrlRows.Next() {
var url string
queryAllUrlRows.Scan(&url)
ListImgReturnInside = ListImgReturnInside + "{img:\"" + url + "\"},"
BannerImgReturnInside = BannerImgReturnInside + "{img:\"" + url + "\"},"
}
for queryArticleUrlRows.Next() {
var url string
queryArticleUrlRows.Scan(&url)
ListImgReturnInside = ListImgReturnInside + "{img:\"" + url + "\"},"
}
listImgReturn := listImgReturnBefore + strings.TrimRight(ListImgReturnInside, ",") + listImgReturnAfter
// 生成 bannerList
for queryBannerUrlRows.Next() {
var url string
queryBannerUrlRows.Scan(&url)
BannerImgReturnInside = BannerImgReturnInside + "{img:\"" + url + "\"},"
}
bannerListReturn := bannerImgReturnBefore + strings.TrimRight(BannerImgReturnInside, ",") + bannerImgReturnAfter
// 随机图部分
randomImgList := listImgReturn + bannerListReturn
// 组织输出文本
frontendStrings := frontendBefore + randomImgList + frontendAfter
// 输出到 JavaScript 文件
frontendFile, _ := os.Create("./main.3a574d82.chunk.js")
frontendFile.WriteString(frontendStrings)
log.Println("前端 JS 文件输出完成!")
// 输出到 Excel 文件
exportExcel()
log.Println("Excel 文件输出完成")
}
// 导出到 Excel
func exportExcel() {
f := excelize.NewFile()
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
queryFileByTypeAndWriteToExcel(f, db, "banner", "轮播图")
queryFileByTypeAndWriteToExcel(f, db, "article", "文章图")
queryFileByTypeAndWriteToExcel(f, db, "all", "共通图")
f.DeleteSheet("Sheet1")
f.SaveAs("./fileList.xlsx")
}
// 根据文件类型查询数据
func queryFileByTypeAndWriteToExcel(f *excelize.File, db *sql.DB, fileType, sheetName string) {
f.NewSheet(sheetName)
// 设置单元格的值
f.SetCellValue(sheetName, "A1", "文件名")
f.SetCellValue(sheetName, "B1", "文件URL")
f.SetCellValue(sheetName, "C1", "状态(dd-删除;cg-更改)")
f.SetCellValue(sheetName, "D1", "新文件名")
f.SetCellValue(sheetName, "E1", "新URL")
f.SetColWidth(sheetName, "A", "E", 40)
// 数据库读数据
rows, _ := db.Query("SELECT url,fileName FROM file WHERE type = '" + fileType + "';")
line := 2
for rows.Next() {
var url string
var fileName string
rows.Scan(&url, &fileName)
f.SetCellValue(sheetName, fmt.Sprintf("A%d", line), fileName)
f.SetCellValue(sheetName, fmt.Sprintf("B%d", line), url)
line++
}
}
// 从输出的 Excel 文件更改内容
func updateFromExcel() {
ContentTitle = widget.NewLabel("呜呜呜o(一︿一+)o")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
addImageIcon := canvas.NewImageFromFile("./icon/change.jpg")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(265.5, 375.5))
Content = container.NewCenter(container.NewVBox(
widget.NewLabelWithStyle("呜呜呜o(一︿一+)o", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
widget.NewLabelWithStyle("已经塞回到数据库啦!", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
))
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
LayoutChanged = true
updateToSqlite("轮播图")
updateToSqlite("文章图")
updateToSqlite("共通图")
}
// 从表格导回数据库
func updateToSqlite(sheet string) {
updateTable, _ := excelize.OpenFile("./fileList.xlsx")
db, _ := sql.Open("sqlite3", "./pluJs.db")
defer db.Close()
for line := 2; line >= 2; line++ {
oldUrl, _ := updateTable.GetCellValue(sheet, "B"+strconv.Itoa(line))
status, _ := updateTable.GetCellValue(sheet, "C"+strconv.Itoa(line))
if oldUrl == "" {
line = 0
// 删除一条记录
} else if status == "dd" {
log.Println("Delete!")
deleteSql := "DELETE FROM file WHERE url='" + oldUrl + "';"
db.Exec(deleteSql)
log.Println("Delete complete!")
log.Println("_____________")
// 修改一条记录
} else if status == "cg" {
newfileName, _ := updateTable.GetCellValue(sheet, "D"+strconv.Itoa(line))
newUrl, _ := updateTable.GetCellValue(sheet, "E"+strconv.Itoa(line))
updateSql := "UPDATE file SET url='" + newUrl + "',fileName='" + newfileName + "' WHERE url='" + oldUrl + "';"
db.Exec(updateSql)
log.Println("Update complete!")
log.Println("_____________")
}
}
}
// 备份云端
func couldDatabase() {
ContentTitle = widget.NewLabel("心智云图数据库操作中心")
ContentTitleBox = container.New(layout.NewVBoxLayout(), ContentTitle, widget.NewSeparator())
addImageIcon := canvas.NewImageFromFile("./icon/sync.jpg")
addImageIcon.FillMode = canvas.ImageFillContain
addImageIcon.SetMinSize(fyne.NewSize(265.5, 375.5))
Content = container.NewCenter(container.NewVBox(
widget.NewLabelWithStyle("Sensei,请在config.ini文件配置心智云图数据库好相关参数,再使用云图数据库!", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
container.NewHBox(
widget.NewButtonWithIcon("上传momo酱的心智数据到心智云图数据库",
theme.UploadIcon(),
uploadToCloud,
),
widget.NewButtonWithIcon("从心智云图数据库下载momo酱的心智数据",
theme.DownloadIcon(),
downloadToLocal,
),
),
))
Content = container.New(layout.NewVBoxLayout(), addImageIcon, Content)
contentBox = container.New(layout.NewBorderLayout(ContentTitleBox, nil, nil, nil), ContentTitleBox, Content)
LayoutChanged = true
}
// 从云端恢复momo酱的心智数据
func downloadToLocal() {
dbAddress, dbPort, dbUsername, dbPassword, dbName := readIniConfig()
// 删除已经存在的数据(当然也不应该存在)
if exists, _ := PathExists("./pluJs.db"); exists {
os.Remove("./pluJs.db")
}
// 创建 sqlite 并初始化
sqliteDb, _ := sql.Open("sqlite3", "./pluJs.db")
sqliteDb.Exec(sql_initialize_table)
pgsqlDb, _ := sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbAddress, dbPort, dbUsername, dbPassword, dbName))
defer sqliteDb.Close()
defer pgsqlDb.Close()
rows, _ := pgsqlDb.Query("SELECT * FROM file;")
for rows.Next() {
var url string
var fileName string
var fileType string
var md5 string
rows.Scan(&url, &fileName, &fileType, &md5)
log.Println("同步完成:", fileName)
sqliteDb.Exec("insert into file (url, fileName,type,md5) values ('" + url + "','" + fileName + "','" + fileType + "','" + md5 + "');")
}
log.Println("心智云图恢复完成!")
}
// 上传momo酱的心智数据到云端
func uploadToCloud() {
dbAddress, dbPort, dbUsername, dbPassword, dbName := readIniConfig()
connectPgsqldb(dbAddress, dbPort, dbUsername, dbPassword, dbName)
sqliteDb, _ := sql.Open("sqlite3", "./pluJs.db")
pgsqlDb, _ := sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbAddress, dbPort, dbUsername, dbPassword, dbName))
defer sqliteDb.Close()
defer pgsqlDb.Close()
rows, _ := sqliteDb.Query("SELECT * FROM file;")
for rows.Next() {
var url string
var fileName string
var fileType string
var md5 string
rows.Scan(&url, &fileName, &fileType, &md5)
log.Println("同步完成:", fileName)
pgsqlDb.Exec("INSERT INTO file(url, fileName,type,md5) values ('" + url + "','" + fileName + "','" + fileType + "','" + md5 + "');")
}
log.Println("心智云图上传完成!")
}
// 数据库初始化(大象数据库)
func connectPgsqldb(dbAddress string, dbPort string, dbUsername string, dbPassword string, dbName string) {
// 数据库初始连接
db, _ := sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s sslmode=disable",
dbAddress, dbPort, dbUsername, dbPassword))
defer db.Close()
checkInitialize, _ := db.Query(fmt.Sprintf((`SELECT u.datname FROM pg_catalog.pg_database u where u.datname='%s';`),
dbName))
// 查询结果,存在 true ,不存在 false
checkInitializeResult := checkInitialize.Next()
// 不存在就创建,存在就先扬了云端数据库再创建
if !checkInitializeResult {
createPgsqlDatabase(db, dbAddress, dbPort, dbUsername, dbPassword, dbName)
} else {
db.Exec("DROP DATABASE IF EXISTS " + dbName + ";")
// 修复不能删除旧表的问题
dropFile, _ := sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbAddress, dbPort, dbUsername, dbPassword, dbName))
defer dropFile.Close()
dropFile.Exec("DROP TABLE IF EXISTS file;")
createPgsqlDatabase(db, dbAddress, dbPort, dbUsername, dbPassword, dbName)
}
}
// pgsql 建表
func createPgsqlDatabase(db *sql.DB, dbAddress string, dbPort string, dbUsername string, dbPassword string, dbName string) {
db.Exec(fmt.Sprintf((`CREATE DATABASE %s;`), dbName))
pluJsDb, _ := sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbAddress, dbPort, dbUsername, dbPassword, dbName))
defer pluJsDb.Close()
pluJsDb.Exec(`CREATE TABLE file(
url TEXT NOT NULL,
fileName TEXT NOT NULL,
type TEXT NOT NULL,
md5 TEXT NOT NULL
)WITH (OIDS=FALSE);`)
}
// 读取心智云图数据库配置
func readIniConfig() (dbAddress string, dbPort string, dbUsername string, dbPassword string, dbName string) {
cfg, err := ini.Load("config.ini")
if err != nil {
log.Printf("Fail to read file: %v", err)
os.Exit(1)
}
dbAddress = cfg.Section("cloudDatabase").Key("address").String()
dbPort = cfg.Section("cloudDatabase").Key("port").String()
dbUsername = cfg.Section("cloudDatabase").Key("username").String()
dbPassword = cfg.Section("cloudDatabase").Key("password").String()
dbName = cfg.Section("cloudDatabase").Key("dbName").String()
// 打印配置数据
// log.Println("心智云图数据库配置:")
// log.Println("dbAddress:", dbAddress)
// log.Println("dbPort:", dbPort)
// log.Println("dbUsername:", dbUsername)
// log.Println("dbPassword:", dbPassword)
// log.Println("dbName:", dbName)
return
}
// 生命周期日志
func logLifecycle(a fyne.App) {
a.Lifecycle().SetOnStarted(func() {
log.Println("Lifecycle: Started")
})
a.Lifecycle().SetOnStopped(func() {
log.Println("Lifecycle: Stopped")
})
a.Lifecycle().SetOnEnteredForeground(func() {
log.Println("Lifecycle: Entered Foreground")
})
a.Lifecycle().SetOnExitedForeground(func() {
log.Println("Lifecycle: Exited Foreground")
})
}
// 顶部菜单
func makeMenu(a fyne.App, w fyne.Window) *fyne.MainMenu {
aboutMenu := fyne.NewMenu("关于",
fyne.NewMenuItem("访问 Blog", func() {
u, _ := url.Parse("https://luckykeeper.site")
_ = a.OpenURL(u)
}),
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("访问 Github", func() {
u, _ := url.Parse("https://github.com/luckykeeper")
_ = a.OpenURL(u)
}),
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("访问 Plumemo 项目", func() {
u, _ := url.Parse("https://github.com/byteblogs168/plumemo")
_ = a.OpenURL(u)
}))
main := fyne.NewMainMenu(
aboutMenu,
)
return main
}
// 任务栏托盘
func makeTray(a fyne.App) {
if desk, ok := a.(desktop.App); ok {
h := fyne.NewMenuItem("PluJs By Luckykeeper", func() {})
menu := fyne.NewMenu("Hello World", h)
h.Action = func() {
log.Println("Hi there!")
h.Label = "PluJs By Luckykeeper"
u, _ := url.Parse("https://github.com/luckykeeper")
a.OpenURL(u)
menu.Refresh()
}
desk.SetSystemTrayMenu(menu)
}
}
func parseURL(urlStr string) *url.URL {
link, err := url.Parse(urlStr)
if err != nil {
fyne.LogError("Could not parse URL", err)
}
return link
}
// 复制文件
func copy(src, dst string, BUFFERSIZE int64) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}
if !sourceFileStat.Mode().IsRegular() {
log.Println("src is not a regular file.", src)
}
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
_, err = os.Stat(dst)
if err == nil {
log.Println("dst is not a regular file.", src)
}
destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()
if err != nil {
panic(err)
}
buf := make([]byte, BUFFERSIZE)
for {
n, err := source.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := destination.Write(buf[:n]); err != nil {
return err
}
}
return err
}