Ejemplo n.º 1
0
def login():
    """用户登录 (GET|POST)

    :uri: /users/login
    :param name: 用户名
    :param password: 密码
    :param type: 登陆类型(name, phone,)
    :returns: {'user': object, 'ut': string}
    """
    params = request.values
    login_type = params.get('type', 'name')
    name = params.get("name", None)
    password = params.get("password", None)
    if name is None or password is None or login_type not in ['name', 'phone']:
        return error.InvalidArguments

    user = User.login(name, password, login_type=login_type)
    if not user:
        return error.LoginFailed

    # 初始化用户任务
    UserTask.create_and_init_user_tasks(str(user._id))

    token = User.gen_token(str(user._id))
    return {'user': user.format(), 'ut': token}
Ejemplo n.º 2
0
def refresh_token():
    """刷新用户token时间 (GET|POST&LOGIN)

    :uri: /users/refresh_token
    :returns: {'user': Object}
    """
    ut = request.values.get("ut", None)
    user = request.authed_user
    token = User.set_token(ut, str(user._id))
    if token:
        User.recommend_users(str(user._id))
        # 更新用户活跃时间
        user.update_model({'$set': {'update_at': time.time()}})
    # 初始化用户任务
    UserTask.create_and_init_user_tasks(str(user._id))

    return {'user': user.format(include_fields=['passid'])}
Ejemplo n.º 3
0
def register():
    """用户注册 (GET|POST)

    :uri: /users/register
    :param name: 用户名
    :param password: 密码
    :param nickname: 昵称
    :returns: {'user': object, 'ut': string}
    """
    params = request.values.to_dict()
    name = params.get("name", None)
    # delete password from data so that we don't save it to mongo
    password = str(params.pop("password", None))
    nickname = params.get('nickname', None)
    if not name or not password or not nickname:
        return error.InvalidArguments

    invalid_error = User.invalid_password(password)
    if invalid_error:
        return invalid_error

    invalid_error = User.invalid_nickname(nickname)
    if invalid_error:
        return invalid_error

    if User.get_by_name(name):
        return error.UserExists

    user = User.init()
    user.update(params)

    salt = os.urandom(const.PWD_HASH_LEN)
    pwd = User.gen_pwd_hash(password, salt)
    user._salt = Binary(salt)
    user._password = Binary(pwd)
    uid = user.create_model()
    new_user = User.get_one(uid)

    # 初始化用户任务
    UserTask.create_and_init_user_tasks(str(new_user._id))

    token = User.gen_token(str(uid))
    return {'user': new_user.format(), 'ut': token}
def do_task(worker, job):
    from wanx.base.log import print_log
    from wanx.base import const, util, jpush
    from wanx.models.user import User
    from wanx.models.game import Game
    from wanx.models.live import Live_Activity
    from wanx.models.activity import ActivityConfig
    from wanx.models.game import GameActivity
    from wanx.models.task import BEGIN_LIVE, UserTask
    from wanx.base.xredis import Redis
    from wanx.base.xmysql import MYDB
    from wanx.platforms.xmatch import Xmatch

    data = job.data
    # 记录日志
    print_log('create_live_activity', data)

    data = json.loads(data)
    # 记录日志
    print_log('create_live_activity',
              '%s ==========================> Start' % (data['event_id']))

    user = User.get_one(data['user_id'], check_online=False)
    if not user:
        return ''
    # 主播上线push推送
    push_content = u"您关注的主播 {0} 正在直播,点击围观".format(user.nickname)
    message_content = u"您关注的主播 {0} 正在直播,速速去围观吧!".format(user.nickname)
    an_link = "playsdk://video_live/{0}/".format(data['event_id'])
    ios_link = "playsdk://live/{0}".format(data['event_id'])
    jpush.jpush_schedule_create(data['event_id'], user, push_content,
                                message_content, an_link, ios_link)

    # 赛事预约直播上线推送
    battle = Xmatch.getbattle(user._id, data['name'])
    if battle:
        battle = battle['data']['battle']
        push_title = u'您预约的比赛正在进行'
        push_content = u"{0}  vs  {1}正在直播,点击围观".format(battle['team_1'],
                                                       battle['team_2'])
        message_content = u"您预约的比赛{0}  vs  {1}正在直播!".format(
            battle['team_1'], battle['team_2'])
        if battle.get('players', '') != "":
            push_content = message_content = battle['live_name']
        an_link = "playsdk://video_live/{0}/".format(data['event_id'])
        ios_link = "playsdk://live/{0}".format(data['event_id'])
        jpush.jpush_withtitle_create(data['event_id'], battle['_id'],
                                     push_title, push_content, message_content,
                                     an_link, ios_link)

    game = Game.get_one(data['game_id'], check_online=False)
    if not game:
        return ''

    aid = None
    aids = ActivityConfig.get_by_type(const.FROM_LIVE)
    for a in ActivityConfig.get_list(aids):
        gids = GameActivity.game_activity_ids(a['_id'])
        if gids and data['game_id'] not in gids:
            continue
        aid = a['_id']
        break

    if not aid:
        return ''
    print_log('create_live_activity',
              '%s ==========================> activity_id' % (aid))
    activity_live = Live_Activity.get_activity_live(str(user._id), aid)
    if not activity_live:
        key = 'lock:activity:live:%s:%s' % (str(user._id), aid)
        with util.Lockit(Redis, key) as locked:
            if locked:
                return ''
        activity_live = Live_Activity.init()
        activity_live.author = ObjectId(data['user_id'])
        activity_live.game = ObjectId(data['game_id'])
        activity_live.event_id = data['event_id']
        activity_live.activity_id = ObjectId(aid)
        activity_live.create_model()
        # 如果没有活动任务则创建
        UserTask.create_and_init_user_tasks(str(user._id))
    if not MYDB.is_closed():
        MYDB.close()
    try:
        UserTask.check_user_tasks(user._id, BEGIN_LIVE, 1, data['game_id'],
                                  aid)
    except Exception as e:
        print_log('create_live_activity', '%s ========' % e)
    if not MYDB.is_closed():
        MYDB.close()

    # 记录日志
    print_log('create_live_activity',
              '%s ==========================> Finished' % (data['event_id']))
    return ''