shareQrCode.vue 11 KB

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