diff --git a/api.go b/api.go index 1a504cd..13a4b1c 100644 --- a/api.go +++ b/api.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" + "github.com/line/line-bot-sdk-go/v7/linebot" "github.com/rs/zerolog/log" ) @@ -26,6 +27,9 @@ func ApiUpdateEmail(c *gin.Context) { } DB.Model(&User{}).Where("id = ?", data.ID).Update("email", data.Email) OK(c, "ok") + msg := linebot.NewTextMessage("更新成功!") + sticker := linebot.NewStickerMessage("1070", "17840") + SendMessage("", data.ID, msg, sticker, DefaultMsg()) } func ApiPostReceipt(c *gin.Context) { diff --git a/api_line.go b/api_line.go index bbe5ff1..4476c25 100644 --- a/api_line.go +++ b/api_line.go @@ -50,6 +50,8 @@ func postback(event *linebot.Event) { ApiPrepareClaim(event) case strings.HasPrefix(data, "cat="): ApiListProductOfCat(event) + case strings.HasPrefix(data, "purchase="): + ApiPurchase(event) case data == "list-tags": ApiListCategories(event) } @@ -62,6 +64,9 @@ func controller(event *linebot.Event) { log.Error().Msg("controller not impemented") return } + if !valid(event) { + return + } Hit(msg.Text, event.Source.UserID) switch { @@ -80,6 +85,8 @@ func controller(event *linebot.Event) { case strings.HasPrefix(msg.Text, "我是"): case msg.Text == "我知道": case msg.Text == "我要理賠": + case msg.Text == "修改完成": + case msg.Text == "我要投保": default: // route to watson diff --git a/api_line_products.go b/api_line_products.go index b05f21c..f8a3b8e 100644 --- a/api_line_products.go +++ b/api_line_products.go @@ -1,6 +1,7 @@ package main import ( + "strconv" "strings" "github.com/line/line-bot-sdk-go/v7/linebot" @@ -37,13 +38,40 @@ func ApiListProductOfCat(event *linebot.Event) { data := event.Postback.Data data = strings.TrimPrefix(data, "cat=") - msg := linebot.NewTextMessage(data + ":") + var prods []Product + DB.Find(prods, "id in (select product_id from product_categories where category_name = ?)", data) + + columns := make([]*linebot.CarouselColumn, len(prods)) + for i, prod := range prods { + columns[i] = linebot.NewCarouselColumn( + prod.Img, prod.Name, prod.Brief, + linebot.NewURIAction("保單詳細", flagBase+"/product/"+strconv.Itoa(int(prod.ID))), + linebot.NewPostbackAction("我要投保", "purchase="+prod.Name, "我要投保", ""), + ) + } + template := linebot.NewCarouselTemplate(columns...) + + msg := linebot.NewTemplateMessage("我要投保", template) if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { panic(err) } } -func ApiGetProduct(event *linebot.Event) {} +func ApiPurchase(event *linebot.Event) { + data := event.Postback.Data + data = strings.TrimPrefix(data, "purchase=") + prod := &Product{} + if err := DB.Preload("Categories").First(prod, "name = ?", data); err != nil { + panic(err) + } + + // show amount and finish purchase + msg := linebot.NewTextMessage(`好的, +保費試算為: XXX元 + +稍後將由專人為您服務。`) + SendMessage(event.ReplyToken, event.Source.UserID, msg) +} func ApiListRecommanded(event *linebot.Event) { var prods []Product diff --git a/api_line_user.go b/api_line_user.go index 9df46d2..ce6e753 100644 --- a/api_line_user.go +++ b/api_line_user.go @@ -1,9 +1,13 @@ package main -import "github.com/line/line-bot-sdk-go/v7/linebot" +import ( + "strconv" + + "github.com/line/line-bot-sdk-go/v7/linebot" +) func ApiMe(event *linebot.Event) { - leftBtn := linebot.NewURIAction("修改資料", flagBase+"/profile") + leftBtn := linebot.NewURIAction("修改資料", flagBase+"/profile/"+event.Source.UserID) rightBtn := linebot.NewMessageAction("我的保單", "我的保單") template := linebot.NewConfirmTemplate("請問想辦理甚麼服務?", leftBtn, rightBtn) msg := linebot.NewTemplateMessage("請問想辦理甚麼服務?", template) @@ -12,6 +16,28 @@ func ApiMe(event *linebot.Event) { } } +func valid(event *linebot.Event) bool { + msg, _ := event.Message.(*linebot.TextMessage) + if msg.Text == "我的資料" { + return true + } + usr := &User{} + if err := DB.First(usr, "id = ?", event.Source.UserID).Error; err != nil { + panic(err) + } + if usr.Email == "" { + text := "您的基本資料不完整, 請您協助填寫。" + tmp := linebot.NewButtonsTemplate("", "", text, + linebot.NewURIAction("點我填寫", flagBase+"/profile/"+event.Source.UserID)) + msg := linebot.NewTemplateMessage(text, tmp) + if _, err := SendMessage(event.ReplyToken, event.Source.UserID, msg); err != nil { + panic(err) + } + return false + } + return true +} + func ApiMyOrders(event *linebot.Event) { var orders []Order DB.Order("product_id").Find(&orders, "user_id = ?", event.Source.UserID) @@ -36,7 +62,7 @@ func ApiMyOrders(event *linebot.Event) { for i, prod := range products { columns[i] = linebot.NewCarouselColumn( prod.Img, prod.Name, prod.Brief, - linebot.NewURIAction("保單詳細", flagBase+"/product"), + linebot.NewURIAction("保單詳細", flagBase+"/product/"+strconv.Itoa(int(prod.ID))), linebot.NewPostbackAction("我要理賠", "claim="+prod.Name, "我要理賠", ""), // linebot.NewPostbackAction("我是"+prof.Name, "select="+prof.Name, "我是"+prof.Name, ""), ) diff --git a/db.go b/db.go index b30055d..e540bf3 100644 --- a/db.go +++ b/db.go @@ -44,7 +44,7 @@ func migrateDB() { if err := DB.SetupJoinTable(&User{}, "Products", &Order{}); err != nil { panic(err) } - if err := DB.AutoMigrate(&Node{}, &User{}, &Profile{}, &Product{}, &Order{}); err != nil { + if err := DB.AutoMigrate(&Node{}, &User{}, &Profile{}, &Product{}, &Order{}, &Price{}); err != nil { panic(err) } } diff --git a/main.go b/main.go index 0d9dc63..20fa614 100644 --- a/main.go +++ b/main.go @@ -77,13 +77,16 @@ func main() { server.GET("/check", func(c *gin.Context) { c.Writer.Write(check_web) }) - server.GET("/profile", func(c *gin.Context) { + server.GET("/profile/:uid", func(c *gin.Context) { + log.Debug().Str("uid", c.Param("uid")).Msg("profile") c.Writer.Write(profile_web) }) - server.GET("/claim", func(c *gin.Context) { + server.GET("/claim/:oid", func(c *gin.Context) { + log.Debug().Str("oid", c.Param("oid")).Msg("claim") c.Writer.Write(claim_web) }) - server.GET("/product", func(c *gin.Context) { + server.GET("/product/:pid", func(c *gin.Context) { + log.Debug().Str("pid", c.Param("pid")).Msg("product") c.Writer.Write(product_web) }) diff --git a/model.go b/model.go index c81cf39..bd49282 100644 --- a/model.go +++ b/model.go @@ -27,14 +27,29 @@ type User struct { type Profile struct { ID uint `gorm:"primaryKey"` - Name string - Brief string - Img string + Name string + Brief string + Img string + Age int + Gender string + Occupation string + PersonalID string + Phone string + Address string CreatedAt time.Time UpdatedAt time.Time } +type Price struct { + ProfileID uint `gorm:"primaryKey"` + ProductID uint `gorm:"primaryKey"` + + PlanPrice float32 + PlanUnit int + Detials string +} + type Product struct { ID uint `gorm:"primaryKey"` diff --git a/tmpl/check.html b/tmpl/check.html index 9a0c012..5aaff70 100644 --- a/tmpl/check.html +++ b/tmpl/check.html @@ -1,11 +1,25 @@ - - - - 核身 + + + + + 核身 -

HI

+
+
+
+

身分檢核中

+
+ 60% +
+
+
+ \ No newline at end of file diff --git a/tmpl/claim.html b/tmpl/claim.html index 89f2e52..c941cf8 100644 --- a/tmpl/claim.html +++ b/tmpl/claim.html @@ -3,6 +3,7 @@ + 上傳收據 diff --git a/tmpl/product.html b/tmpl/product.html index a327440..344562c 100644 --- a/tmpl/product.html +++ b/tmpl/product.html @@ -3,6 +3,7 @@ + 保單詳細 diff --git a/tmpl/profile.html b/tmpl/profile.html index 29e012e..0b8847c 100644 --- a/tmpl/profile.html +++ b/tmpl/profile.html @@ -1,11 +1,89 @@ - - - - 修改基本資料 + + + + 修改基本資料 + -

基本資料

+
+
+
+

身分檢核中

+
+ 60% +
+
+
+ + + + + + \ No newline at end of file