package app import ( . "code.hoteas.com/golang/hotime" . "code.hoteas.com/golang/hotime/common" "code.hoteas.com/golang/hotime/dri/download" "fmt" "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/miniprogram/config" "time" //"fmt" "github.com/silenceper/wechat/v2" //"github.com/silenceper/wechat/v2/cache" ) var Wechat = Ctr{ "login": func(that *Context) { nickname := that.Req.FormValue("nickname") avatar := that.Req.FormValue("avatarUrl") encryptedData := that.Req.FormValue("encryptedData") iv := that.Req.FormValue("iv") if that.Session("wechat_id").Data == nil { that.Display(2, "还没有获取token") return } if nickname == "" || avatar == "" || encryptedData == "" || iv == "" { that.Display(3, "参数不足") return } if weixin == nil { weixin = wechat.NewWechat() memory := cache.NewMemory() cfg := &config.Config{ AppID: that.Config.GetString("wechatAppID"), AppSecret: that.Config.GetString("wechatAppSecret"), //Token: "xxx", //EncodingAESKey: "xxxx", Cache: memory, } miniProgram = weixin.GetMiniProgram(cfg) } wechatId := that.Session("wechat_id").ToCeilInt() wchat := that.Db.Get("wechat", "*", Map{"id": wechatId}) if wchat == nil { that.Display(4, "找不到该用户") return } eny := miniProgram.GetEncryptor() re, e := eny.Decrypt(wchat.GetString("retoken"), encryptedData, iv) if e != nil { that.Display(4, e) return } admin := that.Db.Get("admin", "*", Map{"phone": re.PhoneNumber}) if admin == nil { that.Display(5, "你不是本系统用户,无法使用本系统") return } t := time.Now().Unix() path := time.Now().Format(that.Config.GetString("imgPath")) filename := Md5(ObjToStr(t)) + ".jpg" down := download.Down(avatar, that.Config.GetString("tpt")+"/"+path, filename) if down { avatar = path + filename } that.Db.Update("wechat", Map{"admin_id": admin.GetCeilInt("id"), "modify_time": t, "avatar": avatar, "nickname": nickname}, Map{"id": wchat.GetCeilInt64("id")}) that.Db.Update("admin", Map{"avatar_img": avatar, "modify_time": t}, Map{"id": admin.GetCeilInt64("id")}) that.Session("admin_id", admin.GetCeilInt64("id")) that.Display(0, "成功!") }, "code": func(that *Context) { that.Session("admin_id", 1) that.Display(0, Map{"status": 1, "token": that.SessionId}) return code := that.Req.FormValue("code") if code == "" { that.Display(3, "缺少code") return } if weixin == nil { weixin = wechat.NewWechat() memory := cache.NewMemory() cfg := &config.Config{ AppID: that.Config.GetString("wechatAppID"), AppSecret: that.Config.GetString("wechatAppSecret"), //Token: "xxx", //EncodingAESKey: "xxxx", Cache: memory, } miniProgram = weixin.GetMiniProgram(cfg) } //mini := weixin.GetMiniProgram(cfg) a := miniProgram.GetAuth() re, e := a.Code2Session(code) fmt.Println(re) if e != nil { that.Display(4, e) return } wchat := that.Db.Get("wechat", "*", Map{"openid": re.OpenID}) if wchat == nil { wchat = Map{"openid": re.OpenID, "appid": a.AppID, "state": 0, "retoken": re.SessionKey, "modify_time": time.Now().Unix(), "create_time": time.Now().Unix(), "unionid": re.UnionID, } wchat["id"] = that.Db.Insert("wechat", wchat) } else { wchat["modify_time"] = time.Now().Unix() wchat["retoken"] = re.SessionKey wchat["unionid"] = re.UnionID that.Db.Update("wechat", wchat, Map{"id": wchat.GetCeilInt("id")}) } that.Session("wechat_id", wchat.GetCeilInt("id")) if wchat.GetCeilInt("admin_id") == 0 { that.Display(0, Map{"status": 0, "token": that.SessionId}) return } that.Session("admin_id", wchat.GetCeilInt("admin_id")) that.Display(0, Map{"status": 1, "token": that.SessionId}) return }, ////微信注册,0已经完整的注册了,1还没有注册 //"codebase": func(that *Context) { // wx := Weixin(that) // auth := wx.GetOauth() // resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code")) // if err != nil { // that.Display(6, "code错误") // return // } // // //判断用户是否已经注册 // user := that.Db.Get("wechat", Map{"[><]user": "wechat.user_id=user.id"}, "user.id,user.state", Map{"openid": resToken.OpenID}) // if user != nil { // that.Session("user_id", user.Get("id")) // that.Display(0, 0) // return // } // // user_id := that.Db.Insert("user", Map{"time": time.Now().Unix(), "state": 2}) // if user_id == 0 { // that.Display(4, "创建用户失败") // return // } // // wid := that.Db.Insert("wechat", Map{"openid": resToken.OpenID, "appid": that.Config.GetString("wechatAppID"), "state": 1, "user_id": user_id}) // if wid == 0 { // that.Display(4, "关联微信失败!") // return // } // // that.Session("user_id", user.Get("id")) // // that.Display(0, 1) //}, ////微信注册,0已经完整的注册了,1还没有注册 //"code": func(that *Context) { // // orgId := ObjToInt(that.Req.FormValue("org_id")) // //if orgId==0{ // // that.Display(3, "缺少组织id") // // return // //} // // wx := Weixin(that) // auth := wx.GetOauth() // resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code")) // if err != nil { // that.Display(5, "code错误") // return // } // // //判断用户是否已经注册 // user := that.Db.Get("wechat", Map{"[><]user": "wechat.user_id=user.id"}, "user.id,user.state", Map{"openid": resToken.OpenID}) // if user != nil && user.GetCeilInt("id") != 0 && user.GetCeilInt("state") == 0 { // that.Session("user_id", user.Get("id")) // that.Display(0, Map{"type": 0, "token": that.SessionId}) // return // } // //getUserInfo // userInfo, err := auth.GetUserInfo(resToken.AccessToken, resToken.OpenID) // if err != nil { // that.Display(6, "微信个人信息无法获取") // return // } // // //wechatInfo := ObjToMap(userInfo) // t := time.Now().Unix() // wechatInfo := Map{ // "openid": userInfo.OpenID, // "acttoken": resToken.AccessToken, // "retoken": resToken.RefreshToken, // "appid": that.Config.GetString("wechatAppID"), // "unionid": userInfo.Unionid, // "nickname": userInfo.Nickname, // "avatar": userInfo.HeadImgURL, // } // if orgId != 0 { // wechatInfo["org_id"] = orgId // wechatInfo["status"] = 0 // } // // wechatDb := that.Db.Get("wechat", "*", Map{"openid": wechatInfo.GetString("openid")}) // // if wechatDb != nil { // // that.Db.Update("wechat", wechatInfo, Map{"id": wechatDb.GetCeilInt("id")}) // //userInfo["wid"]=wechatDb.GetCeilInt("wid") // } else { // wechatInfo["create_time"] = t // wechatInfo["id"] = that.Db.Insert("wechat", wechatInfo) // } // wechatDb = that.Db.Get("wechat", "*", Map{"openid": wechatInfo.GetString("openid")}) // // that.Session("wechatInfo", wechatDb) // fmt.Println(wechatDb) // fmt.Println(that.Session("wechatInfo")) // //that.Display(0, 1) // that.Display(0, Map{"type": 1, "token": that.SessionId}) //}, //网页签名 //"sign": func(that *Context) { // // if that.Req.FormValue("sign_url") == "" { // that.Display(2, "参数不足") // return // } // signUrl := that.Req.FormValue("sign_url") // signUrl = "https://hycb.hoteas.com/wxapp" // // js := officialAccount.GetJs() // //js.GetConfig() // //js := weixin.GetJs() // cfg1, e := js.GetConfig(signUrl) // if e != nil { // that.Display(7, e) // return // } // // sign := Map{ // "appId": cfg1.AppID, // "timestamp": cfg1.Timestamp, // "nonceStr": cfg1.NonceStr, // "signature": cfg1.Signature, // } // // that.Display(0, sign) // //}, }