Beispiel #1
0
    def POST(self):
        web.header('Content-Type', 'application/json')
        param = web.input(app_id='', session='', secret='', sign='')

        if '' in (param.app_id, param.session, param.secret, param.sign):
            return json.dumps({'ret': -2, 'msg': '参数错误'})

        uname = app_helper.logged(param.session)  # 检查session登录
        if uname:
            #验证签名
            md5_str = app_helper.generate_sign(
                [param.app_id, param.session, param.secret])
            if md5_str != param.sign:
                return json.dumps({'ret': -1, 'msg': '签名验证错误'})

            db_user = db.app_user.find_one({'uname': uname}, {'coupon': 1})
            if db_user == None:  # 不应该发生
                return json.dumps({'ret': -5, 'msg': '未找到用户信息'})

            # 检查是否有新红包
            app_helper.check_hb(uname)

            # 这里应该增加对有效期的检查!!!
            coupon = []
            unused = 0
            for i in db_user['coupon']:
                if app_helper.time_str(format=1) > i[1]:  # 过期抵用券不返回 2015-08-22
                    continue
                coupon.append({
                    'id': i[0],
                    'valid': i[1],
                    'cash': i[2],
                    'status': 'unused' if i[3] == 1 else 'used',
                })
                unused += (1 if i[3] == 1 else 0)

            # 返回
            return json.dumps({
                'ret': 0,
                'data': {
                    'coupon': coupon,
                    'total': len(coupon),
                    'unused': unused,
                }
            })
        else:
            return json.dumps({'ret': -4, 'msg': '无效的session'})
Beispiel #2
0
    def POST(self, version='v1'):
        web.header('Content-Type', 'application/json')
        #print web.input()
        param = web.input(app_id='',
                          session='',
                          rand='',
                          invitation='',
                          sign='')

        if '' in (param.app_id, param.session, param.rand, param.sign):
            return json.dumps({'ret': -2, 'msg': '参数错误'})

        #验证签名
        md5_str = app_helper.generate_sign(
            [param.app_id, param.session, param.rand, param.invitation])
        if md5_str != param.sign:
            return json.dumps({'ret': -1, 'msg': '签名验证错误'})

        session = app_helper.get_session(param.session)
        if session == None:
            return json.dumps({'ret': -4, 'msg': '无效的session'})

        if param.rand.strip() != session['rand']:
            if session['uname'] == '18516569412' and param.rand.strip(
            ) == '9998':
                None
            elif session['uname'] in setting.inner_number.keys() and \
             param.rand.strip()==setting.inner_number[session['uname']]:
                None
            else:
                return json.dumps({'ret': -5, 'msg': '短信验证码错误'})

        db.app_sessions.update_one(
            {'session_id': session['session_id']},
            {'$set': {
                'login': 1,
                'attime': time.time(),
            }})

        #邀请码
        if param.has_key('invitation'):
            invitation = param.invitation.lower()

            if db.invitation.find({
                    'code': invitation
            }).count() == 0:  # 无效地推邀请码
                if db.app_user.find({
                        'my_invit_code': invitation
                }).count() == 0:  # 无效用户邀请码
                    invitation = ''
            if invitation != '':
                r = db.app_user.find_one({'uname': session['uname']},
                                         {'invitation': 1})
                if r.get('invitation', '') != '':  # 已填邀请码
                    invitation = ''
        else:
            invitation = ''

        invitation_coupon = 0
        if invitation != '':
            # 赠送优惠券
            invitation_coupon = 1
            valid = app_helper.time_str(time.time() + 3600 * 24 * 10,
                                        1)  # 有效期10天
            r = db.app_user.find_one_and_update(
                {'uname': session['uname']},
                {
                    '$set': {
                        'invitation': invitation,
                        'last_time': app_helper.time_str()
                    },
                    '$push': {
                        'coupon':
                        (app_helper.my_rand(), valid, '5.00', 1, 14.9, 'apple')
                    },  # 邀请码送5+4+3
                },
                {
                    'address': 1,
                    'new_coupon': 1
                })
            db.app_user.update_one({'uname' : session['uname']},{ # 4元
             '$push' : {'coupon' : (app_helper.my_rand(), valid, '4.00', 1, 24.9, 'apple')},
            })
            db.app_user.update_one({'uname' : session['uname']},{ # 3元
             '$push' : {'coupon' : (app_helper.my_rand(), valid, '3.00', 1, 19.9, 'apple')},
            })
        else:
            r = db.app_user.find_one_and_update(
                {'uname': session['uname']},
                {'$set': {
                    'last_time': app_helper.time_str()
                }}, {
                    'address': 1,
                    'new_coupon': 1
                })

        if len(r['address']) > 0:  # 应该实现:返回最近使用的地址 !!!!
            addr = {
                'id': r['address'][0][0],
                'name': r['address'][0][1],
                'tel': r['address'][0][2],
                'addr': r['address'][0][3],
            }
        else:
            addr = {}

        # 检查是否有新红包
        app_helper.check_hb(session['uname'])

        # 返回
        if version == 'v3':
            # 是否有新收到的抵用券,进行提示
            if r.has_key('new_coupon') and r['new_coupon'] > 0:
                alert = True
                message = '掌柜送您%d张抵用券,请在个人中心查看哦' % (r['new_coupon'] +
                                                    invitation_coupon)
                db.app_user.update_one({'uname': session['uname']},
                                       {'$set': {
                                           'new_coupon': 0
                                       }})
            else:
                alert = False
                message = ''
            return json.dumps({
                'ret': 0,
                'data': {
                    'session': session['session_id'],
                    'login': True,
                    'addr': addr,
                    'uname': session['uname'],
                    'alert': alert,
                    'message': message
                }
            })
        else:  # v1,v2
            return json.dumps({
                'ret': 0,
                'data': {
                    'session': session['session_id'],
                    'login': True,
                    'addr': addr,
                    'uname': session['uname'],
                }
            })
