Skip to content

Commit

Permalink
fix: 修复一些已知问题
Browse files Browse the repository at this point in the history
  • Loading branch information
afzw committed Dec 8, 2023
1 parent 99030a0 commit a4d9ca3
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion public/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1(dev)
v0.0.1(dev)
6 changes: 3 additions & 3 deletions src/_config/config.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const config: App.Config = {
name: 'ExpressDemo',
host: '127.0.0.1',
port: 27017,
debug: false
// username: '',
// password: '',
debug: false,
username: 'root',
password: 'root'
// uri: '',
},
redis: {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/item/item.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import callAsync from '@/lib/callAsync'
import { NextFunction, Request, Response } from 'express'
import ItemService from '@/business/item/item.service'
import { ItemDoc, ItemFilter, ItemUpdate } from '@/entities/item.model'
import { Paging } from '@/dao/utils'
import { getPagingOptions } from '@/dao/utils'
import fileDao from '@/dao/file.dao'
import { FileDoc, FileFilter, FileProps } from '@/entities/file.model'
import itemDao from '@/dao/item.dao'
Expand All @@ -26,7 +26,7 @@ class ItemController {
/** 查询items */
public static async search(req: Request, res: Response, next: NextFunction) {
const { name, price, ownerId } = req.query
const pagingOptions = new Paging(req.query)
const pagingOptions = getPagingOptions(req.query)

const filter: ItemFilter = {}
if (name) filter.name = name
Expand Down
6 changes: 3 additions & 3 deletions src/business/item/item.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import callAsync from '@/lib/callAsync'
import { ItemDoc, ItemFilter } from '@/entities/item.model'
import { ItemDoc, ItemFilter, ItemProps } from '@/entities/item.model'
import ItemDao from '@/dao/item.dao'
import _ from 'lodash'
import ItemStore from './item.store'
Expand All @@ -13,7 +13,7 @@ class ItemService {
* @param createInfo 创建item的属性信息
* @return 新建的item
*/
public static async createItem(createInfo: Pojo): Promise<ItemDoc> {
public static async createItem(createInfo: Partial<ItemProps>): Promise<ItemDoc> {
const createProps = _.pick(createInfo, ItemStore.theCreateKeys())

const [errCreate, newItem] = await callAsync(ItemDao.create(createProps))
Expand Down Expand Up @@ -51,7 +51,7 @@ class ItemService {
* @param updateInfo 更新属性信息
* @return 更新后的item
*/
public static async updateItem(itemId: string, updateInfo: Pojo): Promise<ItemDoc> {
public static async updateItem(itemId: string, updateInfo: Partial<ItemProps>): Promise<ItemDoc> {
const filter: ItemFilter = { _id: itemId }
const updateProps = _.pick(updateInfo, ItemStore.theCreateKeys())
const queryOptions = { new: true }
Expand Down
4 changes: 2 additions & 2 deletions src/dao/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Paging from './paging'
import { getPagingOptions } from './paging'
import MongooseBase from './base'

export { MongooseBase, Paging }
export { MongooseBase, getPagingOptions }
42 changes: 27 additions & 15 deletions src/dao/utils/paging.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
/** 分页器 */
class Paging {
/** 分页参数 */
interface PagingParams {
/** 每页数据量 */
public limit = 10
limit?: number
/** 跳过的数据量 */
public skip = 0
/** 排序依据 */
public orderField: string | null = null
skip?: number
/** 排序依据字段 */
orderField?: string
/** 是否倒序 */
public descend = false
descend?: boolean
}

const defaultPagingParams: PagingParams = {
limit: 10,
skip: 0
}

/**
* 获取分页选项
*/
function getPagingOptions(query: Partial<PagingParams>) {
const { limit, skip, orderField, descend } = query

const options = Object.assign({}, defaultPagingParams)

constructor(pojo: Pojo) {
const { limit, skip, orderField, descend } = pojo
if (typeof limit === 'number' || typeof Number(limit) === 'number') options.limit = Number(limit)
if (typeof skip === 'number' || typeof Number(skip) === 'number') options.skip = Number(skip)
if (orderField && typeof orderField === 'string') options.orderField = orderField
if (typeof descend === 'boolean') options.descend = descend

if (typeof limit === 'number' || typeof Number(limit) === 'number') this.limit = Number(limit)
if (typeof skip === 'number' || typeof Number(skip) === 'number') this.skip = Number(skip)
if (orderField && typeof orderField === 'string') this.orderField = orderField
if (typeof descend === 'boolean') this.descend = descend
}
return options
}

export default Paging
export { getPagingOptions }
4 changes: 2 additions & 2 deletions src/lib/encryption/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import crypto from 'crypto'
* SH512加密
*/
export function encryptStringUsingSH512(str: string) {
return crypto.createHash('sh512').update(str).digest('hex')
return crypto.createHash('sha512').update(str).digest('hex')
}

/**
* 生成随机的32位16进制字符串(常用作salt值)。
*/
export function genRandom32BitsHexString() {
return crypto.randomBytes(length).toString('hex')
return crypto.randomBytes(16).toString('hex')
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fs/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import callAsync from '@/lib/callAsync'
import { moveFile, createDirRecursively, deleteFile, readFile, appendFile } from '@/lib/fs/base'
import { getFileNameByPath } from '@/lib/fs/utils'
import AppError from '../error'
import { AppError } from '../error'

/** 保存文件的选项 */
interface SaveOptions {
Expand Down
3 changes: 2 additions & 1 deletion src/loaders/router/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function routeValidator(req: Request, res: Response, next: NextFunction)
if (!route) return res.status(500).send({ error: 'route not found: ' + req.route.path })

// 校验路由权限
if (route.permission !== 'public') if (req.user?.status !== 1) return res.status(403).send({ error: '账号非法!' })
if (route.permission === 'public') return next()
if (req.user?.status !== 1) return res.status(403).send({ error: '账号非法!' })

let pass = false
if (route.permission instanceof Array) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ declare global {
method: 'GET' | 'POST' | 'PUT' | 'DELETE'
middlewares?: unknown[]
permission?: string
threshold?: unknown
threshold?: any
csrf?: boolean
}
/** 程序错误 */
Expand Down

0 comments on commit a4d9ca3

Please sign in to comment.