iedc-go/example/provider/salesman.go
2022-05-13 09:30:09 +08:00

144 lines
4.2 KiB
Go

package provider
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
)
var Salesman = Ctr{
"test": func(that *Context) {
that.Session("salesman_id", 1)
that.Session("wechat_id", 1)
that.Display(0, 1)
},
"info": func(that *Context) {
if that.Session("salesman_id").Data == nil {
that.Display(2, "没有登录")
return
}
salesman := that.Db.Get("salesman", "*", Map{"id": that.Session("salesman_id").Data})
if salesman == nil {
that.Display(4, "找不到该业务员")
return
}
if salesman.GetString("nickname") == "" {
wechat := that.Db.Get("wechat", "*", Map{"salesman_id": salesman.GetCeilInt64("id")})
if wechat != nil {
salesman["nickname"] = wechat.GetString("nickname")
salesman["avatar"] = wechat.GetString("avatar")
that.Db.Update("salesman", Map{"nickname": wechat.GetString("nickname"), "avatar": wechat.GetString("avatar")}, Map{"id": salesman.GetCeilInt64("id")})
}
}
salesman["user"] = that.Db.Count("user", Map{"AND": Map{"salesman_id": that.Session("salesman_id").Data, "del_flag": 0}})
salesman["matters"] = that.Db.Count("matters", Map{"AND": Map{"salesman_id": that.Session("salesman_id").Data, "del_flag": 0}})
salesman["admin"] = that.Db.Get("admin", "id,name,avatar", Map{"id": salesman.GetCeilInt64("admin_id")})
salesman["provider"] = that.Db.Get("provider", "*", Map{"id": salesman.GetCeilInt64("provider_id")})
if salesman["provider"] != nil {
salesman["provider_salesman"] = that.Db.Get("salesman", "id,nickname,name,avatar", Map{"id": salesman.GetMap("provider").GetCeilInt64("salesman_id")})
}
that.Display(0, salesman)
},
"search": func(that *Context) {
if that.Session("salesman_id").Data == nil {
that.Display(2, "没有登录")
return
}
salesman := that.Db.Get("salesman", "*", Map{"id": that.Session("salesman_id").Data})
if salesman == nil {
that.Display(4, "找不到该业务员")
return
}
provider := that.Db.Get("provider", "*", Map{"id": salesman.GetCeilInt64("provider_id")})
if provider.GetCeilInt("salesman_id") != salesman.GetCeilInt("id") {
that.Display(0, Slice{})
return
}
page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
data := Map{"del_flag": 0, "provider_id": salesman.GetCeilInt64("provider_id")}
keywords := that.Req.FormValue("keywords")
if keywords != "" {
data["OR"] = Map{"name[~]": keywords, "nickname[~]": keywords, "phone[~]": keywords}
}
startTime := that.Req.FormValue("starttime")
finishTime := that.Req.FormValue("finishtime")
if startTime != "" {
data["modifye_time[>=]"] = startTime
}
if finishTime != "" {
data["modifye_time[<=]"] = finishTime
}
count := that.Db.Count("salesman", data)
res := that.Db.Page(page, pageSize).PageSelect("salesman", "id,name,nickname,avatar", data)
that.Display(0, Map{"total": count, "data": res})
},
//用户微信公众号或者小程序登录
"login": func(that *Context) {
code := that.Req.FormValue("code")
phone := that.Req.FormValue("phone")
if phone != that.Session("phone").ToStr() && code == that.Session("code").ToStr() {
that.Display(3, "手机号或者验证码错误")
return
}
if that.Session("wechat_id").Data == nil {
that.Display(2, "还未登录")
return
}
wechat := that.Db.Get("wechat", "*", Map{"id": that.Session("wechat_id").ToCeilInt()})
salesman := that.Db.Get("salesman", "*", Map{"phone": phone})
if salesman == nil {
that.Display(3, "找不到企服商")
return
}
if wechat == nil {
that.Display(2, "还未绑定微信")
return
}
//有用户直接返回
if wechat.GetCeilInt("salesman_id") != 0 && wechat.GetCeilInt64("salesman_id") != salesman.GetInt64("id") {
that.Display(5, "你已经绑定了其他商户")
return
}
that.Db.Update("wechat", Map{"salesman_id": salesman.GetCeilInt64("id")}, Map{"id": wechat.GetInt64("id")})
that.Db.Update("salesman", Map{"login_time[#]": "now()", "modify_time[#]": "now()"},
Map{"id": salesman.GetCeilInt("id")})
wechat["salesman_id"] = salesman.GetCeilInt64("id")
that.Session("salesman_id", salesman.GetCeilInt64("id"))
that.Display(0, "登录成功!")
},
}