@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')
try:
if msg_type == 'text':
content = xmlData.find("Content").text
try:
if content == '免费领铲子':
user_wx_info = UserWxInfo.objects.get(openid=from_user_name)
sign_time = user_wx_info.sign_time
if user_wx_info.focus_state == 1:
if sign_time:
if int(now) == int(sign_time):
reply_info = "今日铲子已领,请明日再来哦 <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>更多免费领铲子攻略</a> ~"
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
else:
user_wx_info.sign_time = now
user_wx_info.sign_state = 1
user_wx_info.save()
# 用户账户变动保存
try:
_ = FinanceAccount.objects.get(uid=user_wx_info.uid)
except Exception as e:
pass
else:
_.uptime = int(time.time())
_.shovel = decimal.Decimal(wx_num) + _.shovel # 铲子数量保存
_.save()
# 签到领铲子保存
data = FinanceShovelLog(uid=user_wx_info.uid, incr_num=decimal.Decimal(wx_num), reason='奖励', type=2,
reason_meta='公众号领取', shovel=decimal.Decimal(wx_num) + _.shovel)
data.save()
reply_info = "领取成功,铲子+%s <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>更多免费领铲子攻略</a> ~" % wx_num
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
else:
user_wx_info.sign_time = now
user_wx_info.sign_state = 1
user_wx_info.save()
# 用户账户变动保存
try:
_ = FinanceAccount.objects.get(uid=user_wx_info.uid)
except Exception as e:
pass
else:
_.uptime = int(time.time())
_.shovel = int(wx_num) + _.shovel # 铲子数量保存
_.save()
# 签到领铲子保存
data = FinanceShovelLog(uid=user_wx_info.uid, incr_num=wx_num, reason='奖励', type=2,
reason_meta='公众号领取', shovel=int(wx_num) + _.shovel)
data.save()
reply_info = "领取成功,铲子+%s <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>更多免费领铲子攻略</a> ~" % wx_num
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
if user_wx_info.focus_state == 0:
reply_info = "领取失败,你的微信未绑定星矿账号,请绑定成功后再进行领取吧~"
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
except:
reply_info = "领取失败,你的微信未绑定星矿账号,请绑定成功后再进行领取吧~"
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
if content == '获得铲子':
reply_info = "免费获取更多铲子,点此查看 <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>铲子获取攻略</a> ~"
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
if content == '邀请有奖':
scheme = config["starkeyword_antd"]["scheme"]
host = config["starkeyword_antd"]["host"]
long_url ="%s://%s/subject/20191111/home"% (scheme, host)
shorts_url = get_short_url(long_url)
reply_info = '【招募矿主天天挖宝】邀2个好友抢小米AI音箱,点击挖宝:%s' % shorts_url
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
else:
reply_info = "请您输入准确的关键词,如:免费领铲子,邀请有奖,获得铲子"
replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
return HttpResponse(replyMsg.send())
except Exception as e:
replyMsg = TextMsg(from_user_name, to_user_name, '你的微信未绑定星矿账号,请绑定成功后再进行领取吧~')
return HttpResponse(replyMsg.send())
微信公众号关注后,关键词回复
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.