Ejemplo n.º 1
0
    def do_login(self, data, userID):
        '''验证入参'''
        if ("userName" not in data.keys()):
            raise CustomError(-20005, "userName")
        if ("passWord" not in data.keys()):
            raise CustomError(-20005, "passWord")

        passWord = data['passWord'].encode('utf-8')
        userName = data['userName']
        '''获取会员信息'''
        pymysqlHandle = PymysqlHandle()
        result = pymysqlHandle.selectUserInfoWithUserName(userName)
        if result == None:
            raise CustomError(-10001)

        sqlpass = result["passWord"]
        '''验证会员密码'''
        if not bcrypt.checkpw(passWord, sqlpass.encode('utf-8')):
            raise CustomError(-10002)
        '''查询section'''

        section = pymysqlHandle.getSection(result["userID"])

        if section != None:
            nowtime = time.time()
            if nowtime - section["createtime"] > 24 * 3600:
                pymysqlHandle.deleteSection(result["userID"])
                section = None
            else:
                if data["UUID"] != section["UUID"] and "UUID" in data.keys():
                    raise CustomError(-10001)

        if section == None:
            '''创建section'''
            salt = ''.join(
                random.sample(string.ascii_letters + string.digits, 32))
            uuid = "0000"
            if "UUID" in data.keys():
                uuid = data["UUID"]

            data = {
                'userID': result["userID"],
                'validityPeriod': 24,
                'createtime': time.time(),
                'session': salt,
                'UUID': uuid
            }
            pymysqlHandle.insterSection(data)
        del result["passWord"]
        result["section"] = salt
        return result
Ejemplo n.º 2
0
    def do_wxLogin(self, data, userID):

        if "js_code" not in data.keys():
            raise CustomError(-20005, "js_code")
        if "nickName" not in data.keys():
            raise CustomError(-20005, "nickName")

        parm = {
            'appid': 'wx2d3eaf4304b22f25',
            'secret': '456d14753450c85718b290fa187bc369',
            'js_code': data["js_code"],
            'grant_type': 'authorization_code'
        }
        r = requests.post('https://api.weixin.qq.com/sns/jscode2session', parm)
        if r.status_code == 200:
            wxresult = PythonString.jsonPase(r.text)
            if 'errcode' not in wxresult.keys():

                pymysqlHandle = PymysqlHandle()
                result = pymysqlHandle.selectUserInfoBykey(
                    "wxOpenid", wxresult["openid"])
                data = {
                    'wxOpenid': wxresult['openid'],
                    'userName': data["nickName"],
                    'userID': result["userID"],
                    'headImage': data["headImg"]
                }
                if result == None:
                    pymysqlHandle.insetUser(data)
                    result = pymysqlHandle.selectUserInfoBykey(
                        "wxOpenid", wxresult["openid"])
                else:
                    pymysqlHandle.replaceUserInfo(data, result["userID"])

                userID = result["userID"]
                '''创建section'''

                section = pymysqlHandle.getSection(result["userID"])

                if section != None:
                    nowtime = time.time()
                    if nowtime - section["createtime"] > 24 * 3600:
                        pymysqlHandle.deleteSection(result["userID"])
                        section = None

                if section == None:
                    '''创建section'''
                    salt = ''.join(
                        random.sample(string.ascii_letters + string.digits,
                                      32))
                    createtime = time.time()
                    userID = result["userID"]

                    data = {
                        'userID': userID,
                        'validityPeriod': 24,
                        'createtime': createtime,
                        'session': salt,
                    }
                    pymysqlHandle.insterSection(data)
                    result["session"] = salt
                    return result

            else:
                raise CustomError(-30002)
        else:
            raise CustomError(-30001)