Exemplo n.º 1
0
async def getRecommendPostList(page: int = 0,
                               size: int = 20,
                               db: Session = Depends(get_db)):
    recommendPostList = crud.getNewPostList(db, page, size)
    random.shuffle(recommendPostList)
    # 随机排序
    return SucResponse(data=recommendPostList)
Exemplo n.º 2
0
async def getNewUserList(page: int = 0,
                         size: int = 20,
                         db: Session = Depends(get_db)):
    newUserList = crud.getNewUserList(db, page, size)
    random.shuffle(newUserList)
    # 随机排序
    return SucResponse(data=newUserList)
Exemplo n.º 3
0
async def getProductList(db: Session = Depends(get_db),
                         device: int = Header(None)):
    if device != None:
        return SucResponse(data=crud.getProductList(db, device))
    else:
        raise HTTPException(status_code=400,
                            detail="missing header params 'device'")
Exemplo n.º 4
0
async def register(user: schemas.CreateUser, db: Session = Depends(get_db)):
    if len(user.phone) == 0:
        return BaseErrResponse(code=400, message="手机号不能为空")
    else:
        if (crud.getUserByPhone(db, user.phone)):
            return BaseErrResponse(code=403, message="该手机号已注册!")
        else:
            constellation = get_constellation(user.birthday.month,
                                              user.birthday.day)
            db_user = models.User(phone=user.phone,
                                  nickname=user.nickname,
                                  birthday=user.birthday,
                                  gender=user.gender,
                                  avatar=user.avatar,
                                  provice=user.provice,
                                  city=user.city,
                                  area=user.area,
                                  address=user.address,
                                  latitude=user.latitude,
                                  longitude=user.longitude,
                                  whats_up=user.whats_up,
                                  constellation=constellation,
                                  tag=user.tag,
                                  profile_photo_urls=user.avatar)
            db.add(db_user)
            db.commit()
            db.refresh(db_user)
            return SucResponse()
Exemplo n.º 5
0
async def getUserPostList(userId: int,
                          page: int = 0,
                          size: int = 20,
                          db: Session = Depends(get_db),
                          sourceUserId: int = Depends(token_is_true)):
    postList = crud.getUserPostList(db, userId, page, size, sourceUserId)
    return SucResponse(data=postList)
Exemplo n.º 6
0
async def submitVisitRecord(userId: int,
                            db: Session = Depends(get_db),
                            sourceUserId: int = Depends(token_is_true)):
    visit_record = crud.getVisitRecordByBothUserId(db, sourceUserId, userId)
    # 存在访问记录
    if visit_record:
        visit_record.visit_times = visit_record.visit_times + 1
        db.commit()
        db.refresh(visit_record)
        return SucResponse()
    else:
        visit_record = models.VisitRecord(source_user_id=sourceUserId,
                                          destination_user_id=userId)
        db.add(visit_record)
        db.commit()
        db.refresh(visit_record)
        return SucResponse()
Exemplo n.º 7
0
async def dislikePost(postId: int,
                      db: Session = Depends(get_db),
                      sourceUserId: int = Depends(token_is_true)):
    like_record = crud.getPostLikeRecord(db, sourceUserId, postId)
    # 存在点赞记录
    if like_record:
        # 不是取消点赞状态的改为取消点赞
        if like_record.status != 0:
            like_record.status = 0
            db.commit()
            db.refresh(like_record)
            return SucResponse()
        else:
            return BaseErrResponse(code=403, message="未点赞该动态")
    else:
        # 数据库不存在记录的话 也不会新增type=1的记录 不存在即不喜欢
        return SucResponse()
Exemplo n.º 8
0
async def reportComment(commentId: int,
                        report: schemas.ReportComment,
                        db: Session = Depends(get_db),
                        sourceUserId: int = Depends(token_is_true)):
    db_report_comment = models.ReportComment(source_user_id=sourceUserId,
                                             comment_id=commentId,
                                             type=report.type)
    db.add(db_report_comment)
    db.commit()
    db.refresh(db_report_comment)
    return SucResponse()
Exemplo n.º 9
0
async def getNearbyPostList(page: int = 0,
                            size: int = 20,
                            db: Session = Depends(get_db),
                            userId: int = Depends(token_is_true)):
    currentUser = crud.getUserByUserId(db, userId)
    if currentUser.latitude == None or currentUser.longitude == None:
        raise HTTPException(status_code=403, detail="无法获取您的位置,请先打开定位")
    else:
        nearbyPostList = crud.getNearbyPostList(db, currentUser, page, size)
        random.shuffle(nearbyPostList)
        # 随机排序
        return SucResponse(data=nearbyPostList)
