def wechatlogin(self, request): session_key = request.data_format.get("session_key") appId = WECHAT_APPID sessionKey = session_key encryptedData = request.data_format.get("encryptedData") iv = request.data_format.get("iv") pc = WXBizDataCrypt(appId, sessionKey) res = pc.decrypt(encryptedData, iv) try: user = UserModelSerializerToRedis(Users.objects.get(uuid=res.get('openId') if 'unionId' not in res else res['unionId']),many=False).data except Users.DoesNotExist: user = Users.objects.create(**{ "userid": idGenerator.userid('4001'), "uuid": res.get('openId') if 'unionId' not in res else res['unionId'], "mobile": res.get('openId') if 'unionId' not in res else res['unionId'], "rolecode": '4001', "name": res.get("nickName"), "sex": res.get("sex"), "addr": "{}-{}-{}".format(res.get("country"), res.get("city"), res.get("province")), "pic": res.get("avatarUrl"), "appid": res.get("watermark")['appid'] }) token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) print(token) return {"data": { "user": UsersSerializers(user, many=False).data, "token": token }}
def login1(self, request): """ 普通玩家 :param request: :return: """ try: user = Users.objects.get(loginname=request.data_format.get('loginname'), rolecode__in=['2001']) except Users.DoesNotExist: raise PubErrorCustom("登录账号或密码错误!") if user.passwd != self.request.data_format.get('password'): raise PubErrorCustom("登录账号或密码错误!") if user.status == 1: raise PubErrorCustom("登陆账号已到期!") elif user.status == 2: raise PubErrorCustom("已冻结!") token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) return {"data": { "token": token, "userinfo":{ "rolecode":user.rolecode } }}
def register(self, request): """ 普通玩家注册 :param request: :return: """ if Users.objects.filter(loginname=request.data_format.get('loginname')).count(): raise PubErrorCustom("该账号已存在!") user = Users.objects.create(**{ "userid":idGenerator.userid("2001"), "name": request.data_format.get("loginname",""), "loginname":request.data_format.get("loginname",""), "passwd": request.data_format.get("password", ""), "mobile": request.data_format.get("mobile", ""), "code": request.data_format.get("code", ""), "rolecode":"2001" }) token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) return {"data": { "token": token, "userinfo": { "rolecode": user.rolecode } }}
def login2(self, request): """ 试玩玩家 :param request: :return: """ user = Users.objects.create(**{ "userid": idGenerator.userid("2002"), "rolecode": "2002", "bal":1000.0 }) user.name = user.userid user.loginname = user.userid user.save() token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) return {"data": { "token": token, "userinfo":{ "rolecode":user.rolecode } }}
def wechatauth(self, request): params = dict( js_code=request.data_format.get("js_code"), appid=WECHAT_APPID, secret=WECHAT_SECRET, grant_type="authorization_code", ) wechat_res = send_request_other( url="https://api.weixin.qq.com/sns/jscode2session", params=params) if not wechat_res.get("openid"): raise PubErrorCustom("获取用户错误,腾讯接口有误!") print(wechat_res) data={} token=False try: user=Users.objects.get(uuid=wechat_res.get('openid')) data = UsersSerializers(user,many=False).data token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) except Users.DoesNotExist: pass return {"data":{ "user" : data, "session_key":wechat_res.get("session_key"), "token":token }}
def login(self, request, *args, **kwargs): """ 登录 :param request: :param args: :param kwargs: :return: """ try: user = Users.objects.get(uuid=request.data_format.get("loginname")) except Users.DoesNotExist: raise PubErrorCustom("登录账户或密码错误!") if user.passwd != request.data_format.get("password"): raise PubErrorCustom("登录账户或密码错误!") if user.status == 1: raise PubErrorCustom("登陆账号已到期!") elif user.status == 2: raise PubErrorCustom("已冻结!") token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).set(res) return {"data": token}
def getUser(self, request): return { "data": UserModelSerializerToRedis(Users.objects.filter( rolecode=request.query_params_format['rolecode']), many=True).data }
def login1(self, request): try: user = Users.objects.get(uuid=request.data_format.get("mobile","")) except Users.DoesNotExist: user = Users.objects.create(**{ "userid": idGenerator.userid('4001'), "uuid": request.data_format.get("mobile",""), "rolecode": '4001', "mobile": request.data_format.get("mobile",""), "name": request.data_format.get("mobile",""), "sex": "", "addr": "", "pic": "", "appid": "" }) if not request.data_format.get('vercode',None): raise PubErrorCustom("验证码不能为空!") rRes = RedisVercodeHandler().get(user.uuid) if rRes and not len(rRes) or not rRes: raise PubErrorCustom("请先获取验证码!") if request.data_format.get('vercode',None) != rRes: raise PubErrorCustom("验证码错误!") token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) res=Address.objects.filter(userid=user.userid,moren='0') address = AddressModelSerializer(res, many=True).data[0] if res.count() else {} return {"data": { "token": token, "userinfo":UsersSerializers(user, many=False).data, "address":address }}
def login(self, request): try: user = Users.objects.get(uuid=request.data_format.get('username')) except Users.DoesNotExist: raise PubErrorCustom("登录账户错误!") if user.passwd != self.request.data_format.get('password'): raise PubErrorCustom("密码错误!") if user.status == 1: raise PubErrorCustom("登陆账号已到期!") elif user.status == 2: raise PubErrorCustom("已冻结!") token = get_token() res = UserModelSerializerToRedis(user, many=False).data RedisTokenHandler(key=token).redis_dict_set(res) return {"data": {"token": token}}