Beispiel #1
0
def _get_mpuser_access_token(user):
    mp_user = get_binding_weixin_mpuser(user)
    if mp_user:
        mpuser_access_token = get_mpuser_accesstoken(mp_user)
    else:
        return False

    if mpuser_access_token is None:
        return False

    if mpuser_access_token.is_active:
        return mpuser_access_token
    else:
        return None
Beispiel #2
0
	def handle(self, user_id, **options):
		user = User.objects.get(id=user_id)

		mp_user = get_binding_weixin_mpuser(user)
		if mp_user is None:
			return None

		if not mp_user.is_service or not mp_user.is_certified:
			return None
			
		mpuser_access_token = get_mpuser_accesstoken(mp_user)
		weixin_api = get_weixin_api(mpuser_access_token)

		result = weixin_api.api_shakearound_device_aopplyid(3,u'测试')
		print result
Beispiel #3
0
def _check_access_token(user_profile):
    mp_user = get_binding_weixin_mpuser(user_profile.user)
    if mp_user is None:
        return None

    if not mp_user.is_service or not mp_user.is_certified:
        return None

    mpuser_access_token = get_mpuser_accesstoken(mp_user)

    if mpuser_access_token is None:
        return None

    if mpuser_access_token.is_active:
        return mpuser_access_token
    else:
        return None
Beispiel #4
0
def _get_ticket(user_id, screen_id):
    mp_user = get_binding_weixin_mpuser(user_id)
    mpuser_access_token = get_mpuser_accesstoken(mp_user)
    weixin_api = get_weixin_api(mpuser_access_token)
    if mp_user.is_certified and mp_user.is_service and mpuser_access_token.is_active:
        try:
            qrcode_ticket = weixin_api.create_qrcode_ticket(
                screen_id, QrcodeTicket.PERMANENT)
            return qrcode_ticket.ticket
        except:
            return _get_ticket(user_id, screen_id)

    else:
        try:
            qrcode_ticket = weixin_api.create_qrcode_ticket(
                screen_id, QrcodeTicket.PERMANENT)
            return qrcode_ticket.ticket
        except:
            return ''
Beispiel #5
0
def __get_qrcode(user_id):
    try:
        user = User.objects.get(id=user_id)
    except:
        notify_msg = u"微信会员二维码__get_qrcode errror :id={}cause:\n{}".format(
            user_id, unicode_full_stack())
        watchdog_error(notify_msg)
        return None, None

    mp_user = get_binding_weixin_mpuser(user)
    if mp_user:
        mpuser_access_token = get_mpuser_accesstoken(mp_user)
    else:
        return None, None

    if mpuser_access_token is None:
        return None, None

    if mpuser_access_token.is_active:
        weixin_api = get_weixin_api(mpuser_access_token)
        try:
            ticket = None
            qrcode_ticket = weixin_api.create_qrcode_ticket(int(user.id))
            try:
                ticket = qrcode_ticket.ticket
            except:
                ticket = None

            #qcrod_info = weixin_api.get_qrcode(ticket)
            if ticket:
                return ticket, 604000
            else:
                return None, None
        except:
            notify_msg = u"微信会员二维码__get_qrcode errror :id={}cause:\n{}".format(
                user_id, unicode_full_stack())
            watchdog_error(notify_msg, 'WEB', user_id)
            return None, None

    else:
        return None, None
Beispiel #6
0
	def api_put(request):
		"""
		响应PUT
		"""
		data = request_util.get_fields_to_be_save(request)
		data['permission'] = True if data['permission']=='1' else False
		data['is_limit_first_buy'] = True if data['is_limit_first_buy']=='1' else False
		data['is_limit_cash'] = True if data['is_limit_cash']=='1' else False
		weizoom_card_ids = data['weizoom_card_ids'].split(',')
		weizoom_card_passwords = data['weizoom_card_passwords'].split(',')
		rebate = app_models.Rebate(**data)
		ticket_id = app_models.Rebate.objects.all().count() + 10001 #ID从1W开始计算,为了防止跟带参数二维码重复
		rebate.ticket_id = ticket_id
		rebate.save()
		data = json.loads(rebate.to_json())
		data['id'] = data['_id']['$oid']
		index = 0
		for weizoom_card_id in weizoom_card_ids:
			weizoom_card_info = AppsWeizoomCard(
				owner_id = request.manager.id,
				belong_to = data['id'],
				weizoom_card_id = weizoom_card_id,
				weizoom_card_password = encrypt_password(weizoom_card_passwords[index])
			)
			weizoom_card_info.save()
			index += 1

		if settings.MODE != 'develop':
			mp_user = get_binding_weixin_mpuser(request.manager)
			mpuser_access_token = get_mpuser_accesstoken(mp_user)
			weixin_api = get_weixin_api(mpuser_access_token)
			try:
				qrcode_ticket = weixin_api.create_qrcode_ticket(ticket_id, QrcodeTicket.PERMANENT)
				ticket = qrcode_ticket.ticket
			except Exception, e:
				print 'get qrcode_ticket fail:', e
				ticket = ''
