def rzlend(): req_data = None api_logger.info(request.headers) if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: api_logger.warn('%s 请求参数未上传-json' % request.remote_addr) return jsonify({ 'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定' }) api_logger.debug(req_data) # 验证上传的必须的数据是否存在 if all((req_data.get('token', False),)): token=req_data.get('token') user_id = get_token_user_id(token) if dao.user_verify(user_id): return jsonify({ "code":200, "msg":"已认证" }) else: return jsonify({ "code": 201, "msg": "未认证" }) return jsonify({ "code": 203, "msg": "未获得token" })
def place_order(): token = request.args.get('token', None) if token is None: return jsonify({'code': 202, 'msg': '没有登录,请先登录'}) if bool(cache.check_token(token)): total_kf = 0 user_id = cache.get_token_user_id(token) carts_lists = dao.show_cars(user_id) for cart in carts_lists: data1 = {} goods_id = cart['goods_id'] goods = dao.show_sort(goods_id) data1['goodsId'] = goods['goodsId'] data1['goodsName'] = goods['goodsName'] data1['littlePic'] = goods['littlePic'] data1['kfCoin'] = goods['kfCoin'] cart['goods'] = data1 if cart['checked'] == 1: kf = goods['kfCoin'] total_kf += kf * cart['cart_goods_num'] else: pass return jsonify({ 'code': 200, 'msg': '购物车详情为', 'goods_list': carts_lists, 'totel': total_kf }) else: return jsonify({"msg": "token值错误或者过期,请先登录"})
def burse_balance(): token = request.args.get('token', None) print(token) if token: user_id = cache.get_token_user_id(token) print(user_id) vip_fee = request.args.get('vip_fee') burse_balance = UserDao().get_burse(user_id)[0]['u_burse_balance'] print(burse_balance) if burse_balance: if int(vip_fee) > int(burse_balance): api_logger.warning("余额不足") return jsonify({ "code":207, 'msg':'您的余额不足!' }) else: burse_balance = int(burse_balance) - int(vip_fee) UserDao().burse_change(user_id,burse_balance) api_logger.info("充值成功") return jsonify({ 'code':200, 'msg':'恭喜您,您已成功充值会员!' }) else: api_logger.warning("余额为0") return jsonify("您还没有余额,请充值") else: api_logger.info("user_id为空") return jsonify('查无此用户')
def add_order(): req_data = None if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: return jsonify({'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定'}) token = req_data.get('token', None) if token is None: return jsonify({'code': 202, 'msg': '没有登录,请先登录'}) if bool(cache.check_token(token)): print() user_id = cache.get_token_user_id(token) kfCion_totel = req_data['kfCoin'] users = dao.check_user(user_id) print(users) if int(users['userKfCoin']) < kfCion_totel: return jsonify({'code': 202, 'msg': '空粉币不足不能下单'}) else: req_data['totalfuZuan'] = 0 req_data['totalkfCoin'] = kfCion_totel req_data['pay'] = 0 req_data['user_id'] = user_id req_data.pop('token') req_data.pop('kfCoin') print(req_data) order_list = dao.save(**req_data) if order_list: del_carts = dao.query_checked(user_id) if del_carts: return jsonify({'code': 200, 'msg': '下单成功~'}) else: return jsonify({'code': 303, 'msg': '由于莫种原因 ,下单失败'}) else: return jsonify({"msg": "token值错误或者过期,请先登录"})
def save_address(): req_data = None if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: return jsonify({'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定'}) token = req_data.get('token', None) if token is None: return jsonify({'code': 202, 'msg': '没有登录,请先登录'}) if bool(cache.check_token(token)): user_id = cache.get_token_user_id(token) req_data.pop('token') req_data['user_id'] = user_id order_add = dao.select_add(user_id) if order_add: save_add = dao.wklc_default_address(**req_data) if save_add: return jsonify({'code': 200, 'msg': '储存地址成功'}) else: return jsonify({'code': 300, 'msg': '储存地址失败'}) else: del_old_add = dao.del_add(user_id) if del_old_add: save_new_add = dao.wklc_default_address(**req_data) if save_new_add: return jsonify({'code': 200, 'msg': '储存地址成功'}) else: return jsonify({'code': 300, 'msg': '储存地址失败'}) else: return jsonify({'code': 300, 'msg': '储存地址失败'}) else: return jsonify({"msg": "token值错误或者过期,请先登录"})
def address_view(): # 新增收货地址 # [{'name:'tom'},{'phone_num':'18333332222'},{'address_details':'陕西省西安市雁塔区玫瑰公馆'}, # {'addr_type':'0'}] data = request.get_json().get('data', None) addr_data = {key: value for d in data for key, value in d.items()} token = request.args.get("token", None) if token is None: return jsonify({"code": 201, "msg": "token查询参数必须提供"}) u_id = get_token_user_id(token) addr_data['user_id_id'] = u_id if u_id: # resp 为一个列表 if not addr_data: return jsonify({'code': '202', 'msg': '参数error'}) else: # tom,123456789,西安市#高新区#高新6路,0,1 # 插入数据address表 dao = AddressDao() dao.saves(**addr_data) return jsonify({ 'code': '200', 'msg': '更新地址成功!', 'data': addr_data }) else: return jsonify({'code': '202', 'msg': 'token不正确'})
def add_goods(): req_data = None if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: return jsonify({'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定'}) token = req_data.get('token', None) if token is None: return jsonify({'code': 202, 'msg': '没有登录,请先登录'}) if bool(cache.check_token(token)): user_id = cache.get_token_user_id(token) req_data.pop('token') req_data['user_id'] = user_id req_data['checked'] = 0 goods_id = req_data.get('goods_id') cart = dao.check_login_name(user_id, goods_id) if cart: req_data['cart_goods_num'] = 1 print("******", req_data) dao.save(**req_data) return jsonify({'code': 200, 'msg': '添加商品成功'}) else: goods = dao.check_cart(user_id, goods_id) goods_num = int(goods['cart_goods_num']) goods_num += 1 dao.update_goods_num(user_id, goods_id, goods_num) return jsonify({'code': 200, 'msg': '添加商品成功'}) else: return jsonify({"msg": "token值错误或者过期,请先登录"})
def reduce_goods(): req_data = None if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: return jsonify({'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定'}) token = req_data.get('token', None) if token is None: return jsonify({'code': 202, 'msg': '没有登录,请先登录'}) if bool(cache.check_token(token)): user_id = cache.get_token_user_id(token) req_data.pop('token') req_data['user_id'] = user_id goods_id = req_data.get('goods_id') cart = dao.check_login_name(user_id, goods_id) if cart: return jsonify({'code': 203, 'msg': '请先添加商品!'}) else: goods = dao.check_cart(user_id, goods_id) goods_num = int(goods['cart_goods_num']) if goods_num > 0: goods_num -= 1 print(goods_num) dao.update_goods_num(user_id, goods_id, goods_num) return jsonify({'code': 200, 'msg': '删除商品成功'}) else: dao.del_goods(user_id, goods_id) return jsonify({'code': 202, 'msg': '商品已经删除到0,删除该商品记录'}) else: return jsonify({"msg": "token值错误或者过期,请先登录"})
def pay_order(): from views import check_requirments check_errors = check_requirments("POST", 'token', 'addr_id', 'goods_ids', 'goods_cnt', 'o_status') if check_errors: return jsonify({'code': 202, 'msg': check_errors}) user_id = cache.get_token_user_id(request.form.get('token')) o_status = request.form.get('o_status') if o_status == '0': #跳转到支付页面 orderDao = Order_Dao() balance = orderDao.query_user_sale(user_id) total_price = request.form.get('total_price') order_id = request.form.get('order_id') if balance >= total_price: balance -= total_price #更新用户余额 orderDao.to_update_order('1', user_id, order_id) # rd.set(order_id, 250) # rd.expire(order_id, 24 * 3600) # # if not rd.get(order_id): # orderDao.to_update_order('2', user_id, order_id) return jsonify({'code': 200, 'msg': '支付成功!'}) else: return jsonify({'code': 201, 'msg': '余额不足!'})
def upload_avator(): # 上传的头像字段为 img # 表单参数: token file: FileStorage = request.files.get('img', None) token = request.form.get('token', None) if all((bool(file), bool(token))): # 验证文件的类型, png/jpeg/jpg, 单张不能超过2M # content-type: image/png, image/jpeg user_id = get_token_user_id(token) print(file.content_length, 'bytes') if file.content_type in ( 'image/png', 'image/jpeg', ): filename = uuid.uuid4().hex \ + os.path.splitext(file.filename)[-1] file.save(filename) # 上传到oss云服务器上 key = oss.upload_file(filename) os.remove(filename) # 删除临时文件 # 将key写入到DB中 # dao.update_pwd(key,user_id) return jsonify({'code': 200, 'msg': '上传文件成功', 'file_key': key}) else: return jsonify({'code': 201, 'msg': '图片格式只支持png或jpeg'}) return jsonify({'code': 100, 'msg': 'POST请求参数必须有img和token'})
def upload_head(): # 上传文件的头像字段 img # 表单参数token file = request.files.get('img',None) token = request.form.get('token',None) if all((bool(file),bool(token))): # 验证图片类型 print(type(file)) if file.content_type in ('image/png','image/jpeg'): filename = uuid.uuid4().hex+os.path.splitext(file.filename)[-1] file.save(filename) # 上传到云服务器 file_key = oss.upload_file(filename) os.remove(filename) user_id = get_token_user_id(token) UserDao().set_userinfo(key='u_headpic',value=file_key,where='id',args=user_id) img_url = oss.get_url(file_key) return jsonify({ 'code':200, 'msg':'头像上传成功', 'img':img_url }) else: return jsonify({ 'code': 207, 'msg': '图片只支持png和jpeg' }) else: return jsonify({ 'code':207, 'msg':'POST参数必须有img和token' })
def change_pwd(): req_data = None if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: return jsonify({'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定'}) token = req_data.get('token', None) if token is None: return jsonify({'code': 202, 'msg': '没有登录,请先登录'}) if bool(cache.check_token(token)): user_id = cache.get_token_user_id(token) pwd = dao.select_pwd(user_id) pwd1 = pwd['password'] oldpwd = req_data['oldpwd'] newpwd = req_data['newpwd'] once_newpwd = req_data['once_newpwd'] if check_password(oldpwd, pwd1): if newpwd == once_newpwd: m_new_password = make_password(str(newpwd)) new_password = dao.update_pwd(m_new_password, user_id) if new_password: return jsonify({'code': 200, 'msg': '修改成功'}) else: return jsonify({'code': 201, 'msg': '两次输入密码不一致'}) else: return jsonify({'code': 202, 'msg': '原密码错误'}) else: return jsonify({"msg": "token值错误或者过期,请先登录"})
def user_add(): token = request.args.get('token', None) user_id = get_token_user_id(token) if user_id: r_data = request.get_json() print(r_data) phone = r_data['phone'] address = r_data['address'] linkman = r_data['name'] num = r_data['bottom'] sex = r_data['sex'] tag = r_data['tag'] # user_id = r_data['user_id data = { 'address': address, # 地址 'bottom': num, # 门牌号 'name': linkman, # 联系人 'phone': phone, # 手机 'user_id': user_id, 'sex': sex, 'tag': tag } User_Featuare_Dao().saves(**data) api_logger.info("地址成功添加") return jsonify({'code': 200, 'msg': '地址添加成功', 'data': data}) return jsonify({'code': 207, 'msg': '你还未登录请登录'})
def collects(): # 收藏商品或店铺 req_data = request.get_json() token = req_data.get('token') if check_token(token): user_id = get_token_user_id(token) clt_id = req_data.get('clt_id') type_id = req_data.get('type_id') if (clt_id and type_id) is not None: dao = BaseDao() save_data = { 'user_id': user_id, 'clt_id': clt_id, 'type_id': type_id } result = dao.save('u_clt', **save_data) if result: return jsonify({'code': 200, 'msg': '收藏成功!'}) else: return jsonify({'code': 400, 'msg': '查询数据库失败!'}) elif clt_id is not None and type_id is None: # 取消收藏 if clt_id: dao = GoodsDao() delet = dao.del_collect(clt_id) if delet: return jsonify({'code': 200, 'msg': '该收藏信息已删除!'}) else: return jsonify({'code': 400, 'msg': '删除失败!'}) else: return jsonify({'code': 400, 'msg': '未接收到需要删除的ID!'}) else: return jsonify({'code': 400, 'msg': '请上传商铺或商品ID和type类型!'}) else: return jsonify({'code': 400, 'msg': 'token验证失败!'})
def user_add_order(): # 验证用户是否已登录 token = request.args.get('token', None) if token is None: return jsonify({ 'code': 202, 'msg': 'token查询参数必须提供或者登录后获取token' }) if cache.check_token(token): user_id = cache.get_token_user_id(token) order_info = request.get_json() # 验证订单参数(和t_order表字段对应或order_detail) # 下订单的业务处理 # 生成订单号(2019070200001, 2019070200002) dao = OrderDao() next_order_num = dao.next_order_num() order_info['order_num'] = next_order_num order_info['user_id'] = user_id tasks.add_order.delay(**order_info) return jsonify({ 'code': 201, 'msg': '正在抢单', 'oder_num': next_order_num })
def user_identify(): # 添加银行卡和身份证 u_data = request.get_json() token = u_data.get('token') if check_token(token): dao = UserDao() user_id = get_token_user_id(token) print(user_id) data = dao.check_real(user_id) print(data) if data: u_bank = data[0]['u_bank'] user_card = data[0]['user_card'] if (u_bank and user_card) is not None: return jsonify({'code': 300, 'msg': '您已添加过该信息!'}) else: dict1 = { 'u_bank': u_data.get('u_bank'), 'user_card': u_data.get('user_card'), 'is_val': 1, 'asset': 0 } for k, v in dict1.items(): user_save = dao.update('jd_user', k, v, 'user_id', user_id) if user_save: return jsonify({'code': 200, 'msg': '验证通过!'}) else: return jsonify({'code': 400, 'msg': '添加失败!'}) else: return jsonify({'code': 400, 'msg': '数据库查询失败!'}) else: return jsonify({'code': 400, 'msg': 'token验证失败!'})
def kill_id(): r_data = request.get_json() if r_data: token = r_data['token'] user_id = get_token_user_id(token) r.delete(token) if UserDao().del_userinfo(user_id): return jsonify({'code':200,'msg':'账户注销成功'})
def logout(): r_data = request.get_json() if r_data: token = r_data['token'] user_id = get_token_user_id(token) r.delete(token) if UserDao().set_userinfo('is_active',False,'id',user_id): return jsonify({'code':200,'msg':'当前用户已经退出登录'})
def user(): token = request.args.get('token') if token: if check_token(token): user_id = get_token_user_id(token) print(user_id) s_data = UserDao().get_profile(user_id) return jsonify({'code':200,'msg':'OK','data':s_data}) return jsonify({'code':207,'msg':'未登录,请前去登录'})
def mine_info(): # " 我的" 页面所需要的数据信息 u_data = request.get_json() token = u_data.get('token') if check_token(token): user_id = get_token_user_id(token) print(user_id) user_data = UserDao().get_profile(user_id) # 查询用户详细信息 vou_data = VouDao().check_vouchers(user_id) # 查询优惠卷信息 coll_data1 = GoodsDao().check_collect_goods(user_id) # 收藏的商品信息 coll_data2 = GoodsDao().check_collect_shops(user_id) # 收藏的店铺信息 goods_list = GoodsDao().check_goods() # 从商品详情表中取20组商品信息,元组类型 print(user_data, vou_data, coll_data1, coll_data2, goods_list) if all((user_data, vou_data, coll_data1, coll_data2, goods_list)): re_data = { "accountInfo": { "userID": user_id, "userName": user_data[0]['user_name'], # 用户名 "userPhoto": user_data[0]['u_img'], # 头像 }, "assetInfo": { "vouchers": vou_data[0]['cop_id'], # 优惠卷 "balance": user_data[0]['asset'], # 余额 "integral": user_data[0]['u_intg'], # 积分 "redPacket": 80 }, "browseInfo": { "collectProduct": len(coll_data1), # 收藏的商品的数量 "collectShop": len(coll_data2), # 收藏的店铺的数量 "footprint": 16 }, "productList": goods_list[:20] # 20 组商品信息 } return jsonify({'code': 200, 'data': re_data}) else: re_data = { "accountInfo": { "userID": user_id, "userName": user_data[0]['user_name'], # 用户名 "userPhoto": user_data[0]['u_img'], # 头像 }, "assetInfo": { "vouchers": 0, # 优惠卷 "balance": 0, # 余额 "integral": 100, # 积分 "redPacket": 80 }, "browseInfo": { "collectProduct": 0, # 收藏的商品的数量 "collectShop": 0, # 收藏的店铺的数量 "footprint": 16 }, "productList": goods_list[:20] # 20 组商品信息 } return jsonify({'code': 200, 'data': re_data}) else: return jsonify({'code': 400, 'msg': 'token验证失败!'})
def create_traffic_order(): token = request.get_json().get('token', None) if cache.check_token(token): # 获取请求参数 user_id_id = cache.get_token_user_id(token) req_data = request.get_json() payasset = req_data.get('payasset', None) tel = req_data.get('tel', None) if not all((payasset, tel)): return jsonify({ 'code': 401, 'msg': '手机号和流量数值必须上传', }) # payasset = get_tra_asset(phonetra) dao = cart_orderDao() cart_cal_rel = payasset # 优惠券ID voucher_id = request.get_json().get('voucher_id', None) vou_resp = dao.check_vou(user_id_id, voucher_id) if vou_resp: if vou_resp[0]['vuc'] == 0: vou_price = vou_resp[0]['minusprice'] cart_cal_rel = payasset - vou_price if vou_resp[0]['vuc'] == 1: vou_price = vou_resp[0]['minusprice'] if payasset > 200 and payasset < 500: cart_cal_rel = payasset - vou_price elif payasset > 500 and payasset < 1000: cart_cal_rel = payasset - vou_price elif payasset > 1000: cart_cal_rel = payasset - vou_price # 将订单写入数据库 o_num = get_order_num() dict1 = { "o_num": o_num, "o_addr": '不需要地址', "o_conn": tel, "o_time": datetime.datetime.now(), "o_status": 0, "o_total": payasset, "o_relpay": cart_cal_rel, "o_shopper_id": 'phonetraffic', "o_user_id": user_id_id } print(dict1) dao.save_detail(**dict1) rd.set(cart_cal_rel, o_num) rd.expire(cart_cal_rel, 2 * 60 * 60) # dao.delete_vou(user_id_id, voucher_id) data = {"orderID": o_num, "total": cart_cal_rel} return jsonify({'code': 200, 'msg': '订单生成成功', 'data': data}) else: return jsonify({'code': 400, 'msg': '您还未登陆,请先登录'})
def user_comment(): token = request.args.get('token', None) if token is None: api_logger.warning("未登录") return jsonify({'code': 207, 'msg': '未登录,请登录'}) if check_token(token): user_id = get_token_user_id(token) data = User_Featuare_Dao().user_comment(user_id) api_logger.info("评论显示成功") return jsonify({"code": 200, "data": data, "msg": "查询成功"})
def del_address(): token = request.args.get('token', None) # r_data = request.get_data() # print(r_data) sid = request.args.get('id') user_id = get_token_user_id(token) # user_id = r_data['user_id'] User_Featuare_Dao().del_address(sid, user_id) api_logger.info("地址删除成功") return jsonify({"code": 200, "msg": "删除成功!"})
def change_password(): r_data = request.get_json() if r_data: token = r_data['token'] user_id = get_token_user_id(token) u_password = r_data['u_password'] u_password = make_password(u_password) if UserDao().set_userinfo('u_password', u_password, 'id', user_id): return jsonify({'code': 200, 'msg': '用户密码修改成功'}) return jsonify({'code': 207, 'msg': '用户名已存在'}) return jsonify({'code':207,'msg':'请输入正确的参数'})
def cart_detl(): try: token = request.args.get("token") if not token: return jsonify({"code": 400, "msg": "您还未登录,请先登录!"}) user_id = get_token_user_id(token) # 通过token获取id dao = CartDao() data = dao.cart_details(user_id) return jsonify({"code": 200, "msg": "查询购物车详情成功", "data": data}) except: return jsonify({"code": 400, "msg": "查询失败"})
def change_tel(): r_data = request.get_json() if r_data: token = r_data['token'] user_id = get_token_user_id(token) phone = r_data['phone'] if len(phone)== 11: if UserDao().check_phone(phone): if UserDao().set_userinfo('u_tel',phone,'id',user_id): return jsonify({'code':200,'msg':'手机号修改成功','u_tel':phone}) return jsonify({'code':207,'msg':'手机号认证失败,请重新输入!'})
def pat_list(): # 获取患者列表 try: token = request.args.get('token') if not token: return jsonify({"code": 400, "msg": "您还未登录,请先登录!"}) user_id = get_token_user_id(token) # 通过token获取id dao = UserDao() pat = dao.patient_list(user_id) return jsonify({"code": 200, "msg": "获取患者列表成功!", "data": pat}) except: return jsonify({'code': 400, 'msg': '暂无数据'})
def to_rzlend(): req_data = None api_logger.info(request.headers) if request.headers['Content-Type'].startswith('application/json'): req_data = request.get_json() if req_data is None: api_logger.warn('%s 请求参数未上传-json' % request.remote_addr) return jsonify({ 'code': 9000, 'msg': '请上传json数据,且参数必须按api接口标准给定' }) api_logger.debug(req_data) # 验证上传的必须的数据是否存在 if all((req_data.get('userName', False), req_data.get('userCard', False), req_data.get('token',False))): userName = req_data['userName'] userCard = req_data["userCard"] token = req_data['token'] data={} data['user_id'] = get_token_user_id(token) if dao.toverify(userName,userCard): data['verify_id']= dao.toverify(userName,userCard)['id'] if not dao.query_user_verify(data['verify_id']): if dao.save_verify(**data): print(data['user_id']) if dao.update_user(data['user_id']): return jsonify({ 'code': 200, 'msg': "姓名和身份证号,对上了" }) else: return jsonify({ 'code': 201, 'msg': "认证失败" }) else: return jsonify({ 'code': 202, 'msg': "此卡已被绑定" }) else: return jsonify({ 'code':203, 'msg':"请输入正确的姓名和身份证号" }) else: return jsonify({ 'code':204, 'msg': "传入参数不足" })
def change_user(): r_data = request.get_json() if r_data: token = r_data['token'] user_id = get_token_user_id(token) user_name = r_data['user_name'] if UserDao().check_username('user_name'): if UserDao().set_userinfo('u_username', user_name, 'id', user_id): return jsonify({'code': 200, 'msg': '用户名修改成功', 'u_username': user_name}) return jsonify({'code': 207, 'msg': '用户名修改失败'}) return jsonify({'code': 207, 'msg': '用户名已存在'}) return jsonify({'code':207,'msg':'请输入正确的参数'})
def sub_cart(): try: token = request.args.get("token") if not token: return jsonify({"code": 400, "msg": "您还未登录,请先登录!"}) user_id = get_token_user_id(token) # 通过token获取id med_id = request.args.get("med_id") dao = CartDao() data = dao.sub_carts(user_id, med_id) # 执行删除购物车函数 return jsonify({"code": 200, "msg": "删除购物车成功", "data": data}) except: return jsonify({"code": 400, "msg": "删除失败"})