Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
def get_new_seller(rds, sellers):
    online_sellers = []
    for seller in sellers:
        status = Supporter.get_user_status(rds, seller['id'])
        seller['status'] = status
        if status == Supporter.STATUS_ONLINE:
            online_sellers.append(seller)

    if len(online_sellers) == 0:
        #假设第一个客服是管理员
        seller = sellers[0]
    else:
        index = random.randint(0, len(online_sellers) - 1)
        seller = sellers[index]
    return seller
Ejemplo n.º 5
0
def get_new_seller(rds, sellers):
    online_sellers = []
    for seller in sellers:
        status = Supporter.get_user_status(rds, seller['id'])
        seller['status'] = status
        if status == Supporter.STATUS_ONLINE:
            online_sellers.append(seller)

    if len(online_sellers) == 0:
        #假设第一个客服是管理员
        seller = sellers[0]
    else:
        index = random.randint(0, len(online_sellers) - 1)
        seller = sellers[index]
    return seller
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)