shareQrCode.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="container">
  3. <canvas :style="{ width: '554rpx', height: '1200rpx',}" canvas-id="myCanvas" id="myCanvas" class="hb"></canvas>
  4. <!-- #ifndef MP -->
  5. <div class="preserve">
  6. <div class="line"></div>
  7. <div class="tip">长按保存图片</div>
  8. <div class="line"></div>
  9. </div>
  10. <!-- #endif -->
  11. <!-- #ifdef MP -->
  12. <view class='keep' @click='saveShareQrcode' v-if="!loading">保存海报</view>
  13. <!-- #endif -->
  14. </view>
  15. </template>
  16. <script>
  17. import {
  18. spreadBanner,
  19. getWxmpInviteQrcode
  20. } from '@/api/user.js';
  21. import {
  22. qrcode
  23. } from '@/api/user.js';
  24. import {
  25. mapState
  26. } from 'vuex';
  27. export default {
  28. // #ifdef MP
  29. onShareAppMessage: function(res) {
  30. // if (res.from === 'button') {
  31. // 保存邀请人
  32. let path = '/pages/index/index?' + 'spread=' + this.userInfo.uid;
  33. console.log('path', path)
  34. let data = {
  35. path: path,
  36. imageUrl: this.poster,
  37. title: this.userInfo.nickname + '邀请您进入母婴界严选'
  38. };
  39. console.log('---data---', data)
  40. return data;
  41. // }
  42. },
  43. // #endif
  44. data() {
  45. return {
  46. bgimg: '',
  47. qrimg: '',
  48. shareList: [],
  49. swiperIndex: 0,
  50. poster: '', // 当前海报
  51. loading: true
  52. }
  53. },
  54. onLoad(option) {
  55. // console.log('---option---',option)
  56. // let path = '/pages/index/index?' + 'spread=' + this.userInfo.uid;
  57. // console.log('path',path)
  58. // if(option.scene){
  59. // // 存储邀请人
  60. // this.spread = opt.pid;
  61. // uni.setStorageSync('spread', opt.pid);
  62. // }
  63. this.loadData();
  64. },
  65. computed: {
  66. ...mapState(['userInfo']),
  67. },
  68. methods: {
  69. bindchange(e) {
  70. let shareList = this.shareList;
  71. this.swiperIndex = e.detail.current;
  72. // #ifdef MP
  73. this.poster = shareList[this.swiperIndex].poster;
  74. // #endif
  75. // // #ifndef MP
  76. // this.poster = shareList[this.swiperIndex].wap_poster;
  77. // // #endif
  78. console.log(this.poster)
  79. },
  80. // 保存海报
  81. savePosterPath: function() {
  82. let that = this;
  83. if (that.poster == '') {
  84. // that.poster = that.shareList[0].poster;
  85. that.poster = this.bgimg
  86. }
  87. uni.downloadFile({
  88. url: that.poster,
  89. success(resFile) {
  90. if (resFile.statusCode === 200) {
  91. uni.getSetting({
  92. success(res) {
  93. if (!res.authSetting['scope.writePhotosAlbum']) {
  94. uni.authorize({
  95. scope: 'scope.writePhotosAlbum',
  96. success() {
  97. uni.saveImageToPhotosAlbum({
  98. filePath: resFile.tempFilePath,
  99. success: function(res) {
  100. return that.$api.msg(
  101. '保存成功');
  102. },
  103. fail: function(res) {
  104. return that.$api.msg(res
  105. .errMsg);
  106. },
  107. complete: function(res) {},
  108. })
  109. },
  110. fail() {
  111. uni.showModal({
  112. title: '您已拒绝获取相册权限',
  113. content: '是否进入权限管理,调整授权?',
  114. success(res) {
  115. if (res.confirm) {
  116. uni.openSetting({
  117. success: function(
  118. res) {
  119. console
  120. .log(
  121. res
  122. .authSetting
  123. )
  124. }
  125. });
  126. } else if (res.cancel) {
  127. return that.$api.msg(
  128. '已取消!');
  129. }
  130. }
  131. })
  132. }
  133. })
  134. } else {
  135. uni.saveImageToPhotosAlbum({
  136. filePath: resFile.tempFilePath,
  137. success: function(res) {
  138. return that.$api.msg('保存成功');
  139. },
  140. fail: function(res) {
  141. return that.$api.msg(res.errMsg);
  142. },
  143. complete: function(res) {},
  144. })
  145. }
  146. },
  147. fail(res) {
  148. }
  149. })
  150. } else {
  151. return that.$api.msg(resFile.errMsg);
  152. }
  153. },
  154. fail(res) {
  155. return that.$api.msg(res.errMsg);
  156. }
  157. })
  158. },
  159. // #ifdef MP-WEIXIN
  160. // 保存画图图片到本地
  161. seav(url) {
  162. uni.showLoading({
  163. title: '生成中...',
  164. mask: true
  165. });
  166. uni.saveImageToPhotosAlbum({
  167. filePath: this.poster,
  168. complete(result) {
  169. uni.hideLoading();
  170. console.log(result);
  171. uni.showToast({
  172. title: '保存图片成功!',
  173. duration: 2000,
  174. icon: 'none'
  175. });
  176. }
  177. });
  178. },
  179. // #endif
  180. // 获取海报
  181. loadData() {
  182. let obj = this;
  183. uni.showLoading({
  184. title: '获取中',
  185. mask: true,
  186. });
  187. // spreadBanner({
  188. // // #ifdef H5
  189. // type: 2,
  190. // // #endif
  191. // // #ifdef MP
  192. // type: 1
  193. // // #endif
  194. // }).then(res =>{
  195. // uni.hideLoading();
  196. // obj.shareList = res.data;
  197. // console.log('obj.shareList',obj.shareList);
  198. // }).catch(err => {
  199. // uni.hideLoading();
  200. // });
  201. getWxmpInviteQrcode().then(res => {
  202. console.log(res)
  203. obj.bgimg = res.data.bgimg
  204. obj.qrcode = res.data.qrcode
  205. uni.hideLoading();
  206. obj.createPoster()
  207. })
  208. },
  209. async createPoster() {
  210. let that = this
  211. // 获取设备信息,主要获取宽度,赋值给canvasW 也就是宽度:100%
  212. this.SystemInfo = await this.getSystemInfo();
  213. // 获取商品主图,二维码信息,APP端会返回图片的本地路径(H5端只能返回原路径)
  214. this.goodsImg = await this.getImageInfo(that.bgimg);
  215. this.ewmImg = await this.getImageInfo(that.qrcode);
  216. // this.canvasW = this.SystemInfo.windowWidth; // 画布宽度
  217. this.ratio = this.SystemInfo.windowWidth / 750;
  218. this.canvasW = 554 * this.ratio;
  219. this.canvasH = 1200 * this.ratio;
  220. this.ewmW = 180 * this.ratio;
  221. console.log(this.canvasH, 'this.canvasH')
  222. // this.canvasH = this.goodsImg.height + this.ewmW + 200; // 画布高度 = 主图高度+二维码高度 + 文字图片的间距(大概50)
  223. // 如果主图,二维码图片,设备信息都获取成功,开始绘制海报,这里需要用setTimeout延时绘制,否则可能会出现图片不显示。
  224. if (this.goodsImg.errMsg == 'getImageInfo:ok' && this.ewmImg.errMsg == 'getImageInfo:ok' && this
  225. .SystemInfo.errMsg == 'getSystemInfo:ok') {
  226. console.log('ok')
  227. uni.showToast({
  228. icon: 'loading',
  229. mask: true,
  230. duration: 10000,
  231. title: '海报绘制中',
  232. });
  233. setTimeout(() => {
  234. var ctx = uni.createCanvasContext('myCanvas', this);
  235. // 填充背景色,白色
  236. ctx.setFillStyle('#fff'); // 默认白色
  237. ctx.fillRect(0, 0, this.canvasW, this.canvasH) // fillRect(x,y,宽度,高度)
  238. // 绘制商品主图,二维码
  239. ctx.drawImage(this.goodsImg.path, 0, 0, this.canvasW, this
  240. .canvasH) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度)
  241. ctx.drawImage(this.ewmImg.path, (this.canvasW / 2 - this.ewmW / 2),this.canvasH - 85*this.ratio - this.ewmW,
  242. this.ewmW, this.ewmW) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度,二维码的宽,高)
  243. // 3、绘制商品标题,多余文字自动换行
  244. ctx.setFontSize(16); // setFontSize() 设置字体字号
  245. ctx.setFillStyle('#333'); // setFillStyle() 设置字体颜色
  246. ctx.draw(false, (ret) => { // draw方法 把以上内容画到 canvas 中。
  247. console.log(ret)
  248. uni.showToast({
  249. icon: 'none',
  250. title: '生成成功!',
  251. });
  252. that.loading = false
  253. that.fina = true
  254. uni.canvasToTempFilePath({ // 保存canvas为图片
  255. canvasId: 'myCanvas',
  256. quality: 1,
  257. fileType: 'jpg',
  258. complete: function(res) {
  259. // 在H5平台下,tempFilePath 为 base64, // 图片提示跨域 H5保存base64失败,APP端正常输出临时路径
  260. console.log(res)
  261. that.canvasShow = false
  262. that.shareQrcodeUrl = res.tempFilePath
  263. that.$forceUpdate()
  264. setTimeout(function() {
  265. console.log(that.shareQrcodeUrl, that
  266. .canvasShow)
  267. }, 2000)
  268. },
  269. })
  270. });
  271. }, 1500)
  272. } else {
  273. console.log('err')
  274. }
  275. },
  276. // 获取设备信息
  277. getSystemInfo() {
  278. return new Promise((req, rej) => {
  279. uni.getSystemInfo({
  280. success: function(res) {
  281. req(res)
  282. }
  283. });
  284. })
  285. },
  286. // 获取图片信息
  287. getImageInfo(image) {
  288. return new Promise((req, rej) => {
  289. uni.getImageInfo({
  290. src: image,
  291. success: function(res) {
  292. req(res)
  293. },
  294. });
  295. })
  296. },
  297. //保存图片
  298. saveShareQrcode() {
  299. console.log(this.shareQrcodeUrl)
  300. uni.saveImageToPhotosAlbum({
  301. filePath: this.shareQrcodeUrl,
  302. success: (res) => {
  303. uni.showToast({
  304. icon: 'none',
  305. position: 'bottom',
  306. title: "成功保存到相册",
  307. });
  308. },
  309. fail: (err) => {
  310. //重新提示用户打开保存图片的授权
  311. if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  312. uni.showModal({
  313. title: '提示',
  314. content: '需要您授权保存相册',
  315. showCancel: false,
  316. success(res) {
  317. if (res.confirm) {
  318. uni.openSetting({
  319. success(settingdata) {
  320. if (settingdata.authSetting[
  321. 'scope.writePhotosAlbum']) {
  322. uni.showModal({
  323. title: '提示',
  324. content: '获取权限成功,再次保存图片即可成功',
  325. showCancel: false,
  326. })
  327. } else {
  328. uni.showModal({
  329. title: '提示',
  330. content: '获取权限失败,无法保存到相册',
  331. showCancel: false
  332. })
  333. }
  334. }
  335. })
  336. }
  337. }
  338. })
  339. }
  340. },
  341. })
  342. }
  343. }
  344. }
  345. </script>
  346. <style lang="scss">
  347. page {
  348. background: #a3a3a3;
  349. height: 100%;
  350. }
  351. .container {
  352. width: 100%;
  353. .posters-box {
  354. width: 100%;
  355. height: 1000rpx;
  356. margin-top: 40rpx;
  357. .slide-image {
  358. width: 100%;
  359. height: 100%;
  360. border-radius: 15rpx;
  361. }
  362. }
  363. .posters-box .slide-image.active {
  364. transform: none;
  365. transition: all 0.2s ease-in 0s;
  366. }
  367. .posters-box .slide-image.quiet {
  368. transform: scale(0.8333333);
  369. transition: all 0.2s ease-in 0s;
  370. }
  371. .keep {
  372. font-size: 30rpx;
  373. background: $base-color;
  374. color: #fff;
  375. width: 600rpx;
  376. height: 80rpx;
  377. border-radius: 50rpx;
  378. text-align: center;
  379. line-height: 80rpx;
  380. margin: 38rpx auto;
  381. }
  382. }
  383. .preserve {
  384. color: #fff;
  385. text-align: center;
  386. margin-top: 38rpx;
  387. display: flex;
  388. align-items: center;
  389. justify-content: center;
  390. .line {
  391. width: 100rpx;
  392. height: 1px;
  393. background-color: #fff;
  394. }
  395. .tip {
  396. margin: 0 20rpx;
  397. font-size: 28rpx;
  398. }
  399. }
  400. .hb {
  401. margin: auto;
  402. }
  403. </style>