def unbind_device_token(): rds = g.rds appid = request.appid uid = request.uid obj = request.get_json(force=True, silent=True, cache=False) if obj is None: logging.debug("json decode err:%s", e) raise ResponseMeta(400, "json decode error") device_token = obj.get("apns_device_token", "") pushkit_device_token = obj.get("pushkit_device_token", "") ng_device_token = obj.get("ng_device_token", "") xg_device_token = obj.get("xg_device_token", "") xm_device_token = obj.get("xm_device_token", "") hw_device_token = obj.get("hw_device_token", "") gcm_device_token = obj.get("gcm_device_token", "") jp_device_token = obj.get("jp_device_token", "") if not device_token and not pushkit_device_token \ and not ng_device_token and not xg_device_token \ and not xm_device_token and not hw_device_token \ and not gcm_device_token and not jp_device_token: raise ResponseMeta(400, "invalid param") User.reset_user_device_token(rds, appid, uid, device_token, pushkit_device_token, ng_device_token, xg_device_token, xm_device_token, hw_device_token, gcm_device_token, jp_device_token) return make_json_response({"success": True}, 200)
def unbind_device_token(): rds = g.rds appid = request.appid uid = request.uid obj = json.loads(request.data) device_token = obj.get("apns_device_token", "") pushkit_device_token = obj.get("pushkit_device_token", "") ng_device_token = obj.get("ng_device_token", "") xg_device_token = obj.get("xg_device_token", "") xm_device_token = obj.get("xm_device_token", "") hw_device_token = obj.get("hw_device_token", "") gcm_device_token = obj.get("gcm_device_token", "") jp_device_token = obj.get("jp_device_token", "") if not device_token and not pushkit_device_token \ and not ng_device_token and not xg_device_token \ and not xm_device_token and not hw_device_token \ and not gcm_device_token and not jp_device_token: raise ResponseMeta(400, "invalid param") User.reset_user_device_token(rds, appid, uid, device_token, pushkit_device_token, ng_device_token, xg_device_token, xm_device_token, hw_device_token, gcm_device_token, jp_device_token) return make_json_response({"success":True}, 200)
def unbind_device_token(): rds = g.imrds appid = config.APP_ID uid = request.uid obj = json.loads(request.data) device_token = obj["apns_device_token"] if obj.has_key( "apns_device_token") else "" ng_device_token = obj["ng_device_token"] if obj.has_key( "ng_device_token") else "" xg_device_token = obj["xg_device_token"] if obj.has_key( "xg_device_token") else "" xm_device_token = obj["xm_device_token"] if obj.has_key( "xm_device_token") else "" hw_device_token = obj["hw_device_token"] if obj.has_key( "hw_device_token") else "" gcm_device_token = obj["gcm_device_token"] if obj.has_key( "gcm_device_token") else "" jp_device_token = obj["jp_device_token"] if obj.has_key( "jp_device_token") else "" if not device_token and not ng_device_token and not xg_device_token \ and not xm_device_token and not hw_device_token \ and not gcm_device_token and not jp_device_token: raise ResponseMeta(400, "invalid param") User.reset_user_device_token(rds, appid, uid, device_token, ng_device_token, xg_device_token, xm_device_token, hw_device_token, gcm_device_token, jp_device_token) return make_json_response({"success": True}, 200)
def unregister(): uid = request.uid store_id = request.store_id obj = {} if request.data: obj = json.loads(request.data) device_token = obj["apns_device_token"] if obj.has_key( "apns_device_token") else "" ng_device_token = obj["ng_device_token"] if obj.has_key( "ng_device_token") else "" xg_device_token = obj["xg_device_token"] if obj.has_key( "xg_device_token") else "" xm_device_token = obj["xm_device_token"] if obj.has_key( "xm_device_token") else "" hw_device_token = obj["hw_device_token"] if obj.has_key( "hw_device_token") else "" gcm_device_token = obj["gcm_device_token"] if obj.has_key( "gcm_device_token") else "" jp_device_token = obj["jp_device_token"] if obj.has_key( "jp_device_token") else "" User.reset_user_device_token(g.imrds, config.APP_ID, uid, device_token, ng_device_token, xg_device_token, xm_device_token, hw_device_token, gcm_device_token, jp_device_token) Supporter.set_user_offline(g.imrds, uid) return make_json_response({"success": True}, 200)
def refresh_token(): if not request.data: return INVALID_PARAM() db = g._db obj = json.loads(request.data) refresh_token = obj["refresh_token"] rt = token.RefreshToken() if not rt.load(g.rds, refresh_token): return INVALID_REFRESH_TOKEN() seller = Seller.get_seller(db, rt.user_id) if not seller: return INVALID_REFRESH_TOKEN() access_token = login_gobelieve(int(rt.user_id), seller['name'], config.APP_ID, config.APP_SECRET) if not access_token: return CAN_NOT_GET_TOKEN() seller = Seller.get_seller(db, rt.user_id) tok = create_token(3600, False) tok["refresh_token"] = obj["refresh_token"] tok["access_token"] = access_token tok['uid'] = rt.user_id tok['store_id'] = seller['store_id'] tok['name'] = seller['name'] t = token.AccessToken(**tok) t.user_id = rt.user_id t.save(g.rds) return make_json_response(tok, 200)
def update_user(uid): obj = json.loads(request.data) if obj.has_key('status'): if obj['status'] == 'online': Supporter.set_user_online(g.imrds, uid) elif obj['status'] == 'offline': Supporter.set_user_offline(g.imrds, uid) else: raise ResponseMeta(400, 'invalid status') return make_json_response({"success":True}, 200)
def update_user(uid): obj = json.loads(request.data) if obj.has_key('status'): if obj['status'] == 'online': Supporter.set_user_online(g.imrds, uid) elif obj['status'] == 'offline': Supporter.set_user_offline(g.imrds, uid) else: raise ResponseMeta(400, 'invalid status') return make_json_response({"success": True}, 200)
def get_one_supporter(): rds = g.rds db = g._db appid = request.appid uid = request.uid store_id = request.args.get('store_id', 0) store_id = int(store_id) if not store_id: raise ResponseMeta(400, 'require store_id param') sellers = Seller.get_sellers(db, store_id) if not sellers: raise ResponseMeta(400, 'store no supporter') seller = None #获取上次对话的客服id last_store_id, last_seller_id = User.get_seller(rds, appid, uid) if store_id == last_store_id and last_seller_id > 0: for s in sellers: if s['id'] == last_seller_id: status = Supporter.get_user_status(rds, s['id']) s['status'] = status seller = s break if not seller: seller = get_new_seller(rds, sellers) User.set_seller(rds, appid, uid, store_id, seller['id']) name = "" if seller.has_key('name') and seller['name']: name = seller['name'].split('@')[0] resp = { "seller_id":seller['id'], "name":name, "status":seller["status"] } return make_json_response({"data":resp} , 200)
def get_one_supporter(): rds = g.rds db = g._db appid = request.appid uid = request.uid store_id = request.args.get('store_id', 0) store_id = int(store_id) if not store_id: raise ResponseMeta(400, 'require store_id param') sellers = Seller.get_sellers(db, store_id) if not sellers: raise ResponseMeta(400, 'store no supporter') seller = None #获取上次对话的客服id last_store_id, last_seller_id = User.get_seller(rds, appid, uid) if store_id == last_store_id and last_seller_id > 0: for s in sellers: if s['id'] == last_seller_id: status = Supporter.get_user_status(rds, s['id']) s['status'] = status seller = s break if not seller: seller = get_new_seller(rds, sellers) User.set_seller(rds, appid, uid, store_id, seller['id']) name = "" if seller.has_key('name') and seller['name']: name = seller['name'].split('@')[0] resp = { "seller_id": seller['id'], "name": name, "status": seller["status"] } return make_json_response({"data": resp}, 200)
def unbind_device_token(): rds = g.rds appid = request.appid uid = request.uid obj = json.loads(request.data) device_token = obj["apns_device_token"] if obj.has_key("apns_device_token") else "" ng_device_token = obj["ng_device_token"] if obj.has_key("ng_device_token") else "" xg_device_token = obj["xg_device_token"] if obj.has_key("xg_device_token") else "" xm_device_token = obj["xm_device_token"] if obj.has_key("xm_device_token") else "" hw_device_token = obj["hw_device_token"] if obj.has_key("hw_device_token") else "" gcm_device_token = obj["gcm_device_token"] if obj.has_key("gcm_device_token") else "" jp_device_token = obj["jp_device_token"] if obj.has_key("jp_device_token") else "" if not device_token and not ng_device_token and not xg_device_token \ and not xm_device_token and not hw_device_token \ and not gcm_device_token and not jp_device_token: raise ResponseMeta(400, "invalid param") User.reset_user_device_token(rds, appid, uid, device_token, ng_device_token, xg_device_token, xm_device_token, hw_device_token, gcm_device_token, jp_device_token) return make_json_response({"success":True}, 200)
def access_token(): if not request.data: return INVALID_PARAM() obj = json.loads(request.data) username = obj["username"] password = obj["password"] platform = obj.get('platform', 0) device_id = obj.get('device_id', '') if not username or not password: return INVALID_PARAM() db = g._db rds = g.rds uid = None store_id = None try: seller_id = int(username) except ValueError: seller_id = 0 if seller_id: seller = Seller.get_seller(db, seller_id) else: seller = Seller.get_seller_with_number(db, username) if check_seller_password(seller, password): uid = seller['id'] store_id = seller['store_id'] if not uid: return INVALID_USER() access_token = login_gobelieve(uid, seller['name'], config.APP_ID, config.APP_SECRET, device_id, platform) if not access_token: return CAN_NOT_GET_TOKEN() tok = create_token(3600, True) tok['uid'] = uid tok['store_id'] = store_id tok['access_token'] = access_token tok['name'] = seller['name'] tok['status'] = 'online' t = token.AccessToken(**tok) t.save(rds) t = token.RefreshToken(**tok) t.save(rds) #用户上线 Supporter.set_user_online(rds, uid) now = int(time.time()) obj = { "timestamp": now, "device_name": obj.get("device_name", ""), "device_id": obj.get("device_id", ""), "platform": obj.get("platform", 0) } PLATFORM_WEB = 3 PLATFORM_WIN32 = 4 PLATFORM_DARWIN = 5 PLATFORM_LINUX = 6 if platform >= 3: content = json.dumps({"login_pc": obj}) else: content = json.dumps({"login": obj}) send_sys_message(uid, content, config.APP_ID, config.APP_SECRET) return make_json_response(tok, 200)
def CAN_NOT_GET_TOKEN(): e = {"error": "获取imsdk token失败"} logging.warn("获取imsdk token失败") return make_json_response(e, 400)
def INVALID_REFRESH_TOKEN(): e = {"error": "非法的refresh token"} logging.warn("非法的refresh token") return make_json_response(e, 400)
def INVALID_USER(): e = {"error": "非法的用户名或密码"} logging.warn("非法的用户名或密码") return make_json_response(e, 400)
def INVALID_PARAM(): e = {"error": "非法输入"} logging.warn("非法输入") return make_json_response(e, 400)