Skip to content

Commit

Permalink
增加照片导入功能
Browse files Browse the repository at this point in the history
  • Loading branch information
vitozyf committed Dec 30, 2019
1 parent fea180b commit b5a6a2b
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ yarn-error.log*
*.ntvs*
*.njsproj
*.sln
*.sw?
*.sw?
config.js
68 changes: 54 additions & 14 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}"
>
{{ item.name ? item.name : item.key }}
<img v-if="item.photo" :src="item.photo" :width="50" :height="50" />
</a>
</li>
</ul>
Expand All @@ -34,26 +35,17 @@
:key="item"
class="itemres"
:style="resCardStyle"
:data-id="item"
@click="showRes = false"
>
<span
class="key"
:style="{
fontSize: list[item - 1] && list[item - 1].name ? '36px' : null,
lineHeight:
list[item - 1] && list[item - 1].name ? '80px' : null
}"
v-if="list[item - 1] && list[item - 1].name"
>
{{ item }}
</span>
<span
class="cont"
:style="{
fontSize: list[item - 1] && list[item - 1].name ? '36px' : null,
lineHeight:
list[item - 1] && list[item - 1].name ? '80px' : null
}"
v-if="!photos.find(d => d.id === item)"
>
<span v-if="list[item - 1] && list[item - 1].name">
{{ list[item - 1].name }}
Expand All @@ -62,6 +54,13 @@
{{ item }}
</span>
</span>
<img
v-if="photos.find(d => d.id === item)"
:src="photos.find(d => d.id === item).value"
alt="photo"
:width="160"
:height="160"
/>
</span>
</div>
</div>
Expand All @@ -71,6 +70,7 @@
<Tool
@toggle="toggle"
@resetConfig="reloadTagCanvas"
@getPhoto="getPhoto"
:running="running"
:closeRes="closeRes"
/>
Expand All @@ -95,6 +95,7 @@ import {
} from '@/helper/index';
import { luckydrawHandler } from '@/helper/algorithm';
import Result from '@/components/Result';
import { database, DB_STORE_NAME } from '@/helper/db';
export default {
name: 'App',
Expand Down Expand Up @@ -143,15 +144,20 @@ export default {
const datas = [];
for (let index = 1; index <= this.config.number; index++) {
const listData = this.list.find(d => d.key === index);
const photo = this.photos.find(d => d.id === index);
datas.push({
key: index,
name: listData ? listData.name : ''
name: listData ? listData.name : '',
photo: photo ? photo.value : ''
});
}
return datas;
},
categoryName() {
return conversionCategoryName(this.category);
},
photos() {
return this.$store.state.photos;
}
},
created() {
Expand Down Expand Up @@ -192,10 +198,30 @@ export default {
category: ''
};
},
watch: {
photos: {
deep: true,
handler() {
this.$nextTick(() => {
this.reloadTagCanvas();
});
}
}
},
mounted() {
this.startTagCanvas();
setTimeout(() => {
this.getPhoto();
}, 1000);
},
methods: {
getPhoto() {
database.getAll(DB_STORE_NAME).then(res => {
if (res && res.length > 0) {
this.$store.commit('setPhotos', res);
}
});
},
speed() {
return [0.1 * Math.random() + 0.01, -(0.1 * Math.random() + 0.01)];
},
Expand Down Expand Up @@ -323,7 +349,7 @@ export default {
position: absolute;
top: 50%;
left: 50%;
width: 1000px;
width: 1280px;
transform: translateX(-50%) translateY(-50%);
text-align: center;
p {
Expand All @@ -345,15 +371,29 @@ export default {
line-height: 160px;
font-weight: bold;
margin-right: 20px;
margin-top: 20px;
margin-top: 70px;
cursor: pointer;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
.key {
color: red;
}
&::before {
content: attr(data-id);
width: 70px;
height: 50px;
background-color: #fff;
position: absolute;
top: -50px;
left: 50%;
transform: translateX(-50%);
font-size: 30px;
line-height: 50px;
border-radius: 50%;
}
}
}
</style>
200 changes: 200 additions & 0 deletions src/components/Importphoto.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<template>
<el-dialog
:visible="visible"
:append-to-body="true"
width="300px"
@close="$emit('update:visible', false)"
class="c-Importphoto"
>
<el-row>
<label for="idinput">抽奖号码</label>
<el-input
id="idinput"
size="mini"
type="number"
v-model="id"
:min="0"
:max="config.number"
></el-input>
</el-row>
<el-row>
<label for="idinput">照片选择</label>
<span class="selectbg" :data-tip="filename">
<input
ref="uploadinput"
class="upload"
type="file"
accept=".jpg,.png"
@change="inputChange"
/>
</span>
</el-row>
<el-row class="photo">
<label>已选照片</label>
<img v-if="value" :src="value" alt="img" :width="140" :height="140" />
<span v-else>暂未选择</span>
</el-row>
<el-row class="center">
<el-button size="mini" type="primary" @click="saveHandler"
>保存</el-button
>
<el-button size="mini" @click="$emit('update:visible', false)"
>取消</el-button
>
</el-row>
</el-dialog>
</template>
<script>
import { database, DB_STORE_NAME } from '@/helper/db';
export default {
name: 'Importphoto',
props: {
visible: Boolean
},
computed: {
config: {
get() {
return this.$store.state.config;
}
}
},
data() {
return {
id: 0,
value: '',
filename: '点击选择照片'
};
},
methods: {
inputChange(e) {
const fileList = e.target.files;
const formData = new FormData();
formData.append('uploadImg', fileList[0]);
const reader = new FileReader();
const AllowImgFileSize = 1024 * 1024;
const file = fileList[0];
if (file) {
this.filename = file.name;
reader.readAsDataURL(file);
reader.onload = () => {
if (
AllowImgFileSize != 0 &&
AllowImgFileSize < reader.result.length
) {
return this.$message.error('不允许上传大于1M的图片');
} else {
this.value = reader.result;
}
};
}
},
async saveHandler() {
const { id, value } = this;
const ID = Number(id);
if (!ID || ID <= 0) {
return this.$message.error('号码必须大于0的整数');
}
if (!value) {
return this.$message.error('请选择照片');
}
const Data = await database.get(DB_STORE_NAME, ID);
const param = {
id: ID,
value
};
database[Data ? 'edit' : 'add'](
DB_STORE_NAME,
Data ? ID : param,
Data ? param : null
)
.then(res => {
if (res) {
this.$refs.uploadinput.value = '';
this.value = '';
this.filename = '点击选择照片';
this.$emit('update:visible', false);
this.$emit('getPhoto');
this.$message({
message: '保存成功',
type: 'success'
});
} else {
this.$message.error('保存失败');
}
})
.catch(err => {
this.$message.error(err.message);
});
}
}
};
</script>
<style lang="scss">
.c-Importphoto {
.el-dialog {
height: 330px;
}
label {
margin-right: 20px;
vertical-align: top;
}
.el-input {
width: 140px;
}
.el-row {
padding: 5px 0;
}
.center {
text-align: center;
}
.selectbg {
display: inline-block;
border: 1px solid #ccc;
border-radius: 2px;
width: 140px;
height: 28px;
position: relative;
box-sizing: border-box;
&::before {
content: attr(data-tip);
width: 100%;
height: 100%;
text-align: center;
position: absolute;
top: 0;
left: 0;
line-height: 28px;
cursor: pointer;
overflow: hidden;
font-size: 12px;
}
.upload {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 10;
cursor: pointer;
}
}
.photo {
img {
border: 1px solid #ccc;
}
span {
display: inline-block;
border: 1px solid #ccc;
border-radius: 2px;
width: 140px;
height: 140px;
line-height: 140px;
text-align: center;
position: relative;
box-sizing: border-box;
}
}
}
</style>
Loading

0 comments on commit b5a6a2b

Please sign in to comment.