Esempio n. 1
0
def get_agent_info():
    dbg('get_agent_info')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        agent_id = data['agent_id']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    reply['data'] = {
        'id': agent.id,
        'salesman': agent.salesman,
        'level': agent.level,
        'slevel': agent.slevel,
        'name': agent.name,
        'phone': agent.phone,
        'email': agent.email,
        'desc': agent.desc,
        'address': agent.address,
        'remark': agent.remark,
        'expandable': agent.expandable,
        'withdrawable': agent.withdrawable
    }
    return make_response(jsonify(reply), status_code)
Esempio n. 2
0
def get_god_info_common(agent):
    dbg('get_god_info_common')
    God = dbapi.God
    if 'God' in str(agent):
        god = agent
        god_id = god.id
    else:
        # 1. 先查agent_info是否存在
        agent_info = dbapi.get_agent_info(agent_id=agent.id)
        if agent_info:
            return agent_info

        # 2. 若不存在,则查god_info
        while agent.hook_agent_id != 0:
            agent = dbapi.get_agent(id=agent.hook_agent_id)
        god_agent = dbapi.get_god_agent(agent_id=agent.id)
        god = dbapi.get_god(id=god_agent.god_id)
        god_id = god.id

    god_info = dbapi.get_god_info(god_id=god_id)
    if not god_info:
        god_info = dbapi.make_new_god_info(god_id)
        dbapi.db.session.commit()

    return god_info