Beispiel #7
0
def list_channel_qrcode_settings(request):
	mpuser = get_system_user_binded_mpuser(request.user)

	if (mpuser is None) or (not mpuser.is_certified) or (not mpuser.is_service):
		should_show_authorize_cover = True
	else:
		should_show_authorize_cover = False
	
	settings = ChannelQrcodeSettings.objects.filter(owner=request.user)
	setting_ids = [s.id for s in settings]
	relations = ChannelQrcodeHasMember.objects.filter(channel_qrcode_id__in=setting_ids)
	setting_id2count = {}
	member_id2setting_id = {} 
	member_ids = []
	for r in relations:
		member_ids.append(r.member_id)
		member_id2setting_id[r.member_id] = r.channel_qrcode_id
		if r.channel_qrcode_id in setting_id2count:
			setting_id2count[r.channel_qrcode_id] += 1
		else:
			setting_id2count[r.channel_qrcode_id] = 1
	
	webapp_users = member_model.WebAppUser.objects.filter(member_id__in=member_ids)
	webapp_user_id2member_id = dict([(u.id, u.member_id) for u in webapp_users])
	webapp_user_ids = set(webapp_user_id2member_id.keys())
	orders = mall_model.Order.by_webapp_user_id(webapp_user_ids).filter(status=mall_model.ORDER_STATUS_SUCCESSED)
	member_id2total_final_price = {}
	for order in orders:
		member_id = webapp_user_id2member_id[order.webapp_user_id]
		if member_id in member_id2total_final_price:
			member_id2total_final_price[member_id] += order.final_price
		else:
			member_id2total_final_price[member_id] = order.final_price
	
	setting_id2total_final_price = {}
	for member_id in member_id2total_final_price.keys():
		final_price = member_id2total_final_price[member_id]
		setting_id = member_id2setting_id[member_id]
		if setting_id in setting_id2total_final_price:
			setting_id2total_final_price[setting_id] += final_price
		else:
			setting_id2total_final_price[setting_id] = final_price
	
	
	mp_user = get_binding_weixin_mpuser(request.user)
	mpuser_access_token = get_mpuser_accesstoken(mp_user)
	for setting in settings:
		prize_info = decode_json_str(setting.award_prize_info)
		if prize_info['name'] == '_score-prize_':
			setting.cur_prize = '[%s]%d' % (prize_info['type'], prize_info['id'])
		elif prize_info['name'] == 'non-prize':
			setting.cur_prize = prize_info['type']
		else:
			setting.cur_prize = '[%s]%s' % (prize_info['type'], prize_info['name'])
		
		if setting.id in setting_id2count:
			setting.count = setting_id2count[setting.id]
		else:
			setting.count = 0
		if setting.id in setting_id2total_final_price:
			setting.total_final_price = setting_id2total_final_price[setting.id]
		else:
			setting.total_final_price = 0
		
		#如果没有ticket信息则获取ticket信息
		if not setting.ticket:
			try:
				if mp_user.is_certified and mp_user.is_service and mpuser_access_token.is_active:
					weixin_api = get_weixin_api(mpuser_access_token)
					qrcode_ticket = weixin_api.create_qrcode_ticket(int(setting.id), QrcodeTicket.PERMANENT)
			
					try:
						ticket = qrcode_ticket.ticket
					except:
						ticket = ''
					setting.ticket = ticket
					setting.save()
			except:
				pass
	c = RequestContext(request, {
		'first_nav_name': MARKET_TOOLS_NAV,
		'second_navs': export.get_second_navs(request),
		'second_nav_name': SECOND_NAV_NAME,
		'settings': settings,
		'should_show_authorize_cover': should_show_authorize_cover,
		'is_hide_weixin_option_menu': True
	})
	return render_to_response('channel_qrcode/editor/channel_qrcode_settings.html', c)
