package app import ( . "code.hoteas.com/golang/hotime" . "code.hoteas.com/golang/hotime/common" "code.hoteas.com/golang/hotime/dri/wechat" ) 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, "nickname": userInfo.Nickname, "avatar": userInfo.HeadImgURL, "create_time[#]": "now()", "modify_time[#]": "now()", "del_flag": 0, "type": 1, } userId := 0 defer func() { //有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) + "," } that.Db.Update("user", Map{"parent_id": parentId, "index": index1 + ObjToStr(userId) + ","}, Map{"id": userId}) }() //最后验证服务商是否绑定 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 } //用户都有企服人员关联 if user.GetCeilInt("salesman_id") != 0 { return } //用户没有企服商id that.Db.Update("user", Map{"salesman_id": salesman.GetCeilInt64("id"), "provider_id": salesman.GetCeilInt64("provider_id")}, Map{"id": user.GetCeilInt64("id")}) 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")}) }() //如果有则直接返回用户信息到微信小程序里 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")}) 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) } }() 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"), "index": ",", "create_time[#]": "now()", "modify_time[#]": "now()", "login_time[#]": "now()", "del_flag": 0, } user["id"] = that.Db.Insert("user", user) //创建企业 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"]}) 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"), "index": ",", "create_time[#]": "now()", "modify_time[#]": "now()", "login_time[#]": "now()", "del_flag": 0, } user["id"] = that.Db.Insert("user", user) 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"]}) 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) }, }