Exemplo n.º 10
0
async def likeUser(desUserId: int,
                   db: Session = Depends(get_db),
                   sourceUserId: int = Depends(token_is_true)):
    like_record = crud.getUserLikeRecord(db, sourceUserId, desUserId)
    # 存在喜欢或者不喜欢记录的话
    if like_record:
        # 不是喜欢状态的话变成喜欢状态
        if like_record.type != 0:
            like_record.type = 0
            db.commit()
            db.refresh(like_record)
            return SucResponse()
        else:
            return BaseErrResponse(code=403, message="已喜欢该用户")
    else:
        like_record = models.UserLike(source_user_id=sourceUserId,
                                      destination_user_id=desUserId,
                                      type=0)
        db.add(like_record)
        db.commit()
        db.refresh(like_record)
        return SucResponse()
Exemplo n.º 11
0
async def deleteComment(commentId: int,
                        db: Session = Depends(get_db),
                        userId: int = Depends(token_is_true)):
    db_comment = crud.getCommentDetail(db, commentId)
    if db_comment:
        if db_comment.user_id == userId:
            db.delete(db_comment)
            db.commit()
            return SucResponse()
        else:
            return BaseErrResponse(code=403, message="无权限删除该评论")
    else:
        return BaseErrResponse(code=404, message="该评论不存在")
Exemplo n.º 12
0
async def login(user: schemas.LoginRequest, db: Session = Depends(get_db)):
    db_user = crud.getUserByPhone(db, user.phone)
    if db_user:
        token = str(uuid.uuid1()).upper().replace('-', '')
        Redis.write(token, db_user.id)
        result = schemas.LoginResult(id=db_user.id, token=token)
        # 更新上次登录时间
        db_user.last_login_time = time.strftime('%Y-%m-%d %H:%M:%S',
                                                time.localtime(time.time()))
        db.commit()
        db.refresh(db_user)
        return SucResponse(data=result)
    else:
        raise HTTPException(status_code=404, detail="该用户不存在")
Exemplo n.º 13
0
async def deletePost(postId: int,
                     db: Session = Depends(get_db),
                     userId: int = Depends(token_is_true)):
    db_post = crud.getPostDetailById(db, postId)
    if db_post:
        if db_post.user_id == userId:
            db_post.status = 3
            db.commit()
            db.refresh(db_post)
            return SucResponse()
        else:
            return BaseErrResponse(code=403, message="没有权限删除该动态")
    else:
        return BaseErrResponse(code=404, message="该动态不存在")
Exemplo n.º 14
0
async def addComment(postId: int,
                     comment: schemas.CreateComment,
                     db: Session = Depends(get_db),
                     userId: int = Depends(token_is_true)):
    db_comment = models.Comment(post_id=postId,
                                user_id=userId,
                                content=comment.content,
                                type=comment.type,
                                to_user_id=comment.to_user_id)
    if comment.source_comment_id:
        db_comment.source_comment_id = comment.source_comment_id
    db.add(db_comment)
    db.commit()
    db.refresh(db_comment)
    return SucResponse()
Exemplo n.º 15
0
async def likePost(postId: int,
                   postUserId: int,
                   db: Session = Depends(get_db),
                   sourceUserId: int = Depends(token_is_true)):
    like_record = crud.getPostLikeRecord(db, sourceUserId, postId)
    # 存在记录
    if like_record:
        # 不是点赞的改为点赞状态
        if like_record.status != 1:
            like_record.status = 1
            db.commit()
            db.refresh(like_record)
            return SucResponse()
        else:
            return BaseErrResponse(code=403, message="已点赞该动态")
    else:
        like_record = models.PostLike(user_id=sourceUserId,
                                      post_user_id=postUserId,
                                      post_id=postId,
                                      status=1)
        db.add(like_record)
        db.commit()
        db.refresh(like_record)
        return SucResponse()
Exemplo n.º 16
0
async def reportUser(userId: int,
                     report: schemas.ReportUser,
                     db: Session = Depends(get_db),
                     sourceUserId: int = Depends(token_is_true)):
    db_report_user = models.ReportUser(source_user_id=sourceUserId,
                                       destination_user_id=userId,
                                       type=report.type)
    if report.content:
        db_report_user.content = report.content
    if report.evidence_photo_url:
        db_report_user.evidence_photo_url = report.evidence_photo_url
    db.add(db_report_user)
    db.commit()
    db.refresh(db_report_user)
    return SucResponse()
Exemplo n.º 17
0
async def createOrder(order: schemas.CreateOrder,
                      db: Session = Depends(get_db),
                      device: int = Header(None),
                      userId: int = Depends(token_is_true)):
    order_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) \
               + str(time.time()).replace('.', '')[-7:]

    db_order = models.Order(order_id=order_no,
                            product_id=order.product_id,
                            pay_method=order.pay_method,
                            source=device,
                            user_id=userId)
    db.add(db_order)
    db.commit()
    db.refresh(db_order)
    return SucResponse()
