iedc-go/example/app/wechath5.go

288 lines
8.6 KiB
Go
Raw Normal View History

2022-05-03 00:09:25 +00:00
package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"code.hoteas.com/golang/hotime/dri/wechat"
"time"
2022-05-03 00:09:25 +00:00
)
var Wechath5 = Ctr{
//微信注册0已经完整的注册了1还没有注册
"code": func(that *Context) {
if that.Req.FormValue("code") == "" {
that.Display(3, "参数不足")
return
}
appid, resToken, userInfo, err := wechat.H5Program.GetUserInfo(that.Req.FormValue("code"))
if err != nil {
that.Display(4, err)
return
}
//此次获取的微信信息
wechatInfo := Map{
"openid": userInfo.OpenID,
"acttoken": resToken.AccessToken,
"retoken": resToken.RefreshToken,
"appid": appid,
"unionid": userInfo.Unionid,
2022-05-05 08:07:01 +00:00
"nickname": FilterEmoji(userInfo.Nickname),
2022-05-03 00:09:25 +00:00
"avatar": userInfo.HeadImgURL,
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
"type": 1,
}
userId := 0
2022-05-17 04:31:47 +00:00
//这个defer放到上面防止出现用户第一次登陆的时候出现不返回company和salesman的情况
//如果有则直接返回用户信息到微信小程序里
defer func() {
if userId != 0 {
user := that.Db.Get("user", "*", Map{"id": userId})
if user == nil {
that.Display(4, "获取个人信息失败")
return
}
delete(user, "password")
company := that.Db.Get("company", "id,name", Map{"id": user.GetCeilInt("company_id")})
//消除company没有创建的影响
if company == nil {
companyId := that.Db.Insert("company", Map{
"user_id": user["id"],
"provider_id": user["provider_id"],
"salesman_id": user["salesman_id"],
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
})
user["company_id"] = companyId
that.Db.Update("user", Map{"company_id": companyId}, Map{"id": user.GetCeilInt("id")})
company = that.Db.Get("company", "*", Map{"id": user.GetCeilInt("company_id")})
}
salesman := that.Db.Get("salesman", "id,name", Map{"id": user.GetCeilInt("salesman_id")})
provider := that.Db.Get("provider", "id,name", Map{"id": user.GetCeilInt("provider_id")})
user["company"] = company
user["salesman"] = salesman
user["provider"] = provider
that.Display(0, user)
}
}()
2022-05-04 16:29:30 +00:00
defer func() {
2022-05-07 03:02:12 +00:00
2022-05-04 16:29:30 +00:00
//有sn就关联业务员
parentId := ObjToInt(that.Req.FormValue("parent_id"))
if parentId == 0 {
return
}
if userId == 0 || userId == parentId {
return
}
puser := that.Db.Get("user", "`index`,id", Map{"id": parentId})
if puser == nil {
return
}
user := that.Db.Get("user", "parent_id", Map{"id": userId})
if user.GetCeilInt("parent_id") != 0 {
return
}
index1 := puser.GetString("index")
if index1 == "," {
index1 = index1 + ObjToStr(parentId) + ","
}
//2022/5/11 zpw 添加字段用户关联parenid的时间 join_parent_time
that.Db.Update("user", Map{"parent_id": parentId, "index": index1 + ObjToStr(userId) + ",", "join_parent_time[#]": "NOW()"}, Map{"id": userId})
//在这里做判断 能否通过分享领券
//如果通过邀请获得的券超过了三张则不再赠送
//赠送一张券
data := Map{
"user_id": parentId,
"coupon_id": 1,
"code_no": "SN" + time.Now().Format("20060102150405") + getSn(),
"effective_start_time[#]": "NOW()",
2022-05-17 04:31:47 +00:00
"effective_end_time": time.Now().AddDate(0, 6, 0).Format("2006-01-02 15:04:05"),
"source_type": 3,
"status": 0,
"admin_id": user.GetCeilInt("admin_id"),
"create_time[#]": "NOW()",
}
//先判断是否领取过 source_type=3 是通过拉新赠送的券
couponCount := that.Db.Count("coupon_user", Map{"AND": Map{"user_id": parentId, "source_type": 3}})
if couponCount < 3 {
that.Db.Insert("coupon_user", data)
}
2022-05-03 00:09:25 +00:00
2022-05-04 16:29:30 +00:00
}()
2022-05-03 00:09:25 +00:00
//最后验证服务商是否绑定
defer func() {
//有sn就关联业务员
sn := that.Req.FormValue("sn")
if sn == "" {
return
}
if userId == 0 {
return
}
salesman := that.Db.Get("salesman", "*", Map{"sn": sn})
if salesman.GetCeilInt("id") == 0 {
return
}
user := that.Db.Get("user", "*", Map{"id": userId})
if user == nil {
return
}
2022-05-07 03:02:12 +00:00
2022-05-03 00:09:25 +00:00
//用户都有企服人员关联
if user.GetCeilInt("salesman_id") != 0 {
return
}
//用户没有企服商id
//2022/5/11 zpw 添加字段:用户关联企服商业务人员的时间 join_salesman_time
that.Db.Update("user", Map{"salesman_id": salesman.GetCeilInt64("id"), "provider_id": salesman.GetCeilInt64("provider_id"), "join_salesman_time[#]": "NOW()"}, Map{"id": user.GetCeilInt64("id")})
2022-05-03 00:09:25 +00:00
if user.GetCeilInt("company_id") == 0 {
return
}
//绑定企业
that.Db.Update("company", Map{"salesman_id": salesman.GetCeilInt64("id"), "provider_id": salesman.GetCeilInt64("provider_id")}, Map{"id": user.GetCeilInt64("company_id")})
}()
wechat := that.Db.Get("wechat", "*", Map{"AND": Map{"openid": userInfo.OpenID, "del_flag": 0}})
if wechat != nil {
//有用户直接返回
if wechat.GetCeilInt("user_id") != 0 {
that.Db.Update("user", Map{"login_time[#]": "now()"}, Map{"id": wechat.GetCeilInt("user_id")})
that.Session("wechat_id", wechat.GetCeilInt("id"))
that.Session("user_id", wechat.GetCeilInt("user_id"))
userId = wechat.GetCeilInt("user_id")
//that.Display(0, "登录成功")
return
}
//没有用户继续查询数据库看是否有其他unionid
wechat1 := that.Db.Get("wechat", "*", Map{"AND": Map{"unionid": userInfo.Unionid, "openid[!]": userInfo.OpenID, "user_id[>]": 0, "del_flag": 0}})
//其他表有该数据,则更新当前表数据信息
if wechat1 != nil {
wechatInfo["user_id"] = wechat1.GetCeilInt("user_id")
that.Db.Update("wechat", wechatInfo, Map{"id": wechat.GetCeilInt("id")})
that.Db.Update("user", Map{"login_time[#]": "now()"}, Map{"id": wechat1.GetCeilInt("user_id")})
that.Session("wechat_id", wechat.GetCeilInt("id"))
that.Session("user_id", wechatInfo.GetCeilInt("user_id"))
userId = wechatInfo.GetCeilInt("user_id")
//that.Display(0, "登录成功")
return
}
//其他表也没有当前信息则生成user表并更新当前用户,一般不会走到这里来
user := Map{
"nickname": wechatInfo.GetString("nickname"),
"avatar": wechatInfo.GetString("avatar"),
2022-05-04 16:29:30 +00:00
"index": ",",
2022-05-03 00:09:25 +00:00
"create_time[#]": "now()",
"modify_time[#]": "now()",
"login_time[#]": "now()",
"del_flag": 0,
}
user["id"] = that.Db.Insert("user", user)
2022-05-03 01:41:18 +00:00
//创建企业
user["company_id"] = that.Db.Insert("company", Map{
"user_id": user["id"],
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
})
that.Db.Update("user", Map{"company_id": user["company_id"]}, Map{"id": user["id"]})
2022-05-03 00:09:25 +00:00
wechatInfo["user_id"] = user.GetCeilInt("id")
that.Db.Update("wechat", wechatInfo, Map{"id": wechat.GetCeilInt("id")})
that.Session("wechat_id", wechat.GetCeilInt("id"))
that.Session("user_id", wechatInfo.GetCeilInt("user_id"))
userId = wechatInfo.GetCeilInt("user_id")
//that.Display(0, "登录成功")
return
}
user := Map{
"nickname": wechatInfo.GetString("nickname"),
"avatar": wechatInfo.GetString("avatar"),
2022-05-04 16:29:30 +00:00
"index": ",",
2022-05-03 00:09:25 +00:00
"create_time[#]": "now()",
"modify_time[#]": "now()",
"login_time[#]": "now()",
"del_flag": 0,
}
user["id"] = that.Db.Insert("user", user)
2022-05-03 01:41:18 +00:00
user["company_id"] = that.Db.Insert("company", Map{
"user_id": user["id"],
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
})
that.Db.Update("user", Map{"company_id": user["company_id"]}, Map{"id": user["id"]})
2022-05-03 00:09:25 +00:00
wechatInfo["user_id"] = user.GetCeilInt("id")
wechatInfo["id"] = that.Db.Insert("wechat", wechatInfo)
if wechatInfo.GetCeilInt("id") == 0 {
that.Display(4, "创建用户失败")
return
}
that.Session("wechat_id", wechatInfo.GetCeilInt("id"))
that.Session("user_id", wechatInfo.GetCeilInt("user_id"))
userId = wechatInfo.GetCeilInt("user_id")
//that.Display(0, "登录成功")
},
//网页签名
"sign": func(that *Context) {
signUrl := that.Req.FormValue("url")
if signUrl == "" {
that.Display(3, "参数不足")
return
}
cfg1, e := wechat.H5Program.GetSignUrl(signUrl)
if e != nil {
that.Display(4, e)
return
}
sign := Map{
"appId": cfg1.AppID,
"timestamp": cfg1.Timestamp,
"nonceStr": cfg1.NonceStr,
"signature": cfg1.Signature,
}
that.Display(0, sign)
},
}