Пример #1
0
def clear_old_goods_order():
    try:
        results = GoodsOrder.query.filter(GoodsOrder.order_status == 1).all()
        if results:
            for result in results:
                mistiming = dif_time(str(result.create_time))
                Logging.logger.info('订单号:{0} 将在{1}秒后自动关闭'.format(
                    result.order_num, 3600 * 2 - mistiming))
                if mistiming > 3600 * 2:
                    result.order_status = 5
                    result.close_time = datetime.datetime.now()

                    # 用户钢镚回退
                    user_id = result.user_id
                    user_obj = User.query.get(user_id)
                    user_obj.coins += result.price

                    # 商品库存回退
                    goods_id = result.goods_id
                    goods_obj = Goods.query.get(goods_id)
                    goods_obj.available_num += 1
                    redis_store.decr(goods_obj.name)

                    db.session.add(user_obj)
                    db.session.add(result)
                    db.session.add(goods_obj)
                    Logging.logger.info('订单号:{0} 超时未支付,自动关闭'.format(
                        result.order_num))
            db.session.commit()
        return jsonify(errno=0, errmsg="OK")
    except Exception as e:
        Logging.logger.error('errmsg:{0}'.format(e))
        db.session.rollback()
        return jsonify(errno=-1, errmsg='网络异常')
Пример #2
0
def lesson_update_inform():
    try:
        time_obj = UpdateTime.query.filter(UpdateTime.type == 3).first()
        if time_obj:
            # 与当前时间的时间差
            mistiming = dif_time(str(time_obj.create_time))
            print(("mistiming=", mistiming))
            if mistiming > 3600:
                results = UserBTN.query.filter(UserBTN.btn_num == 2, UserBTN.is_send == 0, UserBTN.is_new == 1).all()
                for result in results:
                    # 推送模板消息
                    form_obj = UserFormID.query.filter(UserFormID.user_id == result.user_id).order_by(
                        UserFormID.create_time.desc()).first()
                    if not form_obj:
                        print(('user_id=%s的用户FormID不足' % result.user_id))
                    else:
                        print(('准备向user_id=%s的用户推送免费领课内容更新通知:' % result.user_id))
                        form_id = form_obj.form_id
                        openid = form_obj.openid

                        start_time = time.time()
                        now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time))

                        user_obj = User.query.get(result.user_id)
                        template_id = 'LFMuIoAgGvegGPru0AAh_LR-Z62gzyBgBlQXF3ekYfg'
                        data = {'keyword1': {'value': base64.b64decode(user_obj.nick_nameemoj)},
                                'keyword2': {'value': "免费听课"},
                                'keyword3': {'value': "课程表又更新啦!快点我去给自己充电吧!"},
                                'keyword4': {'value': now}}

                        # data = json.loads(data)
                        page = 'pages/curse/freeCourse/main'

                        # response = threading.Timer(3600, send_msg, (template_id, openid, page, form_id, data))
                        response = send_msg(template_id, openid, page, form_id, data)
                        response = response.json()
                        print(('返回结果:', response))
                        if response.get('errcode') == 0:
                            result.is_send = 1
                            result.is_new = 0
                            db.session.add(result)
                            db.session.delete(form_obj)
                            db.session.commit()
                            print(("推送成功:", response))
                        else:
                            db.session.delete(form_obj)
                            db.session.commit()
                            print(("推送失败:", response))
                if not results:
                    # 全部推送成功再删除
                    db.session.delete(time_obj)
                    db.session.commit()

        return jsonify(errno=0, errmsg="OK")
    except Exception as e:
        print(e)
        db.session.rollback()
        return jsonify(errno=-1, errmsg="网络异常")
