def act_user_sum(request): ''' 踢过球的人数 :param request: :return: ''' if request.method == 'GET': try: openid = request.GET.get('openid') my_id = QyMember.objects.get(openid=openid).id objs = QyActor_activities.objects.filter(member_id=my_id) act_list = [] for obj in objs: act_list.append(obj.activity_id) ac_list = [] obj = QyActivity.objects.filter(pk__in=act_list,activity_state=2) for ob in obj: ac_list.append(ob.id) obs = QyActor_activities.objects.filter(activity_id__in=ac_list) meb_list = [] for ob in obs: if ob.member_id is not my_id: meb_list.append(ob.member_id) return JSONResponse(0,{'sum':len(set(meb_list))}) except: return JSONResponse(1007)
def act_user(request): ''' 踢过球的人列表 :param request: :return: ''' if request.method == 'GET': try: openid = request.GET.get('openid') my_id = QyMember.objects.get(openid=openid).id objs = QyActor_activities.objects.filter(member_id=my_id) act_list = [] for obj in objs: act_list.append(obj.activity_id) ac_list = [] obj = QyActivity.objects.filter(pk__in=act_list,activity_state=2) for ob in obj: ac_list.append(ob.id) obs = QyActor_activities.objects.filter(activity_id__in=ac_list) meb_list = [] for ob in obs: if ob.member_id is not my_id: meb_list.append(ob.member_id) obj = QyMember.objects.filter(pk__in=meb_list).distinct() pag = mypage(request.GET.get('page'), obj) serializer = Member_feedback(pag['page_list'], many=True) return JSONResponse(0,serializer.data) except: return JSONResponse(1007)
def together_ball(request): ''' 我与指定用户一起踢过球总数 :param request:openid :return: ''' if request.method == 'GET': try: member_id = QyMember.objects.get( openid=request.GET.get('openid')).id objs_a = QyActor_activities.objects.filter( member_id=member_id,reg_state=1) objs_act = [] for y in objs_a: objs_act.append(y.activity_id) objs_b = QyActor_activities.objects.filter( member_id=request.GET.get('id'), reg_state=1) for y in objs_b: objs_act.append(y.activity_id) obj = len(QyActivity.objects.filter( pk__in=list(set(objs_act)),activity_state=2)) return JSONResponse(0,{'sum':obj}) except: return JSONResponse(1007) else: return JSONResponse(1054)
def news_create(request): ''' 创建申请信息 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) applicant_openid = data.get('applicant_openid') applicant_id = QyMember.objects.get(openid=applicant_openid).id respondent_id = data.get('respondent_id') # 申请人 与 被申请人 不能一致 if applicant_id == respondent_id: return JSONResponse(1005) with transaction.atomic(): # 创建申请信息 obj = QyNews( applicant_id=applicant_id, respondent_id=respondent_id, content=data.get('content'), apply_time=str(int(time.time())), ) obj.save() objs = QyNews.objects.get(pk=obj.pk) serializer = News_getRespondent(objs, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1007) else: return JSONResponse(1055)
def change_member_info(request): ''' 根据请求信息对当前用户信息进行更改 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) open_id = data.get('openid') # 获取登录用户 user = QyMember.objects.get(openid=open_id) with transaction.atomic(): try: if data.get('avatar') and ( not user.avatar.startswith('http')): os.remove(user.avatar) except: pass if data.get('avatar'): user.avatar = data.get('avatar', user.avatar) user.age = data.get('age', user.age) user.profession = data.get('profession', user.profession) user.phonenum = data.get('phonenum', user.phonenum) user.save() # 返回JSON字符串 serializer = Member_feedback(user, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1000) else: return JSONResponse(1054)
def common_friend(request): ''' 查询'共同球友' :param request: :return: ''' if request.method == 'GET': member_id = request.GET.get('member') friend_id = request.GET.get('friend') try: # 分别取出各自的好友 m = QyGroup.objects.filter(member_a_id=member_id) m_friends = [] for x in m: m_friends.append(x.member_b_id) f = QyGroup.objects.filter(member_a_id=friend_id) f_friends = [] for y in f: f_friends.append(y.member_b_id) # 获取他们的交集 common = [i for i in m_friends if i in f_friends] common_friends = [] for member in common: common_friends.append(QyMember.objects.get(pk=member)) serializer = MemberSerializer(common_friends, many=True) return JSONResponse(0, serializer.data) except: return JSONResponse(1001) else: return JSONResponse(1054)
def MemberRegistration(request): ''' 用户登录时拉取用户信息 :param request: :return: ''' if request.method == 'GET': try: data = request.GET # 判断用户是否已注册 with transaction.atomic(): if QyMember.objects.filter(openid=data.get('openid')): user = QyMember.objects.get(openid=data.get('openid')) if user.avatar.startswith('http'): user.avatar = data.get('avatar', user.avatar) user.nickname = data.get('nickname', user.nickname) user.save() else: user = QyMember(openid=data.get('openid'), avatar=data.get('avatar'), nickname=data.get('nickname'), add_time=int(time.time())) user.save() serializer = Member_feedback(user, many=False) return JSONResponse(0, serializer.data) except Exception as e: return JSONResponse(1000) else: return JSONResponse(1054)
def createMessage(request): ''' 用户创建留言 :param request: :return: ''' if request.method == 'POST': data = json.loads(request.body) try: with transaction.atomic(): member_id = QyMember.objects.get(openid=data.get('openid')).id if not data.get('activity_id'): return JSONResponse(1007) obj = QyMessage(activity=data.get('activity_id'), member_id=member_id, message_content=data.get('message_content'), message_img=data.get('message_img'), add_time=str(int(time.time()))) obj.save() return JSONResponse(0) except Exception as e: return JSONResponse(1001) else: return JSONResponse(1055)
def Callback_verification(request): ''' 微信支付成功回调验证 :param request: :return: ''' if request.method == 'POST': try: request.data = request.body callback_info = wx.verify_notify(request) if callback_info.result['return_code'] == 'SUCCESS': if callback_info.result['result_code'] == 'SUCCESS': with transaction.atomic(): obj = QyPay.objects.get( out_trade_no=callback_info.result['out_trade_no']) obj.pay_state = 3 obj.save() user = QyMember.objects.get(pk=obj.member_id) user.balance = "%.2f" % (eval(user.balance) + eval(obj.pay_amount)) user.save() info = { "return_code": callback_info.result['return_code'], "return_msg": 'OK', } return HttpResponse(wx.to_xml(info)) else: return JSONResponse(1046) except Exception as e: print(e) return JSONResponse(1000) else: return JSONResponse(1054)
def select_member_list(request): ''' 根据条件获取指定用户列表 :param request: :return: ''' if request.method == 'POST': try: phone_num = json.loads(request.body).get('phoneNum') nick_name = json.loads(request.body).get('nickName') page = json.loads(request.body).get('page') if nick_name: user_list = QyMember.objects.filter( nickname__contains=nick_name) elif phone_num: user_list = [] # 查询手机号不为空的用户 for x in QyMember.objects.filter(phonenum__isnull=False): if str(x.phonenum).__contains__(str(phone_num)): user_list.append(x) else: user_list = QyMember.objects.all() pag = mypage(page, user_list) serializer = MemberSerializer(pag['page_list'], many=True) return JSONResponse(0, serializer.data, pag['page_sum'], pag['page_count']) except: return JSONResponse(1000) else: return JSONResponse(1054)
def update_put_forward_record(request): ''' 修改提现记录状态 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) with transaction.atomic(): info = QyPay.objects.get(pk=data.get('id'),pay_type=2) if data.get('pay_state') == 3: info.pay_state = data.get('pay_state', info.pay_state) info.save() elif data.get('pay_state') == 1: user = QyMember.objects.get(pk=info.member_id) user.balance = "%.2f" % (eval(user.balance) + eval(info.pay_amount)) user.save() info.pay_state = data.get('pay_state', info.pay_state) info.save() infoes = QyPay.objects.get(pk=info.pk) serializer = PaySerializer(infoes, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1010) else: return JSONResponse(1054)
def create_put_forward_record(request): ''' 创建提现记录 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) out_trade_no = str(int(time.time() * 1000000)) with transaction.atomic(): member = QyMember.objects.get(openid=data.get('openid')) if eval(member.balance) < eval(data.get('pay_amount')): return JSONResponse(2003) else: member.balance = "%.2f" % (eval(member.balance) - eval(data.get('pay_amount'))) member.save() pay = QyPay( pay_type=2, out_trade_no=out_trade_no, member_id=member.id, pay_amount="%.2f" % eval(data.get('pay_amount')), pay_state=data.get('pay_state'), add_time=int(time.time()) ) pay.save() serializer = PaySerializer(pay, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1000) else: return JSONResponse(1054)
def get_info(request): ''' 获取用户'持卡人','身份证号','银行卡号','银行预留手机号' :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) open_id = data.get('openid') # 获取登录用户 user = QyMember.objects.get(openid=open_id) with transaction.atomic(): user.cardholder = data.get('cardholder',user.cardholder) user.ID_number = data.get('ID_number',user.ID_number) user.opening_bank = data.get('opening_bank',user.opening_bank) user.bank_card_number = data.get('bank_card_number',user.bank_card_number) user.reserved_phone_number = data.get('reserved_phone_number',user.reserved_phone_number) user.save() users = QyMember.objects.get(openid=open_id) serializer = MemberPayment(users, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1000) else: return JSONResponse(1054)
def setAdminPassword(request): ''' 修改密码 :param request: username :return: 成功/失败 ''' if request.method == 'POST': try: data = json.loads(request.body) with transaction.atomic(): username = data.get('username') old_password = data.get('old_password') new_password = data.get('new_password') if old_password == new_password: return JSONResponse(3010) user = User.objects.get(username=username) ret = user.check_password(old_password) if ret: user.set_password(new_password) user.save() return JSONResponse(0, {'username': username}) else: return JSONResponse(3005) except Exception as e: return JSONResponse(3009)
def Weixin_recharge(request): ''' 微信充值并创建充值记录 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) price = int(data.get('price') * 100) if price < 1: return JSONResponse(1047) out_trade_no = str(int(time.time() * 1000000)) request_info = { 'trade_type': 'JSAPI', # 签名类型 'out_trade_no': out_trade_no, # 定单号 'total_fee': price, # 充值金额 分为单位 'openid': data.get('openid'), 'body': '点球吧用户充值', } # 生成统一下单请求:(交易类型,定单号,标价金额,用户标识,通知地址) unifiedorder = wx.unifiedorder(**request_info) if unifiedorder.result['return_code'] == 'SUCCESS': # 如果请求成功 if unifiedorder.result['result_code'] == 'SUCCESS': # 业务结果 prepay_id = 'prepay_id={}'.format(unifiedorder.prepay_id) timestamp = str(int(time.time())) info = { "appId": unifiedorder.result['appid'], "nonceStr": wx.random_str(), "package": prepay_id, "signType": "MD5", "timeStamp": timestamp } paySign = wx._gen_sign(info) info['paySign'] = paySign with transaction.atomic(): member_id = QyMember.objects.get( openid=data.get('openid')).id user = QyPay(pay_type=1, out_trade_no=out_trade_no, member_id=member_id, pay_amount="%.2f" % float(data.get('price')), pay_state=2, add_time=int(time.time())) user.save() # 签名后返回给前端做支付参数 return JSONResponse(0, info) else: return JSONResponse(1046) except: return JSONResponse(1000) else: return JSONResponse(1054)
def log_out(request): ''' 登出 :param request: :return: ''' if request.method == 'GET': try: with transaction.atomic(): logout(request) return JSONResponse(0) except: return JSONResponse(1001)
def file(request): ''' 文件上传 :param request: :return: ''' if request.method == 'POST': try: data = request.FILES.getlist('img') img = MyFile().mybase64(data) return JSONResponse(0,img) except: return JSONResponse(1007)
def myParticipatingActivity(request): ''' 首页 我已报名的且未过期或已取消的活动详情 :param request:openid,sort,address :return: ''' if request.method == 'GET': try: openid = request.GET.get('openid') sort = request.GET.get('sort') member_id = QyMember.objects.get(openid=openid).id # 我组织的活动 my_objs = QyActivity.objects.filter(originator_id=member_id) act_id = [] for x in my_objs: act_id.append(x.id) # 我参加的活动 objs = QyActor_activities.objects.filter(member_id=member_id) objs_act = [] for y in objs: objs_act.append(y.activity_id) # 取并集 act = list(set(act_id).union(set(objs_act))) # 筛选其中没有过期的活动 activities_id = [] for x in act: obj = QyActivity.objects.filter(pk=x,activity_state__lte=1) if obj: activities_id.append(obj[0].id) # 对筛选过的活动排序 if sort == '3': address = request.GET.get('address') activities = QyActivity.objects.filter( pk__in=activities_id, address_detailed__contains=address) else: activities = QyActivity.objects.filter( pk__in=activities_id).order_by(my_sort(sort)) # 分页 pag = mypage(request.GET.get('page'), activities) # 序列化 serializer = Act_info(pag['page_list'],many=True) return JSONResponse(0,serializer.data) except Exception as e: return JSONResponse(1007)
def get_member_info(request): ''' 获取当前用户信息 ''' if request.method == 'GET': try: user_info = QyMember.objects.get(openid=request.GET.get('openid')) # 返回JSON字符串 serializer = MemberLister(user_info, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1000) else: return JSONResponse(1054)
def Activity_info(request): ''' 获取指定活动详情 :param request: :return: ''' if request.method == 'GET': try: obj = QyActivity.objects.get(pk=request.GET.get('pk')) serializer = Act_info(obj, many=False) return JSONResponse(0,serializer.data) except: return JSONResponse(1007) else: return JSONResponse(1054)
def get_recharge_details(request): ''' 获取当前提现充值/提现详情 :param request: :return: ''' if request.method == 'GET': try: obj = QyPay.objects.get(pk=request.GET.get('id'), pay_type=request.GET.get('pay_type')) serializer = PaySerializer(obj, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1010) else: return JSONResponse(1054)
def Boolean(request): ''' 返回审核状态 :param request: :return: ''' if request.method == 'GET': try: info = QyAuditing.objects.get(pk=1) # 返回JSON字符串 serializer = AuditingSerializer(info, many=False) return JSONResponse(0, serializer.data) except: return JSONResponse(1000) else: return JSONResponse(1054)
def delete_admin(request): ''' 删除后台用户 :param request: :return: ''' if request.method == 'POST': try: with transaction.atomic(): data = json.loads(request.body) id = data.get('admin_id') cursor = connection.cursor() cursor.execute('delete from auth_user where id=' + str(id)) return JSONResponse(0) except Exception as e: return JSONResponse(1056)
def select_recharge_record(request): ''' 根据条件获取指定充值/提现列表 :param request: :return: ''' if request.method == 'POST': try: state = json.loads(request.body).get('pay_state') phone_num = json.loads(request.body).get('phoneNum') nick_name = json.loads(request.body).get('nickName') pay_type = json.loads(request.body).get('pay_type') if state and (not nick_name) and (not phone_num): recharge_list = QyPay.objects.filter( pay_state=state, pay_type=pay_type).order_by('-id') elif nick_name and (not state) and (not phone_num): member_list = QyMember.objects.filter( nickname__contains=nick_name) recharge_list = Pay_ergodic(member_list, pay_type) elif phone_num and (not nick_name) and (not state): member_list = [] for x in QyMember.objects.filter(phonenum__isnull=False): if str(x.phonenum).__contains__(str(phone_num)): member_list.append(x) recharge_list = Pay_ergodic(member_list, pay_type) elif state and nick_name: member_list = QyMember.objects.filter( nickname__contains=nick_name) recharge_list = Pay_ergodic(member_list, pay_type, state) elif state and phone_num: member_list = [] for x in QyMember.objects.filter(phonenum__isnull=False): if str(x.phonenum).__contains__(str(phone_num)): member_list.append(x) recharge_list = Pay_ergodic(member_list, pay_type, state) else: recharge_list = QyPay.objects.filter( pay_type=pay_type).order_by('-id') # 进行分页 pag = mypage(json.loads(request.body).get('page'), recharge_list) serializer = PaySerializer(pag['page_list'], many=True) return JSONResponse(0, serializer.data, pag['page_sum'], pag['page_count']) except: return JSONResponse(1000) else: return JSONResponse(1054)
def delete_message(request): ''' 删除留言 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) message_id = data.get('message') QyMessage.objects.get(pk=message_id).delete() QyReply.objects.filter(message_id=message_id).delete() return JSONResponse(0) except: return JSONResponse(1001) else: return JSONResponse(1055)
def Activity_org_sum(request): ''' 获取我曾经组织过的活动数 :param request: :return: ''' if request.method == 'GET': try: originator_id = QyMember.objects.get( openid=request.GET.get('openid')).id obj = len(QyActivity.objects.filter( originator_id=originator_id,activity_state=2)) return JSONResponse(0,{'sum':obj}) except: return JSONResponse(1007) else: return JSONResponse(1054)
def Activity_up(request): ''' 修改指定活动 :param request: :return: ''' if request.method == 'PUT': try: data = json.loads(request.body) obj = QyActivity.objects.get( pk=data.get('pk'),activity_state=1) except: return JSONResponse(1007) try: with transaction.atomic(): try: activity_img = data.get('activity_img') # 修改活动图片并且有历史图片时,删除旧图片 if eval(obj.activity_img) and activity_img: for a in eval(obj.activity_img): os.remove("./{}".format(a)) except: pass obj.activity_img = data.get('activity_img',obj.activity_img) obj.notice = data.get('notice', obj.notice) obj.activity_time = data.get('activity_time', obj.activity_time) obj.address_province = data.get('address_province', obj.address_province) obj.address_city = data.get('address_city', obj.address_city) obj.address_area = data.get('address_area', obj.address_area) obj.address_detailed = data.get('address_detailed', obj.address_detailed) obj.formater = data.get('formater', obj.formater) obj.type = data.get('type', obj.type) obj.upper_limit = data.get('upper_limit', obj.upper_limit) obj.lower_limit = data.get('lower_limit', obj.lower_limit) obj.is_limit = data.get('is_limit', obj.is_limit) obj.limit_time = data.get('limit_time', obj.limit_time) obj.activity_state = data.get('activity_state', obj.activity_state) obj.is_irres = data.get('is_irres',obj.is_irres) obj.irres_time = data.get('irres_time',obj.irres_time) obj.save() serializer = Act_info(obj, many=False) return JSONResponse(0,serializer.data) except Exception as a: return JSONResponse(1000) else: return JSONResponse(1057)
def resetPassword(request): ''' 重置密码 :param request: username :return: 成功/失败 ''' if request.method == 'POST': try: data = json.loads(request.body) with transaction.atomic(): username = data.get('username') user = User.objects.get(username=username) user.set_password('123456') user.save() return JSONResponse(0, {'username': username}) except Exception as e: return JSONResponse(3009)
def UserGetOpenID(request): ''' 根据前端code码返回相应的openID :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) js_code = data.get('code') requestString = 'https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={SECRET}&js_code={JSCODE}&grant_type=authorization_code'.format( APPID=appid, SECRET=appsecret, JSCODE=js_code) r = requests.get(requestString).json() return JSONResponse(0, r) except Exception as e: return JSONResponse(1000) else: return JSONResponse(1054)
def delete_myfriends(request): ''' 删除球友 :param request: :return: ''' if request.method == 'POST': try: data = json.loads(request.body) QyGroup.objects.get(member_a_id=data.get('member_a_id'), member_b_id=data.get('member_b_id')).delete() QyGroup.objects.get(member_b_id=data.get('member_a_id'), member_a_id=data.get('member_b_id')).delete() return JSONResponse(0) except: return JSONResponse(1057) else: JSONResponse(1055)