Beispiel #1
0
def confirm_order():
    token = request.get_json().get("token", None)
    if token is None:  # 传递参数中未传递token
        return jsonify({"code": 201, "msg": "token查询参数必须提供!"})
    c_id = request.args.get("cid")  # 获取查询字符串中的课程id
    types = request.args.get("type")  # 获取查询字符串中的购买类型的
    u_id = cache_.get_token_user_id(token)
    if u_id:
        if types == "fromcart":  # 如果是从购物车去结算
            cart_obj = CartDao()
            course_info = cart_obj.will_pay_course(c_id,
                                                   u_id).get("course_list")
            total_price = cart_obj.will_pay_course(c_id,
                                                   u_id).get("total_price")
            return jsonify({
                "code": 200,
                "products": course_info,
                "total_price": total_price
            })
        elif types == "fromcombat":  # 如果是立即购买结算
            pay_obj = PayDao()
            cid = pay_obj.get_course_id(c_id)
            course_info = pay_obj.get_pay_course(cid).get("pay_course")
            total_price = pay_obj.get_pay_course(cid).get("total_price")
            return jsonify({
                "code": 200,
                "products": course_info,
                "total_price": total_price
            })

    return jsonify({"code": 201, "msg": "用户还未登录或注册!"})
Beispiel #2
0
def changeinfo():
    resp = eval(request.get_data())
    if resp:
        token = resp.pop('token')
        id = get_token_user_id(token)
        file: FileStorage = request.files.get('img', None)
        if all((bool(token),bool(id))):
            # 验证文件的类型, png/jpeg/jpg, 单张不能超过2M
            # content-type: image/png, image/jpeg
            if bool(file):
                if file.content_type in ('image/png','image/jpeg'):
                    filename = uuid.uuid4().hex + os.path.splitext(file.filename)[-1]
                    file.save(filename)
                    key = oss.upload_file(filename) # 上传到oss云服务器上
                    os.remove(filename)  # 删除临时文件
                    PersonDao().perupdate('u_pic', key, id) # 将key写入到DB中
                    for k,v in resp.items():
                        PersonDao().perupdate(k,v,id)
                    return ({'code':200,'msg':'ok!','file_key': key})
                return jsonify({'code': 201,'msg': '图片格式只支持png或jpeg'})
            for k, v in resp.items():
                PersonDao().perupdate(k, v, id)
            return ({'code': 200, 'msg': 'ok!'})
        else:
            return jsonify({'code': 204, 'msg': 'POST请求参数必须有token!'})
    return jsonify({'code': 304, 'msg': '传入数据为空'})
Beispiel #3
0
def my_collect():
    token = request.headers.get("token")
    u_id = cache_.get_token_user_id(token)
    if u_id:
        my_collect = CollectDao().get_my_collect(u_id)  # 获取当前用户的收藏课程
        if my_collect:
            return jsonify({"code": 200, "data": my_collect})
        return jsonify({"code": 201, "msg": "暂无收藏课程"})
    return jsonify({"code": 201, "msg": "用户还未登录或注册"})
Beispiel #4
0
def loginout():
    api_logger.debug('user forget get action!')
    resp = eval(request.get_data())
    if resp:
        token = resp.get('token')
        id = cache_.get_token_user_id(token)  # 从redis中获取id
        r.delete(token)  # 删除服务端token
        UserDao().user_update('is_active', 0, 'id', id)  # 更改激活状态为0
        return jsonify({'code': 200, 'msg': '退出成功!'})
    return jsonify({'code': 304, 'msg': '传入数据为空'})
Beispiel #5
0
def cancel_order():
    token = request.args.get("token", None)
    order_num = request.args.get("trade_number")
    if token is None:
        return jsonify({"code": 201, "msg": "token查询参数必须提供"})
    u_id = cache_.get_token_user_id(token)
    if u_id:
        order_cancel = OrderDao()
        delete_order = order_cancel.delete_order(u_id, order_num)
        if delete_order:
            return jsonify({"code": 200, "msg": "已关闭!"})
        else:
            return jsonify({"code": 200, "msg": "取消订单失败!"})

    return jsonify({"code": 201, "msg": "用户还未登录或注册"})
Beispiel #6
0
def personinfo(key):
    resp = request.args
    if resp: # 验证请求数据
        token = resp.get('token')
        id = get_token_user_id(token)
        if id:
            perinfo = PersonDao().perlist(id)
            if perinfo:
                img_type = int(request.args.get('type', 0))
                img_url = oss.get_url(key) if img_type == 0 else oss.get_small_url(key)
                perinfo[0]['url'] = img_url
                return jsonify(perinfo)
            return jsonify({'code': 201, 'msg': '查询失败!'})
        else:
            return jsonify({'code': 203, 'msg': '你还未登录!'})
    return jsonify({'code': 304, 'msg': '传入数据为空'})
def person_combat():  # 全部实战
    resp = request.args
    if resp:
        token = resp.get('token')
        id = get_token_user_id(token)
        if id:
            per_combat_info = PersonCombatDao().comquery(id, 0)
            if per_combat_info:
                course_id = [i['course_id'] for i in per_combat_info]
                u_course = {
                    i: PersonCombatDao().u_course(course_id[i])
                    for i in range(len(course_id))
                }
                return jsonify(u_course)
            return jsonify({'code': 201, 'msg': '该用户无实战课程'})
        return jsonify({'code': 203, 'msg': '用户尚未登录'})
    return jsonify({'code': 304, 'msg': '传入数据为空'})
