def addAddress(self, request): data = request.data_format if str(data.get('moren')) == '0': Address.objects.filter(userid=request.user['userid']).update( moren='1') if 'id' in data: try: address = Address.objects.select_for_update().get( id=data['id']) except Address.DoesNotExist: raise PubErrorCustom("该地址不存在!") address.name = data.get("name") address.phone = data.get("phone") address.detail = data.get("detail") address.label = data.get("label") address.moren = data.get("moren") address.save() else: address = Address.objects.create(**dict( userid=request.user['userid'], name=data.get("name"), phone=data.get("phone"), detail=data.get("detail"), label=data.get("label"), moren=data.get("moren"), )) return {"data": AddressModelSerializer(address, many=False).data}
def getAddress(self, request): return { "data": AddressModelSerializer( Address.objects.filter(userid=request.user['userid']).order_by( 'moren', '-createtime'), many=True).data }
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 = Users.objects.get(uuid=res.get('openid')) except Users.DoesNotExist: user = Users.objects.create( **{ "userid": idGenerator.userid('4001'), "uuid": res.get('openId') if 'unionId' not in res else res['unionId'], "rolecode": '4001', "mobile": res.get('openId') if 'unionId' not in res else res['unionId'], "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) res = Address.objects.filter( userid=user.userid).order_by('-createtime') address = AddressModelSerializer( res, many=True).data[0] if res.count() else {} return { "data": { "user": UsersSerializers(user, many=False).data, "token": token, "address": address } }
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) print(wechat_res) if not wechat_res.get("openid"): raise PubErrorCustom("获取用户错误,腾讯接口有误!") data = {} token = False address = {} 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) res = Address.objects.filter( userid=user.userid).order_by('-createtime') address = AddressModelSerializer( res[0], many=False).data if len(res) else {} except Users.DoesNotExist: pass return { "data": { "user": data, "session_key": wechat_res.get("session_key"), "token": token, "address": address, "hylogo": Sysparams.objects.get().url } }
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 }}