292 lines
6.2 KiB
Go
292 lines
6.2 KiB
Go
package wechatmini
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
"server/models"
|
|
"server/services/wechatmp"
|
|
)
|
|
|
|
type Config struct {
|
|
ID uint64
|
|
Name string
|
|
AppID string
|
|
AppSecret string
|
|
QrcodeURL string
|
|
Enabled bool
|
|
AutoBind bool
|
|
Remark string
|
|
}
|
|
|
|
var ErrNotConfigured = errors.New("微信小程序未配置或未启用")
|
|
|
|
func appFromRow(row models.WechatMiniApp) (*Config, error) {
|
|
cfg := &Config{
|
|
ID: row.ID,
|
|
Name: row.Name,
|
|
AppID: strings.TrimSpace(row.AppID),
|
|
QrcodeURL: strings.TrimSpace(row.QrcodeURL),
|
|
Enabled: row.Enabled == 1,
|
|
AutoBind: row.AutoBind != 0,
|
|
Remark: row.Remark,
|
|
}
|
|
if row.AppSecret != "" {
|
|
plain, derr := wechatmp.DecryptSecret(row.AppSecret)
|
|
if derr != nil {
|
|
return nil, derr
|
|
}
|
|
cfg.AppSecret = plain
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func LoadConfig() (*Config, error) {
|
|
var rows []models.WechatMiniApp
|
|
_, err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("id").
|
|
All(&rows)
|
|
if err == nil && len(rows) > 0 {
|
|
return appFromRow(rows[0])
|
|
}
|
|
|
|
var legacy models.WechatMiniConfig
|
|
err = models.Orm.QueryTable(new(models.WechatMiniConfig)).OrderBy("id").One(&legacy)
|
|
if err != nil {
|
|
if err == orm.ErrNoRows {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return nil, err
|
|
}
|
|
cfg := &Config{
|
|
ID: legacy.ID,
|
|
AppID: strings.TrimSpace(legacy.AppID),
|
|
Enabled: legacy.Enabled == 1,
|
|
AutoBind: legacy.AutoBind != 0,
|
|
Remark: legacy.Remark,
|
|
}
|
|
if legacy.AppSecret != "" {
|
|
plain, derr := wechatmp.DecryptSecret(legacy.AppSecret)
|
|
if derr != nil {
|
|
return nil, derr
|
|
}
|
|
cfg.AppSecret = plain
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func LoadEnabledConfig() (*Config, error) {
|
|
cfg, err := LoadConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !cfg.Enabled || cfg.AppID == "" || cfg.AppSecret == "" {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func LoadEnabledAppByAppID(appID string) (*Config, error) {
|
|
appID = strings.TrimSpace(appID)
|
|
if appID == "" {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
var row models.WechatMiniApp
|
|
err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("app_id", appID).
|
|
Filter("delete_time__isnull", true).
|
|
One(&row)
|
|
if err != nil {
|
|
if err == orm.ErrNoRows {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return nil, err
|
|
}
|
|
cfg, err := appFromRow(row)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !cfg.Enabled || cfg.AppSecret == "" {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func ListApps() ([]models.WechatMiniApp, error) {
|
|
var rows []models.WechatMiniApp
|
|
_, err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("id").
|
|
All(&rows)
|
|
return rows, err
|
|
}
|
|
|
|
type PublicApp struct {
|
|
ID uint64 `json:"id"`
|
|
Name string `json:"name"`
|
|
AppID string `json:"app_id"`
|
|
QrcodeURL string `json:"qrcode_url"`
|
|
}
|
|
|
|
func ListEnabledApps() ([]PublicApp, error) {
|
|
var rows []models.WechatMiniApp
|
|
_, err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("enabled", 1).
|
|
OrderBy("id").
|
|
All(&rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]PublicApp, 0, len(rows))
|
|
for _, r := range rows {
|
|
list = append(list, PublicApp{
|
|
ID: r.ID,
|
|
Name: strings.TrimSpace(r.Name),
|
|
AppID: strings.TrimSpace(r.AppID),
|
|
QrcodeURL: strings.TrimSpace(r.QrcodeURL),
|
|
})
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
type SaveAppInput struct {
|
|
ID uint64
|
|
Name string
|
|
AppID string
|
|
AppSecret string
|
|
QrcodeURL string
|
|
Enabled bool
|
|
AutoBind bool
|
|
Remark string
|
|
}
|
|
|
|
func SaveApp(in SaveAppInput) (uint64, error) {
|
|
appID := strings.TrimSpace(in.AppID)
|
|
if appID == "" {
|
|
return 0, errors.New("AppID 不能为空")
|
|
}
|
|
now := time.Now()
|
|
|
|
var row models.WechatMiniApp
|
|
if in.ID > 0 {
|
|
err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("id", in.ID).
|
|
Filter("delete_time__isnull", true).
|
|
One(&row)
|
|
if err != nil {
|
|
return 0, errors.New("应用不存在或已删除")
|
|
}
|
|
} else {
|
|
row = models.WechatMiniApp{CreateTime: now}
|
|
}
|
|
|
|
dupQ := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("app_id", appID).
|
|
Filter("delete_time__isnull", true)
|
|
if in.ID > 0 {
|
|
dupQ = dupQ.Filter("id__ne", in.ID)
|
|
}
|
|
var dup models.WechatMiniApp
|
|
if err := dupQ.One(&dup); err == nil {
|
|
name := strings.TrimSpace(dup.Name)
|
|
if name == "" {
|
|
name = dup.AppID
|
|
}
|
|
return 0, fmt.Errorf("AppID 已被应用「%s」占用", name)
|
|
}
|
|
|
|
row.Name = strings.TrimSpace(in.Name)
|
|
row.AppID = appID
|
|
secret := strings.TrimSpace(in.AppSecret)
|
|
if secret != "" && !wechatmp.IsMasked(secret) {
|
|
enc, eerr := wechatmp.EncryptSecret(secret)
|
|
if eerr != nil {
|
|
return 0, eerr
|
|
}
|
|
row.AppSecret = enc
|
|
}
|
|
row.Enabled = 0
|
|
if in.Enabled {
|
|
row.Enabled = 1
|
|
}
|
|
row.QrcodeURL = strings.TrimSpace(in.QrcodeURL)
|
|
row.AutoBind = 0
|
|
if in.AutoBind {
|
|
row.AutoBind = 1
|
|
}
|
|
row.Remark = strings.TrimSpace(in.Remark)
|
|
row.UpdateTime = &now
|
|
|
|
if row.ID == 0 {
|
|
id, err := models.Orm.Insert(&row)
|
|
return uint64(id), err
|
|
}
|
|
if _, err := models.Orm.Update(&row); err != nil {
|
|
return 0, err
|
|
}
|
|
return row.ID, nil
|
|
}
|
|
|
|
func DeleteApp(id uint64) error {
|
|
if id == 0 {
|
|
return errors.New("无效ID")
|
|
}
|
|
now := time.Now()
|
|
_, err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("id", id).
|
|
Filter("delete_time__isnull", true).
|
|
Update(map[string]interface{}{
|
|
"delete_time": now,
|
|
"update_time": now,
|
|
"enabled": 0,
|
|
})
|
|
return err
|
|
}
|
|
|
|
type SaveInput struct {
|
|
AppID string
|
|
AppSecret string
|
|
Enabled bool
|
|
AutoBind bool
|
|
Remark string
|
|
}
|
|
|
|
func SaveConfig(in SaveInput) error {
|
|
var row models.WechatMiniApp
|
|
err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("id").
|
|
One(&row)
|
|
|
|
if err == orm.ErrNoRows {
|
|
_, serr := SaveApp(SaveAppInput{
|
|
Name: "默认小程序",
|
|
AppID: in.AppID,
|
|
AppSecret: in.AppSecret,
|
|
Enabled: in.Enabled,
|
|
AutoBind: in.AutoBind,
|
|
Remark: in.Remark,
|
|
})
|
|
return serr
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, serr := SaveApp(SaveAppInput{
|
|
ID: row.ID,
|
|
Name: row.Name,
|
|
AppID: in.AppID,
|
|
AppSecret: in.AppSecret,
|
|
QrcodeURL: row.QrcodeURL,
|
|
Enabled: in.Enabled,
|
|
AutoBind: in.AutoBind,
|
|
Remark: in.Remark,
|
|
})
|
|
return serr
|
|
}
|