Beispiel #8
0
def order_detail():
    token = request.args.get("token", None)
    order_num = request.args.get("trade_number")
    if token is None:
        return jsonify({"code": 201, "msg": "token查询参数必须提供"})

    u_id = cache_.get_token_user_id(token)
    if u_id:
        order_detail_obj = OrderDao()
        order_detail = order_detail_obj.get_order_detail(u_id, order_num)
        return jsonify({
            "code": 200,
            'msg': "ok",
            "order_detail": order_detail
        })

    return jsonify({"code": 201, "msg": "用户还未登录或注册"})
Beispiel #9
0
def collect_course():
    token = request.headers.get("token")
    type1 = request.args.get("type")
    c_id = request.args.get("cid")
    u_id = cache_.get_token_user_id(token)
    if u_id:
        try:
            if type1 == "0":  # 如果type==0,则添加收藏当前课程
                collect_success = CollectDao().save(
                    "user_favorites", **{
                        "user_id": u_id,
                        "course_id": c_id
                    })
                if collect_success:
                    return jsonify({"code": 200, "msg": "已收藏至我的收藏"})
            elif type1 == "1":  # 如果type==1,则取消收藏当前课程
                collect_cancel = CollectDao().cancel_user_collect(u_id, c_id)
                if collect_cancel:
                    return jsonify({"code": 200, "msg": "已取消收藏"})
        except Exception as e:
            api_logger.error(e)
            return jsonify({"code": 400})
    return jsonify({"code": 201, "msg": "用户还未登录或注册"})
Beispiel #10
0
def my_order():
    token = request.headers.get("token")
    if token is None:
        return jsonify({{"code": 201, "msg": "token查询参数必须提供"}})
    u_id = cache_.get_token_user_id(token)
    if u_id:  # 如果用户登录,返回订单的信息
        order = OrderDao()
        order_all_info = order.get_order_info(u_id)  # 订单的全部信息
        order_pay = order.get_order_pay_state(u_id).get("pay")  # 已支付的订单信息
        order_no_pay = order.get_order_pay_state(u_id).get(
            "no_pay")  # 未支付的订单信息
        if any((order_all_info, order_pay, order_no_pay)):
            return jsonify({
                "code": 200,
                'msg': "ok",
                'data': {
                    "order_all_info": order_all_info,
                    "order_pay": order_pay,
                    "order_no_pay": order_no_pay
                }
            })
        else:
            return jsonify({"code": 200, "msg": "用户暂未相关订单"})
    return jsonify({"code": 201, "msg": "用户还未登录或注册"})
Beispiel #11
0
def add_order():
    token = request.get_json().get("token")
    if token is None:
        return jsonify({"code": 201, "msg": "token查询参数必须提供"})
    resp = request.get_json()
    total = resp.get("total")
    type1 = resp.get("type")
    c_id = resp.get("course")  # 获取的c_id是字符串 <class 'str'>
    u_id = cache_.get_token_user_id(token)
    print(c_id)
    if u_id:
        if type1 == "fromcart":  # 如果是从购物车提交订单
            cart_dao = CartDao()
            course_info = cart_dao.will_pay_course(c_id,
                                                   u_id).get("course_list")
            total_price = cart_dao.will_pay_course(c_id,
                                                   u_id).get("total_price")
            # print("aaaaaaaaaa", course_info,type(course_info))
            if len(c_id) > 1:
                if total_price == total:  # 提交订单传递的总价是否和后台计算的总价相等
                    order_obj = OrderDao()
                    # user_course = UserCourseDao()
                    order_num = order_obj.next_order_num()  # 生成订单编号
                    for course in course_info:  # 创建订单记录
                        order_success = order_obj.save(
                            "orders", **{
                                "order_num": order_num,
                                "user_id": u_id,
                                "course_id": course.get("id"),
                                "price": course.get("price")
                            })

                        if order_success:
                            # 如果创建订单,购买的课程存入用户课程表中成功,则删除购物车记录
                            cart_delete = cart_dao.delete_user_cart_course(
                                course.get("id"), u_id)
                            if cart_delete:
                                continue
                    # 返回订单信息
                    return jsonify({
                        "code": 200,
                        "order_num": order_num,
                        "data": course_info
                    })
                return jsonify({"code": 201, "msg": "价格不正确"})
            elif len(c_id) == 1:
                if total_price == total:
                    order_obj = OrderDao()
                    order_num = order_obj.next_order_num()
                    order_success = order_obj.save(
                        "orders", **{
                            "order_num": order_num,
                            "user_id": u_id,
                            "course_id": course_info.get("id"),
                            "price": course_info.get("price")
                        })
                    if order_success:  # 如果创建订单记录成功,返回订单信息
                        cart_delete = cart_dao.delete_user_cart_course(
                            course_info.get("id"), u_id)
                        if cart_delete:
                            return jsonify({
                                "code": 200,
                                "order_num": order_num,
                                "data": course_info
                            })
                return jsonify({"code": 201, "msg": "价格不正确"})

        elif type1 == "fromcombat":  # 如果是立即购买提交订单
            pay_dao = PayDao()
            cid = pay_dao.get_course_id(c_id)
            course_info = pay_dao.get_pay_course(cid).get("pay_course")
            total_price = pay_dao.get_pay_course(cid).get("total_price")
            if total_price == total:  # 总价相等,创建一条订单记录
                order_obj = OrderDao()
                order_num = order_obj.next_order_num()
                order_success = order_obj.save(
                    "orders", **{
                        "order_num": order_num,
                        "user_id": u_id,
                        "course_id": course_info[0].get("id"),
                        "price": course_info[0].get("price")
                    })
                if order_success:  # 如果创建订单记录成功,返回订单信息
                    return jsonify({
                        "code": 200,
                        "order_num": order_num,
                        "data": course_info
                    })
            return jsonify({"code": 201, "msg": "价格不正确"})
    return jsonify({"code": 201, "msg": "用户未登录或注册"})