153 lines
4.3 KiB
Go
153 lines
4.3 KiB
Go
|
package controller
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"kefu/lib"
|
||
|
"kefu/models"
|
||
|
"kefu/tools"
|
||
|
"kefu/types"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type VirtualProductForm struct {
|
||
|
Id uint `form:"id" json:"id" uri:"id" xml:"id"`
|
||
|
ProductName string `form:"product_name" json:"product_name" uri:"product_name" xml:"product_name" binding:"required"`
|
||
|
Payment string `form:"payment" json:"payment" uri:"payment" xml:"payment" binding:"required"`
|
||
|
Price string `form:"price" json:"price" uri:"price" xml:"price" binding:"required"`
|
||
|
ResourceLink string `form:"resource_link" json:"resource_link" uri:"resource_link" xml:"resource_link" binding:"required"`
|
||
|
ProductCategory string `form:"product_category" json:"product_category" uri:"product_category" xml:"product_category" binding:"required"`
|
||
|
}
|
||
|
|
||
|
func GetVirtualProduct(c *gin.Context) {
|
||
|
entId := c.Query("ent_id")
|
||
|
id := c.Query("id")
|
||
|
product := models.FindVirtualProduct("ent_id = ? and id = ?", entId, id)
|
||
|
product.ResourceLink = ""
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": types.ApiCode.SUCCESS,
|
||
|
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
|
||
|
"result": product,
|
||
|
})
|
||
|
}
|
||
|
func GetVirtualProductLink(c *gin.Context) {
|
||
|
product := c.Query("product")
|
||
|
wechatConfig, _ := lib.NewWechatLib(models.FindConfig("SystemBussinesId"))
|
||
|
host := strings.TrimRight(wechatConfig.WechatHost, "/")
|
||
|
entId, _ := c.Get("ent_id")
|
||
|
kefuName, _ := c.Get("kefu_name")
|
||
|
|
||
|
roleId, _ := c.Get("role_id")
|
||
|
if roleId.(float64) == 1 {
|
||
|
product := models.FindVirtualProduct("id = ?", product)
|
||
|
entId = product.EntId
|
||
|
kefuName = product.KefuName
|
||
|
}
|
||
|
if host == "" {
|
||
|
host = tools.GetHost(c.Request)
|
||
|
}
|
||
|
link := fmt.Sprintf("%s/pay/%s/%s?product=%s", host, entId, kefuName, product)
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": types.ApiCode.SUCCESS,
|
||
|
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
|
||
|
"result": link,
|
||
|
})
|
||
|
}
|
||
|
func PostVirtualProduct(c *gin.Context) {
|
||
|
kefuName, _ := c.Get("kefu_name")
|
||
|
entId, _ := c.Get("ent_id")
|
||
|
|
||
|
var form VirtualProductForm
|
||
|
err := c.Bind(&form)
|
||
|
|
||
|
if err != nil {
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": types.ApiCode.FAILED,
|
||
|
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
|
||
|
"result": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
kefuInfo := models.FindUser(kefuName.(string))
|
||
|
num := models.CountVirtualProducts("ent_id = ? and kefu_name = ?", entId, kefuName)
|
||
|
if int64(kefuInfo.AgentNum+1) <= num && form.Id == 0 {
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": 400,
|
||
|
"msg": fmt.Sprintf("付费资源数超过上限数: %d!", kefuInfo.AgentNum+1),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
m := &models.VirtualProduct{
|
||
|
EntId: entId.(string),
|
||
|
KefuName: kefuName.(string),
|
||
|
ProductName: form.ProductName,
|
||
|
Price: tools.Str2Int(form.Price),
|
||
|
ResourceLink: form.ResourceLink,
|
||
|
CreatedAt: types.Time{},
|
||
|
UpdatedAt: types.Time{},
|
||
|
ProductCategory: form.ProductCategory,
|
||
|
Payment: form.Payment,
|
||
|
}
|
||
|
if form.Id == 0 {
|
||
|
m.AddVirtualProduct()
|
||
|
} else {
|
||
|
m.SaveVirtualProduct("id = ? and ent_id = ?", form.Id, entId)
|
||
|
}
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": types.ApiCode.SUCCESS,
|
||
|
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
|
||
|
})
|
||
|
}
|
||
|
func GetVirtualProducts(c *gin.Context) {
|
||
|
entId, _ := c.Get("ent_id")
|
||
|
|
||
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
||
|
if pageSize > 50 {
|
||
|
pageSize = 10
|
||
|
}
|
||
|
|
||
|
roleId, _ := c.Get("role_id")
|
||
|
search := "1=1 "
|
||
|
var args = []interface{}{}
|
||
|
if roleId.(float64) != 1 {
|
||
|
search += "and ent_id = ? "
|
||
|
args = append(args, entId)
|
||
|
}
|
||
|
|
||
|
count := models.CountVirtualProducts(search, args...)
|
||
|
list := models.FindVirtualProducts(page, pageSize, search, args...)
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": types.ApiCode.SUCCESS,
|
||
|
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
|
||
|
"result": gin.H{
|
||
|
"list": list,
|
||
|
"count": count,
|
||
|
"pagesize": pageSize,
|
||
|
"page": page,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// 删除
|
||
|
func DeleteVirtualProduct(c *gin.Context) {
|
||
|
entId, _ := c.Get("ent_id")
|
||
|
id := c.Query("id")
|
||
|
roleId, _ := c.Get("role_id")
|
||
|
search := "id = ? "
|
||
|
var args = []interface{}{id}
|
||
|
if roleId.(float64) != 1 {
|
||
|
search += "and ent_id = ? "
|
||
|
args = append(args, entId)
|
||
|
}
|
||
|
|
||
|
models.DeleteVirtualProduct(search, args...)
|
||
|
c.JSON(200, gin.H{
|
||
|
"code": types.ApiCode.SUCCESS,
|
||
|
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
|
||
|
})
|
||
|
}
|