Esempio n. 3
0
def get_sub_agent():
    dbg('get_sub_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)

    agent_id = current_user.agent_id
    count, agents = dbapi.get_agent(hook_agent_id=agent_id,
                                    salesman=0,
                                    page=page,
                                    psize=psize)

    data = []
    if count:
        for agent in agents:
            info = {
                'id': agent.id,
                'name': agent.name,
                'level': agent.level,
                'phone': agent.phone,
                'address': agent.address,
                'expandable': agent.expandable,
                'withdrawable': agent.withdrawable,
                'remark': agent.remark
            }
            data.append(info)

    reply['data'] = {'count': count, 'agents': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 4
0
def update_agent():
    dbg('update_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
        agent_id = data['agent_id']
        update = data['update']
        keys = ('name', 'desc', 'address', 'remark', 'expandable',
                'withdrawable')
        for k in update:
            assert (k in keys)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    cur_agent_id = current_user.agent_id
    print(agent_id, cur_agent_id, agent.hook_agent_id)
    if cur_agent_id not in (agent_id, agent.hook_agent_id):
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    agent = dbapi.update_agent(agent, **update)
    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 5
0
def get_agent_setting():
    dbg('get_agent_setting')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        target_agent_id = data['target_agent_id']
    except:
        print_exception_info()
        raise ApiError('update agent setting error', error.ERROR_PARAM)

    agent_id = current_user.agent_id
    target_agent = dbapi.get_agent(id=target_agent_id)
    if not target_agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    agent_setting = dbapi.get_agent_setting(agent_id=target_agent_id)
    reply['data'] = {
        'min_withdraw': agent_setting.min_withdraw,
        'withdraw_fee': agent_setting.withdraw_fee,
        'wallet_pay_enable': agent_setting.wallet_pay_enable,
        'trans_url': agent_setting.trans_url
    }

    return make_response(jsonify(reply), status_code)
Esempio n. 6
0
def add_advertiser():
    dbg('add_advertiser')
    reply ,status_code = {'code': 0, 'msg': ''}, 200
    try:
        data    = request.get_json()
        name    = data['name']
        email   = data['email']
        phone   = data['phone']
        address = data['address']
        desc    = data['desc']
        remark  = data['remark']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    target_advertiser = dbapi.get_advertiser(phone=phone)
    if target_advertiser:
        raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

    agent = dbapi.get_agent(phone=phone)
    if agent:
        raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

    openluat_user = dbapi.get_openluat_user(phone=phone)
    if not openluat_user:
        openluat_user = dbapi.make_new_openluat_user(name, email, phone)
        db.session.commit()

    agent_id = current_user.agent_id
    hook_agent_id = agent_id
    new_agent = dbapi.make_new_advertiser(openluat_user.id, hook_agent_id,
        name, phone, email, desc, address, remark)
    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 7
0
def get_cur_agent():
    dbg('get_cur_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('get_cur_agent error', error.ERROR_AGENT_NOT_FOUND)

    joinup = check_joinup_available(agent_id)

    reply['data'] = {
        'id': agent.id,
        'salesman': agent.salesman,
        'level': agent.level,
        'slevel': agent.slevel,
        'expandable': agent.expandable,
        'name': agent.name,
        'phone': agent.phone,
        'email': agent.email,
        'desc': agent.desc,
        'address': agent.address,
        'remark': agent.remark,
        'joinup': joinup
    }
    return make_response(jsonify(reply), status_code)
Esempio n. 8
0
 def wrapper(*args, **kws):
     agent = dbapi.get_agent(user_id=current_user.id)
     if not agent:
         raise ApiError('ERROR_AGENT_NOT_FOUND',
                        error.ERROR_AGENT_NOT_FOUND)
     current_user.agent_id = agent.id
     current_user.cur_agent = agent
     return func(*args, **kws)
Esempio n. 9
0
def add_device():
    dbg('add_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
        imei = data['imei']
        cat = data['cat']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    if len(imei) != 15:
        raise ApiError('ERROR_INVALIDATE_IMEI', error.ERROR_INVALIDATE_IMEI)

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent or agent.level != 4:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device = dbapi.get_device(imei=imei)
    if device:
        if device.agent_id != 0:
            raise ApiError('ERROR_DEVICE_EXISTS', error.ERROR_DEVICE_EXISTS)
        else:
            # update device
            device.agent_id = agent_id
            device.owner_agent_id = agent_id
            device.cat = cat
    else:
        address_id = 0
        device = dbapi.make_new_device(imei, cat, agent_id, address_id)
    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise
    if not device:
        raise Exception('add device db error')

    data = {}
    if device:
        # 添加第一次设备分销
        from_agent, to_agent = 0, agent_id
        dbapi.make_new_device_distribution(device.id, device.imei, from_agent,
                                           to_agent)
        data = {'id': device.id, 'imei': device.imei, 'cat': device.cat}

    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise

    reply['data'] = data

    return make_response(jsonify(reply), status_code)
Esempio n. 10
0
def refund_income(total_fee, imei):
    dbg('refund_income')
    device = dbapi.get_device(imei=imei)
    if not device:
        dbg('no device: %s' % imei)
        return
    dds = dbapi.get_device_distribution(device_id=device.id)
    rates = (device.l4, device.l3, device.l2, device.l1)
    for i, dd in enumerate(dds):
        if i == len(dds) - 1 and device.salesman_agent_id != 0:
            # 包含业务员
            salesman_rates = (device.sl1, device.sl2, device.sl3)
            all_fee = total_fee * rates[i]
            fee_sum = 0
            ddss = dbapi.get_device_distribution_salesman(device_id=device.id)
            for iterm in ddss:
                # 级业务员提成
                cur_agent = dbapi.get_agent(id=iterm.to_agent)
                if cur_agent:
                    salseman_remark = '业务员退款扣除提成slevel: %d' % cur_agent.slevel
                    fee = all_fee * salesman_rates[cur_agent.slevel - 1]
                    receipt = -fee
                    make_refund_ticheng_wallet_receipt(cur_agent, receipt,
                                                       salseman_remark)
                    fee_sum += fee

            # 退款扣除各所属代理商
            agent = dbapi.get_agent(id=dd.to_agent)
            if not agent:
                dbg('no agent: %d' % dd.to_agent)
                continue
            remark = '退款扣除提成'
            receipt = -(all_fee - fee_sum)
            make_refund_ticheng_wallet_receipt(agent, receipt, remark)

        else:
            # 不包含业务员
            agent = dbapi.get_agent(id=dd.to_agent)
            if not agent:
                dbg('no agent: %d' % dd.to_agent)
                continue
            receipt = -total_fee * rates[4 - agent.level]
            remark = '退款扣除提成'
            make_refund_ticheng_wallet_receipt(agent, receipt, remark)
Esempio n. 11
0
def get_sub_salesman():
    dbg('get_sub_salesman')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    agent_id = current_user.agent_id
    salesmen = dbapi.get_agent(hook_agent_id=agent_id,
                               salesman=1,
                               name_id_only=1)

    data = []
    for salesman in salesmen:
        info = {
            'key': salesman[0],  # agent_id
            'title': salesman[1],  # agent name
        }
        salesmen2 = dbapi.get_agent(hook_agent_id=salesman[0],
                                    salesman=1,
                                    name_id_only=1)
        children = []
        for salesman2 in salesmen2:
            info2 = {
                'key': salesman2[0],  # agent_id
                'title': salesman2[1]  # agent name
            }
            salesmen3 = dbapi.get_agent(hook_agent_id=salesman2[0],
                                        salesman=1,
                                        name_id_only=1)
            children2 = []
            for salesman3 in salesmen3:
                info3 = {
                    'key': salesman3[0],  # agent_id
                    'title': salesman3[1]  # agent name
                }
                children2.append(info3)
            info2['children'] = children2
            children.append(info2)
        info['children'] = children
        data.append(info)

    reply['data'] = {'salesmen': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 12
0
def god_get_device():
    dbg('god_get_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data.get('imei')
        device_id = data.get('device_id')
        dbg(data)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    god = dbapi.get_god(openluat_user_id=current_user.id)
    if not god:
        raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND)

    role_bit = int(current_user.role[5])
    if role_bit not in (1, 2):
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device = None
    if imei:
        device = dbapi.get_device(imei=imei)
    elif device_id:
        device = dbapi.get_device(id=int(device_id))
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)
    if role_bit == 1:
        god_agents = dbapi.get_god_agent(god_id=god.id)
        g = (god_agent.agent_id for god_agent in god_agents)
        if device.agent_id not in g:
            raise ApiError('ERROR_DEVICE_NOT_FOUND',
                           error.ERROR_DEVICE_NOT_FOUND)

    device_address = dbapi.get_device_address(id=device.address_id)
    address = ""
    if device_address:
        address = device_address.address
    agentname = ''
    agent = dbapi.get_agent(id=device.owner_agent_id)
    if agent:
        agentname = agent.name

    reply['data'] = {
        'id': device.id,
        'imei': device.imei,
        'address': address,
        'operator': agentname,
        'remark': device.remark
    }

    return make_response(jsonify(reply), status_code)
Esempio n. 13
0
def device_multi_distribution():
    dbg('device_multi_distribution')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imeis = data['imeis']
        to_agent = data['to_agent']
    except:
        print_exception_info()
        raise ApiError('update devices error', error.ERROR_PARAM)

    agent = dbapi.get_agent(id=to_agent)
    if not agent or agent.level != 4:
        raise ApiError('can only give level 4')

    for imei in imeis:
        device = dbapi.get_device(imei=imei)
        if not device:
            db.session.rollback()
            dbg('imei: %s not exists' % imei)
            reply['code'] = 999
            reply['msg'] = 'imei: %s 不存在' % imei
            status_code = 422
            return make_response(jsonify(reply), status_code)
        elif device.agent_id != 0:
            db.session.rollback()
            dbg('imei: %s already first_distribution' % imei)
            reply['code'] = 999
            reply['msg'] = 'imei: %s 已经被首次分配' % imei
            status_code = 422
            return make_response(jsonify(reply), status_code)

        # 1. 插入设备分组
        from_agent = device.agent_id
        device_distribution = dbapi.make_new_device_distribution(
            device.id, device.imei, from_agent, to_agent)

        # 2. 更细设备
        device.agent_id = to_agent
        device.owner_agent_id = to_agent
        device.utime = datetime.now()
        db.session.add(device)

    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise

    return make_response(jsonify(reply), status_code)
Esempio n. 14
0
def get_coupon():
    dbg('get_coupon')
    reply ,status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        advertiser_id = data.get('advertiser_id')
        device_id = data.get('device_id')
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    page  = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)

    data = []

    if advertiser_id:
        count, coupons = dbapi.get_coupon(advertiser_id=int(advertiser_id), page=page, psize=psize)
    elif device_id:
        count, coupons = dbapi.get_coupon(device_id=int(device_id), page=page, psize=psize)
    else:
        agent = dbapi.get_agent(user_id=current_user.id)
        if not agent:
            raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)
        agent_id = agent.id
        count, coupons = dbapi.get_coupon(agent_id=agent_id, page=page, psize=psize)

    if count:
        for coupon in coupons:
            product = dbapi.get_product(product_id=coupon.product_id)
            pay     = dbapi.get_pay(pay_id=coupon.pay_id)
            info = {
                'id': coupon.id,
                'title': coupon.title,
                'desc': coupon.desc,
                'img': url_for('static', filename=coupon.img),
                'total': coupon.total,
                'left': product.inventory,
                'pay_status': pay.status,
                'time': int(coupon.ctime.timestamp())
            }
            data.append(info)

    reply['data'] = {
        'count': count,
        'coupons': data
    }

    return make_response(jsonify(reply), status_code)
Esempio n. 15
0
def get_nopay_salesman_devices():
    dbg('get_nopay_salesman_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)
    count = 0
    data = []
    if agent.salesman == 0:
        count, devices = dbapi.get_device(agent_id=agent_id,
                                          salesman=0,
                                          page=page,
                                          psize=psize,
                                          nopay=1)
    elif agent.salesman == 1:
        count, devices = dbapi.get_device(agent_id=agent_id,
                                          salesman=1,
                                          page=page,
                                          psize=psize,
                                          nopay=1)
    if count:
        if devices:
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                imei = device.imei
                nopay_count = dbapi.get_record_no_pay(imei=imei, count=True)
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'remark': device.remark,
                    'nopay_count': nopay_count
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 16
0
def search_agent_info():
    dbg('search_agent_info')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        name = data.get('name', '')
        phone = data.get('phone', '')
        salesman = int(data.get('salesman', 0))

        page = request.args.get('page', 1, type=int)
        psize = request.args.get('psize', 10, type=int)

    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent_id = current_user.agent_id

    count, agents = dbapi.get_agent(fuzzy_name=name,
                                    fuzzy_phone=phone,
                                    hook_agent_id=agent_id,
                                    salesman=salesman,
                                    page=page,
                                    psize=psize)

    data = []
    if count:
        for agent in agents:
            info = {
                'id': agent.id,
                'salesman': agent.salesman,
                'level': agent.level,
                'slevel': agent.slevel,
                'name': agent.name,
                'phone': agent.phone,
                'email': agent.email,
                'desc': agent.desc,
                'address': agent.address,
                'remark': agent.remark,
                'expandable': agent.expandable,
                'withdrawable': agent.withdrawable
            }
            data.append(info)

    reply['data'] = {'count': count, 'agents': data}
    return make_response(jsonify(reply), status_code)
Esempio n. 17
0
def update_device_rate():
    dbg('update_device_rate')

    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data['imei']
        rate = float(data['rate'])
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    if rate < 0 or rate > 1:
        raise ApiError('rate not support')

    dd = dbapi.get_device_distribution(imei=imei,
                                       from_agent=current_user.agent_id)
    if not dd:
        raise ApiError('ERROR_NOT_DISTRIBUTION', error.ERROR_NOT_DISTRIBUTION)

    device = dbapi.get_device(id=dd.device_id)
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUNDE', error.ERROR_DEVICE_NOT_FOUND)

    # 更新device_distribution
    dd = dbapi.update_device_distribution(dd, rate=rate)
    db.session.commit()

    # 计算l4,l3,l2,l1
    level = current_user.cur_agent.level
    agent_levels = []
    rates_all = [device.l1, device.l2, device.l3, device.l4]
    dds = dbapi.get_device_distribution(device_id=device.id)
    for dd in dds:
        agent = dbapi.get_agent(id=dd.to_agent)
        agent_levels.append(agent.level)
    update = calc_device_profit(level, rate, agent_levels, rates_all)

    # 更新device
    dbg(update)
    device = dbapi.update_device(device, **update)
    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 18
0
def check_refund_available(device, total_fee):
    dbg('check_refund_available')
    imei = device.imei
    rates = (device.l1, device.l2, device.l3, device.l4)
    dds = dbapi.get_device_distribution(imei=imei)
    for dd in dds:
        agent = dbapi.get_agent(id=dd.to_agent)
        if not agent:
            dbg('can not find agent: %d' % dd.to_agent)
            return False
        wallet = dbapi.get_wallet(user_id=agent.openluat_user_id,
                                  agent_id=agent.id)
        rate = rates[agent.level - 1]
        fee = total_fee * rate
        dbg((agent.id, fee, wallet.withdrawable_balance))
        if fee > wallet.withdrawable_balance:
            return False

    return True
Esempio n. 19
0
def get_agent_info_without_login():
    dbg('get_cur_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        agent_id = request.args.get('agent_id')
        assert (agent_id)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent = dbapi.get_agent(id=int(agent_id))
    if not agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    god_info = get_god_info_common(agent)

    reply['data'] = {
        'logo': url_for('static', filename=god_info.logo),
        'title': god_info.title
    }
    return make_response(jsonify(reply), status_code)
Esempio n. 20
0
def get_sub_salesman_onelevel():
    dbg('get_sub_salesman_onelevel')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        agent_id = data['agent_id']
        page = request.args.get('page', 1, type=int)
        psize = request.args.get('psize', 10, type=int)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    count, salesmen = dbapi.get_agent(hook_agent_id=agent_id,
                                      salesman=1,
                                      page=page,
                                      psize=psize)

    data = []
    if count:
        for salesman in salesmen:
            info = {
                'id': salesman.id,
                'salesman': salesman.salesman,
                'name': salesman.name,
                'level': salesman.level,
                'slevel': salesman.slevel,
                'phone': salesman.phone,
                'email': salesman.email,
                'address': salesman.address,
                'desc': salesman.desc,
                'expandable': salesman.expandable,
                'remark': salesman.remark
            }
            data.append(info)

    reply['data'] = {'count': count, 'salesmen': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 21
0
def update_agent():
    dbg('update_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        agent_id = data['agent_id']
        update = data['update']

        keys = ('name', 'desc', 'address', 'remark')
        for key in update:
            assert (key in keys)

    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])
    if role_bit != 1:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    agent = dbapi.get_agent(id=agent_id)
    god = dbapi.get_god(openluat_user_id=current_user.id)
    god_agent = dbapi.get_god_agent(agent_id=agent_id)
    if god.id != god_agent.god_id:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    agent = dbapi.update_agent(agent, **update)

    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise

    return make_response(jsonify(reply), status_code)
Esempio n. 22
0
def fuzzy_query_device_address(field):
    dbg('fuzzy_query_device_address')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        assert (field in ('address'))
        data = request.get_json()
        value = data[field]
    except:
        print_exception_info()
        raise ApiError('fuzzy_query_device_address error', error.ERROR_PARAM)

    kwargs = {field: value}

    agent_id = current_user.agent_id
    if current_user.cur_agent.salesman == 1:
        upper_agent = dbapi.get_agent(id=current_user.cur_agent.hook_agent_id)
        if upper_agent:
            agent_id = upper_agent.id

    res = dbapi.fuzzy_query_device_address(agent_id, **kwargs)
    reply['data'] = res

    return make_response(jsonify(reply), status_code)
Esempio n. 23
0
def top_agent_get_wechat_withdraw():
    dbg('top_agent_get_wechat_withdraw')
    reply ,status_code = {'code': 0, 'msg': ''}, 200

    try:
        page  = int(request.args.get('page', 1))
        psize = int(request.args.get('psize', 10))
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent_id = current_user.agent_id
    user_id  = current_user.id

    wechat_agent_id = get_top_wechat_agent_id(agent_id)
    if wechat_agent_id != agent_id:
        raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION)

    agent_setting = dbapi.get_agent_setting(agent_id=wechat_agent_id)

    count, res = dbapi.get_pay_to_user(wechat_agent_id=wechat_agent_id,
        page=page, psize=psize)
    data = []
    sum_dict = {}
    if count:
        for pay_to_user in res:
            openluat_user_id = pay_to_user.openluat_user_id
            agent = dbapi.get_agent(user_id=openluat_user_id)
            if agent:
                wallet = dbapi.get_wallet(user_id=openluat_user_id, agent_id=agent_id)
                name = agent.name
                agent_id = agent.id
                if agent_id in sum_dict:
                    dbg('sum in dict')
                    total_fee_sum = sum_dict[agent_id]['total_fee_sum']
                    receipt_sum = sum_dict[agent_id]['receipt_sum']
                else:
                    total_fee_sum = dbapi.get_pay_to_user(user_id=openluat_user_id, total=True)
                    receipt_sum = dbapi.get_agent_wallet_receipt_sum(wallet.id)
                    sum_dict[agent_id] = {
                        'total_fee_sum': total_fee_sum,
                        'receipt_sum': receipt_sum
                    }
            else:
                dbg('agent not found for openluat_user_id: %s' % openluat_user_id)
                name = ''
                total_fee_sum = 0
                receipt_sum = 0

            info = {
                'time': int(pay_to_user.ctime.timestamp()),
                # 'to_nickname': pay_to_user.to_nickname,
                'payment_no': pay_to_user.payment_no,
                'name': name,
                'total_fee': pay_to_user.total_fee,
                'wechat_fee': math.ceil(pay_to_user.total_fee * agent_setting.withdraw_fee),
                'wechat_fee_rate': agent_setting.withdraw_fee,
                'fee': pay_to_user.total_fee - math.ceil(pay_to_user.total_fee * agent_setting.withdraw_fee),
                'total_fee_sum': total_fee_sum,
                'receipt_sum': receipt_sum,
                'balance': int(wallet.balance),
                'withdrawable_balance': int(wallet.withdrawable_balance),
                'status': pay_to_user.status
            }
            data.append(info)

    reply = {
        'count': count,
        'pays': data
    }

    return make_response(jsonify(reply), status_code)
Esempio n. 24
0
def get_agent_or_god(role):
    dbg('get_agent_or_god')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = int(request.args.get('page', 1))
    psize = int(request.args.get('psize', 10))

    role_bit = int(current_user.role[5])

    if role_bit == 2:
        try:
            assert (role == 'god')
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        count, gods = dbapi.get_god(role=1, page=page, psize=psize)
        data = []
        if count and gods:
            for god in gods:
                info = {
                    'id': god.id,
                    'name': god.name,
                    'phone': god.phone,
                    'email': god.email,
                    'remark': god.remark
                }
                data.append(info)

        reply['data'] = {'count': count, 'gods': data}

    elif role_bit == 1:
        try:
            assert (role == 'agent')
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        openluat_user_id = current_user.id
        god = dbapi.get_god(openluat_user_id=openluat_user_id)
        count, res = dbapi.get_god_agent(god_id=god.id, page=page, psize=psize)
        data = []
        if count and res:
            for god_agent in res:
                agent_id = god_agent.agent_id
                agent = dbapi.get_agent(id=agent_id)
                if not agent:
                    continue
                info = {
                    'id': agent.id,
                    'name': agent.name,
                    'level': agent.level,
                    'phone': agent.phone,
                    'address': agent.address,
                    'expandable': agent.expandable,
                    'remark': agent.remark
                }
                data.append(info)

        reply['data'] = {'count': count, 'agents': data}

    elif role_bit == 0:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    return make_response(jsonify(reply), status_code)
Esempio n. 25
0
def add_ali_config():
    dbg('add_ali_config')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:

        name = request.form['name']
        shortname = request.form['shortname']
        appid = request.form['appid']

        priv_file = request.files['0']
        pub_file = request.files['1']

    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent_id = current_user.agent_id

    joinup = check_joinup_available(agent_id)
    if not joinup['ali']:
        raise ApiError('ERROR_ALI_CONFIG_EXISTS',
                       error.ERROR_ALI_CONFIG_EXISTS)

    ali_config = dbapi.get_ali_config(appid=appid)
    if ali_config:
        raise ApiError('ERROR_ALI_CONFIG_EXISTS',
                       error.ERROR_ALI_CONFIG_EXISTS)

    import platform
    if 'Windows' in platform.platform():
        config.ALI_PAY_KEY_PATH = 'keys/pro/alipay'
    if not os.path.exists(config.ALI_PAY_KEY_PATH):
        os.mkdir(config.ALI_PAY_KEY_PATH)
    output_dir = config.ALI_PAY_KEY_PATH + '/' + shortname
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
    priv_path = os.path.join(output_dir, 'rsa_private_key.pem')
    dbg("save priv: %s" % priv_path)
    priv_file.save(priv_path)
    pub_path = os.path.join(output_dir, 'rsa_public_key.pem')
    dbg("save pub: %s" % pub_path)
    pub_file.save(pub_path)

    ali_config = dbapi.make_new_ali_config(name, appid, priv_path, pub_path)
    db.session.commit()

    # 更新各级代理的支付宝信息
    agent_ali = dbapi.update_agent_ali(agent_id, ali_config.id)
    agents3 = dbapi.get_agent(hook_agent_id=agent_id)
    for agent3 in agents3:
        agent_ali = dbapi.update_agent_ali(agent3.id, ali_config.id)
        agents2 = dbapi.get_agent(hook_agent_id=agent3.id)
        for agent2 in agents2:
            agent_ali = dbapi.update_agent_ali(agent2.id, ali_config.id)
            agents1 = dbapi.get_agent(hook_agent_id=agent2.id)
            for agent1 in agents1:
                agent_ali = dbapi.update_agent_ali(agent1.id, ali_config.id)

    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 26
0
def add_wechat_config():
    dbg('add_wechat_config')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:

        name = request.form['name']
        shortname = request.form['shortname']
        appid = request.form['appid']
        appsecret = request.form['appsecret']
        mchid = request.form['mchid']
        mchkey = request.form['mchkey']
        txtname = request.form['txtname']

        cert_file = request.files['0']
        text_file = request.files['1']
        imag_file = request.files['2']

    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent_id = current_user.agent_id
    joinup = check_joinup_available(agent_id)
    if not joinup['wechat']:
        raise ApiError('ERROR_WECHAT_CONFIG_EXISTS',
                       error.ERROR_WECHAT_CONFIG_EXISTS)

    wechat_config = dbapi.get_wechat_config(appid=appid)
    if wechat_config:
        raise ApiError('ERROR_WECHAT_CONFIG_EXISTS',
                       error.ERROR_WECHAT_CONFIG_EXISTS)

    agent_id = current_user.agent_id

    if not os.path.exists('tmp'):
        os.mkdir("tmp")

    cert_filename = '%dcert.zip' % agent_id
    cert_filepath = os.path.join('tmp', cert_filename)
    dbg("save cert: %s" % cert_filepath)
    cert_file.save(cert_filepath)

    text_filename = txtname
    text_filepath = os.path.join('../streetweb', text_filename)
    dbg("save text: %s" % text_filepath)
    text_file.save(text_filepath)

    imag_filename = 'qrcode_%d' % agent_id
    imag_filepath = os.path.join('app/static/img/qrcode', imag_filename)
    qrcode_img = os.path.join('img/qrcode', imag_filename)
    if not os.path.exists('app/static/img/qrcode'):
        os.mkdir('app/static/img/qrcode')
    dbg("save imag: %s" % imag_filepath)
    imag_file.save(imag_filepath)

    import platform
    if 'Windows' in platform.platform():
        config.WECHAT_PAY_KEY_PATH = 'keys/pro'
    output_dir = config.WECHAT_PAY_KEY_PATH + '/' + shortname
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
    tool.extract(cert_filepath, output_dir)

    if os.path.exists(cert_filepath):
        os.remove(cert_filepath)

    redirecturl = config.WECHAT_LIST[1].REDIRECT_URI
    redirect_bind_url = config.WECHAT_LIST[1].REDIRECT_BIND_URL
    key_path = output_dir
    wechat_config = dbapi.make_new_wechat_config(name, appid, appsecret, mchid,
                                                 mchkey, redirecturl,
                                                 redirect_bind_url, key_path,
                                                 qrcode_img)
    db.session.commit()

    # 更新各级代理的微信信息
    agent_wechat = dbapi.update_agent_wechat(agent_id, wechat_config.id)
    agents3 = dbapi.get_agent(hook_agent_id=agent_id)
    for agent3 in agents3:
        agent_wechat = dbapi.update_agent_wechat(agent3.id, wechat_config.id)
        agents2 = dbapi.get_agent(hook_agent_id=agent3.id)
        for agent2 in agents2:
            agent_wechat = dbapi.update_agent_wechat(agent2.id,
                                                     wechat_config.id)
            agents1 = dbapi.get_agent(hook_agent_id=agent2.id)
            for agent1 in agents1:
                agent_wechat = dbapi.update_agent_wechat(
                    agent1.id, wechat_config.id)

    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 27
0
def update_agent_setting():
    dbg('update_agent_setting')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        target_agent_id = data['target_agent_id']
        update = data['update']
        dbg(update)
        keys = ('min_withdraw', 'withdraw_fee', 'wallet_pay_enable',
                'trans_url')
        for k in update:
            assert (k in keys)
    except:
        print_exception_info()
        raise ApiError('update agent setting error', error.ERROR_PARAM)

    if 'min_withdraw' in update and update['min_withdraw'] < 200:
        raise ApiError('ERROR_SETTING_WITH_DRAW',
                       error.ERROR_SETTING_WITH_DRAW)

    if 'withdraw_fee' in update and update['withdraw_fee'] < 0.006:
        raise ApiError('ERROR_SETTING_WITHDRAW_FEE',
                       error.ERROR_SETTING_WITHDRAW_FEE)

    agent_id = current_user.agent_id
    target_agent = dbapi.get_agent(id=target_agent_id)
    if not target_agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    if target_agent.hook_agent_id != 0 and target_agent.hook_agent_id != agent_id:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    # 更新目标代理
    agent_setting = dbapi.get_agent_setting(agent_id=target_agent_id)
    if not agent_setting:
        agent_setting = dbapi.make_new_agent_setting(target_agent_id)
        db.session.commit()
    dbapi.update_agent_setting(agent_setting, **update)

    # 更新level3的代理
    agents3 = dbapi.get_agent(hook_agent_id=target_agent_id)
    for agent3 in agents3:
        agent_id = agent3.id
        agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
        if not agent_setting:
            agent_setting = dbapi.make_new_agent_setting(agent_id)
            db.session.commit()
        dbapi.update_agent_setting(agent_setting, **update)

        # 更新level2的代理
        agents2 = dbapi.get_agent(hook_agent_id=agent_id)
        for agent2 in agents2:
            agent_id = agent2.id
            agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
            if not agent_setting:
                agent_setting = dbapi.make_new_agent_setting(agent_id)
                db.session.commit()
            dbapi.update_agent_setting(agent_setting, **update)

            # 更新level1的代理
            agents1 = dbapi.get_agent(hook_agent_id=agent_id)
            for agent1 in agents1:
                agent_id = agent1.id
                agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
                if not agent_setting:
                    agent_setting = dbapi.make_new_agent_setting(agent_id)
                    db.session.commit()
                dbapi.update_agent_setting(agent_setting, **update)

    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 28
0
def get_god_devices():
    dbg('get_god_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        god_id = data['god_id']
        page = request.args.get('page', 1, type=int)
        psize = request.args.get('psize', 10, type=int)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])
    if role_bit != 2:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    count = 0
    data = []

    ct1, agents = dbapi.get_god_agent(god_id=god_id, page=1, psize=999)
    for agent in agents:
        agent_id = agent.id
        ct2, devices = dbapi.get_device(agent_id=agent_id,
                                        page=page,
                                        psize=psize)
        if ct2:
            count += ct2
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                operator = ''
                operator_level = 0
                agent = dbapi.get_agent(id=device.owner_agent_id)
                if agent:
                    operator = agent.name
                    operator_level = agent.level
                imei = device.imei
                status = dbapi.get_cws_device_status(imei)
                online = -1
                if status:
                    online = status.get('online', -1)
                latest_report = dbapi.get_device_latestreport(imei)
                if not latest_report:
                    online = 0
                else:
                    if imei == '868575021151255':
                        dbg(latest_report['time'])
                    nowts = datetime.now().timestamp()
                    lrts = latest_report['time'].timestamp()
                    if nowts - lrts > 150:
                        if online == 1:
                            online = 0
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'use_state': 0,  # 0空闲,1使用
                    'comm_state': online,  # 0关机,1在线
                    'operator': operator,
                    'operator_level': operator_level,
                    'owner_agent_id': device.owner_agent_id,
                    'map_display': device.map_display,
                    'remark': device.remark,
                    'l4': device.l4,
                    'l3': device.l3,
                    'l2': device.l2,
                    'l1': device.l1
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 29
0
def add_agent_or_god(role):
    dbg('add_agent_or_god')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])

    if role_bit == 2:
        try:
            assert (role == 'god')
            name = data['name']
            email = data['email']
            phone = data['phone']
            remark = data['remark']
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        target_god = dbapi.get_god(phone=phone)
        if target_god:
            raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

        target_god = dbapi.get_god(email=email)
        if target_god:
            raise ApiError('ERROR_EMAIL_EXISTS', error.ERROR_EMAIL_EXISTS)

        openluat_user = dbapi.get_openluat_user(phone=phone)
        if not openluat_user:
            openluat_user = dbapi.make_new_openluat_user(name,
                                                         email,
                                                         phone,
                                                         role='000001000000')
        else:
            openluat_user.role[5] = '1'
            db.session.add(openluat_user)

        db.session.commit()

        openluat_user_id = openluat_user.id
        new_god = dbapi.make_new_god(openluat_user_id, name, phone, email,
                                     remark)
        db.session.commit()

    elif role_bit == 1:
        try:
            assert (role == 'agent')
            level = 4
            name = data['name']
            email = data['email']
            phone = data['phone']
            address = data['address']
            remark = data['remark']
            expandable = 1
            withdrawable = 1
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        target_agent = dbapi.get_agent(phone=phone)
        if target_agent:
            raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

        target_agent = dbapi.get_agent(email=email)
        if target_agent:
            raise ApiError('ERROR_EMAIL_EXISTS', error.ERROR_EMAIL_EXISTS)

        openluat_user = dbapi.get_openluat_user(phone=phone)
        if not openluat_user:
            openluat_user = dbapi.make_new_openluat_user(name, email, phone)
            db.session.commit()

        hook_agent_id = 0
        salesman = 0
        slevel = 0
        new_agent = dbapi.make_new_agent(salesman, openluat_user.id,
                                         hook_agent_id, level, slevel,
                                         expandable, withdrawable, name, phone,
                                         email, address, remark)
        db.session.commit()

        agent_setting = dbapi.make_new_agent_setting(new_agent.id)

        openluat_user_id = current_user.id
        god = dbapi.get_god(openluat_user_id=openluat_user_id)
        god_agent = dbapi.make_new_god_agent(god.id, new_agent.id)
        db.session.commit()

    elif role_bit == 0:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    return make_response(jsonify(reply), status_code)
Esempio n. 30
0
def add_agent():
    dbg('add_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
        salesman = data['salesman']
        level = data['level']
        slevel = data['slevel']
        name = data['name']
        email = data['email']
        phone = data['phone']
        address = data['address']
        remark = data['remark']
        expandable = data['expandable']
        withdrawable = data['withdrawable']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    target_agent = dbapi.get_agent(phone=phone)
    if target_agent:
        raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

    target_agent = dbapi.get_agent(email=email)
    if target_agent:
        raise ApiError('ERROR_EMAIL_EXISTS', error.ERROR_EMAIL_EXISTS)

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not agent',
                       error.ERROR_AGENT_NO_PERMISSION)
    if not agent.expandable:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not expandable',
                       error.ERROR_AGENT_NO_PERMISSION)
    if salesman == 0 and agent.salesman == 0 and level >= agent.level:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not allowed level',
                       error.ERROR_AGENT_NO_PERMISSION)
    if agent.salesman == 1 and slevel < agent.slevel:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not allowed slevel',
                       error.ERROR_AGENT_NO_PERMISSION)

    openluat_user = dbapi.get_openluat_user(phone=phone)
    if not openluat_user:
        openluat_user = dbapi.make_new_openluat_user(name, email, phone)
        db.session.commit()

    hook_agent_id = agent_id
    new_agent = dbapi.make_new_agent(salesman, openluat_user.id, hook_agent_id,
                                     level, slevel, expandable, withdrawable,
                                     name, phone, email, address, remark)
    db.session.commit()

    cur_agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
    if not cur_agent_setting:
        cur_agent_setting = dbapi.make_new_agent_setting(agent_id)

    min_withdraw = cur_agent_setting.min_withdraw
    withdraw_fee = cur_agent_setting.withdraw_fee
    wallet_pay_enable = cur_agent_setting.wallet_pay_enable
    agent_setting = dbapi.make_new_agent_setting(
        new_agent.id,
        min_withdraw=min_withdraw,
        withdraw_fee=withdraw_fee,
        wallet_pay_enable=wallet_pay_enable)

    # 默认微信公众号对接
    old_agent_wechat = dbapi.get_agent_wechat(agent_id=agent_id)
    agent_wechat = dbapi.make_new_agent_wechat(
        new_agent.id, old_agent_wechat.wechat_config_id)

    # 默认阿里生活号对接
    old_agent_ali = dbapi.get_agent_ali(agent_id=agent_id)
    agent_ali = dbapi.make_new_agent_ali(new_agent.id,
                                         old_agent_ali.ali_config_id)

    db.session.commit()

    return make_response(jsonify(reply), status_code)