店铺增加短id

This commit is contained in:
zhl 2021-05-18 16:27:32 +08:00
parent c23efc4de6
commit 5d3b630720
4 changed files with 40 additions and 10 deletions

View File

@ -37,6 +37,7 @@
"moment": "^2.29.1",
"mongoose": "5.10.3",
"mongoose-findorcreate": "^3.0.0",
"nanoid": "^3.1.23",
"qrcode": "^1.4.4",
"querystring": "^0.2.1",
"redis": "^3.1.2",

View File

@ -28,21 +28,27 @@ import {
updateSingleRank
} from '../../services/GameLogic'
import { isObjectId } from '../../utils/string.util'
import { customAlphabet } from 'nanoid'
class PuzzleController extends BaseController {
@role('anon')
@router('get /api/test')
async test(req) {
// let records = await Puzzle.find({})
// for (let record of records) {
// record.length = record.question.trueLength()
// await record.save()
// }
// let record = await Puzzle.findById('6080f20c9b76a1e376878692')
// let len = record.question.trueLength()
//
// return {len, length: record.question.length}
try{
console.time('ss')
const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 10)
for (let i = 0; i < 100; i++) {
console.log(nanoid())
}
console.timeEnd('ss')
} catch (err) {
}
return {}
}

View File

@ -1,12 +1,15 @@
import {
getModelForClass,
index,
modelOptions,
modelOptions, pre,
prop
} from '@typegoose/typegoose'
import { dbconn } from '../../decorators/dbconn'
import { noJson } from '../../decorators/nojson'
import { BaseModule } from '../Base'
import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 10)
class GameInfo {
@prop()
@ -18,8 +21,23 @@ class GameInfo {
@dbconn()
@index({ location: '2dsphere' })
@index({sid: 1}, {unique: true})
@modelOptions({ schemaOptions: { collection: 'shop', timestamps: true } })
@pre<ShopClass>('save', async function() {
if (!this.sid) {
let sid = ''
while (!sid) {
sid = nanoid()
const records = await Shop.find({sid}, {_id: 1}).limit(1);
(records.length > 0) && (sid = '')
}
this.sid = sid
}
})
class ShopClass extends BaseModule {
@prop()
public sid: string
@prop({ required: true })
public name!: string
/**

View File

@ -1639,6 +1639,11 @@ mz@^2.7.0:
object-assign "^4.0.1"
thenify-all "^1.0.0"
nanoid@^3.1.23:
version "3.1.23"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.23.tgz#f744086ce7c2bc47ee0a8472574d5c78e4183a81"
integrity sha512-FiB0kzdP0FFVGDKlRLEQ1BgDzU87dy5NnzjeW9YZNt+/c3+q82EQDUwniSAUxp/F0gFNI1ZhKU1FqYsMuqZVnw==
netmask@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/netmask/-/netmask-2.0.2.tgz#8b01a07644065d536383835823bc52004ebac5e7"