1. 程式人生 > >express+mysql+vue專案入門-詳細配置登入模組(九)

express+mysql+vue專案入門-詳細配置登入模組(九)

express+mysql+vue專案入門-詳細配置登入模組

為了方便使用,在這裡將介面也進行模組化劃分。每個介面模組涉及到3個檔案:controller,entity,sqlmapper

//controler:
"use strict";
const models = require('./../../db');
const express = require('express');
const router = express.Router();
const loginEntity = require('./login.entity')
const resultEntity = require('./../../result')
const loginMapper = require('./login.mapper')
 
router.post('/api/login',(req,res)=>{
	loginMapper.query(req.query,result=>{
		if(result!=null){
		 	resultEntity.setData(result);
		 }
		resultEntity.setCode(0);
		resultEntity.setMessage('訪問了')
		res.send(resultEntity)
	},err=>{
		resultEntity.setData(result);
		resultEntity.setCode(500);
		resultEntity.setMessage('查詢出錯了')
		res.send(resultEntity)
	});
})
	

router.get('/api/userAll',(req,res)=>{
	loginMapper.queryAll(null,result=>{
		if(result!=null){
		 	resultEntity.setData(result);
		 }
		resultEntity.setCode(0);
		resultEntity.setMessage('訪問了')
		res.send(resultEntity)
	},err=>{
		resultEntity.setData(result);
		resultEntity.setCode(500);
		resultEntity.setMessage('查詢出錯了')
		res.send(resultEntity)
	});
})


router.post('/api/updateUser',(req,res)=>{
	loginMapper.update(req.query,result=>{
		resultEntity.setData('');
		resultEntity.setCode(0);
		resultEntity.setMessage('更新成功了')
		res.send(resultEntity)
	},err=>{})
	
})


router.post('/api/userdelete/:user_id',(req,res)=>{
	loginMapper.delete(req.params,result=>{
		resultEntity.setData('');
		resultEntity.setCode(0);
		resultEntity.setMessage('刪除成功了')
		res.send(resultEntity)
	},err=>{})

},err=>{})

router.post('/api/addUser',(req,res)=>{
	loginMapper.add(req.query,result=>{
		resultEntity.setData('');
		resultEntity.setCode(0);
		resultEntity.setMessage('使用者新增成功')
		res.send(resultEntity)
	},err=>{})
})

module.exports = router




//entity
function LoginEntity(){
	this.user_id = '';
	this.userName='';
	this.password='';
	this.phone='';
	this.createTime=''
}

var loginEntity = new LoginEntity();
module.exports = loginEntity;


//mapper
const models = require('./../../db');
function Login(){
	this.rows = [];
	this.getRows = function(){
		return this.rows;
	}
	this.add = function(params,callback,error){
		let valus=[];
		for(let i in params){
			valus.push(params[i])
		}
		console.log( valus )
		var sql = `insert into user (user_id,user_name,login_password,pay_password,user_number,user_photo,verification_code) values (?,?,?,?,?,?,?)`
		models.query(sql,valus,(err,efect)=>{
			if(err){
				error(err)	
			}else{
				callback(efect)
			}
		})
	};
	this.delete = function(params,callback,error){
		var sql = `delete from user where user.user_id=?`
		models.query(sql,params.user_id,(err,efect)=>{
			if(err){
				error(err)	
			}else{
				callback(efect)
			}
		})
	};
	this.update = function(params,callback,error){
		var sql  = `update user  set user.pay_password=? , user.user_photo=? where user.user_id=?`
		models.query(sql,[params.pay_password,params.user_photo,params.user_id],(err,efect)=>{
			if(err){
				error(err)	
			}else{
				callback(efect)
			}
		})	
	};
	this.queryAll = function(params,callback,error){
		var sql = 'select * from user';
		models.query(sql,(err,rows)=>{
			if (err) {
				error(err)	
			}else{
				callback(rows)
			}
		})
	};
	this.query = function(params,callback,error){
		var sql = `select * from user where user.user_name=? and user.login_password=?`
		models.query(sql,[params.user_name,params.login_password],(err,rows)=>{
			if(err){
				error(err)	
			}else{
				callback(rows)
			}
		})
	}
}

var login = new Login();
module.exports = login;