vue2.0搭配php,vue cli2.0 + php + mysql 面向过程升级成面向对象

一、前言

上期用最简单技术形式走通了前后台的需求,制作了一个任务管理系统,方便记录并规划每天的成长。

写任务的核心是为了更好的规划时间,想象让你做一个计划系统你该如何考虑?

1)首先你必须有地方去写,有地方展示

2)展示时需区别未完成和已完成的状态

3)需要规定一个结束日期,并且加入提醒,到指定时间未完成的任务,需要写清理由。

4)......

所以这期做一下调整:

1. 整体界面需要重新修改

2.PHP从面向过程升级成面向对象

3.删除接口从物理删除变为逻辑删除

4.因布局调整所影响前端逻辑的改良

5.基础SQL语法举一反三

二、调整样式

4742fac0adb6

修改前

4742fac0adb6

修改后

三、业务梳理

前端逻辑主要是练习vue基本语法,以及事件触发之间的逻辑关系,如何跟php交互数据。

主要功能:

用户输入添加后变为待办项,完成后用户勾选变为已办项,每一项计划可删除和编辑,还记录了计划的条数和未完成的条数

4742fac0adb6

勾选的样子

4742fac0adb6

修改内容的样子

四、前端布局

上期的布局及样式完全推倒重做,代码如下:

id="box">

任务便签

总数为 {{ arr.length}}个,还有 {{ choose() }}个未完成

class="span1"

@click='finish()'>清除已选

】【

@click='Allfinish()'

style="color:#35b96d;">全部清除

  • v-bind:class='{finish: item.bol}'>

    {{index+1}}

    v-model='item.bol'

    @change="updataFn()">

    v-show='!item.edit'

    @click='edit(index)'>

    v-model='item.des'

    v-show='item.edit'

    v-on:keyup.13='item.edit = !item.edit'

    @blur='item.edit = false;updataFn()'>

    @click='del(item.des)'>

type="text"

class="add"

v-model='msg'

v-on:keyup.13="add">

保存

-------------------------------------------------------------------------------------------------------------------

export default {

name: 'HelloWorld',

data () {

return {

arr: [

// 数据结构:

// des: 任务描述

// bol: 任务完成标志

// edit: 任务编辑标志

// { des:'设计', bol:false, edit:false }

],

msg: ''

}

},

created () {

if (localStorage.getItem('msg')) {

//如果有就读取msg存到arr数组中

this.arr = JSON.parse(localStorage.getItem('msg'))

}

},

methods: {

// 添加(判断msg是否为空,如果不为空就给arr数组里push内容,然后在把msg设置为空)

add: function () {

if (this.msg.replace(/[\r\n]/g, "") == '') {

this.msg = ''

alert('请输入名称');

return;

} else {

for (var i = 0; i 

if (this.arr[i].des == this.msg.replace(/[\r\n]/g, "")) {

this.msg = ''

alert('任务名称不能重复');

return;

}

}

}

this.arr.push({ des: this.msg.replace(/[\r\n]/g, ""), bol: false, edit: false });

localStorage.setItem('msg', JSON.stringify(this.arr))

this.msg = ''

},

//更新到本地存储中

updataFn: function () {

localStorage.setItem('msg', JSON.stringify(this.arr))

},

// 返回未完成的数量

choose: function () {

var num = 0;    // 未完成的数量

this.arr.forEach(function (item) {

if (!item.bol) {

num += 1;

}

});

return num;

},

// 返回未完成的任务

finish: function () {

var forNum = 0;

for (var i = 0; i 

if (this.arr[i].bol) {

forNum++

}

}

if (forNum == 0) {

alert('您需要选中复选框删除');

}

var temp = this.arr;

this.arr = []

for (var i = 0; i 

if (!temp[i].bol) {

this.arr.push(temp[i]);

console.log(temp[i])

}

}

this.updataFn();

},

//单条删除

del (des) {

var index = this.arr.findIndex(item => {

if (item.des == des) {

return true

}

})

this.arr.splice(index, 1)

this.updataFn();

},

//清除每条数据

Allfinish () {

if (this.arr.length == 0) {

alert('没有可删除的选项')

} else {

this.arr = [];

this.updataFn();

}

},

// 编辑

edit: function (i) {

if (this.arr[i].bol) {

return;

}

this.arr[i].edit = true;

}

}

}

