index.js 5.2 KB
Newer Older
L
123  
linju 已提交
1 2 3 4 5
'use strict';
const uniID = require('uni-id')
const uniCaptcha = require('uni-captcha')
const db = uniCloud.database()
const dbCmd = db.command
L
123  
linju 已提交
6 7 8 9 10 11 12 13 14 15 16
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ' + event)
	let params = event.params || {}
	
	//防止黑客恶意破解登陆,连续登陆失败一定次数后,需要用户提供验证码
	const getNeedCaptcha = async () => {
		//当用户最近“2小时内(recordDate)”登陆失败达到2次(recordSize)时。要求用户提交验证码
		const now = Date.now(),
			  recordDate = 120 * 60 * 1000,
			  recordSize = 2;
L
123  
linju 已提交
17 18 19 20 21 22 23 24 25 26 27
		const uniIdLogCollection = db.collection('uni-id-log')
		let recentRecord = await uniIdLogCollection.where({
				deviceId: params.deviceId || context.DEVICEID,
				create_date: dbCmd.gt(now - recordDate),
				type: 'login'
			})
			.orderBy('create_date', 'desc')
			.limit(recordSize)
			.get();
		return recentRecord.data.filter(item => item.state === 0).length === recordSize;
	}
L
123  
linju 已提交
28 29
	
	//设置某些模块不需要token(也就是登陆成功后)才能操作,如果需要token就获取当前操作账户的uid
L
123  
linju 已提交
30
	let noCheckAction = [
L
123  
linju 已提交
31 32 33 34 35 36
		'register', 'checkToken','login', 'logout', 'sendSmsCode',
		'createCaptcha', 'verifyCaptcha','refreshCaptcha', 'inviteLogin',
		'login_by_weixin','login_by_univerify','login_by_apple','loginBySms'
	]
	console.log(event.action);
	if (!noCheckAction.includes(event.action)) {
L
123  
linju 已提交
37 38 39 40 41
		if (!event.uniIdToken) {
			return {
				code: 403,
				msg: '缺少token'
			}
L
123  
linju 已提交
42 43
		}
		let payload = await uniID.checkToken(event.uniIdToken)
L
123  
linju 已提交
44 45 46 47
		if (payload.code && payload.code > 0) {
			return payload
		}
		params.uid = payload.uid
L
123  
linju 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	}
	
	
	//记录成功登陆的日志
	const loginLog = async (res = {}, type = 'login') => {
		const now = Date.now()
		const uniIdLogCollection = db.collection('uni-id-log')
		let logData = {
			deviceId: params.deviceId || context.DEVICEID,
			ip: params.ip || context.CLIENTIP,
			type,
			ua: context.CLIENTUA,
			create_date: now
		};
	
		Object.assign(logData,
			res.code === 0 ? {
				user_id: res.uid,
				state: 1
			} : {
				state: 0
			})
	
		return uniIdLogCollection.add(logData)
L
123  
linju 已提交
72
	}
L
123  
linju 已提交
73 74


L
123  
linju 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

	let res = {}
	switch (event.action) {
		case 'register':
			res = await uniID.register(params);
			break;
		case 'login':
			let passed = false;
			let needCaptcha = await getNeedCaptcha();
			
			if (needCaptcha) {
				res = await uniCaptcha.verify(params)
				if (res.code === 0) passed = true;
			}
			
			if (!needCaptcha || passed) {
L
123  
linju 已提交
91
				res = await uniID.login({...params,"queryField":['username','email','mobile']});
L
123  
linju 已提交
92 93 94 95 96
				await loginLog(res);
				needCaptcha = await getNeedCaptcha();
			}
			
			res.needCaptcha = needCaptcha;
L
123  
linju 已提交
97 98 99 100 101 102 103 104 105 106 107
			break;
		case 'login_by_weixin':
			res = await uniID.loginByWeixin(params);
			loginLog(res)
			break;
		case 'login_by_univerify':
			res = await uniID.loginByUniverify(params)
			break;
		case 'login_by_apple':
			res = await uniID.loginByApple(params)
			loginLog(res)
L
123  
linju 已提交
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
			break;
		case 'checkToken':
			res = await uniID.checkToken(event.uniIdToken);
			break;
		case 'logout':
			res = await uniID.logout(event.uniIdToken)
			break;
		case 'sendSmsCode':
			// 简单限制一下客户端调用频率
			const ipLimit = await db.collection('uni-verify').where({
				ip: context.CLIENTIP,
				created_at: dbCmd.gt(Date.now() - 60000)
			}).get()
			if (ipLimit.data.length > 0) {
				return {
					code: 429,
					msg: '请求过于频繁'
				}
			}
			const templateId = '' // 替换为自己申请的模板id
			if (!templateId) {
				return {
					code: 500,
					msg: 'sendSmsCode需要传入自己的templateId,参考https://uniapp.dcloud.net.cn/uniCloud/uni-id?id=sendsmscode'
				}
			}
			const randomStr = '00000' + Math.floor(Math.random() * 1000000)
			const code = randomStr.substring(randomStr.length - 6)
			res = await uniID.sendSmsCode({
				mobile: params.mobile,
				code,
				type: params.type,
				templateId
			})
			break;
		case 'loginBySms':
			if (!params.code) {
				return {
					code: 500,
					msg: '请填写验证码'
				}
			}
			if (!/^1\d{10}$/.test(params.mobile)) {
				return {
					code: 500,
					msg: '手机号码填写错误'
				}
			}
			res = await uniID.loginBySms(params)
			loginLog(res)
			break;
		case 'inviteLogin':
			if (!params.code) {
				return {
					code: 500,
					msg: '请填写验证码'
				}
			}
			res = await uniID.loginBySms({
				...params,
				type: 'register'
			})
			break;
		case 'getInviteCode':
			res = await uniID.getUserInfo({
				uid: params.uid,
				field: ['my_invite_code']
			})
			if (res.code === 0) {
				res.myInviteCode = res.userInfo.my_invite_code
				delete res.userInfo
			}
			break;
		case 'getInvitedUser':
			res = await uniID.getInvitedUser(params)
			break;
		case 'updatePwd':
			res = await uniID.updatePwd({
				uid: params.uid,
				...params
			})
			break;
		case 'createCaptcha':
			res = await uniCaptcha.create(params)
			break;
		case 'refreshCaptcha':
			res = await uniCaptcha.refresh(params)
			break;
		default:
			res = {
				code: 403,
				msg: '非法访问'
			}
			break;
	}

	//返回数据给客户端
	return res
芊里 已提交
206
};