Issues (124)

app/services/catering/session.js (3 issues)

1
const ModelCustomer = require('../../model/catering/customer')
2
const WeChatSDK = require('../../util/wechat/wechat_sdk')
3
const Redis = require('../../libraries/redis')
4
const Constant = require('../../libraries/constant')
5
const Crypto = require('crypto')
6
7
module.exports = {
8
    wxLogin: async function (code, config) {
9
        let wechatSdk = new WeChatSDK(config)
10
        let result = await wechatSdk.minaLogin(code)
11
        let userInfo = await ModelCustomer.getCustomerByOpenId(result.openid)
12
13
        let uid
14
        let defaultStore = 0
15
        if (!userInfo) {
16
            let insertData = {
17
                'openid': result.openid
18
            }
19
            insertRes = await ModelCustomer.add(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...
20
            uid = insertRes.insertId
21
        } else {
22
            uid = userInfo.id
23
            defaultStore = userInfo.default_store
24
        }
25
26
        let salt = 'OPENID:' + result.openid + ':CATERING-LOGIN:' + new Date().getTime()
27
        let token = Crypto.createHash('md5').update(salt).digest('hex').toUpperCase()
28
29
        let ckey = Constant.CATERING_SESSION + token
30
        let cacheData = {
31
            'open_id': result.openid,
32
            'uid': uid,
33
        }
34
        Redis.set(ckey, JSON.stringify(cacheData))
35
        Redis.expire(ckey, 86400 * 30)
36
37
        let data = {
38
            'token': token,
39
            'uid': uid,
40
            'default_store': defaultStore
41
        }
42
43
        return data
44
    },
45
46
47
    getCustomerInfo: async function (customerId) {
48
        let cacheKey = Constant.CUSTOMER_INFO + customerId
49
        let customerInfo = await Redis.get(cacheKey)
50
        if (!customerInfo) {
51
            customerInfo = await ModelCustomer.detail({ id: customerId })
52
            if (!customerInfo) return false
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...
53
            if (customerInfo) await Redis.set(cacheKey, JSON.stringify(customerInfo))
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...
54
        } else {
55
            customerInfo = JSON.parse(customerInfo)
56
        }
57
58
        // 每次拿都刷新时间
59
        await Redis.expire(cacheKey, Constant.EXPIRE_REFRESH)
60
61
        return customerInfo
62
    },
63
64
}