def GetBal(self, request): try: user = Users.objects.get(userid=request.user['userid']) except Users.DoesNotExist: raise PubErrorCustom("用户不存在!") return {"data": UsersSerializers(user, many=False).data}
def getUserByWechat(self, request): return { "data": UsersSerializers(Users.objects.get(userid=request.user['userid']), many=False).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 = 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 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 userHandler(self, request): logger.info(request.user) if request.user['rolecode'] != 1000: raise PubErrorCustom("只有超级管理员能操作!") if request.method == 'POST': # userid = request.data_format.get('userid') uuid = request.data_format.get("login_name") name = request.data_format.get("name") pic = request.data_format.get("pic") passwd = request.data_format.get("passwd") if not uuid: raise PubErrorCustom('登录名称不能为空!') if not name: name = uuid user = Users.objects.create( **{ "uuid": uuid, "mobile": uuid, "rolecode": 1001, "name": name, "passwd": passwd, "pic": pic }) user.userid = user.id user.save() elif request.method == 'PUT': userid = request.data_format.get('userid') uuid = request.data_format.get("login_name") name = request.data_format.get("name") pic = request.data_format.get("pic") passwd = request.data_format.get("passwd") try: user = Users.objects.get(userid=userid) except Users.DoesNotExist: raise PubErrorCustom('该用户不存在!') user.uuid = uuid if uuid else user.uuid user.mobile = uuid if uuid else user.uuid user.name = name if name else user.name user.pic = pic if pic else user.pic user.passwd = passwd user.save() elif request.method == 'DELETE': Users.objects.filter( userid=request.data_format.get('userid')).delete() elif request.method == 'GET': return { "data": UsersSerializers(Users.objects.filter(rolecode=1001).order_by( '-createtime')[request.page_start:request.page_end], many=True).data }
def getUser(self, request): try: user = Users.objects.get(userid=request.user['userid']) if user.status !='0': raise PubErrorCustom("用户状态异常!") except Users.DoesNotExist: raise PubErrorCustom("用户不存在!") return {"data":UsersSerializers(user,many=False).data}
def cardCz(self, request): rUser = None account = request.data_format['account'] password = request.data_format['password'] try: card = Card.objects.select_for_update().get(account=account, password=password, type='0') if card.useuserid > 0: return {"data": {"a": False}} except Card.DoesNotExist: return {"data": False} try: user = Users.objects.select_for_update().get( userid=request.user['userid']) except Users.DoesNotExist: raise PubErrorCustom("用户非法!") tmp = user.bal user.bal += card.bal if card.rolecode == user.rolecode: flag = False else: request.user['rolecode'] = card.rolecode RedisTokenHandler(key=request.ticket).redis_dict_set(request.user) rUser = UsersSerializers(user, many=False).data flag = True updBalList(user=user, order=None, amount=card.bal, bal=tmp, confirm_bal=user.bal, memo="充值卡充值", cardno=card.account) user.rolecode = card.rolecode user.save() card.useuserid = user.userid card.save() RedisCaCheHandler( method="save", serialiers="CardModelSerializerToRedis", table="card", filter_value=card, must_key="id", ).run() return {"data": {"a": True, "b": flag, "rUser": rUser}}
def thmCz(self, request): rUser = None account = request.data_format['account'] try: user = Users.objects.select_for_update().get( userid=request.user['userid']) except Users.DoesNotExist: raise PubErrorCustom("用户非法!") try: card = DeliveryCode.objects.select_for_update().get( account=account) if card.useuserid > 0: return {"data": {"a": False}} except DeliveryCode.DoesNotExist: return {"data": False} tmp = user.bal user.bal += card.bal if card.rolecode == user.rolecode: flag = False else: request.user['rolecode'] = card.rolecode RedisTokenHandler(key=request.ticket).redis_dict_set(request.user) rUser = UsersSerializers(user, many=False).data flag = True updBalList(user=user, order=None, amount=card.bal, bal=tmp, confirm_bal=user.bal, memo="提货码充值", cardno=card.account) user.rolecode = card.rolecode user.save() card.useuserid = user.userid card.status = '0' card.save() return {"data": {"a": True, "b": flag, "rUser": rUser}}
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 login2(self, request): try: user = Users.objects.get(uuid=request.data_format.get('loginname'), rolecode__in=['1000', '1200']) except Users.DoesNotExist: raise PubErrorCustom("登录账户错误!") if user.passwd != self.request.data_format.get('passwd'): 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": UsersSerializers(user, many=False).data }}
def OrderPayByThm(self, request): thms = request.data_format.get("thms", []) gdid = request.data_format.get("gdid", None) memo = request.data_format.get("memo", "") address = json.dumps(request.data_format.get('address', {})) if not len(thms): raise PubErrorCustom("提货码为空!") try: user = Users.objects.get(userid=request.user['userid']) except Users.DoesNotExist: raise PubErrorCustom("用户非法!") thmObj = None for index, thm in enumerate(thms): try: deli = DeliveryCode.objects.select_for_update().get( gdid=gdid, account=thm, status='1') deli.useuserid = request.user['userid'] deli.status = '0' deli.save() if index == 0: thmObj = deli except DeliveryCode.DoesNotExist: raise PubErrorCustom("提货码{}不正确".format(index + 1)) if not thmObj: raise PubErrorCustom("提货码为空!") orderObj = Order.objects.create(**dict(userid=request.user['userid'])) orderObj.linkid = {"linkids": []} orderObj.amount = Decimal("0.0") res = RedisCaCheHandler( method="get", table="goods", must_key_value=gdid, ).run() if not res or res['gdstatus'] == '1': raise PubErrorCustom("{}商品已下架,请在购物车删除此商品!".format(res['gdname'])) link = OrderGoodsLink.objects.create( **dict(userid=request.user['userid'], orderid=orderObj.orderid, gdid=res['gdid'], gdimg=res['gdimg'], gdname=res['gdname'], gdprice=Decimal(str(res['gdprice'])), gdnum=len(thms), thm='0', thms=json.dumps({"thms": thms}), virtual=res['virtual'])) orderObj.linkid['linkids'].append(link.linkid) orderObj.amount += link.gdprice * int(link.gdnum) orderObj.isthm = '0' orderObj.address = address orderObj.memo = memo orderObj.status = '1' orderObj.isvirtual = res['virtual'] orderObj.linkid = json.dumps(orderObj.linkid) orderObj.save() if link.virtual == '0': cards = Cardvirtual.objects.filter( gdid=link.gdid, status='1').order_by('createtime') if cards.exists(): if len(cards) < link.gdnum: raise PubErrorCustom("暂无存货!") virtualids = json.loads(link.virtualids) count = 0 for card in cards: count += 1 virtualids['ids'].append({ "id": card.id, "account": card.account, "password": card.account }) card.status = '0' card.useuserid = link.userid card.save() if count == link.gdnum: break link.virtualids = json.dumps(virtualids) link.save() else: raise PubErrorCustom("暂无存货!") rUser = None if thmObj.rolecode == user.rolecode: flag = False else: request.user['rolecode'] = thmObj.rolecode RedisTokenHandler(key=request.ticket).redis_dict_set(request.user) rUser = UsersSerializers(user, many=False).data flag = True user.rolecode = thmObj.rolecode user.save() return {"data": {"b": flag, "rUser": rUser}}