//@Mapper("/notify", methods=["POST", "GET"]) func (h *WebHandler) Notify(to uint32, msg string, pushContent string) peony.Renderer { if len(msg) == 0 || to == 0 { return peony.RenderJson(map[string]interface{}{"code": -1, "msg": "invalid parameters."}) } now := time.Now() raw := make(map[string]interface{}) if err := json.Unmarshal([]byte(msg), &raw); err != nil { ERROR.Println(err) return peony.RenderJson(map[string]interface{}{"code": -1, "msg": "message must be json"}) } message := &Msg{From: 0, MsgId: "nil", Type: NotifyMsgType, Raw: raw, Time: now.UnixNano() / 1000000, To: &to} if len(pushContent) > 0 { message.Content = &pushContent } if err := sendMsg(message, pmsg.RouteMsgType); err != nil { return peony.RenderJson(map[string]interface{}{"code": -1, "msg": err.Error()}) } return peony.RenderJson(map[string]interface{}{"code": 0}) }
//@Mapper("/offline/msg_rows", methods=["POST", "GET"]) func (oc *OfflineController) MsgRows(id string) peony.Renderer { var rs = map[string]interface{}{} if id == "" { rs["code"] = -1 rs["error"] = "invaild parameter." return peony.RenderJson(rs) } conn := offlineRedisPool.Get() defer conn.Close() n, err := redis.Int(conn.Do("msg_rows", id)) if err != nil { rs["code"] = -1 rs["error"] = err.Error() return peony.RenderJson(rs) } rs["code"] = 0 rs["total"] = n return peony.RenderJson(rs) }
//@Mapper("/user/add", methods=["POST", "GET"]) func (uc *UserController) Add(name, password string) peony.Renderer { var user = chat.UserInfo{Name: name, Password: password} var err error var rs = map[string]interface{}{} var seq uint32 if len(name) == 0 || len(password) == 0 { rs["code"] = -1 rs["msg"] = "parameter error." return peony.RenderJson(rs) } if seq, err = chat.AddUser(&user); err != nil { rs["code"] = -1 rs["msg"] = err.Error() } else { rs["code"] = 0 rs["userId"] = seq } return peony.RenderJson(rs) }
//@Mapper("/user/login", methods=["POST", "GET"]) func (uc *UserController) Login(name, password string) peony.Renderer { var rs = map[string]interface{}{} if len(name) == 0 || len(password) == 0 { rs["code"] = -1 rs["msg"] = "parameter error." return peony.RenderJson(rs) } rs["code"] = 0 ui, err := chat.GetUserByName(name) if err != nil { rs["code"] = -1 rs["msg"] = err.Error() } else { rs["name"] = ui.Name rs["id"] = ui.Id } return peony.RenderJson(rs) }
//@Mapper("/offline/msg_front", methods=["POST", "GET"]) func (oc *OfflineController) MsgFront(id string) peony.Renderer { var rs = map[string]interface{}{} if id == "" { rs["code"] = -1 rs["error"] = "invaild parameter." return peony.RenderJson(rs) } conn := offlineRedisPool.Get() defer conn.Close() var err error var val []byte m, e := conn.Do("msg_front", id) val, err = redis.Bytes(m, e) if err != nil && err != redis.ErrNil { rs["code"] = -1 rs["error"] = err.Error() return peony.RenderJson(rs) } return &peony.TextRenderer{TextSlice: getContent(val), ContentType: "application/json"} }
//@Mapper("/group/event", methods=["POST", "GET"]) func (h *WebHandler) GroupEvent(from, to uint32, gid uint64, members, name string, event int) peony.Renderer { if from == 0 || gid == 0 { return peony.RenderJson(invalidParam) } var msgType byte = GroupAddMsgType if event == 1 { msgType = GroupMemberDelMsgType } else if event == 2 { msgType = GroupRenameMsgType } else if event == 3 { msgType = GroupRemoveMsgType } message := &Msg{ From: from, MsgId: "nil", Type: msgType, Gid: &gid, SourceType: 3, } if msgType == GroupAddMsgType || msgType == GroupMemberDelMsgType || msgType == GroupRemoveMsgType { if len(members) == 0 { return peony.RenderJson(invalidParam) } if msgType == GroupAddMsgType && len(name) != 0 { message.Name = &name } mSli := strings.Split(members, ",") memberSli := make([]uint32, 0, len(mSli)) for _, m := range mSli { if i, err := strconv.ParseUint(m, 10, 0); err != nil { return peony.RenderJson(map[string]interface{}{"code": -1, "msg": "invalid members."}) } else { memberSli = append(memberSli, uint32(i)) } } message.Members = &memberSli } else { if len(name) == 0 { return peony.RenderJson(invalidParam) } message.Name = &name } var err error if msgType == GroupRemoveMsgType && to != 0 { message.To = &to err = sendMsg(message, pmsg.RouteMsgType) } else { err = sendGroupMsg(message, pmsg.RouteMsgType) } if err != nil { return peony.RenderJson(map[string]interface{}{"code": -1, "msg": err.Error()}) } return peony.RenderJson(map[string]interface{}{"code": 0}) }
//@Mapper("/admin/vendor/exists") func (v *Vendor) Exists(vendor *model.Vendor) peony.Renderer { has, _ := engine.Get(vendor) return peony.RenderJson(!has) }
//@Mapper("/admin/nas/exists") func (v *Nas) Exists(nas *model.Nas) peony.Renderer { has, _ := engine.Get(nas) return peony.RenderJson(!has) }
//@Mapper("/admin/attr/exists") func (v *Attr) Exists(attr *model.Attr) peony.Renderer { has, _ := engine.Get(attr) return peony.RenderJson(!has) }
//@Mapper("/admin/init") func Init(info string) peony.Renderer { InitDB() return peony.RenderJson(true) }
//@Mapper("/admin/user/exists") func (u *User) Exists(user *model.User) peony.Renderer { has, _ := engine.Get(user) return peony.RenderJson(!has) }