Пример #3
0
def banner_update_inform():
    try:
        time_obj = UpdateTime.query.filter(UpdateTime.type == 5).first()
        if time_obj:
            # 与当前时间的时间差
            mistiming = dif_time(str(time_obj.create_time))
            print(("mistiming=", mistiming))
            if mistiming > 1800:
                results = UserBTN.query.filter(UserBTN.btn_num == 4, UserBTN.is_send == 0, UserBTN.is_new == 1).all()
                for result in results:
                    # 推送模板消息
                    form_obj = UserFormID.query.filter(UserFormID.user_id == result.user_id).order_by(
                        UserFormID.create_time.desc()).first()
                    if not form_obj:
                        print(('user_id=%s的用户FormID不足' % result.user_id))
                        db.session.delete(result)
                        db.session.commit()
                    else:
                        print(('准备向user_id=%s的用户推送第一个banner图更新通知:' % result.user_id))
                        form_id = form_obj.form_id
                        openid = form_obj.openid

                        banner = Banner.query.filter_by(status=1).order_by(Banner.sort.asc()).first()
                        if banner:
                            article = Article.query.get(banner.article_id)

                            user_obj = User.query.get(result.user_id)
                            template_id = 'LFMuIoAgGvegGPru0AAh_BBpX72aaJfRf0-LG3-YJzo'
                            data = {'keyword1': {'value': base64.b64decode(user_obj.nick_nameemoj)},
                                    'keyword2': {'value': '您订阅的今日热文更新啦!'},
                                    'keyword3': {'value': article.title}}
                            page = 'pages/tipoff/tipoffcontent/main?arc_id=%s' % article.id

                            # response = threading.Timer(3600, send_msg, (template_id, openid, page, form_id, data))
                            response = send_msg(template_id, openid, page, form_id, data)
                            response = response.json()
                            print(('返回结果:', response))
                            if response.get('errcode') == 0:
                                result.is_send = 1
                                result.is_new = 0
                                db.session.add(result)
                                db.session.delete(form_obj)
                                db.session.commit()
                                print(("推送成功:", response))
                            else:
                                db.session.delete(form_obj)
                                db.session.commit()
                                print(("推送失败:", response))
                if not results:
                    # 全部推送成功再删除
                    db.session.delete(time_obj)
                    db.session.commit()

        return jsonify(errno=0, errmsg="OK")
    except Exception as e:
        print(e)
        db.session.rollback()
        return jsonify(errno=-1, errmsg="网络异常")
Пример #4
0
def steal_friend_step():
    try:
        res = request.get_json()
        user_id = res.get('user_id')
        friend_id = res.get('friend_id')

        Logging.logger.info('request_args:{0}'.format(res))
        if not all([user_id, friend_id]):
            return jsonify(errno=-1, errmsg='参数不完整')

        user_obj = User.query.get(user_id)
        friend_obj = User.query.get(friend_id)
        if not user_obj or not friend_obj:
            return jsonify(errno=-1, errmsg='用户不存在')

        now = datetime.datetime.now()
        # 获取今天零点
        today = now - datetime.timedelta(hours=now.hour,
                                         minutes=now.minute,
                                         seconds=now.second,
                                         microseconds=now.microsecond)
        user_friend = UserFriends.query.filter(
            UserFriends.user_id == user_id, UserFriends.friend_id == friend_id,
            UserFriends.create_time >= today).first()
        if not user_friend:
            return jsonify(errno=-1, errmsg='该好友未被邀请')

        steal_date = user_friend.steal_date
        if not steal_date:
            user_friend.steal_num -= 1
            # user_obj.available_step += 1000
            user_obj.steal_step += 1000
            # print '偷取的步数累计:', user_obj.steal_step
            user_friend.steal_date = datetime.datetime.now()
        else:
            if user_friend.steal_num <= 0:
                return jsonify(errno=0, errmsg="请换个好友偷取吧")
            else:
                mistiming = dif_time(str(steal_date))
                if mistiming < 60 * 5:
                    return jsonify(errno=-1,
                                   errmsg="请在%s秒后再来偷取吧" % (60 * 5 - mistiming))
                else:
                    user_friend.steal_num -= 1
                    # user_obj.available_step += 1000
                    user_obj.steal_step += 1000
                    # print '偷取的步数累计=', user_obj.steal_step
                    user_friend.steal_date = datetime.datetime.now()

        db.session.add(user_obj)
        db.session.add(user_friend)
        db.session.commit()
        return jsonify(errno=0, errmsg="OK")
    except Exception as e:
        Logging.logger.error('[api/steal_friend_step] errmsg:{0}'.format(e))
        db.session.rollback()
        return jsonify(errno=-1, errmsg='网络异常')
