@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 == 'CLICK':
eventkey = xmlData.find('EventKey').text
if eventkey == 'V1001_GET_SHOVEL':
try:
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:
replyMsg = TextMsg(from_user_name, to_user_name, '你的微信未绑定星矿账号,请绑定成功后再进行操作')
return HttpResponse(replyMsg.send())
微信公众号,点击事件
最新推荐文章于 2021-05-27 22:02:47 发布