1.添加商品
A.完成圖片上傳
使用upload組件完成圖片上傳
在element.js中引入upload組件,并注冊
因?yàn)閡pload組件進(jìn)行圖片上傳的時候并不是使用axios發(fā)送請求
所以,我們需要手動為上傳圖片的請求添加token,即為upload組件添加headers屬性
//在頁面中添加upload組件,并設(shè)置對應(yīng)的事件和屬性
<el-tab-pane label="商品圖片" name="3">
<!-- 商品圖片上傳
action:指定圖片上傳api接口
:on-preview : 當(dāng)點(diǎn)擊圖片時會觸發(fā)該事件進(jìn)行預(yù)覽操作,處理圖片預(yù)覽
:on-remove : 當(dāng)用戶點(diǎn)擊圖片右上角的X號時觸發(fā)執(zhí)行
:on-success:當(dāng)用戶點(diǎn)擊上傳圖片并成功上傳時觸發(fā)
list-type :設(shè)置預(yù)覽圖片的方式
:headers :設(shè)置上傳圖片的請求頭 -->
<el-upload :action="uploadURL" :on-preview="handlePreview" :on-remove="handleRemove" :on-success="handleSuccess" list-type="picture" :headers="headerObj">
<el-button size="small" type="primary">點(diǎn)擊上傳</el-button>
</el-upload>
</el-tab-pane>
//在el-card卡片視圖下面添加對話框用來預(yù)覽圖片
<!-- 預(yù)覽圖片對話框 -->
<el-dialog title="圖片預(yù)覽" :visible.sync="previewVisible" width="50%">
<img :src="previewPath" class="previewImg" />
</el-dialog>
//在data中添加數(shù)據(jù)
data(){
return {
......
//添加商品的表單數(shù)據(jù)對象
addForm: {
goods_name: '',
goods_price: 0,
goods_weight: 0,
goods_number: 0,
goods_cat: [],
//上傳圖片數(shù)組
pics: []
},
//上傳圖片的url地址
uploadURL: 'http://127.0.0.1:8888/api/private/v1/upload',
//圖片上傳組件的headers請求頭對象
headerObj: { Authorization: window.sessionStorage.getItem('token') },
//保存預(yù)覽圖片的url地址
previewPath: '',
//控制預(yù)覽圖片對話框的顯示和隱藏
previewVisible:false
}
},
//在methods中添加事件處理函數(shù)
methods:{
.......
handlePreview(file) {
//當(dāng)用戶點(diǎn)擊圖片進(jìn)行預(yù)覽時執(zhí)行,處理圖片預(yù)覽
//形參file就是用戶預(yù)覽的那個文件
this.previewPath = file.response.data.url
//顯示預(yù)覽圖片對話框
this.previewVisible = true
},
handleRemove(file) {
//當(dāng)用戶點(diǎn)擊X號刪除時執(zhí)行
//形參file就是用戶點(diǎn)擊刪除的文件
//獲取用戶點(diǎn)擊刪除的那個圖片的臨時路徑
const filePath = file.response.data.tmp_path
//使用findIndex來查找符合條件的索引
const index = this.addForm.pics.findIndex(item => item.pic === filePath)
//移除索引對應(yīng)的圖片
this.addForm.pics.splice(index, 1)
},
handleSuccess(response) {
//當(dāng)上傳成功時觸發(fā)執(zhí)行
//形參response就是上傳成功之后服務(wù)器返回的結(jié)果
//將服務(wù)器返回的臨時路徑保存到addForm表單的pics數(shù)組中
this.addForm.pics.push({ pic: response.data.tmp_path })
}
}
B.使用富文本插件
依賴項(xiàng)->安裝依賴項(xiàng)->運(yùn)行依賴 ( vue-quill-editor )
想要使用富文本插件vue-quill-editor,就必須先從依賴安裝該插件
引入并注冊vue-quill-editor,打開main.js,編寫如下代碼
//導(dǎo)入vue-quill-editor(富文本編輯器)
import VueQuillEditor from 'vue-quill-editor'
//導(dǎo)入vue-quill-editor的樣式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
......
//全局注冊組件
Vue.component('tree-table', TreeTable)
//全局注冊富文本組件
Vue.use(VueQuillEditor)
使用富文本插件vue-quill-editor
<!-- 富文本編輯器組件 -->
<el-tab-pane label="商品內(nèi)容" name="4">
<!-- 富文本編輯器組件 -->
<quill-editor v-model="addForm.goods_introduce"></quill-editor>
<!-- 添加商品按鈕 -->
<el-button type="primary" class="btnAdd">添加商品</el-button>
</el-tab-pane>
//在數(shù)據(jù)中添加goods_introduce
//添加商品的表單數(shù)據(jù)對象
addForm: {
goods_name: '',
goods_price: 0,
goods_weight: 0,
goods_number: 0,
goods_cat: [],
//上傳圖片數(shù)組
pics: [],
//商品的詳情介紹
goods_introduce:''
}
//在global.css樣式中添加富文本編輯器的最小高度
.ql-editor{
min-height: 300px;
}
//給添加商品按鈕添加間距
.btnAdd{
margin-top:15px;
}
C.添加商品
完成添加商品的操作
在添加商品之前,為了避免goods_cat數(shù)組轉(zhuǎn)換字符串之后導(dǎo)致級聯(lián)選擇器報(bào)錯
我們需要打開vue控制條,點(diǎn)擊依賴,安裝lodash,把a(bǔ)ddForm進(jìn)行深拷貝
//打開Add.vue,導(dǎo)入lodash
<script>
//官方推薦將lodash導(dǎo)入為_
import _ from 'lodash'
//給添加商品按鈕綁定點(diǎn)擊事件
<!-- 添加商品按鈕 -->
<el-button type="primary" class="btnAdd" @click="add">添加商品</el-button>
//編寫點(diǎn)擊事件完成商品添加
add(){
this.$refs.addFormRef.validate(async valid=>{
if(!valid) return this.$message.error("請?zhí)顚懕匾谋韱雾?xiàng)!")
//將addForm進(jìn)行深拷貝,避免goods_cat數(shù)組轉(zhuǎn)換字符串之后導(dǎo)致級聯(lián)選擇器報(bào)錯
const form = _.cloneDeep(this.addForm)
//將goods_cat從數(shù)組轉(zhuǎn)換為"1,2,3"字符串形式
form.goods_cat = form.goods_cat.join(",")
//處理attrs數(shù)組,數(shù)組中需要包含商品的動態(tài)參數(shù)和靜態(tài)屬性
//將manyTableData(動態(tài)參數(shù))處理添加到attrs
this.manyTableData.forEach(item=>{
form.attrs.push({ attr_id:item.attr_id, attr_value:item.attr_vals.join(" ") })
})
//將onlyTableData(靜態(tài)屬性)處理添加到attrs
this.onlyTableData.forEach(item=>{
form.attrs.push({ attr_id:item.attr_id, attr_value:item.attr_vals })
})
//發(fā)送請求完成商品的添加,商品名稱必須是唯一的
const {data:res} = await this.$http.post('goods',form)
if(res.meta.status !== 201){
return this.$message.error('添加商品失敗')
}
this.$message.success('添加商品成功')
//編程式導(dǎo)航跳轉(zhuǎn)到商品列表
this.$router.push('/goods')
})
}
</script>
D.推送代碼
推送goods_list分支到碼云
將代碼添加到暫存區(qū): git add .
將代碼提交到本地倉庫: git commit -m "完成商品功能開發(fā)"
將代碼推送到碼云: git push
切換到master主分支: git checkout master
將goods_list分支代碼合并到master: git merge goods_list
將master推送到碼云: git push
2.訂單列表
A.創(chuàng)建分支
創(chuàng)建order子分支并推送到碼云
創(chuàng)建order子分支: git checkout -b order
將order分支推送到碼云: git push -u origin order
B.創(chuàng)建路由
創(chuàng)建訂單列表路由組件并添加路由規(guī)則
//在components中新建order文件夾,新建Order.vue組件,組件中添加代碼如下
<template>
<div>
<h3>訂單列表</h3>
<!-- 面包屑導(dǎo)航 -->
<el-breadcrumb separator="/">
<el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
<el-breadcrumb-item>訂單管理</el-breadcrumb-item>
<el-breadcrumb-item>訂單列表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片視圖區(qū)域 -->
<el-card>
<!-- 搜索欄 -->
<el-row :gutter="20">
<el-col :span="8">
<el-input placeholder="請輸入內(nèi)容" v-model="queryInfo.query" clearable>
<el-button slot="append" icon="el-icon-search" ></el-button>
</el-input>
</el-col>
</el-row>
</el-card>
</div>
</template>
<script>
export default {
data() {
return {
//查詢條件
queryInfo:{
query:'',
pagenum:1,
pagesize:10
}
}
},
created() {
},
methods: {
}
}
</script>
<style lang="less" scoped>
</style>
//打開router.js導(dǎo)入Order.vue并添加規(guī)則
import Order from './components/order/Order.vue'
path: '/home', component: Home, redirect: '/welcome', children: [
{ path: "/welcome", component: Welcome },
{ path: "/users", component: Users },
{ path: "/rights", component: Rights },
{ path: "/roles", component: Roles },
{ path: "/categories", component: Cate },
{ path: "/params", component: Params },
{ path: "/goods", component: GoodList },
{ path: "/goods/add", component: GoodAdd },
{ path: "/orders", component: Order }
]
C.實(shí)現(xiàn)數(shù)據(jù)展示及分頁
<!-- 卡片視圖區(qū)域 -->
<el-card>
<!-- 搜索欄 -->
<el-row :gutter="20">
<el-col :span="8">
<el-input placeholder="請輸入內(nèi)容" v-model="queryInfo.query" clearable>
<el-button slot="append" icon="el-icon-search"></el-button>
</el-input>
</el-col>
</el-row>
<!-- 訂單表格 -->
<el-table :data="orderList" border stripe>
<el-table-column type="index"></el-table-column>
<el-table-column label="訂單編號" prop="order_number"></el-table-column>
<el-table-column label="訂單價(jià)格" prop="order_price"></el-table-column>
<el-table-column label="是否付款" prop="pay_status">
<template slot-scope="scope">
<el-tag type="success" v-if="scope.row.pay_status === '1'">已付款</el-tag>
<el-tag type="danger" v-else>未付款</el-tag>
</template>
</el-table-column>
<el-table-column label="是否發(fā)貨" prop="is_send"></el-table-column>
<el-table-column label="下單時間" prop="create_time">
<template slot-scope="scope">
{{scope.row.create_time | dateFormat}}
</template>
</el-table-column>
<el-table-column label="操作" width="125px">
<template slot-scope="scope">
<el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
<el-button size="mini" type="success" icon="el-icon-location"></el-button>
</template>
</el-table-column>
</el-table>
<!-- 分頁 -->
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
</el-pagination>
</el-card>
<script>
export default {
data() {
return {
//查詢條件
queryInfo: {
query: '',
pagenum: 1,
pagesize: 10
},
//訂單列表數(shù)據(jù)
orderList: [],
//數(shù)據(jù)總條數(shù)
total: 0
}
},
created() {
this.getOrderList()
},
methods: {
async getOrderList() {
const { data: res } = await this.$http.get('orders', {
params: this.queryInfo
})
if (res.meta.status !== 200) {
return this.$message.error('獲取訂單列表數(shù)據(jù)失敗!')
}
this.total = res.data.total
this.orderList = res.data.goods
},
handleSizeChange(newSize){
this.queryInfo.pagesize = newSize
this.getOrderList()
},
handleCurrentChange(newPage){
this.queryInfo.pagenum = newPage
this.getOrderList()
}
}
}
</script>
D.制作省市區(qū)縣聯(lián)動
打開今天的資料,找到素材文件夾,復(fù)制citydata.js文件到components/order文件夾中
然后導(dǎo)入citydata.js文件
<script>
import cityData from "./citydata.js"
</script>
具體代碼如下:
//給修改地址按鈕添加點(diǎn)擊事件
<el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditAddress"></el-button>
//添加修改地址對話框,在卡片視圖下方添加
<!-- 修改地址對話框 -->
<el-dialog title="修改收貨地址" :visible.sync="addressVisible" width="50%" @close="addressDialogClosed">
<!-- 添加表單 -->
<el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
<el-form-item label="省市區(qū)縣" prop="address1">
<el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
</el-form-item>
<el-form-item label="詳細(xì)地址" prop="address2">
<el-input v-model="addressForm.address2"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="addressVisible = false">取 消</el-button>
<el-button type="primary" @click="addressVisible = false">確 定</el-button>
</span>
</el-dialog>
//js部分的代碼
<script>
import cityData from "./citydata.js"
export default {
data() {
return {
......
//控制修改地址對話框的顯示和隱藏
addressVisible:false,
//修改收貨地址的表單
addressForm:{
address1:[],
address2:''
},
addressFormRules:{
address1:[{ required: true, message: '請選擇省市區(qū)縣', trigger: 'blur' }],
address2:[{ required: true, message: '請輸入詳細(xì)地址', trigger: 'blur' }],
},
//將導(dǎo)入的cityData數(shù)據(jù)保存起來
cityData:cityData
}
},methods: {
......
showEditAddress() {
//當(dāng)用戶點(diǎn)擊修改收貨地址按鈕時觸發(fā)
this.addressVisible = true;
},
addressDialogClosed(){
this.$refs.addressFormRef.resetFields()
}
}
}
</script>
<style lang="less" scoped>
.el-cascader{
width: 100%;
}
</style>
E.制作物流進(jìn)度對話框
因?yàn)槲覀兪褂玫氖莈lement-ui中提供的Timeline組件,所以需要導(dǎo)入并注冊組件
打開element.js,編寫代碼會進(jìn)行導(dǎo)入和注冊
import {
Timeline,TimelineItem
} from 'element-ui'
Vue.use(Timeline)
Vue.use(TimelineItem)
打開Order.vue文件,添加代碼實(shí)現(xiàn)物流進(jìn)度對話框
<!-- 物流信息進(jìn)度對話框 -->
<el-dialog title="物流進(jìn)度" :visible.sync="progressVisible" width="50%">
<!-- 時間線組件 -->
<el-timeline>
<el-timeline-item v-for="(activity, index) in progressInfo"
:key="index" :timestamp="activity.time">
{{activity.context}}
</el-timeline-item>
</el-timeline>
</el-dialog>
<script>
import cityData from './citydata.js'
export default {
data() {
return {
......
//控制物流進(jìn)度對話框的顯示和隱藏
progressVisible: false,
//保存物流信息
progressInfo: []
}
},methods: {
......
async showProgress() {
//發(fā)送請求獲取物流數(shù)據(jù)
const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
if (res.meta.status !== 200) {
return this.$message.error('獲取物流進(jìn)度失敗!')
}
this.progressInfo = res.data
//顯示對話框
this.progressVisible = true
}
}
}
</script>
F.推送代碼
將order分支代碼推送至碼云
將代碼添加到暫存區(qū): git add .
將代碼提交到本地倉庫: git commit -m "完成訂單列表功能開發(fā)"
將代碼推送到碼云: git push
切換到master主分支: git checkout master
將goods_list分支代碼合并到master: git merge order
將master推送到碼云: git push
3.數(shù)據(jù)統(tǒng)計(jì)
A.創(chuàng)建子分支
創(chuàng)建report子分支并推送到碼云
創(chuàng)建report子分支: git checkout -b report
將report分支推送到碼云: git push -u origin report
B.創(chuàng)建路由
創(chuàng)建數(shù)據(jù)統(tǒng)計(jì)路由組件并添加路由規(guī)則
//在components中新建report文件夾,新建Report.vue組件,組件中添加代碼如下
<template>
<div>
<h3>數(shù)據(jù)報(bào)表</h3>
<!-- 面包屑導(dǎo)航 -->
<el-breadcrumb separator="/">
<el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
<el-breadcrumb-item>數(shù)據(jù)統(tǒng)計(jì)</el-breadcrumb-item>
<el-breadcrumb-item>數(shù)據(jù)報(bào)表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片視圖區(qū)域 -->
<el-card></el-card>
</div>
</template>
<script>
export default {
data() {
return {
}
},created(){
},methods:{
}
}
</script>
<style lang="less" scoped>
</style>
打開router.js,導(dǎo)入Report.vue并設(shè)置路由規(guī)則
import Report from './components/report/Report.vue'
path: '/home', component: Home, redirect: '/welcome', children: [
{ path: "/welcome", component: Welcome },
{ path: "/users", component: Users },
{ path: "/rights", component: Rights },
{ path: "/roles", component: Roles },
{ path: "/categories", component: Cate },
{ path: "/params", component: Params },
{ path: "/goods", component: GoodList },
{ path: "/goods/add", component: GoodAdd },
{ path: "/orders", component: Order },
{ path: "/reports", component: Report }
]
C.導(dǎo)入ECharts并使用
<template>
<div>
<h3>數(shù)據(jù)報(bào)表</h3>
<!-- 面包屑導(dǎo)航 -->
<el-breadcrumb separator="/">
<el-breadcrumb-item :to="{ path: '/home' }">首頁</el-breadcrumb-item>
<el-breadcrumb-item>數(shù)據(jù)統(tǒng)計(jì)</el-breadcrumb-item>
<el-breadcrumb-item>數(shù)據(jù)報(bào)表</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片視圖區(qū)域 -->
<el-card>
<div id="main" style="width:750px;height:400px;"></div>
</el-card>
</div>
</template>
<script>
//導(dǎo)入echarts
import echarts from 'echarts'
//導(dǎo)入lodash
import _ from 'lodash'
export default {
data() {
return {
//需要跟請求的折線圖數(shù)據(jù)合并的options
options: {
title: {
text: '用戶來源'
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#E9EEF3'
}
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
boundaryGap: false
}
],
yAxis: [
{
type: 'value'
}
]
}
}
},
created() {},
async mounted() {
//在頁面dom元素加載完畢之后執(zhí)行的鉤子函數(shù)mounted
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main'))
//準(zhǔn)備數(shù)據(jù)和配置項(xiàng)
//發(fā)送請求獲取折線圖數(shù)據(jù)
const { data: res } = await this.$http.get('reports/type/1')
if (res.meta.status !== 200) {
return this.$message.error('獲取折線圖數(shù)據(jù)失敗')
}
//合并res.data和this.options
const result = _.merge(res.data,this.options)
// 使用獲取的數(shù)據(jù)展示圖表
myChart.setOption(result)
},
methods: {}
}
</script>
<style lang="less" scoped>
</style>