Пример #5
0
def check_no_help():
    try:
        results = LessonOrder.query.filter(LessonOrder.order_status == 1, LessonOrder.is_help == 0,
                                           LessonOrder.is_send == 0).all()
        if results:
            for result in results:
                # 时间差
                mistiming = dif_time(str(result.create_time))
                print(("mistiming=", mistiming))
                # 误差一分钟,且未发送过消息的order
                if mistiming > 3600 * 2:
                    # 推送模板消息
                    form_obj = UserFormID.query.filter(UserFormID.user_id == result.user_id).order_by(
                        UserFormID.create_time.desc()).first()
                    if not form_obj:
                        print(('user_id=%s的用户FormID不足' % result.user_id))
                    else:
                        print(('准备向user_id=%s的用户推送2小时内没有好友助力通知:' % result.user_id))
                        form_id = form_obj.form_id
                        openid = form_obj.openid

                        lesson_obj = NewLesson.query.get(result.lesson_id)
                        template_id = '1UMnwF_6E3aLd8Cbjl9luT0-cBIeaml-BW9e_8FTAbQ'
                        data = {'keyword1': {'value': lesson_obj.title}, 'keyword2': {'value': "暂时还没有好友为你鼓励"}}
                        # data = json.dumps(data)
                        page = 'pages/curse/getFriends/main?order_id=%s&lesson_id=%s' % (result.id, result.lesson_id)

                        response = send_msg(template_id, openid, page, form_id, data)
                        response = response.json()
                        print(('返回结果:', response))
                        if response.get('errcode') == 0:
                            result.is_send = 1
                            db.session.add(result)
                            db.session.delete(form_obj)
                            db.session.commit()
                            print(("推送成功:", response))
                        else:
                            db.session.delete(form_obj)
                            db.session.commit()
                            print(("推送失败:", response))

        return jsonify(errno=0, errmsg="OK")
    except Exception as e:
        print(e)
        db.session.rollback()
        return jsonify(errno=-1, errmsg="网络异常")
Пример #6
0
def my_lesson_update():
    try:
        today = datetime.datetime.today().date()
        time_obj = UpdateTime.query.filter(UpdateTime.type == 2).filter(
            UpdateTime.create_time.like(str(today) + "%")).first()
        if time_obj:
            # 与当前时间的时间差
            mistiming = dif_time(str(time_obj.create_time))
            print(("mistiming=", mistiming))
            if mistiming > 3600:
                results = LessonOrder.query.filter(LessonOrder.is_pay == 1, LessonOrder.is_new == 1,
                                                   LessonOrder.is_send == 0).all()
                for result in results:
                    # 推送模板消息
                    form_obj = UserFormID.query.filter(UserFormID.user_id == result.user_id).order_by(
                        UserFormID.create_time.desc()).first()
                    if not form_obj:
                        print(('user_id=%s的用户FormID不足' % result.user_id))
                        result.is_new = 0
                        db.session.add(result)
                        db.session.commit()
                    else:
                        print(('准备向user_id=%s的用户推送已购课程更新通知:' % result.user_id))
                        form_id = form_obj.form_id
                        openid = form_obj.openid

                        start_time = time.time()
                        now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time))

                        user_obj = User.query.get(result.user_id)
                        lesson_obj = NewLesson.query.get(result.lesson_id)
                        template_id = 'LFMuIoAgGvegGPru0AAh_P1kS44EotPMIK4L5EB14hE'
                        data = {'keyword1': {'value': base64.b64decode(user_obj.nick_nameemoj)},
                                'keyword2': {'value': now},
                                'keyword3': {'value': lesson_obj.title},
                                'keyword4': {'value': "您喜欢的课程更新啦!点我去收听~"}}
                        # data = json.loads(data)
                        page = 'pages/curse/lessonList/main?lesson_id=%s' % result.lesson_id

                        response = send_msg(template_id, openid, page, form_id, data)
                        response = response.json()
                        print(('返回结果:', response))
                        if response.get('errcode') == 0:
                            result.is_send = 1
                            result.is_new = 0
                            db.session.add(result)
                            db.session.delete(form_obj)
                            db.session.commit()
                            print(("推送成功:", response))
                        else:
                            db.session.delete(form_obj)
                            db.session.commit()
                            print(("推送失败:", response))
                # if not results:
                #     # 全部推送成功
                #     time_obj.is_send = 1
                #     db.session.add(time_obj)
                #     db.session.commit()

        return jsonify(errno=0, errmsg="OK")
    except Exception as e:
        print(e)
        db.session.rollback()
        return jsonify(errno=-1, errmsg="网络异常")
