Issues (124)

app/controller/catering/customer.js (12 issues)

1
const Response = require('../../util/response')
2
const Validate = require('request-validate')
3
const ModelCustomer = require('../../model/catering/customer')
4
const ModelSeller = require('../../model/catering/seller')
5
const ApiError = require('../../util/api_error')
6
const WeChatSDK = require('../../util/wechat/wechat_sdk')
7
const _ = require('underscore')
8
const OssSdk = require('../../libraries/ossSdk')
9
const ConfigOss = require('../../../config/oss')
10
const Uuid = require('../../libraries/uuid')
11
const fs = require('fs')
12
13
module.exports = {
14
15
	detail: async function (ctx) {
16
17
		let data = await ModelCustomer.first(ctx.uid)
18
		return Response.output(ctx, data)
19
	},
20
21
	/**
22
     * @api {put} /api/catering/v1/customer/:customerId 编辑用户信息
23
	 * @apiVersion 1.0.0
24
	 * @apiName 编辑用户信息
25
     * @apiGroup customer
26
     * @apiPermission user
27
	 * 
28
	 * @apiDescription API to edit the customer information.
29
	 * 
30
	 * @apiExample Example usage:
31
    	curl --request PUT \
32
			--url https://garylv.com/api/catering/v1/customers/1 \
33
			--header 'mina-source: catering' \
34
			--header 'store-id: 0'
35
	 * 
36
     * @apiParam {String} [nickName] nickName
37
     * @apiParam {String} [gender] gender
38
     * @apiParam {String} [avatarUrl] avatarUrl
39
     * @apiParam {String} [country] country
40
     * @apiParam {String} [province] province
41
     * @apiParam {String} [city] city
42
     * @apiParam {String} [mobile] mobile
43
     * @apiParam {String} [language] language
44
	 * 
45
	 * @apiSuccess {String} data response data
46
	 * 
47
     * @apiSuccessExample {json} Visual Preview:
48
		{
49
			"code": 200,
50
			"message": "请求成功",
51
			"data": {}
52
		}
53
	 * @apiSampleRequest https://garylv.com/api/catering/v1/customers/:customerId
54
     */
55
	edit: async function (ctx) {
56
		let updateData = {}
57
		if (!!ctx.input.nickName) updateData.nickname = ctx.input.nickName
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...
58
		if (!!ctx.input.gender) updateData.gender = ctx.input.gender
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...
59
		if (!!ctx.input.avatarUrl) updateData.avatar = ctx.input.avatarUrl
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...
60
		if (!!ctx.input.country) updateData.country = ctx.input.country
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...
61
		if (!!ctx.input.province) updateData.province = ctx.input.province
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...
62
		if (!!ctx.input.city) updateData.city = ctx.input.city
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...
63
		if (!!ctx.input.mobile) updateData.mobile = ctx.input.mobile
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...
64
		if (!!ctx.input.language) updateData.language = ctx.input.language
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...
65
66
		let where = {
67
			'id': ctx.uid,
68
		}
69
70
		if (_.isEmpty(updateData)) {
71
			throw new ApiError('common.paramsEmpty', '更新内容')
72
		} else {
73
			let data = await ModelCustomer.edit(updateData, where)
0 ignored issues
show
The variable data seems to be never used. Consider removing it.
Loading history...
74
			return Response.output(ctx, {})
75
		}
76
77
	},
78
79
	/**
80
     * @api {post} /api/catering/v1/customer/:customerId/seller 申请成为商家
81
	 * @apiVersion 1.0.0
82
     * @apiGroup customer
83
     * @apiPermission user
84
	 * 
85
	 * @apiDescription 普通用户申请开店
86
	 * 
87
     * @apiParam {String} company 店铺名称
88
     * @apiParam {String} avatar 店铺头像
89
     * @apiParam {String} tel 店铺联系电话
90
     * @apiParam {String} address 店铺地址
91
     * @apiParam {String} license 店铺营业执照路径
92
	 * 
93
	 * @apiSuccess {String} data response data
94
	 * 
95
     * @apiSuccessExample {json} Visual Preview:
96
		{
97
			"code": 200,
98
			"message": "请求成功",
99
			"data": {}
100
		}
101
	 * @apiSampleRequest https://garylv.com/api/catering/v1/customers/:customerId/sellers
102
     */
103
	applySeller: async function (ctx) {
104
		Validate(ctx.input, {
105
			company: 'required',
106
			avatar: 'required',
107
			tel: 'required',
108
			address: 'required',
109
			license: 'required',
110
		})
111
112
		let check = await ModelSeller.first(ctx.uid)
113
		if (check) {
114
			let updateData = {
115
				company: ctx.input.company,
116
				avatar: ctx.input.avatar,
117
				tel: ctx.input.tel,
118
				address: ctx.input.address,
119
				license: ctx.input.license,
120
			}
121
			await ModelSeller.edit(updateData, { seller_id: ctx.uid })
122
			updateData.seller_id = ctx.uid
123
			return Response.output(ctx, updateData)
124
		}
125
126
		let insertData = {
127
			seller_id: ctx.uid,
128
			company: ctx.input.company,
129
			avatar: ctx.input.avatar,
130
			tel: ctx.input.tel,
131
			address: ctx.input.address,
132
			license: ctx.input.license,
133
		}
134
135
		let data = await ModelSeller.add(insertData)
0 ignored issues
show
The variable data seems to be never used. Consider removing it.
Loading history...
136
		return Response.output(ctx, insertData)
137
138
	},
139
140
	/**
141
     * @api {post} /api/catering/v1/customer/:customerId/mina_qrcodes 小程序码
142
	 * @apiVersion 1.0.0
143
     * @apiGroup customer
144
     * @apiPermission user
145
	 * 
146
	 * @apiDescription 小程序码
147
	 * 
148
     * @apiParam {String} action 行为['apply_seller']
149
     * @apiParam {String} action_id 行为ID
150
	 * 
151
	 * @apiSuccess {String} data response data
152
	 * 
153
     * @apiSuccessExample {json} Visual Preview:
154
		{
155
			"code": 200,
156
			"message": "请求成功",
157
			"data": {}
158
		}
159
	 * @apiSampleRequest https://garylv.com/api/catering/v1/customers/:customerId/mina_qrcodes
160
     */
161
	minaQRcode: async function (ctx) {
162
		Validate(ctx.input, {
163
			action: 'required|in:apply_seller',
164
			action_id: 'required|numeric',
165
		})
166
167
		let config = ctx.header['mina-source']
168
		let wechatSdk = new WeChatSDK(config)
169
170
		let sence = 'seller_id=' + ctx.input.action_id
171
		let page = 'pages/admin/seller/audit'
172
		let imgResult = await wechatSdk.minaTmpQRCode(sence, page)
173
174
		let ossSdk = new OssSdk(ConfigOss.catering)
175
		let object = 'customer/' + ctx.uid + '/qrcode/' + imgResult.img_name
176
		await ossSdk.uploadFile(object, imgResult.img_file)
177
178
		fs.unlink(imgResult.img_file, function (err) {
179
			if (err) {
180
				console.log(err)
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
181
			}
182
			console.log('文件:' + imgResult.img_file + '删除成功!')
183
		})
184
185
		let result = {
186
			'object': object,
187
			'url': ConfigOss.catering.view_server + object
188
		}
189
190
		return Response.output(ctx, result)
191
192
	},
193
194
	oss: async function (ctx) {
195
		console.log('inputttttttttttttt')
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
196
		console.log(ctx.input)
197
		console.log(ctx.input.files.content)
198
		console.log(ctx.input.files.content.path)
199
200
		let files = ctx.input.files.content
201
		let postData = ctx.input.fields
202
203
		Validate(postData, {
204
			folder: 'required'
205
		})
206
207
		let ossSdk = new OssSdk(ConfigOss.catering)
208
		let fileType = files.type.split('/').pop()
209
		let object = 'customer/' + ctx.uid + '/' + postData.folder + '/' + await Uuid.genOrderNo() + '.' + fileType
210
		await ossSdk.uploadFile(object, files.path)
211
212
		let resp = {
213
			'object': object,
214
			'url': ConfigOss.catering.view_server + object + '?x-oss-process=style/preview'
215
		}
216
217
		return Response.output(ctx, resp)
218
	}
219
220
}