# mengchongappBack **Repository Path**: mengchongPro/mengchongappBack ## Basic Information - **Project Name**: mengchongappBack - **Description**: 萌宠app后端 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2019-11-01 - **Last Updated**: 2020-12-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 萌宠--后端 > 项目合并记录 1、[2019/11/02 合并代码(第一次)](https://gitee.com/mengchongPro/mengchongappBack/issues/I14DNT) ## 数据库表设计 #### 用户表:users | 字段名 | 字段类型 | 解释 | 备注 | | -------- | -------- | ---------- | ------------------------ | | userid | String | 用户id | 使用uuid | | username | String | 用户名 | | | password | String | 用户密码 | | | tel | String | 用户手机号 | 用来登陆,需做手机号验证 | #### 轮播图:banners | 字段名 | 字段类型 | 解释 | 备注 | | --------- | -------- | ------------------------ | -------- | | bannersid | String | 轮播图id | 使用uuid | | type | String | 轮播图类型(首页,商城) | | | img | String | 轮播图地址 | | | href | String | 轮播图跳转 | | #### 购物车:carts | 字段名 | 字段类型 | 解释 | 备注 | | ------ | -------- | ------------ | -------- | | cartid | String | 购物车id | 使用uuid | | userid | String | 用户id | | | num | Number | 购物车数量 | | | proid | String | 购物车商品id | | #### 评论:comments | 字段名 | 字段类型 | 解释 | 备注 | | ---------- | -------- | ---------- | -------- | | commentsid | String | 评论id | 使用uuid | | userid | String | 用户id | | | beautid | String | 点赞id | | | text | String | 评论内容 | | | username | String | 评论用户名 | | #### 猜你喜欢: guessYouLike |字段名 | 字段类型| 解释 | |-------------|-----------|--------------| |likesid |String |内容id | |user_token |String |用户token | | likeimgs |String |猜你喜欢图片 | | liketitle |String |猜你喜欢标题 | |likecontent |String |猜你喜欢详情 | |likekeywords |String |猜你喜欢关键字 | |is_publish |String |发布情况 | |like_count |String |详情里面的点赞数 | |read_count |String |阅读数量 | |add_time |String |添加时间 | |update_time |String |更新时间 | #### 首页推荐 :tuijian |字段名 | 字段类型| 解释 | |-------------|-----------|--------------| |tuijianid |String |内容id | |tuijiantitle |String |内容标题 | | tuijianaddtime |String |添加时间 | | tuijianimg |String |内容图片 | #### 头像:headimgs | 字段名 | 字段类型 | 解释 | | ------ | -------- | ---- | | file | Object | | | type | String | | #### 订单:orders | 字段名 | 字段类型 | 解释 | | ------- | -------- | -------- | | orderid | String | 订单id | | userid | String | 用户id | | status | Number | 订单状态 | | list | Array | 订单详情 | #### 商品列表:pros | 字段名 | 字段类型 | 解释 | | ----------- | -------- | -------------------- | | proid | String | 商品id | | goods_id | String | 自带商品id(未使用) | | goods_price | Number | 价格 | | cover | String | 图片 | | title | String | 名称 | | express_fee | String | 是否包邮 | | category | String | 商品类型 | | sales | String | 购买人数 | 后端项目架构** 修改配置文件 **package.json** 添加**dev**指令 数据库相关文件,修改用户集合 (userid,username,password,tel) 创建数据库 mengchong, 修改db.js # 1 实现用户的注册接口 routes/users.js # 2 实现用户的登录接口 routes/users.js # 3 编写导入json数据 ## 3.1 创建产品的集合 sql/collection/pros.js const mongoose = require('./../db.js'); // 引入数据库连接模块 const Schema = mongoose.Schema; // 拿到当前数据库相应的集合对象 // 设计用户表的集合 const proSchema = new Schema({ // 设计用户集合的字段以及数据类型 goods_id: {type: String }, // 商品id goods_price: { type: Number }, //价格 cover: { type: String }, // 商品图片 title: { type: String }, // 商品名称 express_fee: { type: String }, // 是否包邮 category: { type: String }, // 商品类型 sales: {type: String} }) module.exports = mongoose.model('Pro', proSchema); # 3.2 添加产品的相关路由 routes/pro.js + app.js 注册路由 var proRouter = require('./routes/pro'); app.use('/pro', proRouter); # 3.2 导入功能 --- 只需要一次即可 routes/pro.js var fs = require('fs'); var Pro = require('./../sql/collection/pros'); var sql = require('./../sql'); fs.readFile('C:/Users/acer/Desktop/萌宠-卖狗-后/mengchongappBack/商品.json', 'utf-8', function (err, data) { if (err) { console.log(err); } else { obj = JSON.parse(data).data // console.log(obj); } }) // let obj = JSON.parse(filestr) router.get('/import',(req, res, next) => { // res.send(obj) let arr = [] obj.map((item, index) => { // console.log(item.form_data.title) arr.push({ goods_id: item.goods_id, // 商品id goods_price: item.goods_price, //价格 cover: item.form_data.cover, // 商品图片 title: item.form_data.title, // 商品名称 express_fee: item.form_data.express_fee, // 是否包邮 category: item.form_data.category[0], // 商品说明 是不是新上线的之类的 sales: item.form_data.sales // 多少人付款 }) }) // 4、插入数据库 sql.insert(Pro, arr).then(() => { // console.log(arr) res.send(arr) }) }) module.exports = router; # 4 加入购物车业务逻辑 routes/cart.js + app.js ## 4.1 设计购物车的集合 sql/collection/carts.js # sql/collection/carts.js const cartSchema = new Schema({ // 设计用户集合的字段以及数据类型 cartid: {type: String }, cartuserid: { type: String }, cartproid: { type: String }, cartnum: { type: Number } }) # 4.2 routers/cart.js 加入购物车接口 ``` // 加入购物车 购物车数据id 产品id 用户id 产品的数量num router.get('/add', (req, res, next) => { // 1、获取数据 let { userid, proid, num } = req.query; num = num * 1 || 1 // 设定默认数量 // 2、加入购物车 // 如果当前用户的购物车中有这个产品,数量加1,否则加入 sql.find(Cart, { userid, proid }, { _id: 0 }).then(data => { if (data.length === 0) { // 2.1没有改数据 --- 插入数据库操作 sql.insert(Cart, { cartid: 'cart_' + uuid.v1(), userid, proid, num }).then(() => { res.send({ code: '200', message: '加入购物车成功' }) }) } else { // 2.2更新数据库中购物车产品的数量 sql.update(Cart, { userid, proid }, { $inc: { num: 1 } }).then(() => { res.send({ code: '200', message: '加入购物车成功' }) }) } }) }) # 4.2 routers/cart.js 加入购物车接口 // 查询购物车数据 ---- 依据用户id获取购物车的数据,依据 产品id获取产品的信息,然后组合数据,输出数据 router.get('/', function(req, res, next) { // 1、获取用户id let { userid } = req.query; // 2、依据用户id查询购物车的数据 sql.find(Cart, { userid }, { _id: 0 }).then(data => { // 如果没有数据,告诉用户没有数据 if (data.length === 0) { // 2.1 没有数据 res.send(utils.cartnull) } else { // 2.2 有数据,遍历数据,获取数据的基本信息,组合数据 let arr = [] new Promise(resolve => { // 2.2.1内含异步操作 data.map((item, index) => { // 2.2.2遍历数据 // 2.2.3 依据产品的id查询数据的相关信息 sql.find(Pro, { proid: item.proid }, { _id: 0 }).then(data1 => { // console.log(data1) // 2.2.4 组合数据 arr.push({ cartid: item.cartid, userid: userid, proid: item.proid, proname: data1[0].proname, proimg: data1[0].proimg, price: data1[0].price, num: item.num }) // 2.2.5 遍历所有的数据结束 if (index >= data.length - 1) { resolve() } }) }) }).then(() => { // 2.2.6 返回购物车数据结果 res.send({ code: '200', message: '获取购物车列表的数据', length: arr.length, data: arr }) }) } }) }) # 4.3、删除购物车接口 router.get('/delete', (req, res, next) => { // 1、获取删除的条件 let { userid, proid } = req.query // 2、删除 sql.delete(Cart, { userid, proid }).then(() => { res.send(utils.deletesuccess) }) }) 4.4 更新购物车接口 router.get('/update', (req, res, next) => { // 1、获取更新的数据 let { cartid, num } = req.query // 2、更新数据 sql.update(Cart, { cartid }, { $set: { num: num } }).then(() => { res.send(utils.updatesuccess) }) }) # 5、获取产品详情接口 routes/pro.js ``` // 获取产品的详情 router.get('/detail', (req, res, next) => { let { proid } = req.query sql.find(Pro, { proid }, { _id: 0 }).then(data => { res.send({ code: '200', message: '查询该数据成功', data: data[0] }) }) }) # 6轮播图接口 * 数据库集合 sql/collection/banners.js * 数据库插入数据 > public/images复制相关图片,myapp/banner.js插入数据 ``` const sql = require('./sql') const Banner = require('./sql/collection/banners') const uuid = require('node-uuid'); const arr = [] for (var i = 0; i < 2; i++) { arr.push({ bannerid: 'banner_' + uuid.v1(), type: 'home', img: 'images/' + (i + 1) + '.jpg', href: '' }) } sql.insert(Banner, arr) ``` * 编写接口 routes/banner.js + app.js ``` // 获取轮播图 router.get('/', function(req, res, next) { // 依据类型查询相关的轮播图数据 let { type } = req.query type = type || 'home' sql.find(Banner, { type }, { _id: 0 }).then(data => { res.send({ code: '200', message: '获取轮播图数据成功', data: data }) }) }); ``` # 宠物秀接口 // 设计用户表的集合 const proSchema = new Schema({ // 设计用户集合的字段以及数据类型 nickname: {type: String }, // 名字 imgs: {type: String }, // 图片 user_token: { type: String }, //人的id title: { type: String }, // 标题 text: { type: String }, // 文章 headimgurl: { type: String }, // 头像 add_time: { type: String }, // 发表时间 address: {type: String}, //地址 comment: {type: String} // 阅读量 }) router.get('/', function(req, res, next) { // 1、获取前端的查询条件 // 3、查询数据 sql.paging(beaut, {}, { _id: 0 }).then(data => { // 4、返回数据 res.send({ code: '200', success: '查询列表成功', length: data.length, data: data }) }) }); fs.readFile('./json/宠物秀.json', 'utf-8', (err,data)=>{ if(err) throw err; // console.log(data); obj=JSON.parse(data).data // console.log(obj) }) router.get('/import',(req,res,next)=>{ // res.send(obj) let arr = [] obj.map((item,index)=>{ arr.push({ nickname: item.nickname, // 名字 imgs: item.content.imgs[0], // 图片 user_token: item.user_token, //人的id headimgurl: item.headimgurl, // 头像 add_time: item.add_time, // 发表时间 address: item.address, // 地址 title: item.title, // 标题 text: item.content.text, // 文章 comment: item.comment //阅读量 }) }) sql.insert(beaut,arr).then(()=>{ // console.log(arr) res.send(arr) }) })