Spaces:
Sleeping
Sleeping
File size: 3,813 Bytes
ba2906a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import lodash from 'lodash'
import { GTest } from './GTest.js'
export class MysSign extends plugin {
constructor () {
super({
name: 'mysSign',
dsc: 'mys签到',
route: '/mysSign',
rule: [
{
method: 'get',
path: '/:key',
fnc: 'index'
},
{
method: ['get', 'post'],
path: '/:key/:uid',
fnc: 'bbsSign'
},
{
method: 'ws',
path: '/',
fnc: 'connection'
}
]
})
}
index () {
let copyright = GTest.cfg.Copyright
let { key } = this.params
let user = this.getUser(key)
if (!user) {
this.error('UID列表不存在或已失效')
return
}
this.render('mysSign', { key, user, copyright })
}
async bbsSign () {
let { connect, id, key, uid, validate } = this.params
if (!validate && this.params['validate[geetest_validate]']) {
validate = {
geetest_challenge: this.params['validate[geetest_challenge]'],
geetest_validate: this.params['validate[geetest_validate]'],
geetest_seccode: this.params['validate[geetest_seccode]']
}
}
connect = GTest.clients[connect]
let user = connect?.[key]
let uidData = user?.uids.find(v => v.uid == uid)
if (!uidData) {
this.send({ msg: '签到失败:链接已失效,请重新获取' })
return
}
let data = uidData.status || await connect.self.socketSend('doSign', { id, validate, ...uidData }, `${new Date().getTime()}${uid}`)
if (!data) {
data = { msg: '签到失败:请求超时' }
} else if (data.retcode == 0) {
uidData.status = { ...data, msg: data.msg.replace('签到成功', '今天已签到') }
logger.info(`[mysSign] 签到成功, UID: ${uid}`)
}
this.send(data)
}
connection () {
if (GTest.cfg.REGISTER_KEY && this.request.query.key !== GTest.cfg.REGISTER_KEY) return this.close()
this._wait = this._wait || {}
this._key = GTest.randomKey(10, GTest.clients)
this.onClose(() => delete GTest.clients[this._key])
this.onMessage((data) => {
try {
data = JSON.parse(data)
let { id, cmd, payload } = data
this._wait[id] ? this._wait[id](payload) : this[cmd] && this[cmd](payload, id)
} catch (err) {
logger.error(err)
}
})
}
async createUser (data, id) {
let connect = GTest.clients[this._key]
if (!connect.self) {
connect.self = this
}
let key = this.hasUser(id)
if (!key) {
key = GTest.randomKey(6, connect, { connect: this._key, id, uids: data })
/** uid缓存10分钟 */
setTimeout(() => connect && delete connect[key], 600 * 1000)
}
let payload = { link: `${Server.cfg.http.PUBLIC_ADDRESS}${this.route}/${key}` }
this.socketWrite('createUser', payload, id)
}
hasUser (id) {
let ret = false
lodash.forEach(GTest.clients, (connect) => {
lodash.forEach(connect, (user, key) => {
if (user.id == id) {
ret = key
return false
}
})
})
return ret
}
getUser (key) {
let ret = false
if (key) {
lodash.forEach(GTest.clients, (connect) => {
if (connect[key]) {
ret = connect[key]
return false
}
})
}
return ret
}
socketWrite (cmd, payload, id) {
if (!id || (typeof id == 'number' && String(id).length == 13)) id = new Date().getTime()
return this.socket.send(JSON.stringify({ id, cmd, payload, key: this._key }))
}
socketSend (cmd, payload, id) {
return new Promise((resolve, reject) => {
this.socketWrite(cmd, payload, `${id}`)
this._wait[id] = resolve
setTimeout(() => resolve(false) && delete this._wait[id], 5 * 1000)
})
}
}
|