|
|
<template><view id="shareImg" v-if="init"> <view class="img-box"> <image mode="widthFix" :src="info.url" alt></image> </view> <view class="btn-box"> <view class="btn" @tap="downImg">保存海报</view> </view> <!--用户拒绝下载图片授权弹出--> <alert :is_refused="is_refused" bind:close="closeAlert"></alert></view></template><script>import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';import alert from "@/components/alert/alert";
export default { data() { return { info: '', id: '', init: false, channel: '', is_refused: false }; },
onLoad(e) { this.setData({ id: e.id, channel: e.channel }); this.getShearImg(); },
components: { alert }, props: {}, methods: { getShearImg() { wx.showLoading({ title: '加载中', mask: true }); var token = this.$cookieStorage.get('user_token'); var pages;
if (this.channel == 'ec') { pages = 'pages/coupon/onDetail/onDetail'; } else { pages = 'pages/coupon/offDetail/offDetail'; }
this.$http.post({ api: 'api/coupon/share/agent/image', data: { coupon_id: this.id, pages: pages }, header: { Authorization: token } }).then(res => { if (res.statusCode == 200) { res = res.data;
if (res.status) { this.setData({ info: res.data, init: true }); } else { wx.showModal({ content: res.message || '请求失败', showCancel: false }); } } else { wx.showModal({ content: '请求失败', showCancel: false }); }
wx.hideLoading(); }).catch(() => { wx.hideLoading(); wx.showModal({ content: '请求失败', showCancel: false }); }); },
closeAlert() { this.setData({ is_refused: false }); },
// 下载图片
downImg() { if (this.info.url) { wx.showLoading({ title: '正在下载', mask: true }); this.$http.dowloadFile({ api: this.info.url }).then(res => { if (res.statusCode == 200) { wx.getSetting({ success: ret => { // 如果之前没有授权
if (!ret.authSetting['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success: rej => { this.saveImg(res.tempFilePath); }, // 用户拒绝授权
fail: ret => { this.setData({ is_refused: true }); wx.hideLoading(); } }); } else { this.saveImg(res.tempFilePath); } } }); } else { wx.hideLoading(); wx.showToast({ title: '下载图片失败', icon: 'none' }); } }, err => {}); } },
// 保存图片
saveImg(path) { wx.saveImageToPhotosAlbum({ filePath: path, success: res => { wx.hideLoading(); }, fail: rej => { wx.hideLoading(); wx.showToast({ title: '保存图片失败', icon: 'none' }); } }); },
setData: function (obj) { let that = this; let keys = []; let val, data; Object.keys(obj).forEach(function (key) { keys = key.split('.'); val = obj[key]; data = that.$data; keys.forEach(function (key2, index) { if (index + 1 == keys.length) { that.$set(data, key2, val); } else { if (!data[key2]) { that.$set(data, key2, {}); } }
data = data[key2]; }); }); } }, computed: {}, watch: {}};</script><style rel="stylesheet/less" lang="less"> @import "shareImg";</style>
|