Skip to content

Commit

Permalink
update: auth user coupon
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyiming0803 committed Sep 11, 2019
1 parent eb13034 commit f869cdf
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 156 deletions.
18 changes: 0 additions & 18 deletions server/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,4 @@ router.post('/getPhoneCode', async (req, res) => {
}
});

/**
* 重置密码
*/
router.post('/resetPassword', async (req, res) => {
const md5 = crypto.createHash('md5');
const msg = req.body;
const phone = msg.phone;
const pwd = md5.update(msg.pwd).digest('hex');

try {
const data = await db('update tour_user set user_pwd="'+ pwd +'" where user_phone="'+ phone +'"');
if (data) res.json({ code: 0, data: null, message: '' });
else res.json({ code: -1, data: null, message: '密码重置失败,请重新操作' });
} catch (err) {
res.json({ code: -1, data: null, message: err });
}
});

module.exports = router;
90 changes: 90 additions & 0 deletions server/coupon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 优惠券相关
* @author: zhaoyiming
* @since: 2017/9/29
* @update: 2019/9/14
*/

const express = require('express');
const router = express.Router();
const db = require('./db');

/**
* 获取用户已经领取的优惠券
*/
router.get('/record', async (req, res) => {
try {
const { userId } = req.query;
const data = await db('select count(coupon_name) as num, coupon_type from tour_coupon where id in (select coupon_id from tour_coupon_user where user_id="' + userId + '") group by coupon_type');
res.json({ code: 0, data, message: '' });
} catch (e) {
res.json({ code: -1, data: null, message: e });
}
});

/**
* 获取优惠券详情
*/
router.get('/detail', async (req, res) => {
try {
const { id } = req.query;
const [data] = await db('select a.coupon_name, a.coupon_explain, a.coupon_starttime, a.coupon_endtime, a.coupon_ico_path, b.comment_content, b.comment_star, b.comment_user_phone from tour_coupon as a left join tour_comment as b on a.id=b.comment_coupon_id where a.id="' + id + '"');
if (data) {
res.json({ code: 0, data, message: '' });
} else {
res.json({ code: -1, data: null, message: '没有查询到优惠券信息' });
}
} catch (e) {
res.json({ code: -1, data: null, message: e });
}
});

/*
* 立即获取优惠券
*/
router.post('/receive', async (req, res) => {
try {
const { coupon_id, user_id } = req.body;
const [id] = await db('select id from tour_coupon_user where coupon_id="' + coupon_id + '" and user_id="' + user_id + '"');
if (id) {
res.json({ code: -1, data: null, message: '已经领取过了' });
} else {
const { insertId } = await db('insert into tour_coupon_user set coupon_id="' + coupon_id + '", user_id="' + user_id + '"');
if (insertId) {
const data = await db('update tour_coupon set coupon_recived_num=coupon_recived_num+1 where id="' + coupon_id + '"');
if (data) {
res.json({ code: 0, data: 1, message: '领取成功' });
} else {
res.json({ code: -1, data: 0, message: '领取失败,请重新操作' });
}
}
}
} catch (e) {
res.json({ code: -1, data: null, message: e });
}
});

/**
* 评论
*/
router.post('/comment', async (req, res) => {
const { id, phone, starGrade, commentContent, couponId } = req.body;
let addComment = null;
let changeStatus = null;

// 按照一般的逻辑:用户购买或使用产品之后才能进行评论;
// 这里的优惠券暂时没有判断什么时候就算使用了,所以测试执行以下逻辑:
// 用户发表评论这个【动作】即是【使用】优惠券,优惠券使用完之后不能再次使用
// 线上的项目,这块儿的逻辑可以修改下
const [item] = await db('select status from tour_coupon_user where coupon_id="'+ couponId +'" and user_id="'+ id +'"');
if (item && item.status === 0) {
const interdedId = await db('insert into tour_comment set comment_user_phone="'+ phone +'", comment_star="'+ starGrade +'", comment_content="'+ commentContent +'", comment_time="'+ new Date().getTime() +'", comment_coupon_id="'+ couponId +'"');
const res = await db('update tour_coupon_user set status=1 where coupon_id="'+ couponId +'" and user_id="'+ id +'"');
res.json({code: 0, data: {interdedId, res}, message: ''});
} else {
res.json({code: 0, data: status, message: ''});
}

});