Beispiel #3
0
    def POST(self):
        web.header('Content-Type', 'application/json')
        param = web.input(phone_num='', hb_id='')
        #print param

        if '' in (param.phone_num, param.hb_id):
            return json.dumps({'ret': -1, 'msg': '参数错误'})

        if not HB.has_key(param.hb_id):
            return json.dumps({'ret': -2, 'msg': 'hb_id参数错误'})

        # 检查活动时间
        todate = app_helper.time_str(format=1)
        if todate < HB[param.hb_id][0] or todate > HB[param.hb_id][1]:
            return json.dumps({'ret': -3, 'msg': '活动过期'})

        # 检查号码是否已领
        red_envelope_db = db.hb_store.find_one(
            {
                'hb_id': param.hb_id,
                'phone': param.phone_num.strip()
            }, {'phone': 1})
        #如果不存在则插入,存在则提醒已存在
        if red_envelope_db:
            return json.dumps({'ret': -4, 'msg': '已领红包'})
        else:
            # 是否是新客
            new_user = db.app_user.find_one({'uname':
                                             param.phone_num.strip()}) == None

            if new_user:
                # 检查已领金额
                sum0 = list(
                    db.hb_store.aggregate([{
                        '$match': {
                            'hb_id': param.hb_id
                        }
                    }, {
                        '$group': {
                            '_id': '0',
                            'sum': {
                                '$sum': "$hb_new"
                            }
                        }
                    }]))
                if len(sum0) == 0:
                    sum1 = 0
                else:
                    sum1 = sum0[0]['sum']
                print 'sum_new = ', sum0

                if sum1 > pool_size[todate]['new']:
                    return json.dumps({'ret': -5, 'msg': '红包发完'})

                hb_money = 40

                #插入操作
                #得到当前时间
                now_time = app_helper.time_str()
                db.hb_store.insert_one({
                    'hb_id': '002',
                    "phone": param.phone_num.strip(),
                    "received_time": now_time,
                    "hb_money": hb_money,
                    "hb_new": hb_money,
                    "expired_date": ''
                })
            else:
                # 老用户先抽奖
                hb_money = old_pool[random.randint(0, 9)]

                if hb_money == 30:
                    tag = 'old1'
                else:
                    tag = 'old2'

                # 检查已领金额
                sum0 = list(
                    db.hb_store.aggregate([{
                        '$match': {
                            'hb_id': param.hb_id
                        }
                    }, {
                        '$group': {
                            '_id': '0',
                            'sum': {
                                '$sum': "$hb_%s" % tag
                            }
                        }
                    }]))
                if len(sum0) == 0:
                    sum1 = 0
                else:
                    sum1 = sum0[0]['sum']
                print 'sum_%s = ' % tag, sum0

                if sum1 > pool_size[todate][tag]:
                    return json.dumps({'ret': -5, 'msg': '红包发完'})

                #插入操作
                #得到当前时间
                now_time = app_helper.time_str()
                db.hb_store.insert_one({
                    'hb_id': '002',
                    "phone": param.phone_num.strip(),
                    "received_time": now_time,
                    "hb_money": hb_money,
                    "hb_%s" % tag: hb_money,
                    "expired_date": ''
                })

                # 老用户用户,立刻检查是否有新红包
                app_helper.check_hb(param.phone_num.strip())

            return json.dumps({'ret': 0, 'money': hb_money})
Beispiel #4
0
    def POST(self):
        web.header('Content-Type', 'application/json')
        param = web.input(phone_num='', hb_id='')
        #print param

        if '' in (param.phone_num, param.hb_id):
            return json.dumps({'ret': -1, 'msg': '参数错误'})

        if not app_helper.HB.has_key(param.hb_id):
            return json.dumps({'ret': -2, 'msg': 'hb_id参数错误'})

        # 检查活动时间
        now_date = datetime.datetime.now()
        todate = now_date.strftime('%Y-%m-%d')
        if todate < app_helper.HB[param.hb_id][0] or todate > app_helper.HB[
                param.hb_id][1]:
            return json.dumps({'ret': -3, 'msg': '活动过期'})

        # 检查已领金额
        sum0 = list(
            db.hb_store.aggregate([{
                '$match': {
                    'hb_id': param.hb_id
                }
            }, {
                '$group': {
                    '_id': '0',
                    'sum': {
                        '$sum': "$hb_money"
                    }
                }
            }]))
        if len(sum0) == 0:
            sum1 = 0
        else:
            sum1 = sum0[0]['sum']
        print 'sum = ', sum0

        if sum1 > app_helper.HB[param.hb_id][2]:
            return json.dumps({'ret': -5, 'msg': '红包发完'})

        # 检查号码是否已领
        red_envelope_db = db.hb_store.find_one({'phone': param.phone_num},
                                               {'phone': 1})
        #如果不存在则插入,存在则提醒已存在
        if red_envelope_db:
            return json.dumps({'ret': -4, 'msg': '已领红包'})
        else:
            #插入操作
            #得到当前时间
            now_date = datetime.datetime.now()
            overdue_time = (now_date +
                            datetime.timedelta(days=7)).strftime('%Y-%m-%d')
            now_time = now_date.strftime('%Y-%m-%d %H:%M:%S')
            env_money = random.randint(2, 5)
            db.hb_store.insert_one({
                'hb_id': param.hb_id,
                "phone": param.phone_num,
                "received_time": now_time,
                "hb_money": env_money,
                "expired_date": overdue_time
            })

            # 检查是否有新红包
            app_helper.check_hb(param.phone_num)

            return json.dumps({'ret': 0, 'money': env_money})