Esempio n. 1
0
def update_token(request):
    """
    更新token时间
    :param request:
    :return:
    """
    token = request.GET.get('token', None)
    ret = copy.copy(ret_obj)
    if token:
        value = getval(str(token))
        if value:
            setval(str(token), value)
            ret['_status'] = 'ok'
            ret['data'] = {'token': token, 'expire_in': TIMEOUT}
        else:
            ret['_status'] = 'error'
            ret['errormsg'] = 'Token已失效,请重新登录'
    else:
        ret['_status'] = 'error'
        ret['errormsg'] = '传入的Token值错误'
    return HttpResponse(json.dumps(ret))
Esempio n. 2
0
def update_token(request):
    """
    更新token时间
    :param request:
    :return:
    """
    token = request.GET.get("token", None)
    ret = copy.copy(ret_obj)
    if token:
        value = getval(str(token))
        if value:
            setval(str(token), value)
            ret["_status"] = "ok"
            ret["data"] = {"token": token, "expire_in": TIMEOUT}
        else:
            ret["_status"] = "error"
            ret["errormsg"] = "Token已失效,请重新登录"
    else:
        ret["_status"] = "error"
        ret["errormsg"] = "传入的Token值错误"
    return HttpResponse(json.dumps(ret))
Esempio n. 3
0
def third_login(request):
    """
    第三方帐号登录接口,接收openid、access_token、帐号类型(1:qq、2:微信、3:新浪微博)、头像、昵称(更新接口文档)
    :param request:
    :return:
    """
    openid = request.REQUEST.get('openid', None)
    access_token = request.REQUEST.get('access_token', None)
    account_type = request.REQUEST.get('account_type', 0)

    head_url = request.REQUEST.get('head_url', None)
    nick_name = request.REQUEST.get('nickname', None)
    ret = copy.copy(ret_obj)
    account_type = int(account_type)
    if account_type <= 0 or account_type > 3:
        ret['_status'] = 'error'
        ret['errormsg'] = '帐号类型错误'
        return HttpResponse(json.dumps(ret))
    if openid and openid != "null":
        try:
            _id = str(account_type) + '_' + openid
            doc = {
                # '_id': _id,
                'account': _id,
                'access_token': access_token,
                'password': '',
                'type': int(account_type),
                'head_url': head_url,
                'nickname': nick_name,
                'create_date': datetime.datetime.utcnow()
            }
            db = get_db()
            db.ebc_user.update({'_id': _id}, {'$set': doc}, upsert=True)

            guid = uuid.uuid1()
            token = TOKEN_PREFIX + str(guid)
            setval(token, _id)

            data = {
                'is_need_update': 0,
                'type': account_type,
                "token": token,
                'expire_in': TIMEOUT,
                'mosquitto_host': MOSQUITTO_HOST,
                'mosquitto_port': MOSQUITTO_PORT,
                'mosquitto_client_id': MOSQUITTO_PREFIX + str(guid),
                'mosquitto_sub_user': MOSQUITTO_SUB_USER,
                'mosquitto_sub_pwd': MOSQUITTO_SUB_PWD,
                'mosquitto_topic': MOSQUITTO_TOPIC_PREFIX + _id
            }
            ret['_status'] = 'ok'
            ret['data'] = data
            return HttpResponse(json.dumps(ret))
        except Exception as e:
            print(e)
            ret['_status'] = 'error'
            ret['errormsg'] = 'Token创建失败'
            return HttpResponse(json.dumps(ret))

    else:
        ret['_status'] = 'error'
        ret['errormsg'] = 'openid/uid为空'
        return HttpResponse(json.dumps(ret))
Esempio n. 4
0
def third_login(request):
    """
    第三方帐号登录接口,接收openid、access_token、帐号类型(1:qq、2:微信、3:新浪微博)、头像、昵称(更新接口文档)
    :param request:
    :return:
    """
    openid = request.REQUEST.get("openid", None)
    access_token = request.REQUEST.get("access_token", None)
    account_type = request.REQUEST.get("account_type", 0)

    head_url = request.REQUEST.get("head_url", None)
    nick_name = request.REQUEST.get("nickname", None)
    ret = copy.copy(ret_obj)
    account_type = int(account_type)
    if account_type <= 0 or account_type > 3:
        ret["_status"] = "error"
        ret["errormsg"] = "帐号类型错误"
        return HttpResponse(json.dumps(ret))
    if openid and openid != "null":
        try:
            _id = str(account_type) + "_" + openid
            doc = {
                # '_id': _id,
                "account": _id,
                "access_token": access_token,
                "password": "",
                "type": int(account_type),
                "head_url": head_url,
                "nickname": nick_name,
                "create_date": datetime.datetime.utcnow(),
            }
            db = get_db()
            db.ebc_user.update({"_id": _id}, {"$set": doc}, upsert=True)

            guid = uuid.uuid1()
            token = TOKEN_PREFIX + str(guid)
            setval(token, _id)

            data = {
                "is_need_update": 0,
                "type": account_type,
                "token": token,
                "expire_in": TIMEOUT,
                "mosquitto_host": MOSQUITTO_HOST,
                "mosquitto_port": MOSQUITTO_PORT,
                "mosquitto_client_id": MOSQUITTO_PREFIX + str(guid),
                "mosquitto_sub_user": MOSQUITTO_SUB_USER,
                "mosquitto_sub_pwd": MOSQUITTO_SUB_PWD,
                "mosquitto_topic": MOSQUITTO_TOPIC_PREFIX + _id,
            }
            ret["_status"] = "ok"
            ret["data"] = data
            return HttpResponse(json.dumps(ret))
        except Exception as e:
            print(e)
            ret["_status"] = "error"
            ret["errormsg"] = "Token创建失败"
            return HttpResponse(json.dumps(ret))

    else:
        ret["_status"] = "error"
        ret["errormsg"] = "openid/uid为空"
        return HttpResponse(json.dumps(ret))