module.exports = router;
9 changes: 2 additions & 7 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ app.use(bodyParser.urlencoded({extended: true}));

app.use('/auth', require(__dirname + '/auth'));
app.use('/user', require(__dirname + '/user'));


// 个人中心
app.use('/personal', require(__dirname + '/personal/index'));

// 个人资料修改
app.use('/personalEdit', require(__dirname + '/personal/personal-edit'));
app.use('/coupon', require(__dirname + '/coupon'));
app.use('/resion', require(__dirname + '/region'));

// 首页
app.use('/globalCoupon', require(__dirname + '/global-coupon/index'));
Expand Down
59 changes: 0 additions & 59 deletions server/personal/index.js

This file was deleted.

72 changes: 0 additions & 72 deletions server/personal/personal-edit.js

This file was deleted.

23 changes: 23 additions & 0 deletions server/region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 国家和地区相关
* @author: zhaoyiming
* @since: 2019/9/13
*/

const express = require('express');
const router = express.Router();
const db = require('./db');

/**
* 国家与地区列表
*/
router.get('/list', async (req, res) => {
try {
const data = await db('select id, country_name from tour_country');
res.json({ code: 0, data, message: '' });
} catch (e) {
res.json({ code: -1, data: null, message: e });
}
});

module.exports = router;
94 changes: 94 additions & 0 deletions server/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,99 @@
const express = require('express');
const router = express.Router();
const db = require('./db');
const multer = require('./multer');

/**
* 获取用户基本信息
*/

router.get('/info', async (req, res) => {
try {
const { userId } = req.query;
const [data] = await db('select * from tour_user where id="' + userId + '"');
if (data) {
res.json({ code: 0, data, message: '' });
} else {
res.json({ code: -1, data: null, message: '用户不存在' });
}
} catch (e) {
res.json({code: -1, data: null, message: e});
}
});

/**
* 修改头像
*/
router.post('/changeUserHeadpic', multer.array('file'), async (req, res) => {
try {
const file = req.files[0];
const userId = req.body.id;
const filePath = 'static/uploads/images/' + file.filename;
const data = await db('update tour_user set user_headpic="' + filePath + '" where id="' + userId + '"');
if (data) {
// 后期要换成oss地址
res.json({ code: 0, data: file.filename, message: 0 });
} else {
res.json({ code: -1, data: null, message: '修改修改失败,请重新操作' });
}
} catch (e) {
res.json({code: -1, data: null, message: e});
}
});

/*
* 修改用户名
*/
router.post('/changeUserName', async (req, res) => {
try {
const msg = req.body;
const userId = msg.userId;
const userName = msg.userName;
const data = await db('update tour_user set user_name="' + userName + '" where id="' + userId + '"');
if (data) {
res.json({ code: 0, data: null, message: '修改成功' });
} else {
res.json({ code: -1, data: null, message: '用户名修改失败,请重新操作' });
}
} catch (e) {
res.json({code: -1, data: null, message: e});
}
});

/*
* 修改性别
*/
router.post('/changeUserSex', async (req, res) => {
try {
const msg = req.body;
const userId = msg.userId;
const sex = msg.sex;
const data = db('update tour_user set user_sex="' + sex + '" where id="' + userId + '"');
if (data) {
res.json({ code: 0, data: null, message: '修改成功' });
} else {
res.json({ code: -1, data: null, message: error });
}
} catch (e) {
res.json({ code: -1, data: null, message: e });
}
});

/**
* 重置密码
*/
router.post('/resetPassword', async (req, res) => {
try {
const md5 = crypto.createHash('md5');
const msg = req.body;
const phone = msg.phone;
const pwd = md5.update(msg.pwd).digest('hex');
const data = await db('update tour_user set user_pwd="' + pwd + '" where user_phone="' + phone + '"');
if (data) res.json({ code: 0, data: null, message: '' });
else res.json({ code: -1, data: null, message: '密码重置失败,请重新操作' });
} catch (e) {
res.json({ code: -1, data: null, message: e });
}
});

module.exports = router;

0 comments on commit f869cdf

Please sign in to comment.