index.vue 11 KB

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