Beispiel #8
0
def edit_channel_qrcode_setting(request):
	if request.POST:
		setting_id = int(request.POST["setting_id"])
		name = request.POST["name"]
		award_prize_info = request.POST['prize_info'].strip()
		reply_type = int(request.POST.get("reply_type", 0))
		reply_detail = request.POST.get("reply_detail", '')
		reply_material_id = request.POST.get("reply_material_id", 0)
		remark = request.POST.get("remark", '')
		grade_id = int(request.POST.get("grade_id", -1))
		re_old_member = int(request.POST.get("re_old_member", 0))
		
		if reply_type == 0:
			reply_material_id = 0
			reply_detail = ''
		elif reply_type == 1:
			reply_material_id = 0
		elif reply_type == 2:
			reply_detail = ''
		
		if setting_id:
			ChannelQrcodeSettings.objects.filter(owner=request.user, id=setting_id).update(
	 				name=name, 
	 				award_prize_info=award_prize_info,
	 				reply_type=reply_type,
	 				reply_detail=reply_detail,
	 				reply_material_id=reply_material_id,
	 				remark=remark,
	 				grade_id=grade_id,
	 				re_old_member=re_old_member
	 			)
		else:
	 		cur_setting = ChannelQrcodeSettings.objects.create(
	 				owner=request.user, 
	 				name=name, 
	 				award_prize_info=award_prize_info,
	 				reply_type=reply_type,
	 				reply_detail=reply_detail,
	 				reply_material_id=reply_material_id,
	 				remark=remark,
	 				grade_id=grade_id,
	 				re_old_member=re_old_member
	 			)
	 		
			mp_user = get_binding_weixin_mpuser(request.user)
			mpuser_access_token = get_mpuser_accesstoken(mp_user)
			weixin_api = get_weixin_api(mpuser_access_token)
			
			try:
				qrcode_ticket = weixin_api.create_qrcode_ticket(int(cur_setting.id), QrcodeTicket.PERMANENT)
				ticket = qrcode_ticket.ticket
			except:
				ticket = ''
			cur_setting.ticket = ticket
			cur_setting.save()
		
   		return HttpResponseRedirect('/market_tools/channel_qrcode/')
	else:
		member_grades = MemberGrade.get_all_grades_list(request.user_profile.webapp_id)
		c = RequestContext(request, {
			'first_nav_name': MARKET_TOOLS_NAV,
			'second_navs': export.get_second_navs(request),
			'second_nav_name': SECOND_NAV_NAME,
			'member_grades': member_grades
		})
		return render_to_response('channel_qrcode/editor/edit_channel_qrcode_setting.html', c)