--------------------------------------------------------------------------------------------------------------

.redStyle{background:#ccc;display:inline-block;width:20px;height:20px;vertical-align:middle}

h4{color:#252434}

.delBtn{margin-top:15px;float:right;background:url(../../src/assets/del.png) no-repeat;background-size:60%;vertical-align:middle}

#box{width:580px;margin:30px auto;background:#fff;color:#222131;padding:30px 50px;border:1px solid #252434;border-bottom-width:4px;border-radius:3px;font-family:"Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei"}

li:nth-child(2n+2){margin-right:0}

.icon_num{opacity:.8;color:#fff;box-sizing:border-box;border-radius:100%;display:inline-block;background:#35b96d;font-size:11px;left:-5px;width:15px;height:15px;line-height:15px;vertical-align:middle;text-align:center;bottom:-8px;border-radius:100%}

input[type=text]{outline:0;width:100%;border:1px solid #f9f9f9}

li{position:relative;background:#fff;border:1px solid #ccc;border-radius:3px;box-sizing:border-box;padding:0 11px;list-style-type:none;width:47%;line-height:40px;font-size:14px;color:#5e5d6d;margin-bottom:10px;margin-right:16px;float:left;font-size:12px;overflow:hidden}

ul{padding-left:0;margin-top:30px;margin-bottom:20px;overflow:hidden}

.add{width:100%;height:80px;margin-right:10px;margin-bottom:5px;outline:0}

.span1{color:#87ceeb}

.finish{box-sizing:border-box;color:#ccc;text-decoration:line-through;height:40px;line-height:40px;background:#eee;font-size:14px;border:none}

textarea{border:1px solid #252434;font-family:"Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei";padding:5px 10px;box-sizing:border-box;background:#fff;color:#252434}

button{outline:0;width:100%;background:#252434;border:none;height:40px;color:#ccc}

五、后台逻辑

六、下期预告

⚠ 下期会增加定时提醒(包含声音和弹窗提示),历史查询和显示以天为单位。

⚠ 下期会增加删除的再次询问功能,避免误删。

⚠ 下期会调整修改的布局样式。

⚠ 下下期做一个响应式布局,来适配手机端、平板、pc端。

⚠  下下期加入登录和注册功能,加入users表。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个使用Node.js+Vue+MySQL的客服系统,需要以下几个步骤: 1. 创建一个MySQL数据库,用于存储客户信息和聊天记录; 2. 创建一个Node.js后端,提供API接口,用于客户端和管理端进行数据交互; 3. 创建一个Vue前端,实现在线聊天和管理客户的操作。 下面是一个简单的示例,使用Node.js+Vue+MySQL实现客服系统的具体过程和代码: 1. 创建MySQL数据库 同样的,我们需要创建一个MySQL数据库,用于存储客户信息和聊天记录。可以使用上面的SQL语句来创建。 2. 使用Node.js连接MySQL数据库 安装mysql模块,使用以下代码连接MySQL数据库: ``` const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'customer_service' }); connection.connect(); ``` 3. 创建API接口 在Node.js后端中创建API接口,用于客户端和管理端进行数据交互。可以使用Express框架来创建API接口。 ``` const express = require('express'); const app = express(); // 获取所有客户信息 app.get('/customers', (req, res) => { connection.query('SELECT * FROM customers', (error, results, fields) => { if (error) throw error; res.json(results); }); }); // 根据客户ID获取聊天记录 app.get('/messages/:customerId', (req, res) => { const customerId = req.params.customerId; connection.query('SELECT * FROM messages WHERE customer_id = ?', [customerId], (error, results, fields) => { if (error) throw error; res.json(results); }); }); // 发送聊天消息 app.post('/messages', (req, res) => { const { customerId, sender, message } = req.body; connection.query('INSERT INTO messages (customer_id, sender, message) VALUES (?, ?, ?)', [customerId, sender, message], (error, results, fields) => { if (error) throw error; res.json({ success: true }); }); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` 4. 创建Vue前端 使用Vue框架创建前端界面,实现在线聊天和管理客户的操作。可以使用Vue CLI工具来创建Vue项目。 ``` vue create customer-service ``` 创建成功后,在src目录下创建一个components目录,用于存放Vue组件。 5. 实现客户列表组件 在components目录下创建一个Customers.vue组件,用于显示客户列表。 ``` <template> <div> <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> </tr> </thead> <tbody> <tr v-for="customer in customers" :key="customer.id"> <td>{{ customer.name }}</td> <td>{{ customer.email }}</td> <td>{{ customer.phone }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { customers: [] }; }, mounted() { fetch('/customers') .then(response => response.json()) .then(customers => { this.customers = customers; }); } }; </script> ``` 6. 实现聊天记录组件 在components目录下创建一个Messages.vue组件,用于显示聊天记录。 ``` <template> <div> <h3>{{ customer.name }}'s Messages</h3> <ul> <li v-for="message in messages" :key="message.id"> <strong>{{ message.sender }}</strong>: {{ message.message }} </li> </ul> <form @submit.prevent="sendMessage"> <input type="text" v-model="message" placeholder="Type your message here"> <button type="submit">Send</button> </form> </div> </template> <script> export default { props: ['customer'], data() { return { messages: [], message: '' }; }, mounted() { fetch(`/messages/${this.customer.id}`) .then(response => response.json()) .then(messages => { this.messages = messages; }); }, methods: { sendMessage() { fetch('/messages', { method: 'POST', body: JSON.stringify({ customerId: this.customer.id, sender: 'customer', message: this.message }), headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(result => { if (result.success) { this.messages.push({ sender: 'customer', message: this.message }); this.message = ''; } }); } } }; </script> ``` 7. 实现客户详情组件 在components目录下创建一个Customer.vue组件,用于显示客户详情。 ``` <template> <div> <h3>{{ customer.name }}</h3> <p>Email: {{ customer.email }}</p> <p>Phone: {{ customer.phone }}</p> <messages :customer="customer"></messages> </div> </template> <script> import Messages from './Messages.vue'; export default { components: { Messages }, props: ['customer'] }; </script> ``` 8. 实现路由配置 在src目录下创建一个router.js文件,用于配置路由。 ``` import Vue from 'vue'; import VueRouter from 'vue-router'; import Customers from './components/Customers.vue'; import Customer from './components/Customer.vue'; Vue.use(VueRouter); const routes = [ { path: '/', component: Customers }, { path: '/customers/:id', component: Customer } ]; const router = new VueRouter({ mode: 'history', routes }); export default router; ``` 9. 启动客户系统 在根目录下创建一个server.js文件,用于启动Node.js后端。 ``` const express = require('express'); const app = express(); const cors = require('cors'); const mysql = require('mysql'); const router = require('./router'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'customer_service' }); connection.connect(); app.use(cors()); app.use(express.json()); app.use(router); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` 在根目录下创建一个vue.config.js文件,用于配置Vue前端的代理服务器。 ``` module.exports = { devServer: { proxy: { '/': { target: 'http://localhost:3000', ws: true, changeOrigin: true } } } }; ``` 最后,在命令行中分别启动Node.js后端和Vue前端: ``` node server.js npm run serve ``` 以上代码仅仅是一个简单的示例,实际的客服系统需要更多的功能和细节处理,例如用户认证、消息推送等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值