Пример #7
0
def record_sale_num():
    try:
        res = request.get_json()
        sales_id = res.get('sales_id')
        lesson_id = res.get('lesson_id')
        user_id = res.get('user_id')

        print(('请求参数:', request.data))
        if not all([sales_id, lesson_id, user_id]):
            return jsonify(errno=-1, errmsg='参数不完整')

        try:
            sales_id = int(sales_id)
            lesson_id = int(lesson_id)
            user_id = int(user_id)
        except Exception as e:
            print(e)
            return jsonify(errno=-1, errmsg='参数错误')

        sales_obj = Sales.query.get(sales_id)
        if not sales_obj:
            return jsonify(errno=-1, errmsg='推广员ID错误')

        lesson_obj = NewLesson.query.get(lesson_id)
        if not lesson_obj:
            return jsonify(errno=-1, errmsg='课程不存在')

        # 记录新用户识别数量(授权30s的误差内,为新用户)
        now = datetime.datetime.now()
        user_obj = User.query.get(user_id)
        mistiming = dif_time(str(user_obj.create_time))
        print(("mistiming=", mistiming))
        if mistiming <= 30:

            # 按天记录/当天累加
            result = DailySalesQrNum.query.filter(
                DailySalesQrNum.lesson_id == lesson_id,
                DailySalesQrNum.sales_id == sales_id,
                DailySalesQrNum.record_time == now.date()).first()
            if result:
                result.daily_num += 1
                db.session.add(result)
            else:
                daily_qr = DailySalesQrNum()
                daily_qr.lesson_id = lesson_id
                daily_qr.sales_id = sales_id
                daily_qr.daily_num = 1
                db.session.add(daily_qr)

            # 记录总数
            sales = SalesNum.query.filter(
                SalesNum.lesson_id == lesson_id,
                SalesNum.sales_id == sales_id).first()
            if not sales:
                obj = SalesNum()
                obj.sales_id = sales_id
                obj.lesson_id = lesson_id
                db.session.add(obj)
            else:
                sales.num += 1
                db.session.add(sales)

            db.session.commit()
        return jsonify(errno=0, errmsg='OK')
    except Exception as e:
        print(e)
        db.session.rollback()
        return jsonify(errno=-1, errmsg='网络异常')
