项目功能演示视频地址:
https://www.bilibili.com/video/BV1754y1q7cv (本项目的功能都在这里有介绍呀,小编自制,比较简陋,喜欢的博友可以点击看看,点个赞呀!)
modules 文件目录
八张表
user.js表 其余表编写与此相似,除了index.js
module.exports = function(sequelize,DataTypes){
return sequelize.define(
'user',
{
id:{
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
account:{
type: DataTypes.STRING,
field: 'account'
},
password:{
type: DataTypes.STRING,
field: 'password'
},
user_name:{
type: DataTypes.STRING,
field: 'user_name'
},
user_sex: {
type: DataTypes.INTEGER,
field: 'user_sex',
default:0
},
user_phone: {
type: DataTypes.STRING,
field: 'user_phone'
},
user_village: {
type: DataTypes.STRING,
field: 'user_village'
},
user_photo: {
type: DataTypes.STRING,
field: 'user_photo'
},
user_status: {
type: DataTypes.INTEGER,
field: 'user_status',
default:0
},
solt:{
type: DataTypes.STRING,
field: 'solt'
}
}
);
}
index.js 导入模型统一管理 表与表之间的关系
const config = require('../config/mysql_sequelize')
const Sequelize = config.sequelize
//导入模型统一管理
const user = Sequelize.import(__dirname + '/user.js')
const role = Sequelize.import(__dirname + '/role.js' )
const user_role = Sequelize.import(__dirname + '/user_role.js')
const activity = Sequelize.import(__dirname + '/activity.js')
const user_activity = Sequelize.import(__dirname + '/user_activity.js')
const epla = Sequelize.import(__dirname + '/epla.js')
const announce = Sequelize.import(__dirname + '/announce.js')
const approval = Sequelize.import(__dirname + '/approval.js')
//用户与角色多对多
user.belongsToMany(role, { through: 'user_role', foreignKey: 'account', sourceKey: 'account'});
role.belongsToMany(user, { through: 'user_role', foreignKey: 'role_id', sourceKey: 'id' });
//用户与活动多对多
user.belongsToMany(activity, { through: 'user_activity', foreignKey:'account', sourceKey:'account'});
activity.belongsToMany(user, { through: 'user_activity', foreignKey: 'activity_id', sourceKey:'id'});
//活动与老人一对一
activity.hasOne(epla, { foreignKey:'id', sourceKey:'epla_id' })
epla.belongsTo(activity, { foreignKey: 'id', targetKey:'epla_id' })
//用户与通告一对多
user.hasMany(announce, {foreignKey:'account', sourceKey:'account'})
announce.belongsTo(user, {foreignKey:'account', targetKey:'account'})
module.exports = { user, role, user_role, activity, user_activity, epla, announce, approval}
这里只列举了user_dao.js,想看其他的dao类可以下载源码哦!
//引入sequelize对象
// const Sequelize = db.sequelize
var Sequelize = require('sequelize-values')();
//引入数据表模型
// const user = Sequelize.import('../models/user.js')
const { user, role, user_role, activity, user_activity} = require('../models/index.js')
//自动创建表
// user.sync({ force: false });
//数据库操作类
class userDao {
//获取管理员信息
static async getUserAdminInfo(account) {
return await user.findOne({
where: {
account
},
include: {
model: role
}
}).then(function (user) {
return Sequelize.getValues(user);
})
}
//获取所有用户信息
static async getUserInfo() {
return await user.findAll({include:{model:role}}).then(function (user){
return Sequelize.getValues(user);
})
}
//分页-获取所有用户信息
static async userInfoPagination(searchCondition,sikpPage,pageSize) {
return await user.findAndCountAll({
limit:pageSize,
offset:sikpPage,
where:searchCondition,
attributes: ['id', 'account', 'user_name', 'user_status'],
include:[{
model:role
}]
}).then(function (user) {
return Sequelize.getValues(user);
})
}
//获取用户信息
static async getOneUserInfo(account) {
return await user.findOne({
where:{
account
},
include:[{model:role}]
}).then(function (user) {
return Sequelize.getValues(user);
})
}
//新增用户
static async postUser(userInfo) {
return await user.findOrCreate({
where:{
account:userInfo.account
},
defaults:{
account:userInfo.account,
password:userInfo.password,
user_name:userInfo.userName,
user_sex:userInfo.userSex,
user_phone:userInfo.userPhone,
user_village:userInfo.userVillage,
user_photo:userInfo.userPhoto,
user_status:userInfo.userStatus,
solt:userInfo.solt
},
raw:true
})
}
//新增用户--客户端
static async postuser(userInfo) {
return await user.findOrCreate({
where: {
account: userInfo.account
},
defaults: {
account: userInfo.account,
password: userInfo.password,
user_name: userInfo.user_name,
user_sex: userInfo.user_sex,
user_phone: userInfo.user_phone,
user_village: userInfo.user_village,
user_photo: userInfo.user_photo,
user_status: userInfo.user_status,
solt: userInfo.solt
},
raw: true
})
}
//编辑用户
static async putUser(editResult) {
return await user.update({
password:editResult.password,
user_name: editResult.userName,
user_sex: editResult.userSex,
user_phone: editResult.userPhone,
user_village: editResult.userVillage,
user_photo: editResult.userPhoto,
user_status: editResult.userStatus,
solt: editResult.solt
},{
where:{
account:editResult.account
}
})
}
//删除用户--客户端
static async deleteUser(account){
return await user.destroy({
where:{
account:account
}
})
}
//新增用户角色表
static async postUserRole(account, roleId) {
return await user_role.findOrCreate({
where: {
account: account,
role_id: roleId
},
defaults: {
account: account,
role_id: roleId
},
raw: true
})
}
//修改用户角色表
static async putUserRole(account, roleId) {
return await user_role.create({
where: {
account: account,
role_id: roleId
},
defaults: {
account: account,
role_id: roleId
},
raw: true
})
}
//删除用户角色表
static async deleteUserRole(account) {
return await user_role.destroy({
where: {
account: account
}
})
}
//编辑用户(包含图片)--客户端
static async putuserinfo(editResult) {
return await user.update({
user_name: editResult.user_name,
user_sex: editResult.user_sex,
user_phone: editResult.user_phone,
user_village: editResult.user_village,
user_photo: editResult.user_photo
}, {
where: {
account: editResult.account
}
})
}
//获取用户活动表--客户端
static async getuserhasactivity(account){
return await user.findAll({
include:{
model:activity
},
where:{
account:account
}
}).then(function (user) {
return Sequelize.getValues(user)
})
}
//分页获取活动列表--客户端--下拉刷新
static async getactivitylist(sikpPage, pageSize) {
return await activity.findAndCountAll({
limit: pageSize,
offset: sikpPage,
}).then(function (activity) {
return Sequelize.getValues(activity);
})
}
//获取当前村所有活动--客户端
static async getonehometownactivity(activity_address){
return await activity.findAll({
where:{
activity_address:activity_address
}
}).then(function (activity) {
return Sequelize.getValues(activity);
})
}
//获取用户活动关联表--客户端--报名活动
static async getuseractivityconnect(account,activity_id){
return await user_activity.findOne({
where:{
account:account,
activity_id:activity_id
}
}).then(function (user_activity){
return Sequelize.getValues(user_activity)
})
}
//新增用户活动关联表--客户端--报名活动--志愿者
static async postuseractivityconnect(account,activity_id){
return await user_activity.create({
account:account,
activity_id: activity_id,
user_status:3,
status:0,
current_time: null
})
}
//新增用户活动关联表--客户端--参与活动--参与者
static async postparticipantactivity(account, activity_id, current_time) {
return await user_activity.create({
account: account,
activity_id: activity_id,
user_status: 2,
status: 0,
current_time: current_time
})
}
//更新已报名人数--客户端--报名活动--志愿者
static async putactivitynum(activity_id,num){
return await activity.update({
num:num
},{
where:{
id:activity_id
}
})
}
}
module.exports = userDao
const cors = require('koa2-cors'); //跨域处理
var cors_config = new cors(
{
origin: function(ctx) { //设置允许来自指定域名请求
return 'http://localhost:8080' //默认允许本地请求3000端口可跨域
},
maxAge: 5, //指定本次预检请求的有效期,单位为秒。
credentials: true, //是否允许发送Cookie
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],//设置所允许http的请求方法
allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
});
module.exports = cors_config
log4.js 错误日志 的配置
const log4js = require('log4js')
log4js.configure({
appenders: {
error: {
type: 'file', //日志类型
//日志输出位置,当目录文件或文件夹不存在时自动创建
category: 'errLogger', //日志名称
filename: __dirname + '/../logs/error.log/',
maxLogSize: 104800, // 文件最大存储空间
backups: 100 //当文件内容超过文件存储空间时,备份文件的数量
},
response: {
type: 'dateFile',
category: 'resLogger',
filename: __dirname + '/../logs/responses/',
pattern: 'yyyy-MM-dd.log', //日志输出模式
alwaysIncludePattern: true,
maxLogSize: 104800,
backups: 100
}
},
categories: {
error: {appenders: ['error'], level: 'error'},
response: {appenders: ['response'], level: 'info'},
default: { appenders: ['response'], level: 'info'}
},
replaceConsole: true
})
module.exports = log4js
sequelize的配置
var Sequelize = require("sequelize")
var sequelize = new Sequelize('hener','root','root',{
host:'localhost',
dialect:'mysql',
// operatorsAliases:false,
dialectOptions:{
//字符集
charset:'utf8mb4',
collate:'utf8mb4_unicode_ci',
supportBigNumbers: true,
bigNumberStrings: true
},
pool:{
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define:{
paranoid:false,
operatorsAliases: false,
timestamps: false,
freezeTableName: true
},
// 时区
timezone: '+08:00'
});
module.exports = {
sequelize,
Sequelize
};
GET/POST/PUT/DELETE
前端接口定义:
/api/users.js
const router = require('koa-router')()
const userController = require('../../controllers/client/user')
const passport = require("../../util/passport")
router.prefix('/api/users')
router.post('/postlogin', userController.postlogin)
router.post('/postregister', userController.postregister)
router.get('/getresult', userController.getresult)
router.all('/*',
passport.authenticate('jwt', { session: false })
,async (ctx, next) => {
console.log(ctx.request)
await next()
})
router.get('/userinfo', userController.userinfo)
router.put('/putpassword', userController.putpassword)
router.get('/getuserinfo', userController.getuserinfo)
router.put('/putuserinfo', userController.putuserinfo)
router.get('/getuseractivity', userController.getuseractivity)
router.get('/getactivitylist', userController.getactivitylist)
router.get('/getonehometownactivity', userController.getonehometownactivity)
router.get('/getoneactivity', userController.getoneactivity)
router.post('/postenrollactivity', userController.postenrollactivity)
router.get('/getannounces', userController.getannounces)
router.get('/getannouncescol', userController.getannouncescol);
router.get('/getcurrentannounce', userController.getcurrentannounce);
router.get('/getallactivity', userController.getallactivity);
router.post('/postparticipantactivity', userController.postparticipantactivity);
router.delete('/deleteapplicationactivity', userController.deleteapplicationactivity);
router.get('/getactivitynotive', userController.getActivityNotive);
router.get('/getattention', userController.getAttention);
router.post('/postapproval', userController.postApproval);
router.get('/getoneapproval', userController.getOneApproval);
router.put('/putapproval', userController.putApproval);
module.exports = router
后端接口定义:
在这次新媒体技术实训课程中,充分学习了如何自己去设计一个app,自己切图,自己去主动思考,因为上学期学习KOA2和Vue.js不够巩固和充分理解,这一次沉下心去做,重新再来一次,我觉得自己做到了!比之前的自己有进步,我觉得很开心,熬过的夜虽然辛苦,但是当作品呈现出来的时候,原来,自己一直在进步!感谢我的老师对我的点评,给予的鼓励和帮助!感谢自己的兄弟一起互相讨论,一起去研究怎么做会更好的达到要求!在这次课程,我觉得自己比较缺乏的是思考问题的逻辑,这一点我需要好好的向其他同学学习,还有一点是自己还不够主动,和老师交流比较少,比如说那个参与者的问题,如果自己早点和老师交流,就会能更好的去实现,还有一点就是自己在后端花的时间太长了,而没有和前端早日联动,这样给自己带来一些学习的负担!
冲冲冲!
后端部分就总结到这里啦!如果你喜欢小编的分享,就为小编点个赞呗!
项目源码看这里哦,码云地址:https://gitee.com/lwkgood/hener_serve