palapalapla/GT-Manual
0
1import lodash from 'lodash'2import md5 from 'md5'3 4let _connection = {}5 6export class MysSign extends plugin {7 constructor () {8 super({9 name: 'mysSign',10 dsc: 'mys签到',11 route: '/mysSign',12 rule: [13 {14 method: 'get',15 path: '/:key',16 fnc: 'index'17 },18 {19 method: 'get',20 path: '/:key/:uid',21 fnc: 'bbsSign'22 },23 {24 method: 'post',25 path: '/:key/:uid',26 fnc: 'bbsSign'27 },28 {29 method: 'ws',30 path: '/',31 fnc: 'connection'32 }33 ]34 })35 }36 37 index () {38 let { key } = this.params39 let user = this.findUser(key)40 if (!user) {41 this.error('UID列表不存在或已失效')42 return43 }44 this.render('mysSign/main', { key, user, copyright: this.cfg.Copyright })45 }46 47 async bbsSign () {48 let { connect, id, key, uid, validate } = this.params49 if (!validate && this.params['validate[geetest_validate]']) {50 validate = {51 geetest_challenge: this.params['validate[geetest_challenge]'],52 geetest_validate: this.params['validate[geetest_validate]'],53 geetest_seccode: this.params['validate[geetest_seccode]'],54 }55 }56 connect = _connection[connect]57 let user = connect[key]58 let uidData = user?.uids.find(v => v.uid == uid)59 if (!uidData) {60 this.send({ msg: '签到失败:链接已失效,请重新获取' })61 return62 }63 let data = uidData.status64 if (!data) {65 data = await connect.self.socketSend('doSign', { id, validate, ...uidData }, md5(`${new Date().getTime()}${uid}`))66 if (data.retcode == 0) {67 uidData.status = { ...data, msg: data.msg.replace('签到成功', '今天已签到') }68 logger.info(`[mysSign] 签到成功, UID: ${uid}`)69 }70 }71 this.send(data)72 }73 74 connection () {75 if (this.cfg.Key && this.request.query.key !== this.cfg.Key) return this.close()76 this._wait = this._wait || {}77 this._key = this.randomKey(10, _connection)78 this.onClose(() => delete _connection[this._key])79 this.onMessage((data) => {80 try {81 data = JSON.parse(data)82 let { id, cmd, payload, key } = data83 this._wait[id] && this._wait[id](payload) || this[cmd] && this[cmd](payload, id)84 } catch (err) {85 logger.error(err)86 }87 })88 }89 90 async createUser (data, id) {91 let connect = _connection[this._key]92 if (!connect.self) {93 connect.self = this94 }95 let key = this.hasUser(id)96 if (!key) {97 key = this.randomKey(6, connect, { connect: this._key, id, uids: data })98 /** uid缓存10分钟 */99 setTimeout(() => connect && delete connect[key], 600 * 1000)100 }101 let payload = { link: `${this.address}/${key}` }102 this.socketWrite('createUser', payload, id)103 }104 105 hasUser (id) {106 let ret = false107 lodash.forEach(_connection, (connect) => {108 lodash.forEach(connect, (user, key) => {109 if (user.id == id) {110 ret = key111 return false112 }113 })114 })115 return ret116 }117 118 findUser (key) {119 let ret = false120 let connect = this.findConnect(key)121 if (connect) ret = connect[key]122 return ret123 }124 125 findConnect (key) {126 let ret = false127 if (key) {128 lodash.forEach(_connection, (connect) => {129 if (connect[key]) {130 ret = connect131 return false132 }133 })134 }135 return ret136 }137 138 socketWrite (cmd, payload, id) {139 if (!id || typeof id == 'number' && String(id).length == 13) id = new Date().getTime()140 return this.socket.send(JSON.stringify({ id, cmd, payload, key: this._key }))141 }142 143 socketSend (cmd, payload, id) {144 return new Promise((resolve, reject) => {145 this.socketWrite(cmd, payload, `${id}`)146 this._wait[id] = resolve147 setTimeout(() => resolve(false) && delete this._wait[id], 5 * 1000)148 })149 }150 151 get cfg () {152 return Server.cfg.getConfig('GTest')153 }154 155 get address () {156 let { Host } = this.cfg157 let protocol = Server.cfg.http.HTTPS ? 'https' : 'http'158 let port = Server.cfg.http_listen[0]159 if (![80, 443].includes(port)) Host += `:${port}`160 return `${protocol}://${Host}${this.route}`161 }162 163 randomKey (length, checkObj, data) {164 let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'165 let key = lodash.sampleSize(letters, length).join('')166 while (checkObj[key]) {167 key = lodash.sampleSize(letters, length).join('')168 }169 checkObj[key] = data || {}170 return key171 }172}