Beispiel #9
0
def get_channel_qrcode_settings(request):
    """
	获取渠道扫码配置列表
	"""
    #处理搜索
    query = request.GET.get('query', '').strip()
    if query:
        settings = ChannelQrcodeSettings.objects.filter(owner=request.user,
                                                        name__contains=query)
    else:
        settings = ChannelQrcodeSettings.objects.filter(owner=request.user)

    setting_ids = [s.id for s in settings]
    relations = ChannelQrcodeHasMember.objects.filter(
        channel_qrcode_id__in=setting_ids)
    setting_id2count = {}
    member_id2setting_id = {}
    member_ids = []
    for r in relations:
        member_ids.append(r.member_id)
        member_id2setting_id[r.member_id] = r.channel_qrcode_id
        if r.channel_qrcode_id in setting_id2count:
            setting_id2count[r.channel_qrcode_id] += 1
        else:
            setting_id2count[r.channel_qrcode_id] = 1

    webapp_users = member_model.WebAppUser.objects.filter(
        member_id__in=member_ids)
    webapp_user_id2member_id = dict([(u.id, u.member_id)
                                     for u in webapp_users])
    webapp_user_ids = set(webapp_user_id2member_id.keys())
    orders = mall_model.Order.by_webapp_user_id(webapp_user_ids).filter(
        status=mall_model.ORDER_STATUS_SUCCESSED)
    member_id2total_final_price = {}
    for order in orders:
        member_id = webapp_user_id2member_id[order.webapp_user_id]
        if member_id in member_id2total_final_price:
            member_id2total_final_price[member_id] += order.final_price
        else:
            member_id2total_final_price[member_id] = order.final_price

    setting_id2total_final_price = {}
    for member_id in member_id2total_final_price.keys():
        final_price = member_id2total_final_price[member_id]
        setting_id = member_id2setting_id[member_id]
        if setting_id in setting_id2total_final_price:
            setting_id2total_final_price[setting_id] += final_price
        else:
            setting_id2total_final_price[setting_id] = final_price

    response = create_response(200)
    response.data.items = []
    items = []

    mp_user = get_binding_weixin_mpuser(request.user)
    mpuser_access_token = get_mpuser_accesstoken(mp_user)

    for setting in settings:
        cur_setting = JsonResponse()
        prize_info = decode_json_str(setting.award_prize_info)
        if prize_info['name'] == '_score-prize_':
            setting.cur_prize = '[%s]%d' % (prize_info['type'],
                                            prize_info['id'])
        elif prize_info['name'] == 'non-prize':
            setting.cur_prize = prize_info['type']
        else:
            setting.cur_prize = '[%s]%s' % (prize_info['type'],
                                            prize_info['name'])

        if setting.id in setting_id2count:
            setting.count = setting_id2count[setting.id]
        else:
            setting.count = 0
        if setting.id in setting_id2total_final_price:
            setting.total_final_price = setting_id2total_final_price[
                setting.id]
        else:
            setting.total_final_price = 0

        #如果没有ticket信息则获取ticket信息
        if not setting.ticket:
            try:
                if mp_user.is_certified and mp_user.is_service and mpuser_access_token.is_active:
                    weixin_api = get_weixin_api(mpuser_access_token)
                    qrcode_ticket = weixin_api.create_qrcode_ticket(
                        int(setting.id), QrcodeTicket.PERMANENT)

                    try:
                        ticket = qrcode_ticket.ticket
                    except:
                        ticket = ''
                    setting.ticket = ticket
                    setting.save()
            except:
                pass
        cur_setting.id = setting.id
        cur_setting.name = setting.name
        cur_setting.count = setting.count
        cur_setting.total_final_price = round(setting.total_final_price, 2)
        cur_setting.cur_prize = setting.cur_prize
        cur_setting.ticket = setting.ticket
        cur_setting.remark = setting.remark
        items.append(cur_setting)

    #进行分页
    response.data.sortAttr = request.GET.get('sort_attr', 'count')

    if '-' in response.data.sortAttr:
        sorter = response.data.sortAttr[1:]
        is_reverse = True
    else:
        sorter = response.data.sortAttr
        is_reverse = False
    items = sorted(items, reverse=is_reverse, key=lambda b: getattr(b, sorter))
    count_per_page = int(request.GET.get('count_per_page', 15))
    cur_page = int(request.GET.get('page', '1'))
    pageinfo, items = paginator.paginate(
        items,
        cur_page,
        count_per_page,
        query_string=request.META['QUERY_STRING'])

    #
    response.data.items = items
    response.data.pageinfo = paginator.to_dict(pageinfo)

    return response.get_response()
