#小程序授权登录
@csrf_exempt
def grantlogin(request):
code=request.POST["code"]
nickname=request.POST["nickname"]
avatarurl=request.POST["avatarurl"]
beinvite_code=request.POST["beinvite_code"] #获取到邀请码
sessionopen=get_sessionkey_openid(code)
openid=sessionopen[0]
sessionkey=sessionopen[1]
#昵称长度超过8位截取
if len(nickname)>8:
nickname=nickname[:5]+"..."
#加密
nickname=base64.b64encode(bytes(nickname,'utf-8'))
if avatarurl=="":
avatarurl="../img/nullhead.png"
#授权登录仅更新头像、昵称、openid、sessionkey
guserexist=guser.objects.filter(guser_openid=openid)
if guserexist:
#用户存在就不算邀请
userinfo=guser.objects.get(guser_openid=openid)
userinfo.guser_nickname=nickname
userinfo.guser_sessionkey=sessionkey
userinfo.guser_avatar=avatarurl
userinfo.save()
else:
if beinvite_code:
#判断邀请码是否存在
inviteuserexist=guser.objects.filter(guser_invitecode=beinvite_code)
if inviteuserexist:
inviteuserinfo=guser.objects.get(guser_invitecode=beinvite_code)
nowtime=timeadd8()
#生成随机邀请码
invite_code=invitecode()
guser.objects.get_or_create(guser_nickname=nickname,
guser_openid=openid,
guser_sessionkey=sessionkey,
guser_avatar=avatarurl,
guser_createtime=nowtime,
guser_curgold=10000, #首次注册赠送10000金币
guser_invitecode=invite_code
)
#邀请列表更新
invitelist.objects.get_or_create(invitelink_guser=inviteuserinfo.guser_openid,
invite_time=nowtime,
beinvite_guser=openid
)
#邀请人金币变化
goldbefore=inviteuserinfo.guser_curgold
goldchange=10000
goldafter=goldbefore+goldchange
goldremark="邀请奖励"
logintask=threading.Thread(target=golddetailtask, args=(inviteuserinfo.guser_openid,goldbefore,goldchange,goldafter,goldremark,nowtime))
logintask.start()
#邀请人奖励
inviteuserinfo.guser_curgold+=10000
inviteuserinfo.guser_invitenum+=1
inviteuserinfo.save()
else:
#邀请人不存在
nowtime=timeadd8()
#生成随机邀请码
invite_code=invitecode()
guser.objects.get_or_create(guser_nickname=nickname,
guser_openid=openid,
guser_sessionkey=sessionkey,
guser_avatar=avatarurl,
guser_createtime=nowtime,
guser_curgold=10000, #首次注册赠送10000金币
guser_invitecode=invite_code
)
else:
#邀请码不存在
nowtime=timeadd8()
#生成随机邀请码
invite_code=invitecode()
guser.objects.get_or_create(guser_nickname=nickname,
guser_openid=openid,
guser_sessionkey=sessionkey,
guser_avatar=avatarurl,
guser_createtime=nowtime,
guser_curgold=10000, #首次注册赠送10000金币
guser_invitecode=invite_code
)
#线程执行授权登录奖励10000金币
nowtime=timeadd8()
goldbefore=0
goldchange=10000
goldafter=goldbefore+goldchange
goldremark="首次注册"
logintask=threading.Thread(target=golddetailtask, args=(openid,goldbefore,goldchange,goldafter,goldremark,nowtime))
logintask.start()
#返回jb、yb值
guserinfo=guser.objects.get(guser_openid=openid)
#返回session 延后 目前缓存保存openid
# wxsession=get3rd_session(openid)
# encrypted_data=wxsession[0]
# key=wxsession[1]
userinfo={}
userinfo["curgold"]=guserinfo.guser_curgold
userinfo["curyb"]=guserinfo.guser_curyb
userinfo["curpass"]=guserinfo.guser_pass
# userinfo["encrypted_data"]=encrypted_data
# userinfo["key"]=key
userinfo=json.dumps(userinfo)
return HttpResponse(userinfo,content_type="application/json")
#openid生成3rd_session 加密
def get3rd_session(openid):
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted_data = cipher.encrypt(openid.encode('utf-8'))
return encrypted_data, key
#3rd_session生成openid 解密
def getopenid_from_session(encrypted_data, key):
cipher = Fernet(key)
raw_data = cipher.decrypt(encrypted_data)
return raw_data
#根据code获取openid跟sessionkey
def get_sessionkey_openid(wxcode):
#引入常量appid跟secret
appid=CYAPPID
secret=CYSECRET
url="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+wxcode+"&grant_type=authorization_code"
res=requests.get(url)
res=res.text
dictinfo=json.loads(res)
openid=dictinfo["openid"]
sessionkey=dictinfo["session_key"]
return openid,sessionkey
#授权用户信息
@csrf_exempt
def grantinfo(request):
code=request.GET["code"]
res=get_sessionkey_openid(code)
guserexist=guser.objects.filter(guser_openid=res[0])
if guserexist:
guserinfo=guser.objects.get(guser_openid=res[0])
resdata={}
resdata["curgold"]=guserinfo.guser_curgold
resdata["curyb"]=guserinfo.guser_curyb
resdata["countyb"]=guserinfo.guser_countyb
resdata["curpass"]=guserinfo.guser_pass
resdata["passranknum"]=guserinfo.guser_passranknum
resdata["ybranknum"]=guserinfo.guser_ybranknum
resdata["invite_code"]=guserinfo.guser_invitecode
resdata["watchnum"]=guserinfo.guser_watchnum
resdata["invitenum"]=guserinfo.guser_invitenum
resdata=json.dumps(resdata)
return HttpResponse(resdata,content_type="application/json")
else:
return HttpResponse("该用户不存在")