-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
204 lines (175 loc) · 4.85 KB
/
server.py
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
#!/usr/bin/python
# -*- coding:utf-8 -*-
import json
import uuid
import pymysql
from flask import *
from flask_cors import *
import CF
import mysql
from bean import User
from bean import Record
from bean import RecommendItem
"""webapp"""
app = Flask(__name__)
app.secret_key = 'flaskapp'
mysql = mysql.mysql()
''' @app.before_request
def direct():
pass '''
@app.route("/")
def index():
print(uuid.uuid1())
return "/"
# 注册
@app.route("/reg", methods=["POST"])
def signUp():
if request.method == "POST":
params = json.loads(str(request.get_data().decode("utf-8")))
print(params)
name = params.get('user')
psw = params.get('passwd')
sex = params.get('sex')
# 默认为0,即False
if not sex:
sex = 0
if not name or not psw:
return jsonify({
'code': 201,
'data': "bad request"
})
user = User()
user.userName = name
user.userPassword = psw
user.userSex = sex
u = mysql.selectFromUserByUsername(user.userName)
result = None
if not u.userName:
result = mysql.insertIntoUser(user)
else:
return jsonify({
'code': 207,
'data': "user is exits"
})
# if not (result is None)
if not result:
return jsonify({
'code': 204,
'data': "fail"
})
if result == 1:
return jsonify({
'code': 200,
'data': "success"
})
return jsonify({
'code': 205,
'data': "all file"
})
# 登陆
@app.route("/login", methods=["POST"])
def signIn():
if request.method == "POST":
# 参数utf-8编码再转str最后以json字符串方式变成对象
params = json.loads(str(request.get_data().decode("utf-8")))
print(params)
name = params.get('user')
psw = params.get('passwd')
if not name or not psw:
return jsonify({
'code': 201,
'data': "bad request"
})
user = mysql.selectFromUserByUsername(name)
if user is None:
return jsonify({
'code': 203,
'data': 'no user'
})
if psw == user.userPassword:
# 生成uuid 用做token
session['token'] = uuid.uuid1()
# 保存用户名
session['user'] = user.userName
return jsonify({
'code': 200,
'data': user.toJson(),
'token': session.get('token')
})
else:
return jsonify({
'code': 202,
'data': 'bad password'
})
# 主页数据
@app.route("/data")
def getData():
if request.method == "GET":
# 查询数据库
data = mysql.selectFromItem()
return jsonify({
'code': 200,
'data': data
})
# 提交购买数据
@app.route("/buy", methods=["POST"])
def buy():
if request.method == "POST":
params = json.loads(str(request.get_data().decode("utf-8")))
token = params.get('token')
# print(token)
# if token != session.get('token'):
# return jsonify({
# 'code': 201,
# 'data': 'auth fail'
# })
records = params.get('records')
if not records or not token:
return jsonify({
'code': 203,
'data': 'params error'
})
li = []
for arr in records:
tmprecord = Record()
tmprecord.user_id = arr[0]
tmprecord.itemId = arr[1]
tmprecord.item_num = arr[2]
li.append(tmprecord)
if mysql.insertIntoRecord(li):
return jsonify({
'code': 200,
'data': 'success'
})
return jsonify({
'code': 201,
'data': 'fail'
})
@app.route("/recommend", methods=["GET"])
def recommend():
if request.method == "GET":
token = request.args.get("token")
if(token):
cfItem = CF.CFItem()
li = cfItem.recommend()
# for i in li:
# print(i)
# tmp = RecommendItem()
# tmp.itemId = i[0]
# tmp.recommendlist = i[1:]
# data.append(tmp)
print(li)
return jsonify({
'code': 200,
'data': li
})
else:
return jsonify({
'code': 203,
'data': 'params error'
})
if __name__ == "__main__":
# 设置跨域访问
CORS(app, supports_credentials=True)
app.debug = True
app.run(host='0.0.0.0', port=9000)