Пример #8
0
def record_share_qr():
    try:
        res = request.get_json()
        order_id = res.get('order_id')  # 邀友鼓励btn分享识别进入
        lesson_id = res.get('lesson_id')  # 邀友听课btn分享识别进入
        user_id = res.get('qr_user_id')
        # qr_user_id = res.get('qr_user_id')  # 邀友听课btn分享识别进入,传生成二维码的用户ID

        print('请求参数:', request.data)
        try:
            user_id = int(user_id)
        except Exception as e:
            print(e)
            return jsonify(errno=-1, errmsg='参数错误')

        # 记录当天新用户识别数量(授权30s的误差内,为新用户)
        now = datetime.datetime.now()
        user_obj = User.query.get(user_id)
        mistiming = dif_time(str(user_obj.create_time))
        print("mistiming=", mistiming)
        if mistiming <= 30:
            if order_id:
                print('邀友鼓励btn分享识别进入')
                try:
                    order_id = int(order_id)
                except Exception as e:
                    print(e)
                    return jsonify(errno=-1, errmsg='参数错误')

                order_obj = LessonOrder.query.get(order_id)
                if not order_obj:
                    return jsonify(errno=-1, errmsg='订单不存在')

                # 总数累计
                qr = ScanQrNum.query.filter(
                    ScanQrNum.lesson_id == order_obj.lesson_id,
                    ScanQrNum.btn == 2).first()
                if qr:
                    qr.num += 1
                else:
                    qr = ScanQrNum()
                    qr.lesson_id = order_obj.lesson_id
                    # qr.user_id = order_obj.user_id
                    qr.btn = 2
                    qr.num = 1
                db.session.add(qr)

                # 按天记录/当天累加
                results = DailyScanQrNum.query.filter(
                    DailyScanQrNum.lesson_id == order_obj.lesson_id,
                    # DailyScanQrNum.user_id == order_obj.user_id,
                    DailyScanQrNum.btn == 2,
                    DailyScanQrNum.record_time == now.date()).first()
                if results:
                    results.daily_num += 1
                    db.session.add(results)
                else:
                    daily_qr = DailyScanQrNum()
                    daily_qr.lesson_id = order_obj.lesson_id
                    # daily_qr.user_id = order_obj.user_id
                    daily_qr.btn = 2
                    daily_qr.daily_num = 1
                    db.session.add(daily_qr)

                db.session.commit()

            elif lesson_id:
                print('邀友听课btn分享识别进入')
                try:
                    lesson_id = int(lesson_id)
                    # qr_user_id = int(qr_user_id)
                except Exception as e:
                    print(e)
                    return jsonify(errno=-1, errmsg='参数错误')

                lesson_obj = NewLesson.query.get(lesson_id)
                if not lesson_obj:
                    return jsonify(errno=-1, errmsg='课程不存在')

                # qr_user_obj = User.query.get(qr_user_id)
                # if not qr_user_obj:
                #     return jsonify(errno=-1, errmsg='生成二维码的用户ID不存在')

                # 总数累计
                qr = ScanQrNum.query.filter(ScanQrNum.lesson_id == lesson_id,
                                            ScanQrNum.btn == 1).first()
                if qr:
                    qr.num += 1
                else:
                    qr = ScanQrNum()
                    qr.lesson_id = lesson_id
                    # qr.user_id = qr_user_id
                    qr.btn = 1
                    qr.num = 1
                db.session.add(qr)

                # 按天记录/当天累加
                results = DailyScanQrNum.query.filter(
                    DailyScanQrNum.lesson_id == lesson_id,
                    # DailyScanQrNum.user_id == qr_user_id,
                    DailyScanQrNum.btn == 1,
                    DailyScanQrNum.record_time == now.date()).first()
                if results:
                    results.daily_num += 1
                    db.session.add(results)
                else:
                    daily_qr = DailyScanQrNum()
                    daily_qr.lesson_id = lesson_id
                    # daily_qr.user_id = qr_user_id
                    daily_qr.btn = 1
                    daily_qr.daily_num = 1
                    db.session.add(daily_qr)

                db.session.commit()

        return jsonify(errno=0, errmsg='OK')
    except Exception as e:
        print(e)
        db.session.rollback()
        return jsonify(errno=-1, errmsg='网络异常')