def put_user_order(): """取消订单""" try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) order_id = request.json.get("order_id") if not order_id: return response(msg="Bad Request: Miss params: 'order_id'.", code=1, status=400) # 更新 doc = manage.client["order"].update( { "order": order_id, "user_id": user_id, "state": 1 }, {"$set": { "state": -1, "update_time": 1 }}, multi=True) if doc["n"] == 0: return response(msg="Update failed.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s.", code=1, status=500)
def get_user_audit(): """用户审核列表""" try: # 参数 num = request.args.get("num") # ≥1 page = request.args.get("page") # ≥1 # 校验参数 if not num: return response(msg="Bad Request: Miss params: 'num'.", code=1, status=400) if not page: return response(msg="Bad Request: Miss params: 'page'.", code=1, status=400) if int(page) < 1 or int(num) < 1: return response(msg="Bad Request: Params 'page' or 'num' is erroe.", code=1, status=400) # 查询 pipeline = [ {"$match": {"is_auth": False, "state": 1, "type": {"$in": ["org", "user"]}}}, {"$skip": (int(page) - 1) * int(num)}, {"$limit": int(num)}, {"$project": {"_id": 0, "uid": 1, "head_img_url": 1, "nick": 1, "account": 1, "update_time": 1, "id_card_name": 1, "id_card": 1}} ] cursor = manager.client["user"].aggregate(pipeline) data_list = [doc for doc in cursor] return response(data=data_list if data_list else []) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def delete_user_car_goods(): """删除购物车商品""" try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) # 商品id uid_list = request.json.get("uid_list") # array if not uid_list: return response(msg="Bad Request: Miss param 'uid_list'.", code=1, status=400) manage.client["order"].update({"uid": { "$in": uid_list }}, {"$set": { "state": -1 }}, multi=True) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s.", code=1, status=500)
def put_video_order_sort(): """置顶影集排序""" try: # 参数 works_id = request.json.get("works_id") order = request.json.get("order") # 升序 1 降序 -1 if not works_id: return response(msg="Bad Request: Miss params: 'works_id'", code=1, status=400) if order not in [-1, 1]: return response(msg="Bad Request: Params 'order' is error", code=1, status=400) doc = manage.client["works"].update({"uid": works_id}, {"$inc": { "order": order }}) if doc["n"] == 0: return response(msg="Bad Request: 'order' update failed.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def upload_docx_file(): """上传word文件""" try: file = request.files.get("doc") type = request.form.get("type") if not type: return response(msg="Bad Request: Miss params: 'type'.", code=1, status=400) path_p = os.getcwd() + "/statics/files/document" file_ext = file.filename.split(".")[-1] if file_ext != "docx": return response(msg="只允许上传word文件", code=1) # 创建目录 if not os.path.exists(path_p): os.makedirs(path_p) # 写入文件 with open(path_p + f"/{type}.docx", "wb") as f: f.write(file.read()) file_path = f"/document/{type}.docx" doc = manage.client["document"].update( {"type": type}, {"$set": { "file_path": file_path }}) if doc["n"] == 0: return response(msg="Bad Request: Param 'uid' is error.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def wrapper(*args, **kwargs): try: # 验证token module_id = request.headers.get("module_id") permission_id = request.headers.get("permission_id") from utils.util import response if not module_id: return response(msg="Bad Request: Miss params 'module_id'.", code=1, status=400) if not permission_id: return response( msg="Bad Request: Miss params 'permission_id'.", code=1, status=400) user_id = g.user_data["user_id"] doc = client["role"].find_one({ "module_id": module_id, "permission_id": permission_id }) if not doc: return response(msg="您没有操作权限,请联系超级管理员", code=1) except Exception as e: log.error(e) return return f(*args, **kwargs)
def put_lable_priority(): """设置标签优先级""" try: # 参数 priority = request.json.get("priority") label_id = request.json.get("label_id") if not priority: return response(msg="Bad Request: Miss params: 'priority'.", code=1, status=400) if not label_id: return response(msg="Bad Request: Miss params: 'label_id'.", code=1, status=400) doc = manage.client["label"].update( {"uid": label_id}, {"$set": { "priority": float(priority) }}) if doc["n"] == 0: return response(msg="Updated failed.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def post_upload_banner(banner_max=10): """ 上传banner图 :param banner_max: banner上限 """ try: count = manage.client["banner"].find({"state": 1}).count() if count >= banner_max: return response(msg=f"最多支持{banner_max}张轮播图片", code=1) user_id = g.user_data["user_id"] data_list = pic_upload_api(user_id) file_path = data_list[0]["file_path"] # 入库 uid = base64.b64encode(os.urandom(16)).decode() manage.client["banner"].insert({ "uid": uid, "order": count + 1, "state": 1, "pic_url": file_path, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) }) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def put_banner_state(): """删除banner""" try: # 获取banner_id banner_id = request.json.get("banner_id") if not banner_id: return response(msg="Bad Request: Miss params: 'banner_id'", code=1, status=400) # 更新 doc = manage.client["banner"].update({"uid": banner_id}, {"$set": { "state": -1 }}) if doc["n"] == 0: return response(msg="Bad Request: Update failed.", code=1, status=400) doc = manage.client["banner"].find_one({"uid": banner_id}) order = doc["order"] manage.client["banner"].update({ "order": { "$gt": order }, "state": 1 }, {"$inc": { "order": -1 }}, multi=True) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_forgot_password(): """忘记密码""" try: # 获取参数 mobile = request.json.get("mobile") sms_code = request.json.get("sms_code") password = request.json.get("password") # 判断参数是否为空 if not password: return response(msg="请输入密码", code=1) # 校验短信码 doc = manage.client["verify"].find_one({ "uid": mobile, "type": "sms", "code": sms_code }) if not doc: return response(msg="短信码或手机号错误", code=1) # 用户密码加密 password_b64 = base64.b64encode(password.encode()).decode() # 更新密码 manage.client["user"].update_one({"uid": uid}, {"$set": { "password": password_b64 }}) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error:%s." % str(e), code=1, status=500)
def put_report_comment_state(option_max=10): """ 审核举报评论 :param option_max: 最多允许选择的个数 """ try: # 参数 comment_list = request.json.get("comment_list") # array state = request.json.get("state") # -1删除,1标记正常 if comment_list: return response(msg="Bad Request: Miss params: 'comment_list'.", code=1, status=400) if len(comment_list) > option_max: return response(msg=f"最多允许选择{option_max}条评论", code=1) if state not in ["-1", "1"]: return response(msg="Bad Request: Params 'state' is erroe.", code=1, status=400) doc = manage.client["comment"].update({"uid": { "$in": comment_list }}, {"$set": { "state": int(state) }}) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_captcha(): """获取图片码""" data = {} try: # 生成图片唯一id str_items = string.ascii_letters str_random = random.choice(str_items) + f"{int(time.time() * 1000)}" uid = hashlib.md5(str_random.encode()).hexdigest() # 获取图片验证码 name, text, image = captcha.captcha.generate_captcha() print(text) # 图片验证码写入数据库 condition = { "uid": uid, "type": "pic", "code": text, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) } manage.client["verify"].insert_one(condition) # 响应base64格式的图片验证码 pic_b64 = "data:image/jpg;base64," + base64.b64encode(image).decode() # resp = make_response(image) # resp.headers["Content-Type"] = "image/jpg" data["uid"] = uid data["pic"] = pic_b64 return response(data=data) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error:%s." % str(e), code=1, status=500)
def pic_upload_api(user_id): """ 图片上传调用接口 :param user_id: 用户id """ data_list = [] try: # 参数 pic_list = request.files.getlist("pic_list[]") if not pic_list: return response(msg="Bad Request: Miss param: 'pic_list'.", code=1, status=400) file = util.UploadSmallFile(manage.app, ALLOWED_EXTENSIONS, 48 * 1024 * 1024, manage.log) context = file.upload_file("pic_list[]", "files", user_id) if context["code"] == 0: return response(msg=context["msg"], code=1, status=400) data_list = context["data"] return data_list except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_user_history_label(label_max=20): """ 用户历史标签 :param label_max: 标签个数上限 """ try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) # 查询 pipeline = [{ "$match": { "user_id": user_id, "state": 1 } }, { "$project": { "_id": 0 } }, { "$sort": SON(["create_time", -1]) }, { "$limit": label_max }] cursor = manage.client["history_label"].aggregate(pipeline) data_list = [doc for doc in cursor] return response(data=data_list if data_list else []) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s.", code=1, status=500)
def pic_upload_api(user_id): """ 图片上传调用接口 :param user_id: 用户id """ try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) pic_list = request.files.getlist("pic_list[]") if not pic_list: return response(msg="Bad Request: Miss param: 'pic_list'.", code=1, status=400) file = UploadSmallFile(manage.app, 100 * 1024 * 1024, manage.log) context = file.upload_file("pic_list[]", "files", user_id) if context["code"] == 0: return response(msg=context["msg"], code=1, status=400) data_list = context["data"] return data_list except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def post_material_upload_common(domain=constant.DOMAIN): """ 素材上传通用接口 :param domain: 域名 """ data = {} try: # 参数 user_id = g.user_data["user_id"] # if not user_id: # return response(msg="Bad Request: User not logged in.", code=1, status=400) pic_list = request.files.getlist("pic_list[]") if not pic_list: return response(msg="Bad Request: Miss param: 'pic_list'.", code=1, status=400) file = UploadSmallFile(manage.app, 100 * 1024 * 1024, manage.log) context = file.upload_file("pic_list[]", "files", user_id) if context["code"] == 0: return response(msg=context["msg"], code=1, status=400) data = context["data"] for i in data: file_path = i["file_path"] context = GenerateImage.generate_image_origin(i, "files") file_path = context["file_path_b"] i["file_path"] = domain + file_path return response(data=data) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def put_banner_link(): """修改banner链接""" try: # 获取数据 link = request.json.get("link") banner_id = request.json.get("banner_id") if not link: return response(msg="Bad Request: Miss params: 'link'.", code=1, status=400) if not banner_id: return response(msg="Bad Request: Miss params: 'banner_id'", code=1, status=400) # 更新link doc = manage.client["banner"].update({"uid": banner_id}, {"$set": { "link": link }}) if doc == 0: return response(msg="Bad Request: Update failed.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def put_banner_order(): """修改banner的序号""" try: # 获取参数 inc = request.json.get("inc") # 向上传1, 向下传-1 banner_id = request.json.get("banner_id") if not banner_id: return response(msg="Bad Request: Miss params: 'banner_id'", code=1, status=400) # 更新 doc = manager.client["banner"].update({"banner_id": banner_id}, {"$inc": { "order": inc }}) if doc == 0: return response(msg="Bad Request: Update failed.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def post_add_keyword(): """添加关键词""" try: # 获取参数 keyword = request.json.get("keyword") user_id = request.json.get("user_id") if not keyword: return response(msg="Bad Request: Miss params: 'keyword'", code=1, status=400) if not user_id: return response(msg="Bad Request: Miss params: 'user_id'", code=1, status=400) # 添加 manage.client["user_search"].insert({ "user_id": user_id, "keyword": keyword, "state": 0, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) }) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def post_add_recomm_works(upload_max=10): """ 添加推荐作品 :param upload_max: 允许同时上传作品的上限值 """ try: # 获取参数 works_list = request.json.get("works_list") if not works_list: return response(msg="Bad Request: Miss params: 'works_list'.", code=1, status=400) # 最大上传10个 if len(works_list) > upload_max: return response(msg=f"最多允许选择{upload_max}个作品", code=1) doc = manage.client["works"].update({"uid": { "$in": works_list }}, {"$set": { "is_recommend": True }}, multi=True) if doc["n"] == 0: return response(msg="Bad Request: Update failed.", code=1, status=400) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_not_complete_order_count(): """未付款的订单数""" try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) pipeline = [ { "$match": { "user_id": user_id, "state": 1 } }, { "$group": { "_id": "$order" } }, { "$count": "count" }, ] cursor = manage.client["order"].aggregate(pipeline) data_list = [doc for doc in cursor] count = data_list[0]["count"] if data_list else 0 return response(data=count) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s.", code=1, status=500)
def put_user_balance_operation(): """用户余额操作接口""" try: # 参数 user_id = request.json.get("user_id") inc = request.json.get("inc") if not user_id: return response(msg="Bad Request: Miss params: 'user_id'.", code=1, status=400) if not inc: return response(msg="请输入充值金额", code=1) doc = manage.client["user"].update({"uid": user_id}, {"$inc": {"balance": float(inc)}}) if doc["n"] == 0: return response(msg="操作失败", code=1) # 记录操作记录 stamp_time = int(time.time() * 1000) random_str = "%02d" % random.randint(0, 100) order = random_str + f"{stamp_time}" doc = manager.client["user_id"].find_one({"user_id": user_id}) balance = doc["balance"] condition = {"user_id": user_id, "type": "后台充值" if float(inc) >= 0 else "后台扣除", "order": order, "amount": float(inc), "balance": float(inc) + balance, "state": 1, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) } manage.client["balance_record"].insert(condition) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def wrapper(*args, **kwargs): try: user_data = {"user_id": None, "user_info": None} token = request.headers.get("token") if token: pipeline = [{ "$match": { "token": token } }, { "$project": { "_id": 0, "uid": 1, "nick": 1, "sex": 1, "head_img_url": { "$concat": [DOMAIN, "$head_img_url"] }, "sign": 1, "mobile": 1, "background_url": { "$concat": [DOMAIN, "$background_url"] }, "works_num": 1, "label": 1, "login_time": 1, "group": 1, "create_time": 1, "update_time": 1, "auth": 1 } }] cursor = client["user"].aggregate(pipeline) data_list = [doc for doc in cursor] if not data_list: return util.response(msg="登录失效", code=1, status=401) doc = data_list[0] # 判断token是否失效 date0 = datetime.datetime.fromtimestamp( doc.get("login_time") // 1000) # 生成token date1 = datetime.datetime.strptime( date0.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S") date2 = datetime.datetime.strptime( datetime.datetime.now().strftime("%Y-%m-%d"), "%Y-%m-%d") delta = date2 - date1 if delta.days > 1: return util.response(msg="登录失效", code=1, status=401) uid = doc.get("uid") user_data = {"user_id": uid, "user_info": doc} except Exception as e: log.error(e) finally: g.user_data = user_data return f(*args, **kwargs)
def post_pic_material_upload(domain=constant.DOMAIN): """ 素材上传接口 :param domain: 域名 """ try: # 参数 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1, status=400) data_list = pic_upload_api(user_id) # 入库 temp_list = [] for obj in data_list: uid = base64.b64encode(os.urandom(32)).decode() condition = { "uid": uid, "user_id": user_id, "pic_url": obj["file_path"], "big_pic_url": obj["file_path"], "thumb_url": obj["file_path"], "size": obj["size"], "state": 0, "create_time": int(time.time() * 1000), "update_time": int(time.time() * 1000) } temp_list.append(condition) cursor = manage.client["pic_material"].insert(temp_list) id_list = [doc for doc in cursor] pipeline = [{ "$match": { "_id": { "$in": id_list } } }, { "$project": { "_id": 0, "uid": 1, "pic_url": { "$concat": [domain, "$pic_url"] } } }] cursor = manage.client["pic_material"].aggregate(pipeline) data_list = [doc for doc in cursor] if not data_list: raise Exception("Failed to get data.") return response(data=data_list) except Exception as e: manage.log.error(e) return reponse(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_option_works_list(): """作品选择列表""" try: data = works_list_api(False) return response(data=data) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def put_report_comment_state(option_max=10): """ 审核举报评论 :param option_max: 最多允许选择的个数 """ try: # 参数 comment_list = request.json.get("comment_list") # array state = request.json.get("state") # -1删除,1标记正常 if not comment_list: return response(msg="Bad Request: Miss params: 'comment_list'.", code=1, status=400) if len(comment_list) > option_max: return response(msg=f"最多允许选择{option_max}条评论", code=1) if state not in [-1, 1]: return response(msg="Bad Request: Params 'state' is erroe.", code=1, status=400) doc = manage.client["comment"].update({"uid": { "$in": comment_list }}, {"$set": { "state": int(state) }}, multi=True) doc = manage.client["like_records"].update( {"comment_id": { "$in": comment_list }}, {"$set": { "state": int(state) }}, multi=True) # 删除评论时,相应减少works中comment_num if state == -1: cursor = manage.client["comment"].find( {"uid": { "$in": comment_list }}, { "_id": 0, "works_id": 1 }) works_id_list = [doc["works_id"] for doc in cursor] works_id_list = list(set(works_id_list)) doc = manage.client["works"].update( {"uid": { "$in": works_id_list }}, {"$inc": { "comment_num": -1 }}) return response() except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_bad_keyword_list(): """敏感词列表""" try: # 查询 cursor = manage.client["bad"].find({"state": 1}) data_list = [doc["keyword"] for doc in cursor] data_str = "、".join(data_list) return response(data=data_list) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s.", code=1, status=500)
def get_banner(domain=constant.DOMAIN): """ 获取banner :param domain: 域名 """ try: # 获取数据 pipeline = [{ "$match": { "state": 1 } }, { "$sort": SON([("order", 1)]) }, { "$project": { "_id": 0, "uid": 1, "link": 1, "order": 1, "pic_url": { "$concat": [domain, "$pic_url"] }, "create_time": { "$dateToString": { "format": "%Y-%m-%d %H:%M", "date": { "$add": [manage.init_stamp, "$create_time"] } } }, "update_time": { "$dateToString": { "format": "%Y-%m-%d %H:%M", "date": { "$add": [manage.init_stamp, "$update_time"] } } } } }] cursor = manage.client["banner"].aggregate(pipeline) data_list = [] i = 1 for doc in cursor: doc["order"] = i i += 1 data_list.append(doc) return response(data=data_list) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_user_audit_filter(): """用户审核列表搜索""" try: # 参数 num = request.args.get("num") # ≥1 page = request.args.get("page") # ≥1 category = request.args.get("category") # 账号传account, 昵称传nick content = request.args.get("content") # 校验参数 if not num: return response(msg="Bad Request: Miss params: 'num'.", code=1, status=400) if not page: return response(msg="Bad Request: Miss params: 'page'.", code=1, status=400) if int(page) < 1 or int(num) < 1: return response(msg="Bad Request: Params 'page' or 'num' is erroe.", code=1, status=400) if category not in ["account", "nick"]: return response(msg="Bad Request: Params 'category' is error.", code=1, status=400) if not content: return response(msg="请输入内容", code=1) if content and len(content) > search_max: return response(msg="搜索内容最长16个字符,请重新输入", code=1) # 查询 pipeline = [ {"$match": {"is_auth": False, "state": 1, "type": {"$in": ["org", "user"]}, "nick" if category == "nick" else "account": {"$regex": content}}}, {"$skip": (int(page) - 1) * int(num)}, {"$limit": int(num)}, {"$project": {"_id": 0, "uid": 1, "head_img_url": 1, "nick": 1, "account": 1, "update_time": 1, "id_card_name": 1, "id_card": 1}} ] cursor = client.client["user"].aggregate(pipeline) data_list = [doc for doc in cursor] return response(data=data_list if data_list else []) except Exception as e: manage.log.error(e) return response(msg="Internal Server Error: %s." % str(e), code=1, status=500)
def get_user_logout(): """退出登录""" try: # 用户登录状态判断 user_id = g.user_data["user_id"] if not user_id: return response(msg="Bad Request: User not logged in.", code=1) return response(msg="退出成功") except Exception as e: manage.log.error(e) return response(msg="Internal Server Error:%s." % str(e), code=1, status=500)