def flask_upload(): # request.files内容是ImmutableMultiDict([('filename', <FileStorage: 'name.PNG' ('image/png')>)]) print("图片上传操作", request.files) file = request.files["file"] if "file" not in request.files or file.filename == "": # 如果没有文件, 表示上传失败 return "0" # 如果file存在,且满足allowed_file()函数,则进入该函数 if file and allowed_file(file.filename): # 调用”werkzeug.secure_filename()”来使文件名安全,但是会过滤中文名 filename = secure_filename(file.filename) # 更换图片名为用户名+时间戳 name = session.get("ID") sql = SQL("shopping_flask") img_name = name + str(int(time.time())) filename = filename.replace(filename, img_name + ".jpg") # 先删除用户原来的图片头像,再上传新的头像 head = sql.select("select chat_head from user where phone='%s'" % name)[0][0] try: os.remove(head) except Exception: pass # 把上传的图片保存到指定的目录 file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) path = os.path.join(app.config["UPLOAD_FOLDER"], filename) # 将用户的头像相对地址存储到数据库中 sql.IDU("update user set chat_head='%s' where phone='%s'" % (path, name)) print(os.path.join(app.config["UPLOAD_FOLDER"], filename)) return "1" else: return "0"
def update_cart(): name = session.get("ID") sql = SQL("shopping_flask") shop_id = request.values.get("shop_id") sql.IDU("update cart set state='-1' where u_name='%s' and shop_id='%s'" % (name, shop_id)) sql.close() return "1"
def del_history(): name = session.get("ID") sql = SQL("shopping_flask") history = request.values.get("history") sql.IDU("delete from user_history where user_id='%s' and history='%s'" % (name, history)) result = sql.select("select * from user_history where user_id='%s'" % name)[0:8] return json.dumps(result)
def del_store(): sql = SQL("shopping_flask") name = session.get("ID") store_name = request.values.get("store_name") # 删除对应的店铺 sql.IDU("delete from collect_store where username='******' and store='%s'" % (name, store_name)) sql.close() return "1"
def del_shop(): sql = SQL("shopping_flask") name = session.get("ID") shop_id = request.values.get("shop_id") # 删除商品收藏表中的用户收藏 sql.IDU("delete from collect_shop where u_name='%s' and shop_id='%s'" % (name, shop_id)) sql.close() return "1"
def add_shop(): sql = SQL("shopping_flask") name = session.get("ID") shop_id = request.values.get("shop_id") # 往收藏表中添加商品id sql.IDU("insert into collect_shop(u_name, shop_id) values('%s','%s')" % (name, shop_id)) sql.close() return "1"
def add_cart(): name = session.get("ID") sql = SQL("shopping_flask") today = datetime.date.today() number = int(request.values.get("number")) shop_id = request.values.get("shop_id") # 判断购物车中是否存在该商品且没有支付 if sql.select( "select * from cart where shop_id='%s'and u_name='%s'and state='0'" % (shop_id, name)): sql.IDU( "update cart set number=number+'%d',date='%s' where shop_id='%s'" % (number, today, shop_id)) else: sql.IDU( "insert into cart(shop_id, u_name, date, number)values('%s','%s','%s','%s')" % (shop_id, name, today, number)) sql.close() return "1"
def reset_password(): if request.method == "POST": # 获取要重置密码的账号, 重置后将session中保存的账号删除 ID = session.get("reset_ID") # 获取密码进行加密 pwd = generate_password_hash(request.values.get("pwd")) sql = SQL("shopping_flask") sql.IDU("update user set pwd='%s' where phone='%s' or email='%s'" % (pwd, ID, ID)) del session["reset_ID"] return "0" return render_template("register/reset_password.html")
def sign(): if request.method == "POST": username = request.values.get("username") phone = request.values.get("phone") pwd = generate_password_hash(request.values.get("pwd")) print(username, "---", phone, "---", pwd) sql = SQL("shopping_flask") sql.IDU("insert into user(name,phone,pwd) values ('%s','%s','%s')" % (username, phone, pwd)) sql.close() return "1" return render_template("register/register.html")
def pay(): if request.method == "POST": # id:id,name:name,city:city,address:address,phone:phone,title:title,prices:prices id = request.values.get("id") consignee = request.values.get("name") city = request.values.get("city") city = city.replace("-", "") address = request.values.get("address") phone = request.values.get("phone") prices = request.values.get("prices") title = request.values.get("title") link = buy.pay(title, prices) session["pay_shop_id"] = id # 将收货人信息暂时存到session中,如果用户购买了则将数据存到session中 session["consignee"] = consignee session["city"] = city session["address"] = address session["phone"] = phone return json.dumps(link) else: # 如果有用户直接进入到此链接判断session中是否有支付的订单,没有则跳转到购物车页面 if session.get("pay_shop_id"): print("支付成功,更改属性") sql = SQL("shopping_flask") name = session.get("ID") pay_shop_id = session.get("pay_shop_id") sql.IDU( "update cart set state='1' where state='0' and u_name='%s' and shop_id='%s'" % (name, pay_shop_id)) consignee = session.get("consignee") city = session.get("city") address = session.get("address") phone = session.get("phone") # 如果用户支付了,则将数据存放到用户地址表,也就是用户的收货地址 sql.IDU( "insert into user_address(user,name,province,address,phone,shop_id) values " "('%s','%s','%s','%s','%s','%s')" % (name, consignee, city, address, phone, pay_shop_id)) return redirect("/home/my_cart") else: return redirect("/home/my_cart")
def add_store(): sql = SQL("shopping_flask") name = session.get("ID") store_name = request.values.get("store_name") today = datetime.date.today() print(store_name, today) # 往店铺收藏表中添加对应的数据 sql.IDU( "insert into collect_store(username, store, time) values('%s','%s','%s')" % (name, store_name, str(today))) sql.close() return "1"
def amend_email(): name = session.get("ID") sql = SQL("shopping_flask") nick_name = session.get("nick_name") head_img = sql.select("select chat_head from user where phone='%s'" % name)[0][0][8:] if request.method == "POST": new_email = request.values.get("new_email") sql.IDU("update user set email='%s' where phone='%s'" % (new_email, name)) sql.close() return "1" return render_template("amend_email.html", data=locals())
def judge(): print("asdasdsad") name = session.get("ID") sql = SQL("shopping_flask") password = sql.IDU("select pay_pwd from user where phone='%s'" % name)[0][0] pay_pwd = request.values.get("pwd") print(pay_pwd, password) if password: if check_password_hash(password, pay_pwd): return "1" else: return "0" return "-1"
def pay_pwd(): name = session.get("ID") sql = SQL("shopping_flask") nick_name = session.get("nick_name") head_img = sql.select("select chat_head from user where phone='%s'" % name)[0][0][8:] # 密码修改 if request.method == "POST": new_pwd = request.values.get("new_pwd") # 给新密码进行加密,到数据库中进行更新 pwd = generate_password_hash(new_pwd) sql.IDU("update user set pay_pwd='%s' where phone='%s'" % (pwd, name)) return "1" sql.close() return render_template("pay_pwd.html", data=locals())
def del_cart(): name = session.get("ID") sql = SQL("shopping_flask") shop_id = request.values.get("shop_id") state = request.values.get("state") # 判断该商品是否在交易中(state=1), 如果在交易中则不能删除 if state == "1": return "0" else: sql.IDU( "delete from cart where u_name='%s'and shop_id='%s' and state='%s'" % (name, shop_id, state)) # 这里有个问题,就是json.dumps无法序列化时间类型,就会导致is not JSON serializable 错误,切记,如果需要的时间,就将它转换为字符串 result = sql.select( "select commodity.shop_id,titles,Zimg,cart.state " "from commodity, cart where commodity.shop_id=cart.shop_id and u_name='%s'" % name)[0:5] print(result) sql.close() return json.dumps(result)
def amend_data(): name = session.get("ID") nick_name = session.get("nick_name") sql = SQL("shopping_flask") result = sql.select("select * from user where phone='%s'" % name) real_name = result[0][5] address = result[0][6] birthday = str(result[0][7]).split("-") head_img = sql.select("select chat_head from user where phone='%s'" % name)[0][0][8:] sex = sql.select("select sex from user where phone='%s'" % name)[0][0] if request.method == "POST": name = session.get("ID") real_name = request.values.get("real_name") birthday = request.values.get("birthday") sex = request.values.get("sex") sql.IDU( "update user set real_name='%s',birthday='%s',sex='%s' where phone='%s'" % (real_name, birthday, sex, name)) return "1" return render_template("amend_data.html", data=locals())
def search(): name = session.get("ID") sql = SQL("shopping_flask") nick_name = session.get("nick_name") if request.method == "POST": shop_name = request.values.get("shop_name") page = int(request.values.get("page")) * 20 trade_name = "%" + shop_name + "%" search_data = sql.select( "select * from commodity where titles like '%s' limit %s,20" % (trade_name, page)) return json.dumps(search_data) shop_name = request.args.get("shop_name") # 获取搜索的商品名 trade_name = "%" + shop_name + "%" search_data = sql.select("select * from commodity where titles like '%s'" % trade_name) page = len(search_data) page_count = math.ceil(page / 20) # 查找对应种类销量最高的几个数据 if search_data: Sales_data = sql.select( "select * from commodity where kind='%s' order by Sales limit 0,5" % search_data[0][-1]) # 如果用户登录,则将用户的搜索历史存到数据库中 if name and shop_name: # 在html中没有进行查重操作,所以在数据库中进行查找判断 if sql.select( "select * from user_history where user_id='%s' and history='%s'" % (name, shop_name)): pass else: sql.IDU( "insert into user_history(user_id, history) values('%s','%s')" % (name, shop_name)) print("存储历史") sql.close() return render_template("search_data.html", data=locals())
def delete_collect_store(): sql = SQL("shopping_flask") name = session.get("ID") sql.IDU("delete from collect_store where username='******'" % name) return "1"