-
Notifications
You must be signed in to change notification settings - Fork 264
/
foudation_service.go
executable file
·409 lines (375 loc) · 12.9 KB
/
foudation_service.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
package impl
/**
* Copyright 2015 @ 56x.net.
* name : platform_service
* author : jarryliu
* date : 2020-09-05 15:30
* description :
* history :
*/
import (
"context"
de "github.com/ixre/go2o/core/domain/interface/domain"
"github.com/ixre/go2o/core/domain/interface/message/notify"
"github.com/ixre/go2o/core/domain/interface/registry"
"github.com/ixre/go2o/core/domain/interface/valueobject"
"github.com/ixre/go2o/core/infrastructure/domain"
"github.com/ixre/go2o/core/infrastructure/format"
"github.com/ixre/go2o/core/infrastructure/tool/sensitive"
"github.com/ixre/go2o/core/module"
"github.com/ixre/go2o/core/module/bank"
"github.com/ixre/go2o/core/service/proto"
"github.com/ixre/gof/storage"
)
var _ proto.FoundationServiceServer = new(foundationService)
// 基础服务
type foundationService struct {
_rep valueobject.IValueRepo
registryRepo registry.IRegistryRepo
notifyRepo notify.INotifyRepo
_s storage.Interface
serviceUtil
proto.UnimplementedFoundationServiceServer
}
func NewFoundationService(rep valueobject.IValueRepo, registryRepo registry.IRegistryRepo, s storage.Interface, notifyRepo notify.INotifyRepo) *foundationService {
return &foundationService{
_rep: rep,
_s: s,
notifyRepo: notifyRepo,
registryRepo: registryRepo,
}
}
// 检测是否包含敏感词
func (s *foundationService) CheckSensitive(_ context.Context, r *proto.String) (*proto.Bool, error) {
_, b := sensitive.Singleton().CheckSensitive(r.Value)
return &proto.Bool{Value: b}, nil
}
// 替换敏感词
func (s *foundationService) ReplaceSensitive(_ context.Context, r *proto.ReplaceSensitiveRequest) (*proto.String, error) {
v := sensitive.Singleton().ReplaceAll(r.Text, r.Replacement)
return &proto.String{Value: v}, nil
}
func (s *foundationService) CleanCache(_ context.Context, request *proto.CleanCacheRequest) (*proto.CleanCacheResponse, error) {
var count = 0
if len(request.Prefix) > 0 {
count, _ = s._s.DeleteWith(request.Prefix)
} else if len(request.Key) > 0 {
s._s.Delete(request.Key)
count = 1
}
return &proto.CleanCacheResponse{
Count: int32(count),
}, nil
}
// 保存短信API凭据
func (s *foundationService) SaveSmsSetting(_ context.Context, r *proto.SSmsProviderSetting) (*proto.Result, error) {
manager := s.notifyRepo.Manager()
if r.HttpExtra == nil {
r.HttpExtra = &proto.SSmsExtraSetting{}
}
perm := ¬ify.SmsApiPerm{
Provider: int(r.Provider),
Key: r.Key,
Secret: r.Secret,
Signature: r.Signature,
TemplateId: r.TemplateId,
Extra: ¬ify.SmsExtraSetting{
ApiUrl: r.HttpExtra.ApiUrl,
Params: r.HttpExtra.Params,
Method: r.HttpExtra.Method,
Charset: r.HttpExtra.Charset,
SuccessChars: r.HttpExtra.SuccessChars,
},
}
if err := manager.SaveSmsApiPerm(perm); err != nil {
return s.error(err), nil
}
return s.success(nil), nil
}
// 获取短信API凭据, @provider 短信服务商, 默认:http
func (s *foundationService) GetSmsSetting(_ context.Context, req *proto.GetSmsSettingRequest) (*proto.SSmsProviderSetting, error) {
manager := s.notifyRepo.Manager()
perm := manager.GetSmsApiPerm(int(req.Provider))
if perm == nil {
return nil, notify.ErrNoSuchSmsProvider
}
return &proto.SSmsProviderSetting{
Provider: proto.ESmsProvider(perm.Provider),
Key: perm.Key,
Secret: perm.Secret,
Signature: perm.Signature,
HttpExtra: &proto.SSmsExtraSetting{
ApiUrl: perm.Extra.ApiUrl,
Params: perm.Extra.Params,
Method: perm.Extra.Method,
Charset: perm.Extra.Charset,
SuccessChars: perm.Extra.SuccessChars,
},
}, nil
}
// 保存面板HOOK数据,这通常是在第三方应用中初始化或调用,参见文档:BoardHooks
func (s *foundationService) SaveBoardHook(_ context.Context, request *proto.BoardHookSaveRequest) (*proto.Result, error) {
mp := map[string]string{
registry.BoardHookURL: request.HookURL,
registry.BoardHookToken: request.Token,
}
for k, v := range mp {
if ir := s.registryRepo.Get(k); ir != nil {
err := ir.Update(v)
if err == nil {
err = ir.Save()
}
if err != nil {
return s.error(err), nil
}
}
}
return s.success(nil), nil
}
//
//// 删除值
//func (_s *foundationService) DeleteValue(_ context.Context, s2 *proto.String) (*proto.Result, error) {
// err := _s._rep.DeleteValue(s2.Value)
// return _s.result(err), nil
//}
//
//// 根据前缀获取值
//func (_s *foundationService) GetValuesByPrefix(_ context.Context, s2 *proto.String) (*proto.StringMap, error) {
// return &proto.StringMap{
// Value: _s._rep.GetValues(s2.Value),
// }, nil
//}
// 验证超级用户账号和密码
func (s *foundationService) SuperValidate(_ context.Context, user *proto.UserPwd) (*proto.SuperLoginResponse, error) {
if len(user.Password) != 32 {
return &proto.SuperLoginResponse{
ErrMsg: de.ErrNotMD5Format.Error(),
ErrCode: 2}, nil
}
superPwd, _ := s.registryRepo.GetValue(registry.SysSuperLoginToken)
encPwd := domain.Sha1Pwd(user.Username+user.Password, "")
if superPwd != encPwd {
return &proto.SuperLoginResponse{
ErrMsg: de.ErrCredential.Error(),
ErrCode: 1}, nil
}
return &proto.SuperLoginResponse{
ErrCode: 0,
Role: 1,
}, nil
}
// 保存超级用户账号和密码
func (s *foundationService) FlushSuperPwd(_ context.Context, user *proto.UserPwd) (*proto.Result, error) {
if len(user.Password) != 32 {
return s.error(de.ErrNotMD5Format), nil
}
encPwd := domain.Sha1Pwd(user.Username+user.Password, "")
err := s.registryRepo.UpdateValue(registry.SysSuperLoginToken,
encPwd)
return s.error(err), nil
}
// 注册单点登录应用,返回值:
// - 1. 成功,并返回token
// - -1. 接口地址不正确
// - -2. 已经注册
func (s *foundationService) RegisterApp(_ context.Context, app *proto.SSsoApp) (*proto.String, error) {
sso := module.Get(module.SSO).(*module.SSOModule)
token, err := sso.Register(app)
if err == nil {
return &proto.String{
Value: "1:" + token,
}, nil
}
return &proto.String{
Value: err.Error(),
}, nil
}
// 获取应用信息
func (s *foundationService) GetApp(_ context.Context, s2 *proto.String) (*proto.SSsoApp, error) {
sso := module.Get(module.SSO).(*module.SSOModule)
return sso.Get(s2.Value), nil
}
// 获取单点登录应用
func (s *foundationService) GetAllSsoApp(_ context.Context, _ *proto.Empty) (*proto.StringListResponse, error) {
sso := module.Get(module.SSO).(*module.SSOModule)
return &proto.StringListResponse{
Value: sso.Array(),
}, nil
}
// 创建同步登录的地址
func (s *foundationService) GetSyncLoginUrl(_ context.Context, s2 *proto.String) (*proto.String, error) {
panic("not implement")
//return fmt.Sprintf("%_s://%_s%_s/auth?return_url=%_s",
// consts.DOMAIN_PASSPORT_PROTO, consts.DOMAIN_PREFIX_PASSPORT,
// variable.Domain, returnUrl), nil
}
// 获取移动应用设置
func (s *foundationService) GetMoAppConf(_ context.Context, _ *proto.Empty) (*proto.SMobileAppConfig, error) {
c := s._rep.GetMoAppConf()
return &proto.SMobileAppConfig{
AppName: c.AppName,
AppIcon: c.AppIcon,
Description: c.Description,
AndroidVersion: c.AndroidVersion,
AndroidReleaseUrl: c.AndroidReleaseUrl,
IosVersion: c.IosVersion,
IosReleaseUrl: c.IosReleaseUrl,
ShowTplPath_: c.ShowTplPath,
}, nil
}
// 保存移动应用设置
func (s *foundationService) SaveMoAppConf(_ context.Context, config *proto.SMobileAppConfig) (*proto.Result, error) {
dst := &valueobject.MoAppConf{
AppName: config.AppName,
AppIcon: config.AppIcon,
Description: config.Description,
ShowTplPath: config.ShowTplPath_,
AndroidVersion: config.AndroidVersion,
AndroidReleaseUrl: config.AndroidReleaseUrl,
IosVersion: config.IosVersion,
IosReleaseUrl: config.IosReleaseUrl,
WpVersion: "",
WpReleaseUrl: "",
}
err := s._rep.SaveMoAppConf(dst)
return s.error(err), nil
}
// 获取微信接口配置
func (s *foundationService) GetWxApiConfig(_ context.Context, empty *proto.Empty) (*proto.SWxApiConfig, error) {
c := s._rep.GetWxApiConfig()
return &proto.SWxApiConfig{
AppId: c.AppId,
AppSecret: c.AppSecret,
MpToken: c.MpToken,
MpAesKey: c.MpAesKey,
OriId: c.OriId,
MchId: c.MchId,
MchApiKey: c.MchApiKey,
MchCertPath: c.MchCertPath,
MchCertKeyPath: c.MchCertKeyPath,
RedPackEnabled: c.RedPackEnabled,
RedPackAmountLimit: float64(c.RedPackAmountLimit),
RedPackDayTimeLimit: int32(c.RedPackDayTimeLimit),
}, nil
}
// 保存微信接口配置
func (s *foundationService) SaveWxApiConfig(_ context.Context, cfg *proto.SWxApiConfig) (*proto.Result, error) {
dst := &valueobject.WxApiConfig{
AppId: cfg.AppId,
AppSecret: cfg.AppSecret,
MpToken: cfg.MpToken,
MpAesKey: cfg.MpAesKey,
OriId: cfg.OriId,
MchId: cfg.MchId,
MchApiKey: cfg.MchApiKey,
MchCertPath: cfg.MchCertPath,
MchCertKeyPath: cfg.MchCertKeyPath,
RedPackEnabled: cfg.RedPackEnabled,
RedPackAmountLimit: float32(cfg.RedPackAmountLimit),
RedPackDayTimeLimit: int(cfg.RedPackDayTimeLimit),
}
err := s._rep.SaveWxApiConfig(dst)
return s.error(err), nil
}
// ResourceUrl 获取资源地址
func (s *foundationService) ResourceUrl(_ context.Context, s2 *proto.String) (*proto.String, error) {
return &proto.String{Value: format.GetFileFullUrl(s2.Value)}, nil
}
// GetSmsApiSet 获取短信设置
func (s *foundationService) GetSmsApiSet() notify.SmsApiSet {
//return _s._rep.GetSmsApiSet()
//todo: will remove
return notify.SmsApiSet{}
}
// GetChildAreas 获取下级区域
func (s *foundationService) GetChildAreas(_ context.Context, code *proto.Int32) (*proto.AreaListResponse, error) {
var arr []*proto.SArea
for _, v := range s._rep.GetChildAreas(code.Value) {
arr = append(arr, &proto.SArea{
Id: v.Code,
ParentId: v.Parent,
Name: v.Name,
})
}
return &proto.AreaListResponse{
Value: arr,
}, nil
}
// GetAreaNames 获取地区名称
func (s *foundationService) GetAreaNames(_ context.Context, request *proto.GetAreaNamesRequest) (*proto.StringListResponse, error) {
return &proto.StringListResponse{
Value: s._rep.GetAreaNames(request.Value),
}, nil
}
// GetAreaString 获取省市区字符串
func (s *foundationService) GetAreaString(_ context.Context, r *proto.AreaStringRequest) (*proto.String, error) {
if r.Province == 0 || r.City == 0 || r.District == 0 {
return &proto.String{Value: ""}, nil
}
str := s._rep.GetAreaString(r.Province, r.City, r.District)
return &proto.String{Value: str}, nil
}
// GetPayPlatform 获取支付平台
func (s *foundationService) GetPayPlatform(_ context.Context, r *proto.Empty) (*proto.PaymentPlatformResponse, error) {
m := module.Get(module.PAY).(*module.PaymentModule)
pf := m.GetPayPlatform()
ret := &proto.PaymentPlatformResponse{
Value: make([]*proto.PaymentPlatform, len(pf)),
}
for i, v := range pf {
ret.Value[i] = s.parsePayPlatform(v)
}
return ret, nil
}
// GetGlobMchSaleConf_ 获取全局商户销售设置
func (s *foundationService) GetGlobMchSaleConf_(_ context.Context, r *proto.Empty) (*proto.SGlobMchSaleConf, error) {
c := s._rep.GetGlobMchSaleConf()
return &proto.SGlobMchSaleConf{
FxSalesEnabled: c.FxSalesEnabled,
CashBackPercent: float64(c.CashBackPercent),
CashBackTg1Percent: float64(c.CashBackTg1Percent),
CashBackTg2Percent: float64(c.CashBackTg2Percent),
CashBackMemberPercent: float64(c.CashBackMemberPercent),
AutoSetupOrder: int32(c.AutoSetupOrder),
OrderTimeOutMinute: int32(c.OrderTimeOutMinute),
OrderConfirmAfterMinute: int32(c.OrderConfirmAfterMinute),
OrderTimeOutReceiveHour: int32(c.OrderTimeOutReceiveHour),
}, nil
}
// 保存全局商户销售设置
func (s *foundationService) SaveGlobMchSaleConf_(_ context.Context, conf *proto.SGlobMchSaleConf) (*proto.Result, error) {
dst := &valueobject.GlobMchSaleConf{
FxSalesEnabled: conf.FxSalesEnabled,
CashBackPercent: float32(conf.CashBackPercent),
CashBackTg1Percent: float32(conf.CashBackTg1Percent),
CashBackTg2Percent: float32(conf.CashBackTg2Percent),
CashBackMemberPercent: float32(conf.CashBackMemberPercent),
AutoSetupOrder: int(conf.AutoSetupOrder),
OrderTimeOutMinute: int(conf.OrderTimeOutMinute),
OrderConfirmAfterMinute: int(conf.OrderConfirmAfterMinute),
OrderTimeOutReceiveHour: int(conf.OrderTimeOutReceiveHour),
}
err := s._rep.SaveGlobMchSaleConf(dst)
return s.error(err), nil
}
func (s *foundationService) parsePayPlatform(v *bank.PaymentPlatform) *proto.PaymentPlatform {
dst := &proto.PaymentPlatform{
Id: v.ID,
Name: v.Name,
Sign: v.Sign,
Items: make([]*proto.BankItem, len(v.Bank)),
}
if v.Bank == nil {
for i, v := range v.Bank {
dst.Items[i] = s.parseBankItem(v)
}
}
return dst
}
func (s *foundationService) parseBankItem(v *bank.BankItem) *proto.BankItem {
return &proto.BankItem{
Id: v.ID,
Name: v.Name,
Sign: v.Sign,
}
}