-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.js
237 lines (223 loc) · 5.3 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* 插入测试数据
*/
var model = require('./model');
var rp = require('request-promise');
var fs = require('fs');
var util = require('util');
var User = require('./model').User;
var host = 'http://ketang.zhufengpeixun.cn';
//清空数据库
function resetDb() {
return model.Course.remove({})
.then(() => {
return model.ImgCode.remove();
}).then(() => {
return model.Order.remove();
}).then(() => {
return model.Token.remove();
}).then(() => {
return model.User.remove();
}).then(() => {
return model.VCode.remove();
});
}
function getToken() {
return rp({
uri: host+'/token',
json: true
}).then(function (body) {
return body.token;
}).catch(function (err) {
console.log(err);
});
}
//getToken();
//获取验证码图片
async function getImgCode(token){
return rp({
uri: host+'/imgcode',
headers: {
token
},
encoding:null,
json: true
}).then(function (body) {
//fs.writeFile('./img.png',body); //9479
console.log(body);
return body;
}).catch(function (err) {
console.log(err);
});
}
//获取验证码图片
//getImgCode();
//获取短信验证码
async function getVCode(token){
return rp({
uri: host+'/user/vcode?mobile=15718856132&type=register&img_code=9479',
headers: {
token
},
json: true
}).then(function (body) {
return body;
}).catch(function (err) {
console.log(err);
});
}
//getVCode();
//注册用户 user
async function createUser(token){
return rp({
uri: host+'/user',
method: 'POST',
body: {
vcode: "1234",
name:"珠峰课堂",
password:"123456",
mobile:"15718856132"
},
headers: {
token
},
json: true
}).then(function (body) {
return body;
}).catch(function (err) {
console.log(err);
});
}
//createUser();
//用户登录
async function identity(token){
return rp({
uri: host+'/user/identity?mobile=15718856132&password=123456',
headers: {
token
},
json: true
}).then(function (body) {
return body;
}).catch(function (err) {
console.log(err);
});
}
//identity();
//发布课程
async function createCourse(token){
return rp({
uri: host+'/course',
method: 'POST',
body: {
title:'node.js实战',
author:'老师',
description:'从零开始,循序渐进',
price: 3000.00,
start:"2016-10-30",
address: '珠峰',
image: 'http://7xjf2l.com1.z0.glb.clouddn.com/reactnative.png',
auth_profile:'教师简介',
hours: 24,
contents: [
"1.课程概览",
"2.开发环境搭建",
"3.node基础",
"4.koa2"
],
},
headers: {
token
},
json: true
}).then(function (body) {
return body;
}).catch(function (err) {
console.log(err);
});
}
//createCourse();
//查看课程
async function getCourse(token){
return rp({
uri: host+'/course',
headers: {
token
},
json: true
}).then(function (body) {
console.log(util.inspect(body,{depth:3}));
return body.data.courses;
}).catch(function (err) {
console.log(err);
});
}
//getCourse();
//下单
async function createOrder(token,course){
return rp({
uri: host+`/order/${course._id}`,
method: 'POST',
body: {
price:3000,
paytime:new Date(),
status:0,
flowno:'pay001',
paymethod:'alipay'
},
headers: {
token
},
json: true
}).then(function (body) {
//console.log(body);
return body;
}).catch(function (err) {
console.log(err);
});
}
//createOrder();
//获取用户订单
async function getOrder(token){
return rp({
uri: host+'/order',
headers: {
token
},
json: true
}).then(function (body) {
//console.log(util.inspect(body,{depth:100}));
return body;
}).catch(function (err) {
console.log(err);
});
}
//getOrder();
async function mock(){
//删除数据库
await resetDb();
//获取token
var token = await getToken();
console.log('token',token);
var imgCode = await getImgCode(token);
console.log('imgCode',imgCode);
var vCode = await getVCode(token);
console.log('vCode',vCode);
var user = await createUser(token);
console.log('user',user);
var user = await identity(token);
console.log('user',user);
var course = await createCourse(token);
console.log('course',course);
var courses = await getCourse(token);
console.log('courses',courses);
var order = await createOrder(token,courses[0]);
console.log('order',order);
var orders = await getOrder(token);
console.log('orders',orders);
}
mock();
/*
User.findOne({mobile:'15718856132',password:'123456'}).then((data)=>{
console.log(data);
})*/