index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="posterCon" :style="colorStyle">
  3. <view class='poster-poster'>
  4. <view class='tip'><text class='iconfont icon-shuoming'></text>提示:点击图片即可保存至手机相册 </view>
  5. <view class='pictrue'>
  6. <!-- <image :src='image' mode="widthFix"></image> -->
  7. <image class="canvas" :src="posterImage" v-if="posterImage" @click="savePosterPathMp(posterImage)">
  8. </image>
  9. <canvas class="canvas" canvas-id="myCanvas" v-else></canvas>
  10. </view>
  11. </view>
  12. <!-- #ifdef H5 || APP-PLUS -->
  13. <zb-code ref="qrcode" :show="codeShow" :cid="cid" :val="val" :size="size" :unit="unit" :background="background"
  14. :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconsize" :onval="onval"
  15. :loadMake="loadMake" @result="qrR" />
  16. <!-- #endif -->
  17. </view>
  18. </template>
  19. <script>
  20. import zbCode from '@/components/zb-code/zb-code.vue'
  21. import {
  22. getBargainPoster,
  23. getCombinationPoster,
  24. getBargainPosterData,
  25. getCombinationPosterData
  26. } from '@/api/activity.js';
  27. import {
  28. getUserInfo,
  29. imgToBase,
  30. routineCode
  31. } from '@/api/user.js';
  32. // #ifdef APP-PLUS
  33. import {
  34. TOKENNAME,
  35. HTTP_REQUEST_URL
  36. } from '@/config/app.js';
  37. // #endif
  38. import colors from '@/mixins/color.js'
  39. export default {
  40. components: {
  41. zbCode
  42. },
  43. mixins:[colors],
  44. data() {
  45. return {
  46. canvasStatus: true,
  47. posterImage: "",
  48. parameter: {
  49. 'navbar': '1',
  50. 'return': '1',
  51. 'title': '拼团海报',
  52. 'color': true,
  53. 'class': '0'
  54. },
  55. type: 0,
  56. id: 0,
  57. spid: 0,
  58. image: '',
  59. from: '',
  60. uid: "",
  61. //二维码参数
  62. codeShow: false,
  63. cid: '1',
  64. ifShow: true,
  65. val: "", // 要生成的二维码值
  66. size: 200, // 二维码大小
  67. unit: 'upx', // 单位
  68. background: '#FFF', // 背景色
  69. foreground: '#000', // 前景色
  70. pdground: '#000', // 角标色
  71. icon: '', // 二维码图标
  72. iconsize: 40, // 二维码图标大小
  73. lv: 3, // 二维码容错级别 , 一般不用设置,默认就行
  74. onval: true, // val值变化时自动重新生成二维码
  75. loadMake: true, // 组件加载完成后自动生成二维码
  76. src: '', // 二维码生成后的图片地址或base64
  77. codeSrc: "",
  78. wd: 0,
  79. hg: 0,
  80. posterBag: "../static/posterBag.png",
  81. mpUrl: "",
  82. }
  83. },
  84. onLoad(options) {
  85. // #ifdef MP
  86. this.from = 'routine'
  87. // #endif
  88. // #ifdef H5 || APP-PLUS
  89. this.from = 'wechat'
  90. // #endif
  91. var that = this;
  92. if (options.hasOwnProperty('type') && options.hasOwnProperty('id')) {
  93. this.type = options.type
  94. this.id = options.id
  95. if (options.type == 1) {
  96. this.spid = options.spid
  97. uni.setNavigationBarTitle({
  98. title: '砍价海报'
  99. })
  100. } else {
  101. uni.setNavigationBarTitle({
  102. title: '拼团海报'
  103. })
  104. }
  105. } else {
  106. return app.Tips({
  107. title: '参数错误',
  108. icon: 'none'
  109. }, {
  110. tab: 3,
  111. url: 1
  112. });
  113. }
  114. },
  115. onReady() {
  116. // #ifdef H5
  117. if (this.type == 1) {
  118. this.val = window.location.origin + '/pages/activity/goods_bargain_details/index?id=' + this.id +
  119. '&spid=' +
  120. this.$store.state.app.uid
  121. } else if (this.type == 2) {
  122. this.val = window.location.origin + '/pages/activity/goods_combination_status/index?id=' + this.id +
  123. '&spid=' +
  124. this.$store.state.app.uid
  125. }
  126. // #endif
  127. // #ifdef APP-PLUS
  128. if (this.type == 1) {
  129. this.val = HTTP_REQUEST_URL + '/pages/activity/goods_bargain_details/index?id=' + this.id + '&spid=' +
  130. this.$store.state.app.uid
  131. } else if (this.type == 2) {
  132. this.val = HTTP_REQUEST_URL + '/pages/activity/goods_combination_status/index?id=' + this.id +
  133. '&spid=' +
  134. this.$store.state.app.uid
  135. }
  136. // #endif
  137. setTimeout(e => {
  138. this.getPosterInfo();
  139. }, 200)
  140. this.$nextTick(function() {
  141. let selector = uni.createSelectorQuery().select('.pictrue');
  142. selector.fields({
  143. size: true
  144. }, data => {
  145. this.wd = data.width
  146. this.hg = data.height
  147. }).exec();
  148. })
  149. // this.routineCode()
  150. },
  151. onShow(){
  152. uni.removeStorageSync('form_type_cart');
  153. },
  154. methods: {
  155. async getPosterInfo() {
  156. var that = this,
  157. url = '';
  158. let data = {
  159. id: that.id,
  160. 'from': that.from
  161. };
  162. let userData = await getUserInfo()
  163. this.uid = userData.data.uid
  164. let goods_img, mp_code, resData, arr, mpUrl
  165. // #ifdef MP
  166. // #endif
  167. uni.showLoading({
  168. title: '海报生成中',
  169. mask: true
  170. });
  171. let flag = false;
  172. if (that.type == 1) {
  173. await getBargainPosterData(that.id).then(res => {
  174. resData = res.data
  175. }).catch(err => {
  176. flag = true;
  177. that.$util.Tips({
  178. title: err
  179. },'/pages/activity/goods_bargain/index');
  180. })
  181. } else {
  182. await getCombinationPosterData(that.id).then(res => {
  183. resData = res.data
  184. }).catch(err => {
  185. flag = true;
  186. that.$util.Tips({
  187. title: '海报图片获取失败'
  188. });
  189. })
  190. }
  191. if(flag) return false;
  192. // #ifdef H5 || APP-PLUS
  193. goods_img = await this.imgToBase(resData.image)
  194. mp_code = await resData.url?this.imgToBase(resData.url):''
  195. arr = [this.posterBag, goods_img, mp_code || this.codeSrc]
  196. // #endif
  197. // #ifdef MP
  198. mpUrl = await this.downloadFilestoreImage(resData.url)
  199. arr = [this.posterBag, await this.downloadFilestoreImage(resData.image), mpUrl]
  200. // #endif
  201. this.$nextTick((e) => {
  202. that.$util.bargainPosterCanvas(arr, resData.title, resData.label, resData.msg, resData
  203. .price,
  204. this.wd,
  205. this.hg,
  206. (tempFilePath) => {
  207. this.posterImage = tempFilePath
  208. });
  209. })
  210. },
  211. // async routineCode() {
  212. // let res = await routineCode()
  213. // this.mpUrl = res.data.url
  214. // },
  215. //图片转符合安全域名路径
  216. downloadFilestoreImage(url) {
  217. url = this.setDomain(url)
  218. return new Promise((resolve, reject) => {
  219. let that = this;
  220. uni.downloadFile({
  221. url: url,
  222. success: function(res) {
  223. resolve(res.tempFilePath);
  224. },
  225. fail: function() {
  226. return that.$util.Tips({
  227. title: ''
  228. });
  229. }
  230. });
  231. })
  232. },
  233. //替换安全域名
  234. setDomain: function(url) {
  235. url = url ? url.toString() : '';
  236. //本地调试打开,生产请注销
  237. if (url.indexOf('https://') > -1) return url;
  238. else return url.replace('http://', 'https://');
  239. },
  240. async imgToBase(url) {
  241. let res = await imgToBase({
  242. image: url
  243. })
  244. return res.data.image
  245. },
  246. downloadImg() {
  247. },
  248. savePosterPathMp(url) {
  249. let that = this;
  250. // #ifdef APP-PLUS
  251. uni.saveImageToPhotosAlbum({
  252. filePath: url,
  253. success: function(res) {
  254. that.$util.Tips({
  255. title: '保存成功',
  256. icon: 'success'
  257. });
  258. },
  259. fail: function(res) {
  260. that.$util.Tips({
  261. title: '保存失败'
  262. });
  263. }
  264. });
  265. // #endif
  266. // #ifdef MP
  267. uni.getSetting({
  268. success(res) {
  269. if (!res.authSetting['scope.writePhotosAlbum']) {
  270. uni.authorize({
  271. scope: 'scope.writePhotosAlbum',
  272. success() {
  273. uni.saveImageToPhotosAlbum({
  274. filePath: url,
  275. success: function(res) {
  276. that.$util.Tips({
  277. title: '保存成功',
  278. icon: 'success'
  279. });
  280. },
  281. fail: function(res) {
  282. that.$util.Tips({
  283. title: '保存失败'
  284. });
  285. }
  286. });
  287. },
  288. fail: function(res) {
  289. that.$util.Tips({
  290. title: '请先开启文件访问权限'
  291. });
  292. }
  293. });
  294. } else {
  295. uni.saveImageToPhotosAlbum({
  296. filePath: url,
  297. success: function(res) {
  298. that.$util.Tips({
  299. title: '保存成功',
  300. icon: 'success'
  301. });
  302. },
  303. fail: function(res) {
  304. that.$util.Tips({
  305. title: '保存失败'
  306. });
  307. }
  308. });
  309. }
  310. }
  311. });
  312. // #endif
  313. // #ifdef H5
  314. // 创建隐藏的可下载链接
  315. var eleLink = document.createElement('a');
  316. eleLink.download = '海报';
  317. eleLink.href = url;
  318. // 触发点击
  319. document.body.appendChild(eleLink);
  320. eleLink.click();
  321. // #endif
  322. },
  323. qrR(res) {
  324. this.codeSrc = res
  325. },
  326. // #ifdef MP
  327. savePosterPath: function() {
  328. let that = this;
  329. uni.getSetting({
  330. success(res) {
  331. if (!res.authSetting['scope.writePhotosAlbum']) {
  332. uni.authorize({
  333. scope: 'scope.writePhotosAlbum',
  334. success() {
  335. uni.saveImageToPhotosAlbum({
  336. filePath: that.posterImage,
  337. success: function(res) {
  338. that.posterImageClose();
  339. that.$util.Tips({
  340. title: '保存成功',
  341. icon: 'success'
  342. });
  343. },
  344. fail: function(res) {
  345. that.$util.Tips({
  346. title: '保存失败'
  347. });
  348. }
  349. });
  350. }
  351. });
  352. } else {
  353. uni.saveImageToPhotosAlbum({
  354. filePath: that.posterImage,
  355. success: function(res) {
  356. that.posterImageClose();
  357. that.$util.Tips({
  358. title: '保存成功',
  359. icon: 'success'
  360. });
  361. },
  362. fail: function(res) {
  363. that.$util.Tips({
  364. title: '保存失败'
  365. });
  366. }
  367. });
  368. }
  369. }
  370. });
  371. },
  372. // #endif
  373. }
  374. }
  375. </script>
  376. <style>
  377. .posterCon{
  378. position: fixed;
  379. top:0;
  380. width: 100%;
  381. left:0;
  382. height: 100%;
  383. background-color: var(--view-theme);
  384. bottom: 0;
  385. overflow-y: auto;
  386. }
  387. .poster-poster .tip {
  388. height: 80rpx;
  389. font-size: 26rpx;
  390. color: #fff;
  391. text-align: center;
  392. line-height: 80rpx;
  393. user-select: none;
  394. }
  395. .poster-poster .tip .iconfont {
  396. font-size: 36rpx;
  397. vertical-align: -4rpx;
  398. margin-right: 18rpx;
  399. }
  400. .canvas {
  401. width: 100%;
  402. height: 1100rpx;
  403. }
  404. .poster-poster .pictrue {
  405. width: 700rpx;
  406. /* height: 100%; */
  407. margin: 0 auto 50rpx auto;
  408. display: flex;
  409. justify-content: center;
  410. }
  411. .poster-poster .pictrue image {
  412. width: 100%;
  413. /* height: 100%; */
  414. }
  415. </style>