-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
954 lines (942 loc) · 25.1 KB
/
settings.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
package official
import (
"errors"
"net/url"
"regexp"
"github.com/admpub/cache"
"github.com/admpub/license_gen/lib"
"github.com/admpub/log"
"github.com/coscms/sms/providers/aliyun"
"github.com/coscms/sms/providers/twilio"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/subdomains"
dbschemaNging "github.com/admpub/nging/v5/application/dbschema"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/nging/v5/application/library/config"
"github.com/admpub/nging/v5/application/library/license"
"github.com/admpub/nging/v5/application/registry/settings"
"github.com/admpub/nging/v5/application/registry/upload"
xOAuth "github.com/admpub/webx/application/handler/frontend/oauth"
xcache "github.com/admpub/webx/application/library/cache"
cfgIPFilter "github.com/admpub/webx/application/library/ipfilter"
modelApi "github.com/admpub/webx/application/model/official/api"
modelCustomer "github.com/admpub/webx/application/model/official/customer"
modelDBMgr "github.com/nging-plugins/dbmanager/application/model"
)
var configDefaults = map[string]map[string]*dbschemaNging.NgingConfig{
`base`: {
`userCenterLogo`: {
Key: `userCenterLogo`,
Label: `用户中心LOGO`,
Description: ``,
Value: ``,
Group: `base`,
Type: `image`,
Sort: 0,
Disabled: `N`,
},
`siteLogo`: {
Key: `siteLogo`,
Label: `站点LOGO(中)`,
Description: ``,
Value: ``,
Group: `base`,
Type: `image`,
Sort: 0,
Disabled: `N`,
},
`siteLogoLarge`: {
Key: `siteLogoLarge`,
Label: `站点LOGO(大)`,
Description: ``,
Value: ``,
Group: `base`,
Type: `image`,
Sort: 0,
Disabled: `N`,
},
`siteLogoSmall`: {
Key: `siteLogoSmall`,
Label: `站点LOGO(小)`,
Description: ``,
Value: ``,
Group: `base`,
Type: `image`,
Sort: 0,
Disabled: `N`,
},
`siteFavicon`: {
Key: `siteFavicon`,
Label: `收藏夹图标`,
Description: ``,
Value: ``,
Group: `base`,
Type: `image`,
Sort: 0,
Disabled: `N`,
},
`siteName`: {
Key: `siteName`,
Label: `站点名称`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 0,
Disabled: `N`,
},
`siteSlogan`: {
Key: `siteSlogan`,
Label: `站点口号`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 0,
Disabled: `N`,
},
`siteURL`: {
Key: `siteURL`,
Label: `站点网址`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 1,
Disabled: `N`,
},
`hostRule`: {
Key: `hostRule`,
Label: `允许指向本站的域名规则正则表达式`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 1,
Disabled: `N`,
},
`siteICPFiling`: {
Key: `siteICPFiling`,
Label: `ICP备案号`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 2,
Disabled: `N`,
},
`siteICPURL`: {
Key: `siteICPURL`,
Label: `ICP备案网址`,
Description: ``,
Value: `https://beian.miit.gov.cn/`,
Group: `base`,
Type: `text`,
Sort: 2,
Disabled: `N`,
},
`siteClose`: {
Key: `siteClose`,
Label: `关闭站点`,
Description: ``,
Value: `0`, // 0-开放网站;1-关闭网站;2-仅仅会员可以访问;3-仅仅管理员可以访问
Group: `base`,
Type: `text`,
Sort: 3,
Disabled: `N`,
},
`assetsCDN`: {
Key: `assetsCDN`,
Label: `网站素材CDN`,
Description: ``,
Value: `{"frontend":"","backend":""}`, // frontend-前台素材;backend-后台素材(前台引用时有效)
Group: `base`,
Type: `json`,
Sort: 3,
Disabled: `N`,
},
`showAnnouncement`: {
Key: `showAnnouncement`,
Label: `显示公告`,
Description: ``,
Value: `0`,
Group: `base`,
Type: `text`,
Sort: 4,
Disabled: `N`,
},
`siteAnnouncement`: {
Key: `siteAnnouncement`,
Label: `站点公告`,
Description: ``,
Value: ``,
Group: `base`,
Type: `html`,
Sort: 5,
Disabled: `N`,
},
`verifyCodeLifetime`: {
Key: `verifyCodeLifetime`,
Label: `信件验证码有效期`,
Description: `短信/邮件验证码的有效期(分钟)`,
Value: `5`,
Group: `base`,
Type: `text`,
Sort: 0,
Disabled: `N`,
},
`statCode`: {
Key: `statCode`,
Label: `统计代码`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`hashidSalt`: {
Key: `hashidSalt`,
Label: `HashID盐值`,
Description: ``,
Value: `nging`,
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`customerRegister`: {
Key: `customerRegister`,
Label: `前台用户注册`,
Description: ``,
Value: `open`, //close-关闭注册,open-开放注册,invitation-邀请注册
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`customerLogin`: {
Key: `customerLogin`,
Label: `前台用户登录`,
Description: ``,
Value: `open`, //close-关闭登录,open-开放登录
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`addIntegral`: {
Key: `addIntegral`,
Label: `增加消费积分`,
Description: ``,
Value: `{"login":0.0,"register":0.0}`,
Group: `base`,
Type: `json`,
Sort: 30,
Disabled: `N`,
},
`addExperience`: {
Key: `addExperience`,
Label: `添加经验`,
Description: ``,
Value: `{"login":0.0,"register":0.0}`,
Group: `base`,
Type: `json`,
Sort: 30,
Disabled: `N`,
},
`customerPermCacheTTL`: {
Key: `customerPermCacheTTL`,
Label: `前台用户权限缓存时长`,
Description: ``,
Value: `-1`, //-1:禁用缓存;-2:强制更新缓存;>=0:缓存时长(秒)
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`comment`: {
Key: `comment`,
Label: `评论`,
Description: ``,
Value: `open`, //close-关闭评论,open-开放评论无需审核,review-开放评论但需要审核
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`defaultHandler`: {
Key: `defaultHandler`,
Label: `首页路由`,
Description: ``,
Value: ``,
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`ipFilter`: {
Key: `ipFilter`,
Label: `IP过滤`,
Description: ``,
Value: `{"On":false,"PassToken":"","BlockByDefault":false,"TrustProxy":true,"AllowedCountries":[],"BlockedCountries":[],"AllowedIPs":[],"BlockedIPs":[]}`,
Group: `base`,
Type: `json`,
Sort: 30,
Disabled: `N`,
},
`recharge`: {
Key: `recharge`,
Label: `充值`,
Description: ``,
Value: `{"On":false,"MinAmount":100,"DefaultAmount":100}`,
Group: `base`,
Type: `json`,
Sort: 30,
Disabled: `N`,
},
`defaultCurrency`: {
Key: `defaultCurrency`,
Label: `充值`,
Description: ``,
Value: `CNY`,
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`jwtMaxLifetime`: {
Key: `jwtMaxLifetime`,
Label: `JWT有效期`,
Description: `JWT有效期(单位:秒)`,
Value: `0`,
Group: `base`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
},
`cache`: {
`default`: { // 主要缓存
Key: `default`,
Label: `主要缓存`,
Description: ``,
Value: `{"Adapter":"redis","AdapterConfig":"","Interval":300,"OccupyMode":false,"Section":""}`,
Group: `cache`,
Type: `json`,
Sort: 1000,
Disabled: `N`,
},
`fallback`: { // 备用缓存
Key: `fallback`,
Label: `备用缓存`,
Description: ``,
Value: `{"Adapter":"redis","AdapterConfig":"","Interval":300,"OccupyMode":false,"Section":""}`,
Group: `cache`,
Type: `json`,
Sort: 1000,
Disabled: `N`,
},
},
`contact`: {
`siteEmail`: {
Key: `siteEmail`,
Label: `投诉邮箱`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1000,
Disabled: `N`,
},
`siteMobile`: {
Key: `siteMobile`,
Label: `投诉电话`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1001,
Disabled: `N`,
},
`siteQQ`: {
Key: `siteQQ`,
Label: `投诉QQ`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1002,
Disabled: `N`,
},
`customerServiceEmail`: {
Key: `customerServiceEmail`,
Label: `客服邮箱`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1010,
Disabled: `N`,
},
`customerServiceMobile`: {
Key: `customerServiceMobile`,
Label: `客服电话`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1011,
Disabled: `N`,
},
`customerServiceFax`: {
Key: `customerServiceFax`,
Label: `传真号码`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1012,
Disabled: `N`,
},
`customerServiceQQ`: {
Key: `customerServiceQQ`,
Label: `客服QQ`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1013,
Disabled: `N`,
},
`customerServiceTime`: {
Key: `customerServiceTime`,
Label: `客服时间`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1014,
Disabled: `N`,
},
`customerServiceAddress`: {
Key: `customerServiceAddress`,
Label: `联系地址`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1015,
Disabled: `N`,
},
`recvMoneyMethod`: {
Key: `recvMoneyMethod`,
Label: `收款方式(银行名称)`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1050,
Disabled: `N`,
},
`recvMoneyBranch`: {
Key: `recvMoneyBranch`,
Label: `收款银行分行(收款方式为银行类时有效)`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1051,
Disabled: `N`,
},
`recvMoneyAccount`: {
Key: `recvMoneyAccount`,
Label: `收款账号`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1052,
Disabled: `N`,
},
`recvMoneyOwner`: {
Key: `recvMoneyOwner`,
Label: `收款人户名`,
Description: ``,
Value: ``,
Group: `contact`,
Type: `text`,
Sort: 1053,
Disabled: `N`,
},
},
`oauth`: {
`alipay`: {
Key: `alipay`,
Label: `支付宝登录`,
Description: ``,
Value: ``,
Group: `oauth`,
Type: `json`,
Sort: 2000,
Disabled: `Y`,
},
`wechat`: {
Key: `wechat`,
Label: `微信登录`,
Description: ``,
Value: ``,
Group: `oauth`,
Type: `json`,
Sort: 2001,
Disabled: `Y`,
},
`qq`: {
Key: `qq`,
Label: `QQ登录`,
Description: ``,
Value: ``,
Group: `oauth`,
Type: `json`,
Sort: 2002,
Disabled: `Y`,
},
`github`: {
Key: `github`,
Label: `Github登录`,
Description: ``,
Value: ``,
Group: `oauth`,
Type: `json`,
Sort: 2003,
Disabled: `Y`,
},
`microsoft`: {
Key: `microsoft`,
Label: `Microsoft登录`,
Description: ``,
Value: ``,
Group: `oauth`,
Type: `json`,
Sort: 2004,
Disabled: `Y`,
},
},
`thirdparty`: { // 第三方接口控制
`payment`: {
Key: `payment`,
Label: `收银台远程接口`,
Description: `支付功能是否使用收银台远程接口(不启用则使用本地配置的付款账号)`,
Value: `0`, // 接口账号ID
Group: `thirdparty`,
Type: `id`,
Sort: 3000,
Disabled: `N`,
},
`oauth`: {
Key: `oauth`,
Label: `社区登录使用远程接口`,
Description: `社区登录是否使用远程接口(不启用则使用本地配置的社区账号)`,
Value: `0`,
Group: `thirdparty`,
Type: `id`,
Sort: 3000,
Disabled: `N`,
},
`exchangeRate`: {
Key: `exchangeRate`,
Label: `汇率查询接口`,
Description: `汇率查询接口`,
Value: `{"provider":"","apiKey":""}`,
Group: `thirdparty`,
Type: `json`,
Sort: 3000,
Disabled: `N`,
},
},
`sms`: {
`aliyun`: {
Key: `aliyun`,
Label: `阿里云通信`,
Description: ``,
Value: ``,
Group: `sms`,
Type: `json`,
Sort: 2000,
Disabled: `Y`,
},
`twilio`: {
Key: `twilio`,
Label: `Twilio`,
Description: ``,
Value: ``,
Group: `sms`,
Type: `json`,
Sort: 2000,
Disabled: `Y`,
},
},
`frequency`: { //频率
`rateLimiter`: {
Key: `rateLimiter`,
Label: `限流`,
Description: ``,
Value: `{"On":false,"Max":0,"Duration":0,"Prefix":"","SkipInternalError":true,"RedisAddr":"","RedisPassword":"","RedisDB":0,"DBAccountID":0}`,
Group: `frequency`,
Type: `text`,
Sort: 30,
Disabled: `N`,
},
`mobile`: {
Key: `mobile`,
Label: `短信频率`,
Description: `短信的发送频率控制`,
Value: `{"maxPerDay":10,"interval":60}`,
Group: `frequency`,
Type: `json`,
Sort: 2000,
Disabled: `N`,
},
`email`: {
Key: `email`,
Label: `邮件频率`,
Description: `邮件的发送频率控制`,
Value: `{"maxPerDay":10,"interval":60}`,
Group: `frequency`,
Type: `json`,
Sort: 2000,
Disabled: `N`,
},
`message`: {
Key: `message`,
Label: `发送私信`,
Description: `私信发送频率控制`,
Value: `{"maxPerDay":100,"interval":60}`,
Group: `frequency`,
Type: `json`,
Sort: 2000,
Disabled: `N`,
},
`comment`: {
Key: `comment`,
Label: `发布评论`,
Description: `评论发布频率控制`,
Value: `{"maxPerDay":100,"maxPending":10}`,
Group: `frequency`,
Type: `json`,
Sort: 2000,
Disabled: `N`,
},
`article`: {
Key: `article`,
Label: `发布文章`,
Description: `文章发布频率控制`,
Value: `{"maxPerDay":100,"maxPending":10}`,
Group: `frequency`,
Type: `json`,
Sort: 2000,
Disabled: `N`,
},
},
`custom`: { //独立页面配置
`login`: {
Key: `login`,
Label: `登录页面`,
Description: `登录页面左侧区域内容`,
Value: ``,
Group: `custom`,
Type: `text`,
Sort: 2000,
Disabled: `N`,
},
},
`socketio`: {
`enabled`: {
Key: `enabled`,
Label: `socketio开关`,
Description: `控制socket.io服务是否开启`,
Value: ``,
Group: `socketio`,
Type: `text`,
Sort: 2000,
Disabled: `N`,
},
`token`: {
Key: `token`,
Label: `Token`,
Description: `socket.io服务令牌`,
Value: ``,
Group: `socketio`,
Type: `text`,
Sort: 2000,
Disabled: `N`,
},
},
}
func init() {
// 添加默认配置数据
settings.AddConfigs(configDefaults)
// 注册配置模板和逻辑
settings.AddTmpl(`base`, `official/settings/base`, settings.OptAddHookGet(func(ctx echo.Context) error {
var storerEngines []string
for name := range upload.StorerAll() {
storerEngines = append(storerEngines, name)
}
ctx.Set(`storerEngines`, storerEngines)
return nil
}))
settings.Register((&settings.SettingForm{
Short: `缓存配置`,
Label: `缓存配置`,
Group: `cache`,
Tmpl: []string{`official/settings/cache`},
}).AddHookGet(func(ctx echo.Context) error {
dbaM := modelDBMgr.NewDbAccount(ctx)
dbaM.ListByOffset(nil, nil, 0, -1, db.Cond{`engine`: `redis`})
ctx.Set(`dbAccounts`, dbaM.Objects())
ctx.Set(`cacheAdapters`, []string{`redis`, `file`})
ctx.SetFunc(`isDbAccount`, xcache.IsDbAccount)
return nil
}))
settings.Register((&settings.SettingForm{
Short: `短信服务`,
Label: `短信服务`,
Group: `sms`,
Tmpl: []string{`official/settings/sms`},
}).AddHookPost(func(_ echo.Context) error {
return xOAuth.UpdateSMSConfigs()
}))
settings.Register((&settings.SettingForm{
Short: `频率控制`,
Label: `频率控制`,
Group: `frequency`,
Tmpl: []string{`official/settings/frequency`},
}).AddHookGet(func(ctx echo.Context) error {
dbaM := modelDBMgr.NewDbAccount(ctx)
dbaM.ListByOffset(nil, nil, 0, -1, db.Cond{`engine`: `redis`})
ctx.Set(`dbAccounts`, dbaM.Objects())
return nil
}))
settings.Register((&settings.SettingForm{
Short: `联系方式`,
Label: `官方联系方式`,
Group: `contact`,
Tmpl: []string{`official/settings/contact`},
}))
settings.Register((&settings.SettingForm{
Short: `第三方接口`,
Label: `第三方接口设置`,
Group: `thirdparty`,
Tmpl: []string{`official/settings/thirdparty`},
}).AddHookGet(func(ctx echo.Context) error {
m := modelApi.NewAccount(ctx)
m.ListByOffset(nil, nil, 0, -1, db.Cond{`disabled`: `N`})
ctx.Set(`apiAccounts`, m.Objects())
return nil
}))
settings.Register((&settings.SettingForm{
Short: `第三方登录`,
Label: `第三方登录接口设置`,
Group: `oauth`,
Tmpl: []string{`official/settings/oauth`},
}).AddHookPost(func(_ echo.Context) error {
return xOAuth.UpdateAccount()
}))
settings.Register((&settings.SettingForm{
Short: `自定义页面`,
Label: `自定义页面设置`,
Group: `custom`,
Tmpl: []string{`official/settings/custom`},
}))
settings.Register((&settings.SettingForm{
Short: `SocketIO`,
Label: `SocketIO设置`,
Group: `socketio`,
Tmpl: []string{`official/settings/socketio`},
}))
// 注册配置值解码器(用于从数据库读出来之后的解码操作)
// 名称支持"group"或"group.key"两种格式(优先使用指定了"group.key"的解码器,否则使用指定了"group"的解码器),例如:
// settings.RegisterDecoder(`sms`,...)作为sms组的默认解码器
// settings.RegisterDecoder(`sms.twilio`,...)对sms组内key为twilio的配置有效
settings.RegisterDecoder(`base.ipFilter`, func(v *dbschemaNging.NgingConfig, r echo.H) error {
jsonData := cfgIPFilter.NewOptions()
if len(v.Value) > 0 {
com.JSONDecode(com.Str2bytes(v.Value), jsonData)
}
r[`ValueObject`] = jsonData
return nil
})
settings.RegisterDecoder(`base.recharge`, func(v *dbschemaNging.NgingConfig, r echo.H) error {
jsonData := modelCustomer.NewWalletSettings()
if len(v.Value) > 0 {
com.JSONDecode(com.Str2bytes(v.Value), jsonData)
}
r[`ValueObject`] = jsonData
return nil
})
settings.RegisterDecoder(`frequency.rateLimiter`, func(v *dbschemaNging.NgingConfig, r echo.H) error {
jsonData := cfgIPFilter.NewRateLimiterConfig()
if len(v.Value) > 0 {
com.JSONDecode(com.Str2bytes(v.Value), jsonData)
}
r[`ValueObject`] = jsonData
return nil
})
settings.RegisterDecoder(`cache`, func(v *dbschemaNging.NgingConfig, r echo.H) error {
jsonData := &cache.Options{}
if len(v.Value) > 0 {
com.JSONDecode(com.Str2bytes(v.Value), jsonData)
}
r[`ValueObject`] = jsonData
return nil
})
settings.RegisterDecoder(`sms`, func(v *dbschemaNging.NgingConfig, r echo.H) error {
var jsonData interface{}
switch v.Key {
case `twilio`:
jsonData = twilio.New()
case `aliyun`:
jsonData = aliyun.New()
default:
return errors.New(`The decoder is unsupported: ` + v.Key)
}
if len(v.Value) > 0 {
com.JSONDecode(com.Str2bytes(v.Value), jsonData)
}
r[`ValueObject`] = jsonData
return nil
})
license.OnSetLicense(func(data *lib.LicenseData) {
if !config.IsInstalled() {
return
}
if license.LicenseMode() != license.ModeDomain {
return
}
siteURL := config.Setting(`base`).String(`siteURL`)
if len(siteURL) == 0 {
return
}
u, err := url.Parse(siteURL)
if err != nil {
return
}
if license.EqDomain(u.Hostname(), data.Info.Domain) {
return
}
frontendURL := u.Scheme + `://` + data.Info.Domain
config.Setting(`base`).Set(`siteURL`, frontendURL)
subdomains.SetBaseURL(`frontend`, frontendURL)
if config.FromFile() == nil || !config.FromFile().ConnectedDB() {
return
}
v := dbschemaNging.NewNgingConfig(common.NewMockContext())
err = v.UpdateField(nil, `value`, frontendURL, db.And(
db.Cond{`key`: `siteURL`},
db.Cond{`group`: `base`},
))
if err != nil {
panic(err)
}
})
settings.RegisterDecoder(`base.siteURL`, func(v *dbschemaNging.NgingConfig, _ echo.H) error {
// 从数据库里读出来的时候
frontendURL := v.Value
err := license.CheckSiteURL(frontendURL, true)
if err == nil {
return nil
}
log.Error(err)
domain := license.FullDomain()
if len(domain) > 0 {
u, err := url.Parse(frontendURL)
if err != nil {
log.Errorf(`%s: %v`, frontendURL, err.Error())
return nil
}
frontendURL = u.Scheme + `://` + domain
v.Value = frontendURL
log.Warn(`reset siteURL: `, v.Value)
err = v.UpdateField(nil, `value`, v.Value, db.And(
db.Cond{`key`: v.Key},
db.Cond{`group`: v.Group},
))
if err != nil {
panic(err)
}
}
return nil
})
// 注册配置值编码器(用于客户端提交表单数据之后的编码操作,编码结果保存到数据库)
settings.RegisterEncoder(`base.ipFilter`, func(v *dbschemaNging.NgingConfig, r echo.H) ([]byte, error) {
cfg := cfgIPFilter.NewOptions().FromStore(r)
return com.JSONEncode(cfg)
})
settings.RegisterEncoder(`base.recharge`, func(v *dbschemaNging.NgingConfig, r echo.H) ([]byte, error) {
cfg := modelCustomer.NewWalletSettings().FromStore(r)
return com.JSONEncode(cfg)
})
settings.RegisterEncoder(`frequency.rateLimiter`, func(v *dbschemaNging.NgingConfig, r echo.H) ([]byte, error) {
cfg := cfgIPFilter.NewRateLimiterConfig().FromStore(r)
return com.JSONEncode(cfg)
})
settings.RegisterEncoder(`cache`, func(v *dbschemaNging.NgingConfig, r echo.H) ([]byte, error) {
cfg := &cache.Options{
Adapter: r.String(`Adapter`),
AdapterConfig: r.String(`AdapterConfig`),
Interval: r.Int(`Interval`),
OccupyMode: r.Bool(`OccupyMode`),
}
return com.JSONEncode(cfg)
})
settings.RegisterEncoder(`sms`, func(v *dbschemaNging.NgingConfig, r echo.H) ([]byte, error) {
switch v.Key {
case `twilio`:
cfg := twilio.New()
cfg.AccessKey = r.String(`accessKey`)
cfg.AccessSecret = r.String(`accessSecret`)
cfg.From = r.String(`from`)
cfg.CountryCode = r.String(`countryCode`)
return com.JSONEncode(cfg)
case `aliyun`:
cfg := aliyun.New()
cfg.RegionId = r.String(`regionId`, cfg.RegionId)
cfg.AccessKey = r.String(`accessKey`)
cfg.AccessSecret = r.String(`accessSecret`)
cfg.SignName = r.String(`signName`) //默认签名
cfg.TmplCode = r.String(`tmplCode`) //默认模板代码
return com.JSONEncode(cfg)
default:
return nil, errors.New(`The decoder is unsupported: ` + v.Key)
}
})
settings.RegisterEncoder(`base.siteURL`, func(v *dbschemaNging.NgingConfig, formDataMap echo.H) ([]byte, error) {
//写入数据库的时候
frontendURL := formDataMap.String(`value`)
err := license.CheckSiteURL(frontendURL, true)
return []byte(frontendURL), err
})
var updateFrontendURL = func(diff config.Diff) (err error) {
//设置内存变量的时候
frontendURL := diff.String()
subdomains.SetBaseURL(`frontend`, frontendURL)
return
}
config.OnKeySetSettings(`base.siteURL`, updateFrontendURL)
config.OnKeySetSettings(`base.hostRule`, func(diff config.Diff) error {
key := `frontend.hostRuleRegexp`
hostRule := diff.String()
if len(hostRule) == 0 {
echo.Delete(key)
return nil
}
re, err := regexp.Compile(hostRule)
if err != nil {
return err
}
echo.Set(key, re)
return nil
})
}