Issues (124)

app/controller/session.js (4 issues)

1
const Response = require('../util/response')
2
const Validate = require('../util/validate')
3
const ModelUser = require('../model/users')
4
const WechatMina = require('../../config/wechat_mina')
5
const Request = require('request')
6
const Constant = require('../libraries/constant')
7
const Redis = require('../libraries/redis')
8
const Crypto = require('crypto')
9
const ApiError = require('../util/api_error')
10
11
module.exports = {
12
13
	login: async function (ctx) {
14
15
		Validate(ctx, {
16
			'code': 'required',
17
		})
18
19
		let appId = WechatMina.app_id
20
		let appSecret = WechatMina.app_secret
21
		code = ctx.input.code
0 ignored issues
show
The variable code seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.code.
Loading history...
22
23
		let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + appSecret + '&js_code=' + code + '&grant_type=authorization_code'
24
25
		let result = await new Promise((resolve, reject) => {
26
			Request(url, function (err, response, body) {
27
				if (err) {
28
					reject(err)
29
				}
30
				resolve(JSON.parse(body.toString()))
31
			})
32
		})
33
34
		// result.openid = 'test' + new Date().getTime() // debug
35
36
		if (!!result.openid) {
37
			let userInfo = await ModelUser.getUserByOpenId(result.openid)
38
			let uid
39
40
			if (!!userInfo === false) {
41
				let insertData = {
42
					'openid': result.openid
43
				}
44
				insertRes = await ModelUser.addUser(insertData)
0 ignored issues
show
The variable insertRes seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.insertRes.
Loading history...
45
				uid = insertRes.insertId
46
			} else uid = userInfo.id
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
47
48
			let salt = 'OPENID:' + result.openid + ':CARD-LOGIN:' + new Date().getTime()
49
			let token = Crypto.createHash('md5').update(salt).digest('hex').toUpperCase()
50
51
			let ckey = Constant.WECHAT_SESSION + token
52
			let cacheData = {
53
				'open_id': result.openid,
54
				'uid': uid,
55
			}
56
			Redis.set(ckey, JSON.stringify(cacheData))
57
			Redis.expire(ckey, 86400 * 30)
58
59
			let data = {
60
				'token': token,
61
				'uid': uid
62
			}
63
64
			return Response.output(ctx, data)
65
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
66
			throw new ApiError('auth.error', 'no permission')
67
		}
68
69
70
	},
71
72
73
}