def update_pwd(): data = request.get_json() if is_admin_login(request): admin_id = get_admin_id_by_cookie(request) else: return jsonify({'code': 208, 'msg': "登录信息已经过期"}) update_pwd = data['update_pwd'] original_pwd = data['original_pwd'] # 根据用户id查询相关元组,如果该用户存在,继续逻辑判断,否则返回该用户不存在 user = Admin.query.filter(Admin.admin_id == admin_id).first() if user: original_pwd = my_md5(str(original_pwd)) # 根据传进来的id,原密码和数据库里的数据进行比较,成功继续,否则返回密码不一致 account = Admin.query.filter(Admin.nick_name == user.nick_name, original_pwd == user.passwd).first() # 密码匹配成功 if account: user.passwd = my_md5(str(update_pwd)) # 新密码和旧密码一样,返回相同密码,否则将新密码提交 if user.passwd == original_pwd: return jsonify({'code': 203, 'msg': "the same password"}) else: db.session.commit() uid = request.cookies.get('admin_token') # 从redis里删除该用户之前的token redis = get_redis_cli() redis.delete("admin:" + str(uid)) return jsonify({'code': 200, 'msg': "update password successfully"}) else: return jsonify({'code': 203, 'msg': "Passwords are inconsistent!"}) else: return jsonify({'code': 203, 'msg': "admin_id does't exit"})
def notice(): data = request.get_json() sort = data['sort'] if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) user_id = get_user_id_by_cookie(request) if exist_user(user_id): arr = [] redis = get_redis_cli() if sort == "0": # 状态为0的话,表示查看关注列表 result = Notice.query.filter(Notice.user_id == user_id, Notice.sort == sort).all() status_key = "status:" + "concern:" + str(user_id) redis.delete(status_key) for e in result: arr.append({ "notice_title": e.notice_title, "notice_content": e.notice_content, "notice_time": str(e.notice_time), "notice_status": e.notice_status, "sort":sort }) # -1未读 0已读 if e.notice_status == -1: e.notice_status = 0 db.session.commit() if sort == "2": result = Notice.query.filter(Notice.user_id == user_id, Notice.sort == sort).all() status_key = "status:" + "comment:" + str(user_id) redis.delete(status_key) for e in result: arr.append({ "notice_title": e.notice_title, "notice_content": e.notice_content, "notice_time": str(e.notice_time), "notice_status": e.notice_status, "sort": sort }) if e.notice_status == -1: e.notice_status = 0 db.session.commit() if sort == "1": result = Notice.query.filter(Notice.user_id == user_id, Notice.sort == sort).all() status_key = "status:" + "like:" + str(user_id) redis.delete(status_key) for e in result: arr.append({ "notice_title": e.notice_title, "notice_content": e.notice_content, "notice_time": str(e.notice_time), "notice_status": e.notice_status, "sort": sort }) if e.notice_status == -1: e.notice_status = 0 db.session.commit() return jsonify(arr) else: return jsonify({"code": "203", "msg": "用户不存在"})
def notice_(): if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) user_id = get_user_id_by_cookie(request) if exist_user(user_id): redis = get_redis_cli() status_key1 = "status:" + "concern:" + str(user_id) status_key2 = "status:" + "comment:" + str(user_id) status_key3 = "status:" + "like:" + str(user_id) concern_status = 0 comment_status = 0 like_status = 0 if redis.keys(status_key1): concern_status = 1 if redis.keys(status_key2): comment_status = 1 if redis.keys(status_key3): like_status = 1 if (concern_status == 1 or comment_status == 1 or like_status == 1): return jsonify({ "code": "200", "msg": "1", "concern_status": concern_status, "comment_status": comment_status, "like_status": like_status }) return jsonify({"code": "200", "msg": "0"}) else: return jsonify({"code": "203", "msg": "用户不存在"})
def admin_quit(): redis = get_redis_cli() if not is_admin_login(request): return jsonify({'code': 208, 'msg': '管理员信息已过期'}) uuid = request.cookies.get('admin_token') redis.delete("admin:" + str(uuid)) return jsonify({'code': 200, 'msg': '管理员退出成功'})
def update_reward(): file = request.files['file'] if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) user_id = get_user_id_by_cookie(request) if exist_user(user_id): filename = "".join(lazy_pinyin(file.filename)) b = "." redis = get_redis_cli() incr = redis.incr('user-reward') filename = str(incr) + str(filename[filename.rfind(b):]) print(filename) if file and allowed_file(filename): # 这个方法来保证文件名是安全的 filename = secure_filename(filename) file.save( os.path.join(app.config['USER_REWARD_UPLOAD_FOLDER'], filename)) url = "http://192.168.195.10:5005/user/reward/" + filename user = User.query.filter(User.id == user_id).first() user.reward_addr = url db.session.commit() return {"code": "200", "msg": "上传成功", "url": url} else: return {"code": "203", "msg": "上传失败"} else: return {"code": "203", "msg": "抱歉,用户不存在"}
def user_quit(): redis = get_redis_cli() if not is_user_login(request): return jsonify({'code': 208, 'msg': '用户信息已过期'}) user_id = get_user_id_by_cookie(request) uuid = redis.get("user:"******"user:"******"user:" + str(uuid)) return jsonify({'code': 200, 'msg': '用户退出成功'})
def get_token(): uid = request.cookies.get('admin_token') redis = get_redis_cli() uid_redis = redis.get("admin:" + str(uid)) if uid_redis is not None: data = request.get_json() article_id = data["article_id"] article = Article.query.filter(Article.article_id == article_id).first() return delete(article) else: return jsonify({'code': 203, 'msg': 'please login again'})
def dactive_user(): uid = request.cookies.get('admin_token') if uid is not None: redis = get_redis_cli() if redis.ttl("admin:" + str(uid)) <= 0: return jsonify({'code': 208, 'msg': 'try login again'}) user_id = request.args.get("user_id") user = set_user_status(user_id) if user is not None: return jsonify({'code': 200, 'msg': "success"}) return jsonify({'code': 203, 'msg': "user doesn't exit"}) else: return jsonify({'code': 208, 'msg': "please login"})
def update_in(): data = request.get_json() if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) id = get_user_id_by_cookie(request) nick_name = data["nick_name"] # 自我介绍(简介) profile = data["profile"] mail = data["mail"] avatar = data["avatar"] tel = data["tel"] redis = get_redis_cli() user = User.query.filter(User.id == id).first() if user is not None: user.nick_name = nick_name user.profile = profile # mail为 unique user.mail = mail user.avatar = avatar user.tel = tel try: db.session.commit() salt = redis.get("user:"******"id": id, "nick_name": nick_name, "profile": profile, "mail": mail, "avatar": avatar, "tel": tel, "work_id": user.work_id, "gender": user.gender, "passwd": user.passwd, "vir_money": user.vir_money, "followers": user.followers, "follow": user.follow, "profile": user.profile, "reward_addr": user.reward_addr, "effect": user.effect, "status": user.status } redis.set("user:"******"utf-8"), 604800) return jsonify({"code": "200", "msg": "修改成功"}) except: db.session.rollback() return jsonify({"code": "203"}) else: # 该用户不存在 return jsonify({"code": "203"})
def article_pic(): file = request.files['file'] filename = "".join(lazy_pinyin(file.filename)) b = "." redis = get_redis_cli() incr = redis.incr('article-images') filename = str(incr) + str(filename[filename.rfind(b):]) if file and allowed_file(filename): filename = secure_filename(filename) file.save(os.path.join(app.config['ARTICLE_UPLOAD_FOLDER'], filename)) url = "http://192.168.195.10:5005/article/images/" + filename # TODO 文章的部分先返回url,先不存数据库,因为可能有多张图片 return {"code": "200", "msg": "上传成功", "url": url} else: return {"code": "203", "msg": "上传失败"}
def token(account): redis = get_redis_cli() uid = uuid.uuid4() result = User.query.filter_by(work_id=account).first() if result.first_login == 1: first_login = 1 # 先增加虚拟货币数,然后记录下来 result.vir_money += 5 # 添加记录 reward_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 如果打赏用户是0,article_id 也是0, 说明是系统打赏的 reward = Reward(result.id, result.id, 5, reward_time, 0, "系统打赏") db.session.add(reward) else: first_login = 0 result.first_login = 0 db.session.commit() list = { "id": result.id, "nick_name": result.nick_name, "work_id": result.work_id, "tel": result.tel, "gender": result.gender, "avatar": result.avatar, "mail": result.mail, "vir_money": result.vir_money, "follow": result.follow, "followers": result.followers, "profile": result.profile, "reward_addr": result.reward_addr, "effect": result.effect, "status": result.status } # key为 uid, value为 str(list), 有效期604800s redis.set("user:"******"user:"******"avatar": result.avatar, "effect": result.effect, "first_login": first_login })) response.set_cookie("token", str(uid)) return response
def token(account): redis = get_redis_cli() uid = uuid.uuid4() result = Admin.query.filter_by(nick_name=account).first() list = { "id": result.admin_id, "nick_name": result.nick_name, 'head_pic': result.head_pic, 'tel': result.tel, 'mail': result.mail } # key为 uid, value为 str(list), 有效期604800s redis.set("admin:" + str(uid), str(list), 604800) response = make_response( jsonify({ 'code': 200, 'msg': 'login successfully', 'id': result.admin_id, 'nick_name': result.nick_name, 'head_pic': result.head_pic })) response.set_cookie("admin_token", str(uid)) return response
def ad_update_pic(): file = request.files['file'] if not is_admin_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) admin_id = get_admin_id_by_cookie(request) if exist_admin(admin_id): filename = "".join(lazy_pinyin(file.filename)) b = "." redis = get_redis_cli() incr = redis.incr('admin-images') filename = str(incr) + str(filename[filename.rfind(b):]) if file and allowed_file(filename): filename = secure_filename(filename) file.save(os.path.join(app.config['ADMIN_UPLOAD_FOLDER'], filename)) url = "http://192.168.195.10:5005/admin/images/" + filename admin = Admin.query.filter(Admin.admin_id == admin_id).first() admin.head_pic = url db.session.commit() return {"code": "200", "msg": "上传成功", "url": url} else: return {"code": "203", "msg": "上传失败"} else: return {"code": "203", "msg": "抱歉,管理员不存在"}
def send_comment(): data = request.get_json() print(data) if is_user_login(request): user_id = get_user_id_by_cookie(request) else: return jsonify({'code': 208, 'msg': "登录信息已经过期"}) article_id = data['article_id'] comment_id = data['comment_id'] be_comment_user_id = data['be_comment_user_id'] com_content = data['com_content'] redis = get_redis_cli() user = User.query.filter(User.id == user_id).first() user_name = user.nick_name if exist_user(user_id) and exist_article(article_id): print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) comment_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if be_comment_user_id == "null": comment = Comment(None, user_id, comment_id, article_id, comment_time, com_content, None) db.session.add(comment) # 评论文章,个人+1积分 user = User.query.filter(User.id == user_id).first() user.effect += 1 db.session.commit() result = Article.query.filter(Article.article_id == article_id).first() result.heat = int(result.heat) + int(5) # 找出作者的id author_user_id = result.user_id notice_content = str(user_name) + "评论了你的文章:" + str(result.title) notice_title = "你收到了一条评论" n1 = Notice(None, author_user_id, notice_title, notice_content, 0, comment_time, -1, 2, user_id) db.session.add(n1) # 有人评论,作者积分+2 author = User.query.filter(User.id == author_user_id).first() author.effect += 3 db.session.commit() status_key = "status:" + "comment:" + str(author_user_id) if redis.keys(status_key): print(redis.keys(status_key)) else: redis.set(status_key, 1) else: if exist_user(be_comment_user_id): comment = Comment(None, user_id, comment_id, article_id, comment_time, com_content, be_comment_user_id) db.session.add(comment) db.session.commit() notice_title = "你收到了一条回复" notice_content = str(user_name) + "回复了你的评论" n1 = Notice(None, be_comment_user_id, notice_title, notice_content, 0, comment_time, -1, 2, user_id) db.session.add(comment) db.session.add(n1) db.session.commit() status_key = "status:" + "comment:" + str(be_comment_user_id) if redis.keys(status_key): print(redis.keys(status_key)) else: redis.set(status_key, 1) else: return jsonify({"code": "203", "msg": "被评论用户不存在,无法发表此评论"}) # 评论数加1,并更新到redis result = db.session.query(Article).get(article_id) result.comment_num = result.comment_num + 1 db.session.commit() return jsonify({"code": "200", "msg": "评论成功"}) else: return jsonify({"code": "203", "msg": "用户不存在,无法发表此评论"})
def like(): data = request.get_json() # 参数:用户id 文章id 点赞状态 点赞数(?) if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) user_id = get_user_id_by_cookie(request) if not exist_user(user_id): return {"code": 201, "msg": "用户不存在"} article_id = int(data['article_id']) # action == 0 -> 取消点赞, 1 -> 点赞 action = int(data['like_action']) redis = get_redis_cli() time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 是否已经点赞 liked = db.session.query(Fabulous).filter_by(user_id=user_id, article_id=article_id).first() article = db.session.query(Article).filter_by(article_id=article_id).first() if article is None: # 并没有该文章, 报错 return jsonify({"code": 500, "msg": "该文章不存在"}) # 作者id author_id = article.user_id if not exist_user(author_id): return {"code": 203, "msg": "作者不存在"} author = User.query.filter(User.id == author_id).first() # 用户 user = User.query.filter(User.id == user_id).first() notice_title = "你有新的一条点赞信息" notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title) status_key = "status:" + "like:" + str(article.user_id) result = Notice.query.filter(Notice.user_id == author_id, Notice.initiative_id == user_id, Notice.sort == '1').first() if liked is not None: if action == 1: if liked.fabulous_status == 0: liked.fabulous_status = -1 article.fabulous_num += 1 # 有人点赞,作者+1积分 author.effect += 2 # 点赞,个人+1积分 user.effect += 1 db.session.commit() like_count = db.session.query(Article).filter_by(article_id=article_id).first().fabulous_num # 发送推送 # 找出被点赞的文章作者的id # 点赞热度文章加3 article.heat = int(article.heat) + int(3) if result is None: n1 = Notice(None, article.user_id, notice_title, notice_content, 0, time1, -1, 1,user_id) db.session.add(n1) else: result.notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title) db.session.commit() if redis.keys(status_key): redis.incr(status_key) else: redis.set(status_key, 1) return jsonify({"code": 200, "msg": "点赞成功", "like_count": like_count}) return jsonify({"code": 204, "msg": "已经点过赞"}) else: if liked.fabulous_status == 1 or liked.fabulous_status == -1: article.fabulous_num -= 1 liked.fabulous_status = 0 # 有人取消点赞,作者-1积分 author.effect -= 2 # 取消点赞,个人-1积分 user.effect -= 1 article.heat -= 3 db.session.commit() redis_result=redis.get(status_key) if int(redis_result) == 1 : redis.delete(status_key) if int(redis_result) > 1: redis.decr(status_key) return jsonify({"code": 205, "msg": "取消点赞成功"}) else: if action == 1: new_like = Fabulous(None, user_id=user_id, article_id=article_id, fabulous_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), fabulous_status=1) db.session.add(new_like) article.fabulous_num += 1 # 有人点赞,作者+1积分 author.effect += 2 db.session.commit() like_count = db.session.query(Article).filter_by(article_id=article_id).first().fabulous_num # TODO 发推送 # 点赞热度文章加3 article.heat = int(article.heat) + int(3) if result is None: n1 = Notice(None, article.user_id, notice_title, notice_content, 0, time1, -1, 1, user_id) db.session.add(n1) else: result.notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title) db.session.commit() if redis.keys(status_key): redis.incr(status_key) else: redis.set(status_key, 1) else: return jsonify({"code": 203, "msg": "没有点赞无法取消点赞"}) return jsonify({"code": 200, "msg": "点赞成功", "like": like_count})