159 lines
4.4 KiB
Go
159 lines
4.4 KiB
Go
|
package app
|
|||
|
|
|||
|
import (
|
|||
|
. "code.hoteas.com/golang/hotime"
|
|||
|
. "code.hoteas.com/golang/hotime/common"
|
|||
|
"code.hoteas.com/golang/hotime/dri/wechat"
|
|||
|
"fmt"
|
|||
|
)
|
|||
|
|
|||
|
var WechatMini = Ctr{
|
|||
|
"getphone": func(that *Context) {
|
|||
|
|
|||
|
//sessionKey := that.Req.FormValue("sessionkey")
|
|||
|
|
|||
|
if that.Session("wechat_id").Data == nil {
|
|||
|
that.Display(2, "请先登录")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
encryptedData := that.Req.FormValue("encryptedData")
|
|||
|
iv := that.Req.FormValue("iv")
|
|||
|
|
|||
|
if encryptedData == "" || iv == "" {
|
|||
|
that.Display(3, "参数不足")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
wechatIns := that.Db.Get("wechat", "sessionkey", Map{"id": that.Session("wechat_id").ToCeilInt()})
|
|||
|
_, re, e := wechat.MiniProgram.GetPhoneNumber(wechatIns.GetString("sessionkey"), encryptedData, iv)
|
|||
|
|
|||
|
if e != nil {
|
|||
|
that.Display(4, e)
|
|||
|
return
|
|||
|
}
|
|||
|
//临时存储用于校验
|
|||
|
that.Session("wechat_phone", re.PhoneNumber)
|
|||
|
that.Display(0, re.PhoneNumber)
|
|||
|
|
|||
|
},
|
|||
|
//检查是否已经有用户登录了,如果有直接登录,如果没有则查询unionid,有则直接返回用户信息,没有则返回null
|
|||
|
"code": func(that *Context) {
|
|||
|
|
|||
|
code := that.Req.FormValue("code")
|
|||
|
if code == "" {
|
|||
|
that.Display(3, "缺少code")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
appid, re, e := wechat.MiniProgram.GetBaseUserInfo(code)
|
|||
|
fmt.Println(re)
|
|||
|
if e != nil {
|
|||
|
that.Display(4, e)
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
wchat := Map{"openid": re.OpenID,
|
|||
|
"appid": appid,
|
|||
|
"sessionkey": re.SessionKey,
|
|||
|
"unionid": re.UnionID,
|
|||
|
"modify_time[#]": "now()",
|
|||
|
"del_flag": 0,
|
|||
|
"type": 2,
|
|||
|
}
|
|||
|
|
|||
|
userId := 0
|
|||
|
|
|||
|
//如果有则直接返回用户信息到微信小程序里
|
|||
|
defer func() {
|
|||
|
if userId != 0 {
|
|||
|
user := that.Db.Get("user", "*", Map{"id": userId})
|
|||
|
company := that.Db.Get("company", "id,name", 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")})
|
|||
|
if user == nil {
|
|||
|
that.Display(4, "获取个人信息失败")
|
|||
|
return
|
|||
|
}
|
|||
|
delete(user, "password")
|
|||
|
user["company"] = company
|
|||
|
user["salesman"] = salesman
|
|||
|
user["provider"] = provider
|
|||
|
that.Display(0, user)
|
|||
|
}
|
|||
|
}()
|
|||
|
|
|||
|
wechat := that.Db.Get("wechat", "*", Map{"AND": Map{"openid": re.OpenID, "user_id[!]": nil, "del_flag": 0}})
|
|||
|
//有该用户,则登录成功
|
|||
|
if wechat != nil {
|
|||
|
|
|||
|
that.Session("wechat_id", wechat.GetCeilInt("id"))
|
|||
|
that.Session("user_id", wechat.GetCeilInt("user_id"))
|
|||
|
//更新用户信息
|
|||
|
that.Db.Update("wechat", wchat, Map{"id": wechat.GetCeilInt("id")})
|
|||
|
//that.Display(0, "登录成功")
|
|||
|
userId = wechat.GetCeilInt("user_id")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
//如果其他人有相关信息,则直接取用
|
|||
|
wechat = that.Db.Get("wechat", "*", Map{"AND": Map{"unionid": re.UnionID, "user_id[!]": nil, "del_flag": 0}})
|
|||
|
if wechat != nil {
|
|||
|
wechat1 := that.Db.Get("wechat", "*", Map{"AND": Map{"openid": re.OpenID, "del_flag": 0}})
|
|||
|
//有该信息,但没有相关的用户信息,则直接绑定其他的信息上来
|
|||
|
if wechat1 != nil {
|
|||
|
|
|||
|
//更新用户信息
|
|||
|
that.Db.Update("wechat", wchat, Map{"id": wechat1.GetCeilInt("id")})
|
|||
|
|
|||
|
that.Session("wechat_id", wechat1.GetCeilInt("id"))
|
|||
|
that.Session("user_id", wechat.GetCeilInt("user_id"))
|
|||
|
//that.Display(0, "登录成功")
|
|||
|
userId = wechat.GetCeilInt("user_id")
|
|||
|
return
|
|||
|
}
|
|||
|
//没有相关用户信息,则新建wechat并完成登录
|
|||
|
wchat["user_id"] = wechat["user_id"]
|
|||
|
wchat["create_time[#]"] = "now()"
|
|||
|
|
|||
|
wchat["id"] = that.Db.Insert("wechat", wchat)
|
|||
|
if wchat.GetCeilInt("id") == 0 {
|
|||
|
|
|||
|
that.Display(5, "创建wechat失败")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
that.Session("wechat_id", wchat.GetCeilInt("id"))
|
|||
|
that.Session("user_id", wchat.GetCeilInt("user_id"))
|
|||
|
|
|||
|
userId = wchat.GetCeilInt("user_id")
|
|||
|
//that.Display(0, "登录成功")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
user := Map{
|
|||
|
|
|||
|
"create_time[#]": "now()",
|
|||
|
"modify_time[#]": "now()",
|
|||
|
"login_time[#]": "now()",
|
|||
|
"del_flag": 0,
|
|||
|
}
|
|||
|
user["id"] = that.Db.Insert("user", user)
|
|||
|
|
|||
|
wchat["user_id"] = user.GetCeilInt("id")
|
|||
|
wchat["create_time[#]"] = "now()"
|
|||
|
|
|||
|
wchat["id"] = that.Db.Insert("wechat", wchat)
|
|||
|
if wchat.GetCeilInt("id") == 0 {
|
|||
|
that.Display(5, "登录失败")
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
that.Session("wechat_id", wchat.GetCeilInt("id"))
|
|||
|
that.Session("user_id", wchat.GetCeilInt("user_id"))
|
|||
|
|
|||
|
userId = wchat.GetCeilInt("user_id")
|
|||
|
//that.Display(0, nil)
|
|||
|
},
|
|||
|
}
|