index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. uni.showLoading({
  166. title: '海报生成中',
  167. mask: true
  168. });
  169. let flag = false;
  170. if (that.type == 1) {
  171. await getBargainPosterData(that.id).then(res => {
  172. resData = res.data
  173. }).catch(err => {
  174. flag = true;
  175. that.$util.Tips({
  176. title: err
  177. },'/pages/activity/goods_bargain/index');
  178. })
  179. } else {
  180. await getCombinationPosterData(that.id).then(res => {
  181. resData = res.data
  182. }).catch(err => {
  183. flag = true;
  184. that.$util.Tips({
  185. title: '海报图片获取失败'
  186. });
  187. })
  188. }
  189. if(flag) return false;
  190. // #ifdef H5 || APP-PLUS
  191. goods_img = await this.imgToBaseData(resData.image);
  192. // 公众号会返回url,h5环境没有,微信公众号跟h5的二维码需要区分
  193. // mp_code = await resData.url ? this.imgToBaseData(resData.url) : this.codeSrc;
  194. mp_code = await resData.url ? resData.url : this.codeSrc;
  195. arr = [this.posterBag, goods_img, mp_code]
  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 imgToBaseData(url) {
  241. let res = await imgToBase({
  242. image: url
  243. })
  244. return res.data.image
  245. },
  246. savePosterPathMp(url) {
  247. let that = this;
  248. // #ifdef APP-PLUS
  249. uni.saveImageToPhotosAlbum({
  250. filePath: url,
  251. success: function(res) {
  252. that.$util.Tips({
  253. title: '保存成功',
  254. icon: 'success'
  255. });
  256. },
  257. fail: function(res) {
  258. that.$util.Tips({
  259. title: '保存失败'
  260. });
  261. }
  262. });
  263. // #endif
  264. // #ifdef MP
  265. uni.getSetting({
  266. success(res) {
  267. if (!res.authSetting['scope.writePhotosAlbum']) {
  268. uni.authorize({
  269. scope: 'scope.writePhotosAlbum',
  270. success() {
  271. uni.saveImageToPhotosAlbum({
  272. filePath: url,
  273. success: function(res) {
  274. that.$util.Tips({
  275. title: '保存成功',
  276. icon: 'success'
  277. });
  278. },
  279. fail: function(res) {
  280. that.$util.Tips({
  281. title: '保存失败'
  282. });
  283. }
  284. });
  285. },
  286. fail: function(res) {
  287. that.$util.Tips({
  288. title: '请先开启文件访问权限'
  289. });
  290. }
  291. });
  292. } else {
  293. uni.saveImageToPhotosAlbum({
  294. filePath: url,
  295. success: function(res) {
  296. that.$util.Tips({
  297. title: '保存成功',
  298. icon: 'success'
  299. });
  300. },
  301. fail: function(res) {
  302. that.$util.Tips({
  303. title: '保存失败'
  304. });
  305. }
  306. });
  307. }
  308. }
  309. });
  310. // #endif
  311. // #ifdef H5
  312. // 创建隐藏的可下载链接
  313. var eleLink = document.createElement('a');
  314. eleLink.download = '海报';
  315. eleLink.href = url;
  316. // 触发点击
  317. document.body.appendChild(eleLink);
  318. eleLink.click();
  319. // #endif
  320. },
  321. qrR(res) {
  322. this.codeSrc = res
  323. },
  324. // #ifdef MP
  325. savePosterPath: function() {
  326. let that = this;
  327. uni.getSetting({
  328. success(res) {
  329. if (!res.authSetting['scope.writePhotosAlbum']) {
  330. uni.authorize({
  331. scope: 'scope.writePhotosAlbum',
  332. success() {
  333. uni.saveImageToPhotosAlbum({
  334. filePath: that.posterImage,
  335. success: function(res) {
  336. that.posterImageClose();
  337. that.$util.Tips({
  338. title: '保存成功',
  339. icon: 'success'
  340. });
  341. },
  342. fail: function(res) {
  343. that.$util.Tips({
  344. title: '保存失败'
  345. });
  346. }
  347. });
  348. }
  349. });
  350. } else {
  351. uni.saveImageToPhotosAlbum({
  352. filePath: that.posterImage,
  353. success: function(res) {
  354. that.posterImageClose();
  355. that.$util.Tips({
  356. title: '保存成功',
  357. icon: 'success'
  358. });
  359. },
  360. fail: function(res) {
  361. that.$util.Tips({
  362. title: '保存失败'
  363. });
  364. }
  365. });
  366. }
  367. }
  368. });
  369. },
  370. // #endif
  371. }
  372. }
  373. </script>
  374. <style>
  375. .posterCon{
  376. position: fixed;
  377. top:0;
  378. width: 100%;
  379. left:0;
  380. height: 100%;
  381. background-color: var(--view-theme);
  382. bottom: 0;
  383. overflow-y: auto;
  384. }
  385. .poster-poster .tip {
  386. height: 80rpx;
  387. font-size: 26rpx;
  388. color: #fff;
  389. text-align: center;
  390. line-height: 80rpx;
  391. user-select: none;
  392. }
  393. .poster-poster .tip .iconfont {
  394. font-size: 36rpx;
  395. vertical-align: -4rpx;
  396. margin-right: 18rpx;
  397. }
  398. .canvas {
  399. width: 100%;
  400. height: 1100rpx;
  401. }
  402. .poster-poster .pictrue {
  403. width: 700rpx;
  404. /* height: 100%; */
  405. margin: 0 auto 50rpx auto;
  406. display: flex;
  407. justify-content: center;
  408. }
  409. .poster-poster .pictrue image {
  410. width: 100%;
  411. /* height: 100%; */
  412. }
  413. </style>