Exemplo n.º 18
0
async def dislikeUser(desUserId: int,
                      db: Session = Depends(get_db),
                      sourceUserId: int = Depends(token_is_true)):
    like_record = crud.getUserLikeRecord(db, sourceUserId, desUserId)
    # 存在喜欢或者不喜欢记录的话
    if like_record:
        # 不是不喜欢状态的话变成不喜欢状态
        if like_record.type != 1:
            like_record.type = 1
            db.commit()
            db.refresh(like_record)
            return SucResponse()
        else:
            return BaseErrResponse(code=403, message="未喜欢该用户")
    else:
        # 数据库不存在记录的话 也不会新增type=1的记录 不存在即不喜欢
        return BaseErrResponse(code=403, message="未喜欢该用户")
Exemplo n.º 19
0
async def update_user_info(userId: int,
                           user: schemas.UpdateUserInfo,
                           db: Session = Depends(get_db)):
    db_user = crud.getUserByUserId(db, userId)
    if db_user:
        db_user.birthday = user.birthday
        db_user.constellation = get_constellation(user.birthday.month,
                                                  user.birthday.day)
        db_user.avatar = user.avatar
        db_user.tag = user.tag
        db_user.whats_up = user.whats_up
        db_user.profile_photo_urls = user.profile_photo_urls

        db.commit()
        db.refresh(db_user)
        return SucResponse()
    else:
        raise HTTPException(status_code=404, detail="该用户不存在")
Exemplo n.º 20
0
async def update_user_location(userId: int,
                               user: schemas.UpdateUserLocation,
                               db: Session = Depends(get_db)):
    db_user = crud.getUserByUserId(db, userId)
    if db_user:
        db_user.longitude = user.longitude
        db_user.latitude = user.latitude
        db_user.provice = user.provice
        db_user.city = user.city
        db_user.area = user.area
        db_user.last_login_time = time.strftime('%Y-%m-%d %H:%M:%S',
                                                time.localtime(time.time()))
        db_user.address = user.address
        db.commit()
        db.refresh(db_user)
        return SucResponse()
    else:
        return BaseErrResponse(code=404, message="该用户不存在")
Exemplo n.º 21
0
async def postDynamic(post: schemas.CreatePost,
                      db: Session = Depends(get_db),
                      userId: int = Depends(token_is_true)):
    db_post = models.Post(user_id=userId,
                          content=post.content,
                          cover_url=post.cover_url,
                          type=post.type,
                          title=post.title,
                          media_urls=post.media_urls,
                          provice=post.provice,
                          city=post.city,
                          area=post.area,
                          address=post.address,
                          latitude=post.latitude,
                          longitude=post.longitude)
    db.add(db_post)
    db.commit()
    db.refresh(db_post)
    return SucResponse()
Exemplo n.º 22
0
async def verify_sms_code(zone: str = "86",
                          phone: str = Query(None,
                                             min_length=11,
                                             max_length=11),
                          code: str = Query(None, min_length=6, max_length=6)):
    verify_url = "https://webapi.sms.mob.com/sms/verify"
    data = {
        "appkey": config.MOB_APP_KEY,
        "phone": phone,
        "zone": zone,
        "code": code
    }
    req = requests.post(verify_url, data=data, verify=False)
    if req.status_code == 200:
        req_json = req.json()
        status = req_json.get("status", 500)
        msg = req_json.get("error", config.SERVER_ERROR_MESSAGE)
        if status == 200:
            return SucResponse()
        else:
            return BaseErrResponse(code=status, message=msg)

    return ServerErrorResponse()
Exemplo n.º 23
0
async def getLikeMessageList(db: Session = Depends(get_db),
                             userId: int = Depends(token_is_true),
                             page: int = 0,
                             size: int = 20):
    likeMessageList = crud.getLikeMessageList(db, userId, page, size)
    return SucResponse(data=likeMessageList)
Exemplo n.º 24
0
async def getVisitMeUserList(page: int = 0,
                             size: int = 20,
                             db: Session = Depends(get_db),
                             sourceUserId: int = Depends(token_is_true)):
    visitorList = crud.getVistorList(db, sourceUserId, page, size)
    return SucResponse(data=visitorList)
Exemplo n.º 25
0
async def getPostDetail(postId: int, db: Session = Depends(get_db)):
    db_post = crud.getPostDetailById(db, postId)
    if db_post is None or db_post.status == 2:
        raise HTTPException(status_code=403, detail="动态不存在")
    else:
        return SucResponse(data=db_post)
Exemplo n.º 26
0
async def getUser(userId: int, db: Session = Depends(get_db)):
    user = crud.getUserByUserId(db, userId=userId)
    if user:
        return SucResponse(data=user)
    else:
        raise HTTPException(status_code=404, detail="用户不存在")
Exemplo n.º 27
0
async def getPostCommentList(postId: int,
                             page: int = 0,
                             size: int = 20,
                             db: Session = Depends(get_db)):
    comment_list = crud.getPostCommentList(db, postId, page, size)
    return SucResponse(data=comment_list)