基于express的Node.js
每个用户保存为一个json文件,以学号usn命名,如 22054022.json为何芯妍同学的数据文件,节点数据基本字段为
可根据设计自行增加或减少
{
"usn": "2024",
"upass": "",
"uname": "何芯妍",
"udesc":"",
"ubirth":"",
"ugender":"",
"ucell": "",
"uaddr":"",
"imgUrl":""
}
未更新头像时,使用 默认头像
更新头像时,名字name为avatar
用户头像保存在服务器上,用户头像的url保存在用户数据中
wx.request({
url: 'http://127.0.0.1:3000/user/login',
data: e.detail.value,
success: res => {
console.log(res);
switch (res.data.errno) {
case 0:
wx.showToast({
title: res.data.msg,
})
// update app global data
app.globalData.user = res.data.data
// update local storage
wx.setStorageSync('user', res.data.data)
// to home
wx.navigateTo({
url: '../home/home',
})
default:
wx.showToast({
title: res.data.msg,
})
}
},
fail: err => {
console.log(err)
wx.hideLoading()
wx.showToast({
title: 'server down'
})
},
complete: () => {
console.log('get user done logging')
wx.hideLoading()
}
})
router.get('/login', (req, res) => {
console.log('req.query', req.query);
let file = fs.readFile('./public/data/' + req.query.usn + '.json', 'utf8', (err, data) => {
if (err) {
res.json({
"errno": 2,
"msg": 'user not exist'
})
} else {
let jsonFile = JSON.parse(data)
if (req.query.upass === jsonFile.upass) {
res.json({
"errno": 0,
"msg": 'login ok',
"data": jsonFile
})
} else {
res.json({
"errno": 1,
"msg": 'pass not match'
})
}
}
})
})
wx.request({
url: 'http://127.0.0.1:3000/user/register',
method: 'POST',
data: e.detail.value,
success: res => {
console.log(res);
switch (res.data.errno) {
case 0:
//update global data
app.globalData.user = {
usn: this.data.usn,
uname: this.data.uname,
upass: this.data.upass
}
//update local storage
wx.setStorageSync('user', JSON.stringify({
usn: this.data.usn,
uname: this.data.uname,
upass: this.data.upass,
imgUrl: 'https://cdn.pixabay.com/photo/2015/03/03/20/42/man-657869_640.jpg'
}))
wx.showToast({
title: res.data.msg,
})
wx.navigateTo({
url: '../login/login',
})
default:
wx.showToast({
title: res.data.msg,
})
}
},
fail: err => console.log(err),
complete: () => console.log('get user done logging')
})
router.post('/register', (req, res) => {
if (fs.existsSync('./public/data/' + req.body.usn + '.json')) {
res.json({
"errno": 2,
"msg": 'user exist'
})
return;
}
fs.writeFile('./public/data/' + req.body.usn + '.json', JSON.stringify(req.body), (err) => {
if (err) {
console.log('保存文件失败')
res.json({
"errno": 1,
"msg": 'try later'
})
} else {
console.log('保存文件成功')
res.json({
"errno": 0,
"msg": 'register ok'
})
}
})
})
router.post('/modify', (req, res) => {
console.log('modify', req.body);
const data = JSON.parse(fs.readFileSync('./public/data/' + req.body.usn + '.json', 'utf8'));
data[req.body.ukey] = req.body.uvalue
fs.writeFile('./public/data/' + req.body.usn + '.json', JSON.stringify(data, null, 4), (error) => {
if (error) {
console.log('保存文件失败')
res.json({
"errno": 2,
"msg": 'try later'
})
} else {
console.log('保存文件成功')
res.json({
"errno": 0,
"msg": 'modify ok'
})
}
})
})
router.post('/avatar', upload.single('avatar'), (req, res) => {
console.log('req.file', req.file);
const data = JSON.parse(fs.readFileSync('./public/data/' + req.body.usn + '.json', 'utf8'));
data['imgUrl'] = 'http://127.0.0.1:3000/upload/' + req.file.filename
fs.writeFile('./public/data/' + req.body.usn + '.json', JSON.stringify(data, null, 4), (error) => {
if (error) {
console.log('保存文件失败')
res.json({
"errno": 2,
"msg": 'try later'
})
} else {
console.log('保存文件成功')
res.json({
"errno": 0, // 注意:值是数字,不能是字符串
"msg": "update ok",
"url": 'http://127.0.0.1:3000/upload/' + req.file.filename, // 图片 src ,必须
})
}
})
})
const storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, 'public/upload');
},
filename: (req, file, callback) => {
let ext = path.extname(file.originalname)
callback(null, file.fieldname + Date.now() + ext);
}
});