def qiniu_oper(request, oper_type): response = Response.ResponseObj() if oper_type == 'get_token': SecretKey = 'wVig2MgDzTmN_YqnL-hxVd6ErnFhrWYgoATFhccu' AccessKey = 'a1CqK8BZm94zbDoOrIyDlD7_w7O8PqJdBHK-cOzz' q = qiniu.Auth(AccessKey, SecretKey) bucket_name = 'bjhzkq_tianyan' token = q.upload_token(bucket_name) # 可以指定key 图片名称 response.code = 200 response.msg = '生成成功' response.data = {'token': token} elif oper_type == 'test_article': objs = models.Goods.objects.all() for obj in objs: print('obj.id--> ', obj.id, obj.goods_describe) goods_describe = [] for i in json.loads(obj.goods_describe): status = i.get('status') content = i.get('content') if status == 'img' and 'http://tianyan.zhugeyingxiao.com' not in content: path = requests_img_download(content) img = update_qiniu(path) goods_describe.append({ 'status': status, 'content': img, }) else: goods_describe.append(i) obj.goods_describe = goods_describe obj.save() return JsonResponse(response.__dict__)
def update_customer_set_avator(request): try: objs = models.Customer.objects.filter(set_avator__isnull=False) for obj in objs: if 'http://tianyan.zhugeyingxiao.com' not in obj.set_avator: set_avator = requests_img_download(obj.set_avator) set_avator = update_qiniu(set_avator) obj.set_avator = set_avator obj.save() except Exception as e: msg = '警告:{}, \n错误:{}, \n时间:{}'.format( 'celery_更新客户头像到七牛云 发送---警告', e, datetime.datetime.today() ) celery_error_warning(msg) return HttpResponse('')
def updateUserInfo(openid, inviter_user_id, ret_obj, msg=None, enterprise_id=1): # msg访问日志记录 enterprise_id 公司ID """ :param openid: 微信openid :param inviter_user_id: 邀请人id :param ret_obj: 微信数据 :return: """ print('ret_obj -->', ret_obj) """ { 'subscribe_scene': 'ADD_SCENE_QR_CODE', 'city': '丰台', 'openid': 'oX0xv1pJPEv1nnhswmSxr0VyolLE', 'qr_scene': 0, 'tagid_list': [], 'nickname': '张聪', 'subscribe_time': 1527689396, 'country': '中国', 'groupid': 0, 'subscribe': 1, 'qr_scene_str': '{"timestamp": "1527689369548"}', 'headimgurl': 'http://thirdwx.qlogo.cn/mmopen/oFswpUmYn53kTv5QdmmONicVJqp3okrhHospu6icoLF7Slc5XyZWR 96STN9RiakoBQn1uoFJIWEicJgJ1QjR5iaGOgWNQ5BSVqFe5/132', 'province': '北京', 'sex': 1, 'language': 'zh_CN', 'remark': '' } { "openid":"oX0xv1pJPEv1nnhswmSxr0VyolLE", "nickname":"张聪", "sex":1, "language":"zh_CN", "city":"丰台", "province":"北京", "country":"中国", "headimgurl":"http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJWGnNTvluYlHj8qt8HnxMlwbRiad bv4TNrp4watI2ibPPAp2Hu6Sm1BqYf6IicNWsSrUyaYjIoy2Luw/132", "privilege":[] } """ # 保证1个微信只能够关联1个账号 user_objs = models.Userprofile.objects.filter(openid=openid) encode_username = base64_encryption.b64encode(ret_obj['nickname']) path = requests_img_download(ret_obj.get('headimgurl')) set_avator = update_qiniu(path) user_data = { "sex": ret_obj.get('sex'), "country": ret_obj.get('country'), "province": ret_obj.get('province'), "city": ret_obj.get('city'), "headimgurl": ret_obj.get('headimgurl'), "wechat_name": encode_username, "last_active_time": datetime.datetime.today(), "is_send_msg": 0, } if user_objs: user_obj = user_objs[0] if int(user_obj.is_send_msg) == 1: # 解除24小时未互动限制 post_data = { "touser": user_obj.openid, "msgtype": "text", "text": { "content": """限制已解除, 天眼将继续为您推送消息!{}""".format(zhayan) } } data = get_ent_info(user_obj.id) weixin_objs = WeChatApi(data) # 发送客服消息 post_data = bytes(json.dumps(post_data, ensure_ascii=False), encoding='utf-8') weixin_objs.news_service(post_data) user_id = user_objs[0].id user_objs.update(**user_data) else: # encodestr = base64.b64encode(ret_obj['nickname'].encode('utf8')) # encode_username = str(encodestr, encoding='utf8') overdue_date = datetime.datetime.now() + datetime.timedelta(days=30) subscribe = ret_obj.get('subscribe') # 如果没有关注,获取个人信息判断是否关注 if not subscribe: data = get_ent_info(inviter_user_id) weichat_api_obj = WeChatApi(data) ret_obj = weichat_api_obj.get_user_info(openid=openid) subscribe = ret_obj.get('subscribe') user_data['enterprise_id'] = enterprise_id user_data['last_active_time'] = datetime.datetime.today() user_data['wechat_name'] = encode_username user_data['headimgurl'] = ret_obj.get('headimgurl') user_data['inviter_id'] = inviter_user_id user_data['set_avator'] = set_avator user_data['subscribe'] = subscribe user_data['name'] = encode_username user_data['openid'] = ret_obj.get('openid') user_data['overdue_date'] = overdue_date user_data['token'] = get_token() print("user_data --->", user_data) user_obj = models.Userprofile.objects.create(**user_data) user_id = user_obj.id pub_log_access(user_id, msg) # 记录访问日志 return user_id
def user_login_oper(request, oper_type): response = Response.ResponseObj() # 判断该用户是否存在 now = datetime.datetime.today() models.save_code.objects.filter(create_datetime__lt=now).delete() code = request.GET.get('code') objs = models.save_code.objects.filter(save_code=code) if not objs: appid = oper_type.split('_')[-1] oper_type = oper_type.split('_' + appid)[0] models.save_code.objects.create(save_code=code) data = get_ent_info(1, appid) weichat_api_obj = WeChatApi(data) ret_obj = weichat_api_obj.get_openid(code) # 获取用户信息 print("ret_obj['nickname']------------> ", ret_obj['nickname']) try: encode_username = b64encode(ret_obj['nickname']) except Exception: encode_username = '' openid = ret_obj.get('openid') user_data = { "sex": ret_obj.get('sex'), "country": ret_obj.get('country'), "province": ret_obj.get('province'), "city": ret_obj.get('city'), "headimgurl": ret_obj.get('headimgurl'), # 更新微信头像 "wechat_name": encode_username, "last_active_time": datetime.datetime.today(), "is_send_msg": 0, # 互动超时消息 互动过改为未发 } user_objs = models.Userprofile.objects.filter(openid=openid) if user_objs: # 客户已经存在 user_obj = user_objs[0] # uf user_obj.enterprise.status == 1: # 如果后台开启 if int(user_obj.is_send_msg) == 1: # 解除24小时未互动限制 post_data = { "touser": user_obj.openid, "msgtype": "text", "text": { "content": """限制已解除, 天眼将继续为您推送消息!{}""".format(zhayan) } } data = get_ent_info(user_obj.id) weixin_objs = WeChatApi(data) # 发送客服消息 post_data = bytes(json.dumps(post_data, ensure_ascii=False), encoding='utf-8') weixin_objs.news_service(post_data) user_objs.update(**user_data) user_objs = user_objs[0] else: # 不存在,创建用户 path = requests_img_download(ret_obj.get('headimgurl')) set_avator = update_qiniu(path) # 上传至七牛云 # # 如果没有关注,获取个人信息判断是否关注 # if not subscribe: # weichat_api_obj = WeChatApi() # ret_obj = weichat_api_obj.get_user_info(openid=openid) # subscribe = ret_obj.get('subscribe') user_data['last_active_time'] = datetime.datetime.today() user_data['wechat_name'] = encode_username user_data['set_avator'] = set_avator user_data['headimgurl'] = ret_obj.get('headimgurl') user_data['subscribe'] = True user_data['name'] = encode_username user_data['openid'] = ret_obj.get('openid') user_data['token'] = get_token() user_data['overdue_date'] = datetime.datetime.now( ) + datetime.timedelta(days=30) user_objs = models.Userprofile.objects.create(**user_data) user_id = user_objs.id pub_log_access(user_id, msg='用户点击公众号菜单栏登录') # 记录访问日志 redirect_url = '{host}?user_id={user_id}&token={token}&classify_type=1&page_type={page_type}'.format( host=host_url, token=user_objs.token, user_id=user_id, page_type=oper_type, ) print('redirect_url----------------------------> ', redirect_url) return redirect(redirect_url) else: response.code = 301 response.msg = '请重新登录' return JsonResponse(response.__dict__)
def user_oper(request, oper_type, o_id): response = Response.ResponseObj() user_id = request.GET.get('user_id') print('request.POST -->', request.POST) if request.method == "POST": # 设置推荐分类 if oper_type == "update_recommend_classify": classify_id = request.POST.get('classify_id[]') if classify_id: recommend_classify_list = [ int(i) for i in json.loads(classify_id) ] print("recommend_classify_list -->", recommend_classify_list) user_obj = models.Userprofile.objects.get(id=user_id) user_obj.recommend_classify = recommend_classify_list response.code = 200 response.msg = "设置成功" else: response.code = 301 response.msg = "分类id传参异常" # 修改头像 elif oper_type == "update_head_portrait": img_path = request.POST.get('img_path') if img_path: models.Userprofile.objects.filter(id=user_id).update( set_avator=img_path) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = "头像不能传参异常" # 修改姓名 elif oper_type == "update_name": name = request.POST.get('name') if name: if len(name) <= 9: name = b64encode(name) models.Userprofile.objects.filter(id=user_id).update( name=name) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = '名字最大长度为8' else: response.code = 301 response.msg = "姓名传参异常" # 修改手机号 elif oper_type == "update_phone_number": phone_number = request.POST.get('phone_number') if verify_mobile_phone_number(phone_number): models.Userprofile.objects.filter(id=user_id).update( phone_number=phone_number) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = "请填写正确手机号" # 修改微信二维码 elif oper_type == "update_qr_code": qr_code = request.POST.get('qr_code') if qr_code: models.Userprofile.objects.filter(id=user_id).update( qr_code=qr_code) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = "微信二维码传参异常" # 修改个性签名 elif oper_type == "update_signature": signature = request.POST.get('signature') if signature: models.Userprofile.objects.filter(id=user_id).update( signature=signature) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = "个性签名传参异常" # 修改文章底部是否显示产品 elif oper_type == "update_show_product": show_product = request.POST.get('show_product') flag = re.match(r"^[01]$", show_product) if show_product and flag: models.Userprofile.objects.filter(id=user_id).update( show_product=int(show_product)) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = "是否显示产品传参异常" # 设置消息提醒 elif oper_type == 'message_remind_setting': objs = models.Userprofile.objects.filter(id=user_id) objs.update(message_remind=o_id) response.code = 200 response.msg = '修改成功' else: # 查询我的会员 有效期 和剩余天数/会员类型 if oper_type == "member_info": obj = models.Userprofile.objects.get(id=user_id) # vip 类型 vip_type = obj.get_vip_type_display() # 时间对象 - 年月日时分秒 now_datetime_obj = datetime.datetime.now() # 时间对象 - 年月日 now_date_obj = datetime.date(now_datetime_obj.year, now_datetime_obj.month, now_datetime_obj.day) # 计算剩余天数 remaining_days = (obj.overdue_date - now_date_obj).days # 如果已经过期,则剩余过期时间为0,vip类型为vip已过期 if remaining_days <= 0: if remaining_days == 0: remaining_days = '今' else: remaining_days = 0 obj.vip_type = 0 obj.save() obj = models.Userprofile.objects.get(id=user_id) vip_type = obj.get_vip_type_display() response.code = 200 response.data = { 'vip_type': vip_type, 'overdue_date': obj.overdue_date.strftime('%Y-%m-%d'), 'remaining_days': str(remaining_days) + '天' } response.note = { 'vip_type': "vip类型", 'overdue_date': "有效期至", 'remaining_days': "剩余天数" } # 使用微信头像 elif oper_type == 'use_wechat_avatar': objs = models.Userprofile.objects.filter(id=user_id) path = requests_img_download(objs[0].headimgurl) set_avator = update_qiniu(path) objs.update(set_avator=set_avator) response.code = 200 response.msg = '修改成功' # 推广赚钱 展示数据 elif oper_type == 'affiliate': user_pub_objs = models.Userprofile.objects response_data = {} user_obj = user_pub_objs.get(id=user_id) response_data[ 'cumulative_amount'] = user_obj.cumulative_amount # 累计钱数 response_data['make_money'] = user_obj.make_money # 待提钱数 invite_objs = user_pub_objs.filter(inviter_id=user_id) invite_friend_list = [ i.get('id') for i in invite_objs.values('id') ] # 该邀请人 邀请的好友ID print('invite_friend_list----------> ', invite_friend_list) # data_list = [] # for i in invite_friend_list: # data_list.insert(0, i) # data_list.extend([i.get('id') for i in models.Userprofile.objects.filter(inviter_id=i).values('id')]) # print('data_list--------------> ', data_list) invite_objs = models.Userprofile.objects.filter( id__in=invite_friend_list) response_data['invite_number_count'] = invite_objs.count() # 邀请人数量 # 查询充值自己分销的钱数 number_clinch_deal_objs = models.distribute_money_log.objects.filter( inviter_id=user_id).order_by('-create_date') response_data[ 'number_clinch_count'] = number_clinch_deal_objs.count( ) # 成交人数 if o_id and int(o_id) == 1: # 邀请人数详情 invite_number_list = [] for invite_number_obj in invite_objs: # 该用户未充值展示的话 prepaid_text = '用户未开通会员, 邀请付款有现金奖励{}!'.format(qian) if invite_number_obj.renewal_log_set.count( ) >= 1: # 判断该用户是否充值 money_objs = models.distribute_money_log.objects.filter( inviter_id=user_id, user_id=invite_number_obj.id) if money_objs: money = money_objs[0].money prepaid_text = '成为会员, 加入账户{}元!{}'.format( money, qian) invite_number_list.append({ 'create_user__set_avator': invite_number_obj.set_avator, 'name': b64decode(invite_number_obj.name), 'prepaid_text': prepaid_text, }) response_data['invite_number_list'] = invite_number_list elif o_id and int(o_id) == 2: # 成交人数 number_clinch_deal_list = [] for number_clinch_deal_obj in number_clinch_deal_objs: number_clinch_deal_list.append({ 'user_set_avator': number_clinch_deal_obj.user.set_avator, 'user_name': b64decode(number_clinch_deal_obj.user.name), 'price': number_clinch_deal_obj.price, 'money': number_clinch_deal_obj.money, }) response_data[ 'number_clinch_deal_list'] = number_clinch_deal_list response.code = 200 response.msg = '查询成功' response.data = response_data response.note = { 'number_clinch_deal_list': { 'user_set_avator': '充值人头像', 'user_name': '充值人名称', 'price': '充值钱数', 'money': '应得钱数', }, "invite_number_count": '邀请人数量', "cumulative_amount": '累计钱数', "make_money": '待提钱数', "number_clinch_count": '成交人数', "invite_number_list": '邀请人详情', } # 推广赚钱 二维码截图 elif oper_type == 'affiliate_screenshots': user_obj = models.Userprofile.objects.get(id=user_id) img_path = user_obj.promote_earning_qr_code_pictures expire_date = user_obj.generate_models_qr_code_pictures_time if img_path and expire_date > datetime.date.today(): # 如果有 图片 pass else: if img_path: os.remove(img_path) # 删除原图 img_path, expire_date = tuiguang(user_id) user_obj.promote_earning_qr_code_pictures = img_path user_obj.generate_models_qr_code_pictures_time = expire_date user_obj.save() response.code = 200 response.msg = '生成成功' response.data = {'path': img_path} # 查询消息提醒设置 elif oper_type == 'get_message_remind': user_obj = models.Userprofile.objects.get(id=user_id) objs = models.Userprofile.message_remind_status data_list = [] for obj in objs: is_choose = False if obj[0] == user_obj.message_remind: is_choose = True data_list.append({ 'id': obj[0], 'name': obj[1], 'is_choose': is_choose }) response.code = 200 response.msg = '查询成功' response.data = {'data_list': data_list} # 复制昵称 elif oper_type == 'copy_nickname': obj = models.Customer.objects.get(id=o_id) response.code = 200 response.msg = '查询成功' response.data = {'nickname': b64decode(obj.name)} # 判断该用户是否到期 (到期后不能转发任何东西) elif oper_type == 'is_whether': obj = models.Userprofile.objects.get(id=user_id) now = datetime.date.today() flag = False if obj.overdue_date >= now: flag = True response.code = 200 response.data = {'flag': flag} # 判断是否有手机号 elif oper_type == 'is_phone': obj = models.Userprofile.objects.get(id=user_id) flag = False if obj.phone_number: flag = True response.code = 200 response.msg = '查询成功' response.data = {'phone': flag} else: response.code = 402 response.msg = '请求异常' return JsonResponse(response.__dict__)
def get_article(article_url, get_content=None): headers = {'User-Agent': pcRequestHeader[random.randint(0, len(pcRequestHeader) - 1)]} ret = requests.get(article_url, headers=headers, timeout=10, verify=False) ret.encoding = 'utf-8' s = requests.session() is_video_original_link = None # 是否有视频 如果有则返回原文链接 soup = BeautifulSoup(ret.text, 'lxml') if not get_content: title = re.compile(r'var msg_title = (.*);').findall(ret.text)[0].replace('"', '') # 标题 summary = re.compile(r'var msg_desc = (.*);').findall(ret.text)[0].replace('"', '') # 摘要 cover_url = re.compile(r'var msg_cdn_url = (.*);').findall(ret.text)[0].replace('"', '') # 封面 ## 把封面图片下载到本地 now_time = time.time() # print('--------------------请求封面-0-----------> ', datetime.datetime.today(), cover_url) html = s.get(cover_url) # print('--------------------结束请求封面-0-----------> ', datetime.datetime.today()) if 'wx_fmt=gif' in cover_url: cover_name = "/cover_%s.gif" % (now_time) else: cover_name = "/cover_%s.jpg" % (now_time) cover_url = os.path.join('statics', 'img') + cover_name with open(cover_url, 'wb') as file: file.write(html.content) cover_url = update_qiniu(cover_url) # 获取所有样式 style = "" style_tags = soup.find_all('style') for style_tag in style_tags: style += str(style_tag) # 获取所有图片 body = soup.find('div', id="js_content") body.attrs['style'] = "padding: 20px 16px 12px;" img_tags = soup.find_all('img') for img_tag in img_tags: data_src = img_tag.attrs.get('data-src') if data_src: if img_tag.attrs.get('style'): img_tag.attrs['style'] = img_tag.attrs.get('style') now_time = time.time() # print('------------------开始请求图片----------->', datetime.datetime.today(), data_src) html = s.get(data_src) # print('------------------结束请求图片----------->', datetime.datetime.today()) if 'wx_fmt=gif' in data_src: img_name = "/article_%s.gif" % (now_time) else: img_name = "/article_%s.jpg" % (now_time) file_dir = os.path.join('statics', 'img') + img_name with open(file_dir, 'wb') as file: file.write(html.content) img_url = update_qiniu(file_dir) img_tag.attrs['data-src'] = img_url # img_tag.attrs['data-src'] = URL + '/statics/img' + img_name ## 处理视频的URL flag = False # 判断是否有 视频 iframe = body.find_all('iframe', attrs={'class': 'video_iframe'}) for iframe_tag in iframe: shipin_url = iframe_tag.get('data-src') data_cover_url = iframe_tag.get('data-cover') # 封面 if data_cover_url: data_cover_url = unquote(data_cover_url, 'utf-8') data_cover_url = requests_img_download(data_cover_url) # 下载到本地 data_cover_url = update_qiniu(data_cover_url) vid = shipin_url.split('vid=')[1] if 'wxv' in vid: # 下载 flag = True iframe_url = 'https://mp.weixin.qq.com/mp/videoplayer?vid={}&action=get_mp_video_play_url'.format(vid) ret = requests.get(iframe_url) video_path = ret.json().get('url_info')[0].get('url') iframe_tag_new = """<div style="width: 100%; background: #000; position:relative; height: 0; padding-bottom:75%;"> <video style="width: 100%; height: 100%; position:absolute;left:0;top:0;" id="videoBox" src="{}" poster="{}" controls="controls" allowfullscreen=""></video> </div>""".format(video_path, data_cover_url) else: shipin_url = 'https://v.qq.com/txp/iframe/player.html?origin=https%3A%2F%2Fmp.weixin.qq.com&vid={}&autoplay=false&full=true&show1080p=false&isDebugIframe=false'.format( vid ) iframe_tag.attrs['data-src'] = shipin_url iframe_tag.attrs['allowfullscreen'] = True # 是否允许全屏 iframe_tag.attrs['data-cover'] = data_cover_url iframe_tag_new = str(iframe_tag).replace('></iframe>', ' width="100%" height="300px"></iframe>') body = str(body).replace(str(iframe_tag), iframe_tag_new) body = BeautifulSoup(body, 'html.parser') content = str(style) + str(body) content = BeautifulSoup(content, 'html.parser') if not get_content: # 生成css 文件 now = time.time() style = style.replace('<style>', '').replace('</style>', '') style_path = os.path.join('statics', 'article_css') + '/{}.css'.format(now) with open(style_path, 'w') as e: e.write(style) # style_path = URL + '/statics/article_css/{}.css'.format(now) # 分布标签 data_list = [] for i in content: content = convert_content(s, str(i)) # 替换内容 data_list.append(content) if flag: is_video_original_link = article_url # 原文链接 if not get_content: data = { 'title': title, 'summary':b64encode(summary), 'cover_img':cover_url, 'content': json.dumps(data_list), 'style': style_path, 'original_link': is_video_original_link, } else: data = { 'content': json.dumps(data_list), } return data