1 接收微信 服务器get 请求发过来的参数
@csrf_exempt
def weixin_main(request):
# get 请求是验证
if request.method == "GET":
# 接收微信服务器get请求发过来的参数
signature = request.GET.get('signature', None)
timestamp = request.GET.get('timestamp', None)
nonce = request.GET.get('nonce', None)
echostr = request.GET.get('echostr', None)
# 服务器配置中的token
token = 'gongchang'
# 把参数放到list中排序后合成一个字符串,再用sha1加密得到新的字符串与微信发来的signature对比,如果相同就返回echostr给服务器,校验通过
hashlist = [token, timestamp, nonce]
hashlist.sort()
hashstr = ''.join([s for s in hashlist])
hashstr = hashlib.sha1(hashstr).hexdigest()
if hashstr == signature:
return HttpResponse(echostr)
else:
return HttpResponse("field")
elif request.method == "POST":
webData = request.body
xmlData = ET.fromstring(webData)
msg_type = xmlData.find('MsgType').text
to_user_name = xmlData.find('ToUserName').text
from_user_name = xmlData.find('FromUserName').text
creat_time = xmlData.find('CreateTime').text
info = Config.objects.get(config_name='shovel_free')
data = info.config_value
data = json.loads(data)
wx_num = data.get('wx_num')
now = time.strftime('%Y%m%d')
if msg_type == 'event':
event = xmlData.find('Event').text
# 关注事件
if event == 'subscribe':
eventkey = xmlData.find('EventKey').text # qrscene_14 为关注数据形式
# 获取access_token
# 微信公共号返回二种信息流 1未关注 qrscene_6 2 已关注 不发送 qrscene_data_123
try:
eventkey = int(eventkey[8:].encode("utf-8"))
except:
try:
eventkey = str(eventkey[13:].encode("utf-8"))
reply_info = "提取码为:%s" % eventkey
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
except:
# 用户关注时,自动推送信息
content = "终于等到你~感谢关注! ❤\n我们为你提供海量关键词的快速挖掘与智能导出服务,同时还可对关键词进行统一管理," \
"便于使用。\n在这里,你可以享受:\n挖词进度即时通知,挖词结果一键查询,还可免费领铲子哦!\n" \
"第一时间告诉你最新的优惠福利,做你最贴心的挖词小助手。\n回复【免费领铲子】,即可每天免费领取%s把铲子," \
"快来试一下吧" % wx_num
replyMsg = TextMsg(from_user_name, to_user_name, content)
return HttpResponse(replyMsg.send())
# 统一获取微信公共号
access_token = get_access_token()
if access_token:
# 获取微信用户的基本信息
user_info_url = u'https://api.weixin.qq.com/cgi-bin/user/info'
params = {
'access_token': access_token,
'openid': from_user_name,
'lang': settings.WEIXIN_APPSECRET_TT,
}
try:
userinfo = requests.get(user_info_url, params=params).json()
nickname = userinfo['nickname']
try:
user_wx_info = UserWxInfo.objects.get(openid=from_user_name, uid=eventkey)
user_wx_info.wx_username = nickname
user_wx_info.focus_state = 1
user_wx_info.save()
except:
# 两种情况 1.微信公共号未被使用 2. 重新绑定新用户,老用户解除绑定
try:
user_wx_info = UserWxInfo.objects.get(openid=from_user_name)
user_wx_info.openid = ''
user_wx_info.focus_state = 0
user_wx_info.save()
except:
pass
try:
user_wx_info = UserWxInfo.objects.get(uid=int(eventkey))
user_wx_info.openid = from_user_name
user_wx_info.wx_username = nickname
user_wx_info.focus_state = 1
user_wx_info.save()
except:
user_com = UserWxInfo(uid=eventkey, openid=from_user_name, wx_username=nickname,
focus_state=1, focus_state_done=1)
user_com.save()
# 用户账户表铲子变动更新
try:
_ = FinanceAccount.objects.get(uid=eventkey)
except Exception as e:
pass
else:
_.shovel = int(5) + _.shovel # 铲子数量保存
_.save()
shovel_obj = FinanceAccount.objects.get(uid=eventkey)
# 产自明细表
data = FinanceShovelLog(uid=eventkey, incr_num=5, reason='奖励', type=2,
reason_meta='首次关注微信公众号', shovel=shovel_obj.shovel)
data.save()
except:
# 微信公共号获取出现错误不做任何处理
# raise FieldError("openid", "获取openid错误")
pass
# 用户关注时,自动推送信息
content = "终于等到你~感谢关注! ❤\n我们为你提供海量关键词的快速挖掘与智能导出服务,同时还可对关键词进行统一管理," \
"便于使用。\n在这里,你可以享受:\n挖词进度即时通知,挖词结果一键查询,还可免费领铲子哦!\n" \
"第一时间告诉你最新的优惠福利,做你最贴心的挖词小助手。\n回复【免费领铲子】,即可每天免费领取%s把铲子," \
"快来试一下吧" % wx_num
replyMsg = TextMsg(from_user_name, to_user_name, content)
return HttpResponse(replyMsg.send())