Example #1
0
def migu_change_pwd():
    """更改密码(GET|POST)

    :uri: /migu/change_password
    :param phone: 手机号
    :param old_pwd: 旧密码
    :param new_pwd: 新密码
    :returns: {}
    """
    user = request.authed_user
    params = request.values
    old_pwd = params.get('old_pwd', None)
    new_pwd = params.get('new_pwd', None)
    phone = params.get('phone', None) or (user and user.phone)

    if not old_pwd or not new_pwd or not phone:
        return error.InvalidArguments

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

    openid = Migu.get_identityid(phone, old_pwd, const.CENTER_ACCOUNT_PHONE)
    if isinstance(openid, error.ApiError):
        return openid

    ret = Migu.center_update_pwd(openid, old_pwd, new_pwd)
    if isinstance(ret, error.ApiError):
        return ret

    return {}
Example #2
0
def migu_register():
    """用户注册(GET|POST)

    :uri: /migu/register_phone
    :param phone: 手机号
    :param password: 密码
    :param code: 验证码
    :param sessionid: 短信sessionid
    :returns: {'user': object, 'ut': string}
    """
    params = request.values.to_dict()
    phone = params.get('phone', None)
    code = params.get('code', None)
    sessionid = params.get('sessionid', None)
    password = params.get('password', None)
    if not phone or not code or not password or not sessionid:
        return error.InvalidArguments

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

    # 用户中心注册
    ret = Migu.center_register(phone, password, const.CENTER_ACCOUNT_PHONE,
                               code, sessionid)
    if isinstance(ret, error.ApiError):
        return ret

    # 进行用户绑定
    migu_uid = Migu.get_identityid(phone, password, const.CENTER_ACCOUNT_PHONE)
    if isinstance(migu_uid, error.ApiError):
        return migu_uid

    user = User.get_platform_user('migu', migu_uid)
    if not user:
        user = User.get_by_phone(phone)
        if user:
            info = dict(partner_migu={'id': migu_uid},
                        nickname=u'咪咕用户%s%s' %
                        (migu_uid[-4:], random.randint(1000, 9999)),
                        gender=random.randint(1, 2),
                        name='$mg$%s%s' %
                        (migu_uid[-4:], random.randint(1000, 9999)))
            user = user.update_model({'$set': info})
        else:
            info = dict(phone=phone,
                        nickname=u'咪咕用户%s%s' %
                        (migu_uid[-4:], random.randint(1000, 9999)),
                        gender=random.randint(1, 2),
                        name='$mg$%s%s' %
                        (migu_uid[-4:], random.randint(1000, 9999)))
            user = User.create_platform_user('migu', migu_uid, data=info)
    else:
        # 如果用户没有绑定手机并且手机号没有被绑定, 则自动进行手机号绑定
        if not user.phone and not User.get_by_phone(phone):
            info = dict(phone=phone)
            user.update_model({'$set': info})

    ut = User.gen_token(str(user._id))
    return {'user': user.format(), 'ut': ut}
Example #3
0
def platform_login():
    """第三方平台token登录 (GET|POST)

    :uri: /platform/users/login
    :param platform: 平台标识{'csdk':付费SDK, 'weixin':微信, 'qq':QQ, 'migu':咪咕}
    :param token: 用户平台token
    :returns: {'user': object, 'ut': string}
    """
    params = request.values
    platform = params.get('platform', None)
    token = params.get("token", None)
    if not token or platform not in const.PARTNER:
        return error.InvalidArguments

    data = Migu.token_validate(token)
    if isinstance(data, error.ApiError):
        return data

    openid = data.get('msisdn', None)
    migu_uid = data.get('identityID', None)
    passid = data.get('passID', None)
    if isinstance(migu_uid, error.ApiError):
        return migu_uid

    if platform == 'migu':
        # 进行用户绑定
        user = User.get_platform_user('migu', migu_uid)
        if not user:
            user = User.get_by_phone(openid)
            if user:
                info = dict(partner_migu={
                    'id': migu_uid,
                    'passid': passid
                },
                            name='$mg$%s%s' %
                            (migu_uid[-4:], random.randint(1000, 9999)))
                user = user.update_model({'$set': info})
            else:
                info = dict(phone=openid,
                            nickname=u'咪咕用户%s%s' %
                            (migu_uid[-4:], random.randint(1000, 9999)),
                            name='$mg$%s%s' %
                            (migu_uid[-4:], random.randint(1000, 9999)),
                            partner_migu={
                                'id': migu_uid,
                                'passid': passid
                            })
                user = User.create_platform_user('migu', migu_uid, data=info)
        else:
            info = {}
            # 如果用户没有绑定手机并且手机号没有被绑定, 则自动进行手机号绑定
            if not user.phone and not User.get_by_phone(openid):
                info = dict(phone=openid)
            # 如果用户未绑定passid,则自动进行passid绑定
            if passid not in user.partner_migu:
                info.update(
                    dict(partner_migu={
                        'id': migu_uid,
                        'passid': passid
                    }))
            if info:
                user = user.update_model({'$set': info})

        # 同步咪咕用户名密码
        if user:
            User.change_pwd(user, token)
    else:
        # 判断平台用户是否已经用户系统中存在
        user = User.get_platform_user(platform, openid)
        first_login = not user

        info = {}
        if platform == 'csdk':
            info = ChargeSDK(token).get_open_info()
        elif platform == 'weixin':
            info = WeiXin(token, openid).get_open_info(first_login)
        elif platform == 'qq':
            info = QQ(token, openid).get_open_info(first_login)

        if not info:
            return error.LoginFailed

        # 如果平台用户在用户系统中不存在, 则创建, 通过平台ID(openid)进行关联
        if first_login:
            open_id = info.pop('openid')
            user = User.create_platform_user(platform, open_id, data=info)
            # 给咪咕平台发送请求进行咪咕账号注册并登录绑定
            try:
                if platform in ['qq', 'weixin'] and user:
                    password = '******' % (openid[-4:])
                    ret = Migu.center_register(open_id, password,
                                               const.CENTER_ACCOUNT_INDIV)
                    if not isinstance(ret, error.ApiError):
                        openid = Migu.get_identityid(
                            open_id, password, const.CENTER_ACCOUNT_INDIV)
                        if not isinstance(openid, error.ApiError):
                            if not User.get_platform_user('migu', openid):
                                info = {'partner_migu': {'id': openid}}
                                user.update_model({'$set': info})
            except:
                pass

    if not user:
        return error.LoginFailed

    ut = User.gen_token(str(user._id))
    return {'user': user.format(include_fields=['passid']), 'ut': ut}