90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
|
|
package delivery
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/url"
|
||
|
|
"reflect"
|
||
|
|
|
||
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/config"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
func currentScope(ctx *gin.Context) (models.DeliveryBasic, models.GasBasic, bool) {
|
||
|
|
_, point, station, ok := CurrentDeliveryAccount(ctx)
|
||
|
|
return point, station, ok
|
||
|
|
}
|
||
|
|
|
||
|
|
func ListMenu(ctx *gin.Context) {
|
||
|
|
account, _, _, ok := CurrentDeliveryAccount(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
menus := MenusForRole(account.RoleCode)
|
||
|
|
infra.Response.Success(ctx, gin.H{"total": len(menus), "list": menus})
|
||
|
|
}
|
||
|
|
|
||
|
|
func InvitationQRCode(ctx *gin.Context) {
|
||
|
|
_, point, station, ok := CurrentDeliveryAccount(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
registerURL, _ := url.Parse(config.Spec.Global.UserRegisterURL)
|
||
|
|
query := registerURL.Query()
|
||
|
|
query.Set("gas_identity", station.Identity)
|
||
|
|
query.Set("delivery_identity", point.Identity)
|
||
|
|
registerURL.RawQuery = query.Encode()
|
||
|
|
infra.Response.Success(ctx, gin.H{"register_url": registerURL.String()})
|
||
|
|
}
|
||
|
|
|
||
|
|
func ListProfile(ctx *gin.Context) {
|
||
|
|
_, point, _, ok := CurrentDeliveryAccount(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
respondList(ctx, []models.DeliveryBasic{point}, 1)
|
||
|
|
}
|
||
|
|
|
||
|
|
func respondList(ctx *gin.Context, list any, total int64) {
|
||
|
|
response, err := common.PublicResourceResponse(list)
|
||
|
|
if err != nil {
|
||
|
|
infra.Response.Error(ctx, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
infra.Response.Success(ctx, gin.H{"total": total, "list": response})
|
||
|
|
}
|
||
|
|
|
||
|
|
func listScoped(ctx *gin.Context, model any, query *gorm.DB, order string) {
|
||
|
|
page, size := common.PageSize(ctx)
|
||
|
|
query = common.ApplyKeywordFilter(ctx, query, model)
|
||
|
|
var total int64
|
||
|
|
if err := query.Count(&total).Error; err != nil {
|
||
|
|
infra.Response.Error(ctx, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
list := reflect.New(reflect.SliceOf(reflect.TypeOf(model).Elem()))
|
||
|
|
if err := query.Order(order).Offset((page - 1) * size).Limit(size).Find(list.Interface()).Error; err != nil {
|
||
|
|
infra.Response.Error(ctx, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
respondList(ctx, list.Elem().Interface(), total)
|
||
|
|
}
|
||
|
|
|
||
|
|
func respondRecord(ctx *gin.Context, query *gorm.DB, model any) {
|
||
|
|
if err := query.First(model).Error; err != nil {
|
||
|
|
common.RespondRecordError(ctx, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
response, err := common.PublicResourceResponse(model)
|
||
|
|
if err != nil {
|
||
|
|
infra.Response.Error(ctx, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
infra.Response.Success(ctx, response)
|
||
|
|
}
|
||
|
|
|
||
|
|
func db() *gorm.DB { return impl.DBService }
|