def add_cart_view(): token = request.get_json().get("token") c_id = request.args.get("cid") # 验证是否登录 u_id = get_token_user_id(token) print(u_id,type(u_id)) print("cid",c_id,type(c_id)) if u_id: add_cart = CartDao() id = add_cart.get_course_id(c_id) cart = add_cart.check_cart(uid=int(u_id),cid=id) #如果已登录,通过当前用户和课程id,查询购物车是否有该条记录 print("cart",not bool(cart)) # 如果没有则创建一条购物车记录 if not bool(cart): try: state = add_cart.save("carts", **{"is_select": True,"course_id": id,"user_id": u_id}) if state: result = {"code": 200, "msg": "商品已添加至购物车!"} else: result = {"code": 200, "msg":"商品添加购物车失败!"} return jsonify(result) except Exception as e: api_logger.error("%s save 失败" % c_id) return jsonify({"code": 201, "msg": e}) result = {"code": 201, "msg": '该商品在购物车中已存在~'} return jsonify(result) api_logger.warn("%s 未登录 " % u_id) result = {"code": 202, "msg": "用户还未登录或注册"} return jsonify(result)
def send_msg(): resp = eval(request.get_data()) if resp: u_phone = resp.get('u_phone') code = ''.join([str(random.randint(0, 9)) for _ in range(6)]) res = eval(send_sms_code(u_phone, code).decode()) if res['Code'] == 'OK': try: rd.setex(u_phone, code, 120) # 保存到redis缓存 except Exception as e: api_logger.error(e) return jsonify({'code': 802, 'msg': '短信验证码保存失败' }) api_logger.info('发送手机号:%s,短信验证码为:%s' % (u_phone, code)) return jsonify({'code': 200, 'msg': '短信验证码发送成功!' }) if res['Code'] == 'isv.BUSINESS_LIMIT_CONTROL': return jsonify({'code': 303, 'msg': '频繁验证,请稍后再试' }) return jsonify({'code': 303, 'msg': '请输入正确的手机号码' })
def main_view(): if request.method == "POST": # r_data = request.get_json() r_data = request.get_json() print(r_data) print(type(r_data)) try: lat = r_data['lat'] print(lat) lon = r_data['lon'] if all((lat, lon)): wheel = dao.wheel() main_type_img = dao.main_type() main_small_img = dao.main_small_img() shops = dao.youxuan_shops() foodlists = [] for shop in shops: food_data = {} shop_id = shop['id'] good_pic = dao.get_goodpic(shop_id) food_data['shop_id'] = shop_id food_data['shop_name'] = shop['shop_name'] food_data['shop_img'] = shop['img_url'] food_data['good_img'] = good_pic food_data['app_delivery_tip'] = shop['app_delivery_tip'] foodlists.append(food_data) # goodfood = dao.youxuan_goods() goodfood = dao.youxuan_goods() for good in goodfood: good['shop_name'] = dao.get_shopname(good['g_shop_id']) good['shop_img'] = dao.get_shop_head_pic(good['g_shop_id']) shoplists = dao.shoplists() nearbylists = dao.shop_nearby(lat, lon) shop_discounts2 = [] for shop in shops: shop_id = shop['id'] shop_discounts2.append(ShopDao().get_discounts2(shop_id)) result = { "code": 200, "msg": "ok", "data": { "wheel": wheel, "main_type_img": main_type_img, "main_small_img": main_small_img, "foodlist": foodlists, "goodfood": goodfood, "shoplists": shoplists, "nerbylists": nearbylists, "shop_discounts2": shop_discounts2 } } api_logger.info("主页面显示成功") return jsonify(result) except Exception as e: api_logger.error("参数错误%s" % e) return jsonify({'code': 207, 'msg': '请传入正确的参数lat和lon'}) else: api_logger.error("请求方式错误") return jsonify({"code": 207, "msg": "请求参数错误"})
def __exit__(self, exc_type, exc_val, exc_tb): if exc_type is None: self.conn.commit() else: api_logger.error(exc_val) self.conn.rollback() return True # 异常不会继续向外抛出
def delete_user_cart_course(self, cid, uid): api_logger.info("%s have been delete", cid) sql = "delete from carts where course_id = %s and user_id=%s" try: success = False with self.db as c: c.execute(sql,(cid,uid)) success = True return success except Exception as e: api_logger.error("delete cart_course failed", e )
def check_sms(u_phone, msg_code): res = None try: r_msg_code = r.get('msg' + u_phone) if not r_msg_code: return {'code': 203, 'msg': '短信验证码不存在,或已过期'} r_msg_code = r_msg_code.decode() if r_msg_code != msg_code: return {'code': 204, 'msg': '短信验证码输入错误'} except Exception as e: api_logger.error(e) res = {'code': 202, 'msg': '数据库查询失败'} return res
def get_shop_type(): if request.method == "POST": type = request.get_json() type_id = type.get("type_id") if type_id: data = dao.all_shop(type_id) api_logger.info("全部小分类") return jsonify({"code": 200, "msg": "ok", "data": data}) api_logger.error("请求参数错误") return jsonify({ "code": 207, "msg": "请求参数错误", })
def user_regist(): r_data = request.get_json() print(r_data,request.headers.get('Content-Type')) if r_data: phone = r_data['phone'] code = r_data['code'] #判断接受的数据是否为空 if all((phone,code)): res = check_sms(phone,code) print(res) if not res: if UserDao().check_phone(phone): c_data = { 'u_username' : "KMP" + phone, 'u_tel' : phone, 'u_headpic' : '', 'u_nickname' : "Nk" + phone, 'u_email':phone+"@tel.com", 'is_vip':False, 'is_active':True, } print(c_data) UserDao().save(**c_data) else: if UserDao().set_userinfo(key='is_active',value=True,where='u_tel',args=phone): pass else: return jsonify({'code':207,'msg':'服务器出现异常,请稍后再试!!!'}) user_id = UserDao().get_id('u_tel',phone) print(user_id) data = UserDao().get_profile(user_id) token = uuid.uuid4().hex save_token(token, user_id) api_logger.info("登录成功") return jsonify({ 'code':200, 'msg':'登录成功,欢迎使用MT外卖品台', 'token':token, 'data':data }) api_logger.error("手机号或验证码错误") return jsonify({ "code": 207, "msg": "手机号或者验证码错误!!!" })
def cart_view(): token = request.get_json().get("token") #验证当前用户是否登录 u_id = get_token_user_id(token) if u_id: # 通过用户id查询当前用户的购物车记录,及课程信息 cart_obj = CartDao() try: course_info = cart_obj.get_cart_course(u_id) total_price = cart_obj.total(u_id) result = {"code": 200, "checked": True, "products":course_info, "total": total_price} except Exception as e: api_logger.error("checkout course_info failed!,cause:%s" % e) result = {"code": 201, "msg": e} return jsonify(result) api_logger.warn("%s 未登录 " % u_id) result = {"code": 202, "msg": "用户还未登录或注册"} return jsonify(result)
def collect_course(): token = request.headers.get("token") type1 = request.args.get("type") c_id = request.args.get("cid") u_id = cache_.get_token_user_id(token) if u_id: try: if type1 == "0": # 如果type==0,则添加收藏当前课程 collect_success = CollectDao().save( "user_favorites", **{ "user_id": u_id, "course_id": c_id }) if collect_success: return jsonify({"code": 200, "msg": "已收藏至我的收藏"}) elif type1 == "1": # 如果type==1,则取消收藏当前课程 collect_cancel = CollectDao().cancel_user_collect(u_id, c_id) if collect_cancel: return jsonify({"code": 200, "msg": "已取消收藏"}) except Exception as e: api_logger.error(e) return jsonify({"code": 400}) return jsonify({"code": 201, "msg": "用户还未登录或注册"})