fusenapi/server/backend/test/tpl_test.go

315 lines
11 KiB
Go
Raw Normal View History

2023-06-27 10:09:56 +00:00
package test
import (
"bytes"
"fusenapi/model/gmodel"
"fusenapi/utils/collect"
"fusenapi/utils/format"
"html/template"
"log"
"testing"
)
func TestCaseTpl(t *testing.T) {
priceInfo := []map[string]interface{}{
{"Price": "$10", "Num": 100},
{"Price": "$20", "Num": 200},
{"Price": "$30", "Num": 300},
{"Price": "$40", "Num": 400},
}
priceHtml := `
{{range $i, $parr := .}}
{{if lt $i 3}}
<div class="pr-card">
<p class="pr">${{$parr.Price}}</p>
<p class="num">{{$parr.Num}}<span> Units</span></p>
</div>
{{else}}
<div class="pr-card" style="margin-right: 8px;">
<p class="pr">${{$parr.Price}}</p>
<p class="num">{{$parr.Num}}<span> Units</span></p>
</div>
{{end}}
{{end}}`
tpl := template.Must(template.New("demoHtml").Parse(priceHtml))
buf := bytes.NewBufferString("")
tpl.Execute(buf, priceInfo)
html := buf.String()
log.Println(html)
}
func Randomproduct() []*gmodel.FsQuotationProduct {
var quotationId1 int64 = 1
name1 := "T恤"
size1 := "S,M,L,XL,XXL"
var cycle1 int64 = 7
var isGift1 int64 = 0
img1 := "https://xxx.jpg"
var status1 int64 = 1
var ctime1 int64 = 1623311101
var sort1 int64 = 1
sid1 := "abcd1234"
priceInfo1 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]`
remark1 := ""
var num1 int64 = 100
var quotationId2 int64 = 1
name2 := "帽子"
size2 := "通用"
var cycle2 int64 = 3
var isGift2 int64 = 1
img2 := "https://xxx.jpg"
var status2 int64 = 1
var ctime2 int64 = 1623311102
var sort2 int64 = 2
sid2 := "abcd1235"
priceInfo2 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]`
remark2 := ""
var num2 int64 = 50
var quotationId3 int64 = 2
name3 := "水壶"
size3 := "350ml,500ml,650ml"
var cycle3 int64 = 14
var isGift3 int64 = 0
img3 := "https://xxx.jpg"
var status3 int64 = 1
var ctime3 int64 = 1623311103
var sort3 int64 = 1
sid3 := "abcd1236"
priceInfo3 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]`
remark3 := ""
var num3 int64 = 200
var quotationId4 int64 = 2
name4 := "手机壳"
size4 := "iPhoneX,iPhoneXS,iPhoneXR"
var cycle4 int64 = 5
var isGift4 int64 = 1
img4 := "https://xxx.jpg"
var status4 int64 = 1
var ctime4 int64 = 1623311104
var sort4 int64 = 2
sid4 := "abcd1237"
priceInfo4 := `[{"num":500,"price":0.1},{"num":1000,"price":0.06},{"num":1500,"price":0.06}]`
remark4 := "可定制颜料颜色"
var num4 int64 = 150
products := []*gmodel.FsQuotationProduct{
&gmodel.FsQuotationProduct{
Id: 1,
QuotationId: &quotationId1,
Name: &name1,
Size: &size1,
Cycle: &cycle1,
IsGift: &isGift1,
Img: &img1,
Status: &status1,
Ctime: &ctime1,
Sort: &sort1,
Sid: &sid1,
PriceInfo: &priceInfo1,
Remark: &remark1,
Num: &num1,
},
&gmodel.FsQuotationProduct{
Id: 2,
QuotationId: &quotationId2,
Name: &name2,
Size: &size2,
Cycle: &cycle2,
IsGift: &isGift2,
Img: &img2,
Status: &status2,
Ctime: &ctime2,
Sort: &sort2,
Sid: &sid2,
PriceInfo: &priceInfo2,
Remark: &remark2,
Num: &num2,
},
&gmodel.FsQuotationProduct{
Id: 3,
QuotationId: &quotationId3,
Name: &name3,
Size: &size3,
Cycle: &cycle3,
IsGift: &isGift3,
Img: &img3,
Status: &status3,
Ctime: &ctime3,
Sort: &sort3,
Sid: &sid3,
PriceInfo: &priceInfo3,
Remark: &remark3,
Num: &num3,
},
&gmodel.FsQuotationProduct{
Id: 4,
QuotationId: &quotationId4,
Name: &name4,
Size: &size4,
Cycle: &cycle4,
IsGift: &isGift4,
Img: &img4,
Status: &status4,
Ctime: &ctime4,
Sort: &sort4,
Sid: &sid4,
PriceInfo: &priceInfo4,
Remark: &remark4,
Num: &num4,
},
}
return products
}
func TestTpl2(t *testing.T) {
products := Randomproduct()
tplcontent := `
{{$product_num := len .products}}
{{$page := 3}}
{{$page_total := add $page $product_num}}
{{range $arr := .products}}
{{$price := json_decode $arr.price_info}}
{{$price_html := .priceHtml}}
{{$price_html}}
{{if gt $arr.is_gift 0 }}{{/* 赠品 */}}
// 赠品
<div class="box">
<div style="position: relative; margin-bottom: 30px;">
<p class="f-m" style="font-size: 39px; font-weight: 800; margin-bottom: 12px; line-height: 32px;">{{$arr.name}}</p>
<p class="f-b" style="font-size: 25px; font-weight: 800;line-height: 19px;">{{$arr.size}}</p>
<div class="logo">
<img src="https://fusenapi.kayue.cn:8010/storage/pdf/logo.svg" alt="logo">
</div>
</div>
<div class="img-box" style="height: 611px;margin-bottom: 32px; overflow: hidden;">
<div class="img" style="background-image: url({{$arr.img}});"></div>
</div>
<div>
<div class="pr-card big">
<p class="pr">FREE!</p>
<p class="num">{{$arr.num}}<span> Units</span></p>
</div>
<div>
<div class="card-box" style="display: inline-block;width: 183px;line-height: 36px;padding-left: 16px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">Lead Time</div>
<div class="card-box" style="display: inline-block;width: 183px;height: 52px; padding-left: 15px;margin-bottom: 8px;background-color: #E0E2EB; font-weight: 600;">
<span style="font-size: 35px;line-height: 52px;">{{$arr.cycle}}</span><span style="font-size: 26px;line-height: 52px;">Days</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Design</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Storage</span>
</div>
<div class="card-box more-card" style="margin-bottom: 0;">
<span class="free">FREE </span><span class="text">Shipping</span>
</div>
</div>
</div>
</div>
{{else}}{{/* 非赠品 */}}
{{if $arr.size}}
// 有尺寸的
<div class="box">
<div style="position: relative; margin-bottom: 30px;">
<p class="f-m" style="font-size: 39px; font-weight: 800; margin-bottom: 12px; line-height: 32px;">{{$arr.name}}</p>
<p class="f-b" style="font-size: 25px; font-weight: 800;line-height: 19px;">{{$arr.size}}</p>
<div class="logo">
<img src="https://fusenapi.kayue.cn:8010/storage/pdf/logo.svg" alt="logo">
</div>
</div>
<div class="img-box" style="height: 611px;margin-bottom: 32px; overflow: hidden;">
<div class="img"
style="background-image: url({{$arr.img}});"></div>
</div>
<div>
<div class="card-box"
style="float: left; width: 530px;line-height: 36px;padding-left: 19px;margin-right: 8px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">Unit Price (Tax included)</div>
<div class="card-box"
style="display: inline-block;width: 183px;line-height: 36px;padding-left: 16px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">
Lead Time</div>
{{$price_html}}
<div>
<div class="card-box"
style="display: inline-block;width: 183px;height: 52px; padding-left: 15px;margin-bottom: 8px;background-color: #E0E2EB; font-weight: 600;">
<span style="font-size: 35px;line-height: 52px;">{{$arr.cycle}}</span><span
style="font-size: 26px;line-height: 52px;">Days</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Design</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Storage</span>
</div>
<div class="card-box more-card" style="margin-bottom: 0;">
<span class="free">FREE </span><span class="text">Shipping</span>
</div>
</div>
</div>
</div>
{{else}}
<div class="box">
<div style="position: relative; margin-bottom: 32px;">
<p class="f-m" style="font-size: 39px; font-weight: 800; margin-bottom: 3px; line-height: 32px;">
{{$arr.name}}</p>
<div class="logo">
<img src="https://fusenapi.kayue.cn:8010/storage/pdf/logo.svg" alt="logo">
</div>
</div>
<div class="img-box" style="height: 641px;margin-bottom: 31px; overflow: hidden;">
<div class="img" style="background-image: url({{$arr.img}});">
</div>
</div>
<div>
<div class="card-box"
style="float: left;width: 530px;line-height: 36px;padding-left: 19px;margin-right: 8px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">
Unit Price (Tax included)</div>
<div class="card-box"
style="display: inline-block;width: 183px;line-height: 36px;padding-left: 16px;margin-bottom: 4px;background-color: #C2C5CB;font-size: 18px; font-weight: 500;">
Lead Time</div>
{{$price_html}}
<div>
<div class="card-box"
style="display: inline-block;width: 183px;height: 52px; padding-left: 15px;margin-bottom: 8px;background-color: #E0E2EB; font-weight: 600;">
<span style="font-size: 35px;line-height: 52px;">{{$arr.cycle}}</span><span
style="font-size: 26px;line-height: 52px;">Days</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Design</span>
</div>
<div class="card-box more-card">
<span class="free">FREE </span><span class="text">Storage</span>
</div>
<div class="card-box more-card" style="margin-bottom: 0;">
<span class="free">FREE </span><span class="text">Shipping</span>
</div>
</div>
</div>
</div>
{{end}}
{{end}}
<div class="page-box" style="background-color: rgba(125, 125, 125, 0.16);color: #000;text-align: center;height: 50px;line-height: 50px;margin-top: 30px;">{{$page}}/{{$page_total}}</div>
{{$page = add $page 1}}
{{end}} `
tpl := template.New("demo")
tpl.Funcs(format.TemplateFuncMap)
tpl = template.Must(tpl.Parse(tplcontent))
buf := bytes.NewBufferString("")
tpl.Execute(buf, map[string]interface{}{"products": collect.StructSliceJson2Maps(products)})
html := buf.String()
log.Println(html)
}