Beispiel #10
0
def _get_channel_qrcode_items(request):
    #处理搜索
    from mall.models import *
    query = request.GET.get('query', '').strip()
    sort_attr = request.GET.get('sort_attr', '-created_at')
    created_at = '-created_at'
    if 'created_at' in sort_attr:
        created_at = sort_attr

    setting = MemberChannelQrcodeSettings.objects.filter(owner=request.manager)

    if setting.count() > 0:
        member_channel_qrcodes = MemberChannelQrcode.objects.filter(
            member_channel_qrcode_setting=setting[0])
        if query:
            member_ids = [
                qrcode.member_id for qrcode in member_channel_qrcodes
            ]
            query_hex = byte_to_hex(query)
            members = member_model.Member.objects.filter(
                id__in=member_ids).filter(username_hexstr__contains=query_hex)
            member_ids = [m.id for m in members]
            member_channel_qrcodes = member_channel_qrcodes.filter(
                member_id__in=member_ids)
    else:
        return create_response(500).get_response()

    member_channel_qrcode_ids = [
        qrcode.id for qrcode in member_channel_qrcodes
    ]

    relations = MemberChannelQrcodeHasMember.objects.filter(
        member_channel_qrcode__in=member_channel_qrcode_ids)
    member_channel_qrcode_id2count = {}
    member_id2member_channel_qrcode_id = {}
    member_id2relation = {}
    member_ids = []
    for r in relations:
        member_ids.append(r.member_id)
        member_id2member_channel_qrcode_id[
            r.member_id] = r.member_channel_qrcode_id
        member_id2relation[r.member_id] = r
        if r.member_channel_qrcode_id in member_channel_qrcode_id2count:
            member_channel_qrcode_id2count[r.member_channel_qrcode_id] += 1
        else:
            member_channel_qrcode_id2count[r.member_channel_qrcode_id] = 1

    webapp_users = member_model.WebAppUser.objects.filter(
        member_id__in=member_ids)
    webapp_user_id2member_id = dict([(u.id, u.member_id)
                                     for u in webapp_users])
    webapp_user_ids = set(webapp_user_id2member_id.keys())
    orders = Order.by_webapp_user_id(webapp_user_ids).filter(
        status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP,
                    ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED))

    member_id2total_final_price = {}
    member_id2cash_money = {}
    member_id2weizoom_card_money = {}
    for order in orders:
        member_id = webapp_user_id2member_id[order.webapp_user_id]
        if member_id2relation[member_id].is_new or member_id2relation[
                member_id].created_at <= order.created_at:
            if member_id in member_id2total_final_price:
                member_id2total_final_price[
                    member_id] += order.final_price + order.weizoom_card_money
                member_id2cash_money[member_id] += order.final_price
                member_id2weizoom_card_money[
                    member_id] += order.weizoom_card_money
            else:
                member_id2total_final_price[
                    member_id] = order.final_price + order.weizoom_card_money
                member_id2cash_money[member_id] = order.final_price
                member_id2weizoom_card_money[
                    member_id] = order.weizoom_card_money

    member_channel_qrcode_id2total_final_price = {}
    member_channel_qrcode_id2cash_money = {}
    member_channel_qrcode_id2weizoom_card_money = {}
    for member_id in member_id2total_final_price.keys():
        final_price = member_id2total_final_price[member_id]
        cash_money = member_id2cash_money[member_id]
        weizoom_card_money = member_id2weizoom_card_money[member_id]
        member_channel_qrcode_id = member_id2member_channel_qrcode_id[
            member_id]
        if member_channel_qrcode_id in member_channel_qrcode_id2total_final_price:
            member_channel_qrcode_id2total_final_price[
                member_channel_qrcode_id] += final_price
            member_channel_qrcode_id2cash_money[
                member_channel_qrcode_id] += cash_money
            member_channel_qrcode_id2weizoom_card_money[
                member_channel_qrcode_id] += weizoom_card_money
        else:
            member_channel_qrcode_id2total_final_price[
                member_channel_qrcode_id] = final_price
            member_channel_qrcode_id2cash_money[
                member_channel_qrcode_id] = cash_money
            member_channel_qrcode_id2weizoom_card_money[
                member_channel_qrcode_id] = weizoom_card_money

    response = create_response(200)
    #response.data.items = []
    items = []

    mp_user = get_binding_weixin_mpuser(request.manager)
    mpuser_access_token = get_mpuser_accesstoken(mp_user)

    for qrcode in member_channel_qrcodes:
        current_qrcode = JsonResponse()

        if qrcode.id in member_channel_qrcode_id2count:
            qrcode.count = member_channel_qrcode_id2count[qrcode.id]
        else:
            qrcode.count = 0
        if qrcode.id in member_channel_qrcode_id2total_final_price:
            qrcode.total_final_price = member_channel_qrcode_id2total_final_price[
                qrcode.id]
            qrcode.cash_money = member_channel_qrcode_id2cash_money[qrcode.id]
            qrcode.weizoom_card_money = member_channel_qrcode_id2weizoom_card_money[
                qrcode.id]
        else:
            qrcode.total_final_price = 0
            qrcode.cash_money = 0
            qrcode.weizoom_card_money = 0

        #如果没有ticket信息则获取ticket信息
        if not qrcode.ticket:
            try:
                if mp_user.is_certified and mp_user.is_service and mpuser_access_token.is_active:
                    weixin_api = get_weixin_api(mpuser_access_token)
                    qrcode_ticket = weixin_api.create_qrcode_ticket(
                        int(qrcode.id), QrcodeTicket.PERMANENT)

                    try:
                        ticket = qrcode_ticket.ticket
                    except:
                        ticket = ''
                    qrcode.ticket = ticket
                    qrcode.save()
            except:
                pass
        current_qrcode.id = qrcode.id
        current_qrcode.name = qrcode.member.username_for_html
        current_qrcode.count = qrcode.count
        current_qrcode.total_final_price = round(qrcode.total_final_price, 2)
        current_qrcode.ticket = qrcode.ticket
        current_qrcode.created_at = qrcode.created_at.strftime(
            '%Y-%m-%d %H:%M:%S')

        items.append(current_qrcode)
    return items