食谱导出功能

This commit is contained in:
huangdeliang 2021-03-26 16:09:27 +08:00
parent 6fe2d84e5e
commit 4ce5f3f7da
3 changed files with 67 additions and 3 deletions

View File

@ -50,6 +50,7 @@
"element-ui": "2.13.2", "element-ui": "2.13.2",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"fuse.js": "3.4.4", "fuse.js": "3.4.4",
"html2canvas": "^1.0.0-rc.7",
"immer": "^8.0.1", "immer": "^8.0.1",
"js-beautify": "1.10.2", "js-beautify": "1.10.2",
"js-cookie": "2.2.0", "js-cookie": "2.2.0",

View File

@ -19,6 +19,17 @@
> >
另存为模板 另存为模板
</el-button> </el-button>
<el-button
size="mini"
type="primary"
style="margin-left: 12px"
icon="el-icon-download"
@click="handleOnExportImg"
v-loading="downloading"
:disabled="downloading"
>
导出图片
</el-button>
</section> </section>
<section> <section>
<span class="font_size_style"> <span class="font_size_style">
@ -96,6 +107,7 @@ const { mapActions, mapState, mapMutations } = createNamespacedHelpers(
"recipes" "recipes"
); );
import TemplateDialog from "@/components/TemplateDialog"; import TemplateDialog from "@/components/TemplateDialog";
import html2canvans from "html2canvas";
export default { export default {
name: "RecipesHeaderCom", name: "RecipesHeaderCom",
components: { components: {
@ -104,6 +116,7 @@ export default {
data() { data() {
return { return {
loading: false, loading: false,
downloading: false,
nFontSize: 0, nFontSize: 0,
fontSizeOpts: [ fontSizeOpts: [
{ value: 8, label: "8" }, { value: 8, label: "8" },
@ -117,7 +130,14 @@ export default {
}, },
updated() {}, updated() {},
computed: { computed: {
...mapState(["recipesId", "reviewStatus", "fontSize", "leftShow"]), ...mapState([
"recipesId",
"reviewStatus",
"fontSize",
"leftShow",
"healthyData",
"recipesData",
]),
}, },
watch: {}, watch: {},
methods: { methods: {
@ -193,12 +213,46 @@ export default {
return "info"; return "info";
} }
}, },
handleOnExportImg() {
this.downloading = true;
this.$nextTick(() => {
const centerContentDom = document.getElementById("center_content");
if (!centerContentDom) {
this.downloading = false;
return;
}
centerContentDom.style.overflow = "visible";
const recipesDom = document.getElementById("recipes_content");
if (!recipesDom) {
this.downloading = false;
return;
}
recipesDom.style.overflow = "visible";
html2canvans(recipesDom, {
scale: 1.5,
height: recipesDom.scrollHeight,
}).then((canvas) => {
const { name } = this.healthyData;
const startNum = this.recipesData[0].numDay;
const endNum = this.recipesData[this.recipesData.length - 1].numDay;
const link = document.createElement("a");
link.download = `${name}_第${startNum}${endNum}天.jpeg`;
link.href = canvas.toDataURL();
link.click();
centerContentDom.style.overflow = "auto";
recipesDom.style.overflow = "auto";
this.downloading = false;
});
});
},
...mapActions(["saveRecipes", "updateReviewStatus"]), ...mapActions(["saveRecipes", "updateReviewStatus"]),
...mapMutations(["updateStateData", "updateFontSize", "toggleLeftShow"]), ...mapMutations(["updateStateData", "updateFontSize", "toggleLeftShow"]),
}, },
}; };
</script> </script>
<style rel="stylesheet/scss" lang="scss" scope> <style lang="scss" scoped>
.recipes_header_com_wrapper { .recipes_header_com_wrapper {
.header_btns { .header_btns {
display: flex; display: flex;
@ -219,5 +273,14 @@ export default {
font-size: 12px; font-size: 12px;
margin-right: 12px; margin-right: 12px;
} }
/deep/ .el-loading-spinner {
margin-top: -10px;
}
/deep/.el-loading-spinner .circular {
width: 20px;
height: 20px;
}
} }
</style> </style>

View File

@ -8,7 +8,7 @@
<VerifyView /> <VerifyView />
</div> </div>
</div> </div>
<div class="content" v-loading="recipesDataLoading"> <div class="content" v-loading="recipesDataLoading" id="center_content">
<RecipesView <RecipesView
v-if="!!recipesData.length" v-if="!!recipesData.length"
:data="recipesData" :data="recipesData"