+ 添加数据资源操作相关的API
This commit is contained in:
@@ -103,10 +103,7 @@ func GetServiceInfo(ctx *jxcontext.Context) interface{} {
|
||||
func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTokenInfo map[string]interface{}, err error) {
|
||||
imgURL := ""
|
||||
if hashCode != "" {
|
||||
db := dao.GetDB()
|
||||
if skuName, err := dao.GetSkuNameByHashCode(db, hashCode); err == nil {
|
||||
imgURL = skuName.Img
|
||||
}
|
||||
imgURL, _ = GetDataResource(ctx, hashCode)
|
||||
}
|
||||
|
||||
putPolicy := storage.PutPolicy{
|
||||
@@ -123,6 +120,51 @@ func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTok
|
||||
return upTokenInfo, err
|
||||
}
|
||||
|
||||
func RegisterDataResource(ctx *jxcontext.Context, name, resourceURL, mimeType, hashCode string) (dataRes *model.DataResource, err error) {
|
||||
if model.ValideMimeTypes[mimeType] == 0 {
|
||||
return nil, fmt.Errorf("MIME type:%s非法", mimeType)
|
||||
}
|
||||
dataRes = &model.DataResource{
|
||||
Name: name,
|
||||
HashCode: hashCode,
|
||||
ResoureType: mimeType,
|
||||
MainURL: resourceURL,
|
||||
}
|
||||
vendorID := jxutils.GuessDataResourceVendor(resourceURL)
|
||||
switch vendorID {
|
||||
case model.VendorIDQiNiuCloud:
|
||||
dataRes.QiniuURL = resourceURL
|
||||
case model.VendorIDEBAI:
|
||||
dataRes.EbaiURL = resourceURL
|
||||
case model.VendorIDMTWM:
|
||||
dataRes.MtwmURL = resourceURL
|
||||
}
|
||||
|
||||
dao.WrapAddIDCULEntity(dataRes, ctx.GetUserName())
|
||||
if err = dao.CreateEntity(dao.GetDB(), dataRes); err != nil {
|
||||
dataRes = nil
|
||||
}
|
||||
return dataRes, err
|
||||
}
|
||||
|
||||
func GetDataResource(ctx *jxcontext.Context, hashCode string) (resourceURL string, err error) {
|
||||
db := dao.GetDB()
|
||||
dataRes, err := dao.GetDataResource(db, hashCode, "")
|
||||
if err != nil {
|
||||
if dao.IsNoRowsError(err) {
|
||||
skuName, err2 := dao.GetSkuNameByHashCode(db, hashCode)
|
||||
if err = err2; err == nil {
|
||||
resourceURL = skuName.Img
|
||||
} else if dao.IsNoRowsError(err) {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resourceURL = dataRes.MainURL
|
||||
}
|
||||
return resourceURL, err
|
||||
}
|
||||
|
||||
func GetPlaces(ctx *jxcontext.Context, keyword string, includeDisabled bool, params map[string]interface{}) ([]*model.Place, error) {
|
||||
sql := `
|
||||
SELECT *
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
"git.rosy.net.cn/jx-callback/globals/api2"
|
||||
"git.rosy.net.cn/jx-callback/globals/testinit"
|
||||
@@ -27,3 +28,11 @@ func TestGetQiniuUploadToken(t *testing.T) {
|
||||
}
|
||||
fmt.Print(token)
|
||||
}
|
||||
|
||||
func TestGetDataResource(t *testing.T) {
|
||||
dataRes, err := GetDataResource(jxcontext.AdminCtx, "1D3E4A8259F359FB4CF47D541843950D")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(utils.Format4Output(dataRes, false))
|
||||
}
|
||||
|
||||
@@ -27,6 +27,21 @@ import (
|
||||
var (
|
||||
routinePool *routinepool.Pool
|
||||
skuNamePat *regexp.Regexp
|
||||
|
||||
resourceTypeMap = map[int][]string{
|
||||
model.VendorIDQiNiuCloud: []string{
|
||||
"image.jxc4.com",
|
||||
},
|
||||
model.VendorIDJD: []string{
|
||||
"img30.360buyimg.com",
|
||||
},
|
||||
model.VendorIDMTWM: []string{
|
||||
"",
|
||||
},
|
||||
model.VendorIDEBAI: []string{
|
||||
"image-star.elemecdn.com",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
type OrderSkuList []*model.OrderSku
|
||||
@@ -692,3 +707,19 @@ func GetAuthType4Vendor(vendorID int) (authType string) {
|
||||
}
|
||||
return authType
|
||||
}
|
||||
|
||||
func GuessDataResourceVendor(resourceURL string) (vendorID int) {
|
||||
vendorID = -1
|
||||
for tmpVendorID, urlList := range resourceTypeMap {
|
||||
for _, v := range urlList {
|
||||
if strings.Index(resourceURL, "//"+v) >= 0 {
|
||||
vendorID = tmpVendorID
|
||||
break
|
||||
}
|
||||
}
|
||||
if vendorID >= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return vendorID
|
||||
}
|
||||
|
||||
@@ -72,6 +72,8 @@ var (
|
||||
VendorIDXiaoWM: "XiaoWM",
|
||||
VendorIDYiLianYun: "Yilianyun",
|
||||
VendorIDZhongWu: "ZhongWu",
|
||||
|
||||
VendorIDQiNiuCloud: "Qiniu",
|
||||
}
|
||||
|
||||
VendorTypeName = map[int]string{
|
||||
|
||||
@@ -13,19 +13,13 @@ var (
|
||||
type DataResource struct {
|
||||
ModelIDCUL
|
||||
|
||||
HashCode string `orm:"size(48);unique" json:"hash_code"`
|
||||
ResoureType string // 资料的mime type
|
||||
HashCode string `orm:"size(48);unique" json:"hashCode"`
|
||||
|
||||
Name string `orm:"size(48)" json:"name"`
|
||||
ResoureType string `orm:"size(48)" json:"resoureType"` // 资料的mime type
|
||||
Name string `orm:"size(48);index" json:"name"`
|
||||
|
||||
MainURL string
|
||||
QiniuURL string
|
||||
EbaiURL string
|
||||
MtwmURL string
|
||||
}
|
||||
|
||||
func (*DataResource) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"HashCode", "ResoureType"},
|
||||
}
|
||||
MainURL string `orm:"size(1024);column(main_url);index" json:"mainURL"`
|
||||
QiniuURL string `orm:"size(1024);column(qiniu_url);index" json:"qiniuURL"`
|
||||
EbaiURL string `orm:"size(1024);column(ebai_url);index" json:"ebaiURL"`
|
||||
MtwmURL string `orm:"size(1024);column(mtwm_url);index" json:"mtwmURL"`
|
||||
}
|
||||
|
||||
23
business/model/dao/common.go
Normal file
23
business/model/dao/common.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
)
|
||||
|
||||
func GetDataResource(db *DaoDB, hashCode, fullURL string) (dataRes *model.DataResource, err error) {
|
||||
sql := `
|
||||
SELECT t1.*
|
||||
FROM data_resource t1
|
||||
WHERE 1 = 1`
|
||||
sqlParams := []interface{}{}
|
||||
if hashCode != "" {
|
||||
sql += " AND t1.hash_code = ?"
|
||||
sqlParams = append(sqlParams, hashCode)
|
||||
}
|
||||
if fullURL != "" {
|
||||
sql += " AND (t1.main_url = ? OR t1.qiniu_url = ? OR t1.ebai_url = ? OR t1.mtwm_url = ?)"
|
||||
sqlParams = append(sqlParams, fullURL, fullURL, fullURL, fullURL)
|
||||
}
|
||||
err = GetRow(db, &dataRes, sql, sqlParams...)
|
||||
return dataRes, err
|
||||
}
|
||||
Reference in New Issue
Block a user