Esempio n. 1
0
def add_loan():
    """新增并审核项目"""
    add_data = {"member_id": config.get("user", 'member_id'), "title": "小荔枝",
            "amount": 3000, "loan_rate": 3.2, "loan_term": 1, "loan_date_type": 1, "bidding_days": 1}
    headers = eval(config.get('api', 'headers'))
    token_info = login_1(session=request)
    token = token_info['token_type'] + ' ' + token_info['token']
    headers['Authorization'] = token
    add_res = request.get_json(method='post',
                               url=config.get('api', 'base_url') + config.get('api', 'add_url'),
                               json=add_data,
                               headers=headers)
    if add_res['code'] == 0:
        log.info('项目新增成功')
        """项目新增成功,审核项目"""
        admin = config.get('admin', 'mobile_phone')
        pwd = config.get('admin', 'password')
        is_register(admin, pwd, typ=0)
        audit_data = {"loan_id": add_res['data']['id'], "approved_or_not": "true"}
        token_info = login(session=request, phone=admin, pwd=pwd)
        headers['Authorization'] = token_info['token_type'] + ' ' + token_info['token']
        audit_res = request.get_json(method='PATCH',
                                     url=config.get('api', 'base_url') + config.get('api', 'audit_url'),
                                     json=audit_data,
                                     headers=headers)
        if audit_res['code'] == 0:
            log.info(f"{add_res['data']['id']}项目审核成功")
            return None
        log.error(f"{add_res['data']['id']}项目审核失败")
        return None
    log.error('新增项目失败')
    return None
def invest_json_data(data):
    if isinstance(data, dict):
        member_id = eval(config.get('user', 'member_id'))
        if data['member_id'] == 'id':
            data['member_id'] = member_id
        elif data['member_id'] == 'other_id':
            data['member_id'] = member_id - 1

        if data['loan_id'] == 'id':
            loan_info = get_loan_info(my=False)
            data['loan_id'] = loan_info['id']
        elif data['loan_id'] == 'my_loan':
            loan_info = get_loan_info()
            data['loan_id'] = loan_info['id']
        elif data['loan_id'] == 'not_start':
            loan_info = get_loan_info(my=False, status=1)
            data['loan_id'] = loan_info['id']
        elif data['loan_id'] == 'full':
            loan_info = get_loan_info(my=False, status=3)
            data['loan_id'] = loan_info['id']

        if data['amount'] == 'over_loan':
            """超过项目可投余额"""
            loan_amount = get_invest_amount(loan_id=data['loan_id'])
            data['amount'] = float(loan_amount) + 100
        elif data['amount'] == 'over_leave':
            """超过账户余额"""
            loan_amount = get_invest_amount(loan_id=data['loan_id'])
            leave_amount = get_leave_amount(data['member_id'])
            if loan_amount <= leave_amount:
                change_amount(member_id, amount=(float(loan_amount)-100))
            data['amount'] = float(loan_amount)
    else:
        log.error('传入的数据不是字典类型!')
    return data
 def request(self, method, url, **kwargs):
     """其他请求"""
     try:
         response = requests.request(method=method, url=url, **kwargs)
     except Exception as e:
         log.error(e)
     else:
         return response
 def post(self, url, data=None, json=None, **kwargs):
     """post请求"""
     try:
         response = requests.post(url=url, data=data, json=json, **kwargs)
     except Exception as e:
         log.error(e)
     else:
         return response
 def get(self, url, params=None, **kwargs):
     """get请求"""
     try:
         response = requests.get(url=url, params=params, **kwargs)
     except Exception as e:
         log.error(e)
     else:
         return response
 def get_json(self, method, url, **kwargs):
     """访问接口,获取json内容"""
     try:
         res = self.request(method, url, **kwargs)
         json_dic = res.json()
     except Exception as e:
         log.error(e)
     else:
         return json_dic
Esempio n. 7
0
 def execute(self, query, args=None):
     """执行更新操作的sql语句"""
     try:
         # 执行语句
         self.cursor.execute(query=query, args=args)
         # 提交
         self.connect.commit()
     except Exception as e:
         # 若发送异常,则回滚
         self.connect.rollback()
         log.error(e)
Esempio n. 8
0
 def fetchmany(self, query, args=None, size=10):
     """获取部分查询结果"""
     try:
         self.connect.commit()
         self.cursor.execute(query=query, args=args)
         res = self.cursor.fetchmany(size)
     except Exception as e:
         self.connect.rollback()
         log.error(e)
     else:
         return res
Esempio n. 9
0
 def fetchall(self, query, args=None):
     """获取查询结果的所有数据"""
     try:
         self.connect.commit()
         self.cursor.execute(query=query, args=args)
         res = self.cursor.fetchall()
     except Exception as e:
         self.connect.rollback()
         log.error(e)
     else:
         return res
Esempio n. 10
0
 def open(self):
     """加载工作簿,获取表单"""
     try:
         self.wb = load_workbook(filename=self.filename)
         if isinstance(self.sheet_name, str):
             self.sheet = self.wb[self.sheet_name]
         else:
             self.sheet = self.wb.worksheets[self.sheet_name]
     except Exception as e:
         log.error(e)
         raise e
Esempio n. 11
0
 def fetchone(self, query, args=None):
     """获取1条数据"""
     """
     sql     type:string  "select * from member where id=%s or mobile_phone=%s"
     args    type:list or tuple
             type: dict    %s ==>  %(name)s
     """
     try:
         self.connect.commit()   # 同步数据
         # 执行SQL语句
         self.cursor.execute(query=query, args=args)
         # 获取结果
         res = self.cursor.fetchone()
     except Exception as e:
         self.connect.rollback()     # 回滚
         log.error(e)
     else:
         return res
Esempio n. 12
0
def get_invest_info(invest_id=None, loan_id=None):
    """查询投资记录"""
    db = MySQLHandler()
    if invest_id is not None:
        """根据投资记录id查询投资信息"""
        sql = 'select * from invest where id=%s;'
        invest_info = db.fetchone(query=sql, args=[invest_id, ])
        db.close()
        return invest_info
    elif loan_id is not None:
        """项目项目id查询 已投金额"""
        sql = 'select amount from invest where loan_id=%s;'
        invested_amount = db.fetchall(query=sql, args=[loan_id, ])
        db.close()
        return invested_amount
    db.close()
    log.error('未传入查询参数!')
    return None
Esempio n. 13
0
def login_1(session):
    """登录"""
    mobile_phone = config.get('user', 'mobile_phone')
    password = config.get('user', 'password')

    login_url = config.get('api', 'base_url') + config.get('api', 'login_url')
    headers = eval(config.get('api', 'headers'))
    login_data = {"mobile_phone": mobile_phone, "pwd": password}
    try:
        login_json = session.get_json(method='post', url=login_url, json=login_data, headers=headers)
    except Exception as e:
        log.error(e)
    else:
        if login_json['code'] == 0:
            log.info(f'用户{[mobile_phone]}登录成功!')
            return login_json['data']['token_info']
        else:
            log.info(f'用户{[mobile_phone]}登录失败!')
            return None