index.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <view style="height: 100%;">
  3. <view class='distribution-posters'>
  4. <swiper :indicator-dots="indicatorDots" :autoplay="autoplay" :circular="circular" :interval="interval"
  5. :duration="duration" @change="bindchange" previous-margin="40px" next-margin="40px">
  6. <block v-for="(item,index) in spreadList" :key="index">
  7. <swiper-item>
  8. <image :src="item.pic" class="slide-image" :class="swiperIndex == index ? 'active' : 'quiet'"
  9. mode='aspectFill' />
  10. </swiper-item>
  11. </block>
  12. </swiper>
  13. <!-- #ifdef MP -->
  14. <view class='keep bg-color' @click='savePosterPath'>保存海报</view>
  15. <!-- #endif -->
  16. <!-- #ifndef MP -->
  17. <div class="preserve acea-row row-center-wrapper">
  18. <div class="line"></div>
  19. <div class="tip">长按保存图片</div>
  20. <div class="line"></div>
  21. </div>
  22. <!-- #endif -->
  23. </view>
  24. <!-- #ifdef MP -->
  25. <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  26. <!-- #endif -->
  27. <view class="canvas" v-if="canvasStatus">
  28. <canvas style="width:750px;height:1190px;" canvas-id="canvasOne"></canvas>
  29. <canvas canvas-id="qrcode" :style="{width: `${qrcodeSize}px`, height: `${qrcodeSize}px`}" />
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. // #ifdef H5
  35. import uQRCode from '@/js_sdk/Sansnn-uQRCode/uqrcode.js'
  36. // #endif
  37. import {
  38. getUserInfo,
  39. spreadBanner
  40. } from '@/api/user.js';
  41. import {
  42. toLogin
  43. } from '@/libs/login.js';
  44. import {
  45. mapGetters
  46. } from "vuex";
  47. // #ifdef MP
  48. import {
  49. base64src
  50. } from '@/utils/base64src.js'
  51. import authorize from '@/components/Authorize';
  52. import {
  53. getQrcode
  54. } from '@/api/api.js';
  55. // #endif
  56. import home from '@/components/home';
  57. import {
  58. imageBase64
  59. } from "@/api/public";
  60. export default {
  61. components: {
  62. // #ifdef MP
  63. authorize,
  64. // #endif
  65. home
  66. },
  67. data() {
  68. return {
  69. imgUrls: [],
  70. indicatorDots: false,
  71. circular: false,
  72. autoplay: false,
  73. interval: 3000,
  74. duration: 500,
  75. swiperIndex: 0,
  76. spreadList: [],
  77. poster: '',
  78. isAuto: false, //没有授权的不会自动授权
  79. isShowAuth: false, //是否隐藏授权
  80. qrcodeSize: 1000,
  81. PromotionCode: '',
  82. base64List: [],
  83. canvasStatus: true //海报绘图标签
  84. };
  85. },
  86. computed: mapGetters(['isLogin', 'uid', 'userInfo']),
  87. watch: {
  88. isLogin: {
  89. handler: function(newV, oldV) {
  90. if (newV) {
  91. this.userSpreadBannerList();
  92. }
  93. },
  94. deep: true
  95. }
  96. },
  97. onLoad() {
  98. if (this.isLogin) {
  99. this.userSpreadBannerList();
  100. } else {
  101. toLogin();
  102. }
  103. },
  104. /**
  105. * 用户点击右上角分享
  106. */
  107. // #ifdef MP
  108. onShareAppMessage: function() {
  109. return {
  110. title: this.userInfo.nickname + '-分销海报',
  111. imageUrl: this.spreadList[0].pic,
  112. path: '/pages/index/index?spid=' + this.uid,
  113. };
  114. },
  115. // #endif
  116. onReady() {},
  117. methods: {
  118. userSpreadBannerList: function() {
  119. let that = this;
  120. uni.showLoading({
  121. title: '获取中',
  122. mask: true,
  123. })
  124. spreadBanner({
  125. page: 1,
  126. limit: 5
  127. }).then(res => {
  128. uni.hideLoading();
  129. that.$set(that, 'spreadList', res.data);
  130. that.getImageBase64(res.data);
  131. }).catch(err => {
  132. uni.hideLoading();
  133. });
  134. },
  135. getImageBase64: function(images) {
  136. uni.showLoading({
  137. title: '海报生成中',
  138. mask: true
  139. });
  140. let that = this;
  141. // #ifdef H5
  142. let spreadList = []
  143. // 生成一个Promise对象的数组
  144. images.forEach(item => {
  145. const oneApi = imageBase64({
  146. url: item.pic
  147. }).then(res => {
  148. return res.data.code;
  149. })
  150. spreadList.push(oneApi)
  151. })
  152. Promise.all(spreadList).then(result => {
  153. that.$set(that, 'base64List', result);
  154. that.make();
  155. that.setShareInfoStatus();
  156. })
  157. // #endif
  158. // #ifdef MP
  159. that.base64List = images.map(item => {
  160. return item.pic
  161. });
  162. // #endif
  163. // #ifdef MP
  164. that.getQrcode();
  165. // #endif
  166. },
  167. // 小程序二维码
  168. getQrcode() {
  169. let that = this;
  170. let data = {
  171. pid: that.uid,
  172. path: 'pages/index/index'
  173. }
  174. let arrImagesUrl = "";
  175. uni.downloadFile({
  176. url: this.base64List[0],
  177. success: (res) => {
  178. arrImagesUrl = res.tempFilePath;
  179. }
  180. });
  181. getQrcode(data).then(res => {
  182. base64src(res.data.code, res => {
  183. that.PromotionCode = res;
  184. });
  185. setTimeout(() => {
  186. that.PosterCanvas(arrImagesUrl, that.PromotionCode, that.userInfo.nickname, 0);
  187. }, 300);
  188. }).catch(err => {
  189. uni.hideLoading();
  190. that.$util.Tips({
  191. title: err
  192. });
  193. that.$set(that, 'canvasStatus', false);
  194. });
  195. },
  196. // 生成二维码;
  197. make() {
  198. let that = this;
  199. let href = '';
  200. // #ifdef H5
  201. href = window.location.href.split('/pages')[0];
  202. // #endif
  203. uQRCode.make({
  204. canvasId: 'qrcode',
  205. text: href + '/pages/index/index?spread=' + that.uid,
  206. size: this.qrcodeSize,
  207. margin: 10,
  208. success: res => {
  209. that.PromotionCode = res;
  210. setTimeout(() => {
  211. that.PosterCanvas(this.base64List[0], that.PromotionCode, that.userInfo
  212. .nickname, 0);
  213. }, 300);
  214. },
  215. complete: (res) => {},
  216. fail: res => {
  217. uni.hideLoading();
  218. that.$util.Tips({
  219. title: '海报二维码生成失败!'
  220. });
  221. }
  222. })
  223. },
  224. PosterCanvas: function(arrImages, code, nickname, index) {
  225. let context = uni.createCanvasContext('canvasOne')
  226. context.clearRect(0, 0, 0, 0);
  227. let that = this;
  228. uni.getImageInfo({
  229. src: arrImages,
  230. success: function(res) {
  231. context.drawImage(arrImages, 0, 0, 750, 1190);
  232. context.save();
  233. context.drawImage(code, 110, 925, 140, 140);
  234. context.restore();
  235. context.setFontSize(28);
  236. context.fillText(nickname, 270, 980);
  237. context.fillText('邀请您加入', 270, 1020);
  238. setTimeout(() => {
  239. context.draw(true, function() {
  240. uni.canvasToTempFilePath({
  241. destWidth: 750,
  242. destHeight: 1190,
  243. canvasId: 'canvasOne',
  244. fileType: 'jpg',
  245. success: function(res) {
  246. // 在H5平台下,tempFilePath 为 base64
  247. uni.hideLoading();
  248. that.spreadList[index].pic = res
  249. .tempFilePath;
  250. that.$set(that, 'poster', res
  251. .tempFilePath);
  252. that.$set(that, 'canvasStatus', false);
  253. }
  254. })
  255. })
  256. }, 100);
  257. },
  258. fail: function(err) {
  259. uni.hideLoading();
  260. that.$util.Tips({
  261. title: '无法获取图片信息'
  262. });
  263. }
  264. });
  265. },
  266. onLoadFun: function(e) {
  267. this.$set(this, 'userInfo', e);
  268. this.userSpreadBannerList();
  269. },
  270. // 授权关闭
  271. authColse: function(e) {
  272. this.isShowAuth = e
  273. },
  274. bindchange(e) {
  275. let base64List = this.base64List;
  276. let index = e.detail.current;
  277. this.swiperIndex = index;
  278. let arrImagesUrl = "";
  279. uni.downloadFile({
  280. url: base64List[index],
  281. success: (res) => {
  282. arrImagesUrl = res.tempFilePath;
  283. setTimeout(() => {
  284. this.$set(this, 'canvasStatus', true);
  285. this.PosterCanvas(arrImagesUrl, this.PromotionCode, this.userInfo.nickname,
  286. index);
  287. }, 300);
  288. }
  289. });
  290. },
  291. // 点击保存海报
  292. savePosterPath: function() {
  293. let that = this;
  294. uni.getSetting({
  295. success(res) {
  296. if (!res.authSetting['scope.writePhotosAlbum']) {
  297. uni.authorize({
  298. scope: 'scope.writePhotosAlbum',
  299. success() {
  300. uni.saveImageToPhotosAlbum({
  301. filePath: that.poster,
  302. success: function(res) {
  303. that.$util.Tips({
  304. title: '保存成功',
  305. icon: 'success'
  306. });
  307. },
  308. fail: function(res) {
  309. that.$util.Tips({
  310. title: '保存失败'
  311. });
  312. }
  313. });
  314. }
  315. });
  316. } else {
  317. uni.saveImageToPhotosAlbum({
  318. filePath: that.poster,
  319. success: function(res) {
  320. that.$util.Tips({
  321. title: '保存成功',
  322. icon: 'success'
  323. });
  324. },
  325. fail: function(res) {
  326. that.$util.Tips({
  327. title: '保存失败'
  328. });
  329. }
  330. });
  331. }
  332. }
  333. });
  334. },
  335. setShareInfoStatus: function() {
  336. if (this.$wechat.isWeixin()) {
  337. let configAppMessage = {
  338. desc: '分销海报',
  339. title: this.userInfo.nickname + '-分销海报',
  340. link: '/pages/index/index?spread=' + this.uid,
  341. imgUrl: this.spreadList[0].pic
  342. };
  343. this.$wechat.wechatEvevt(["updateAppMessageShareData", "updateTimelineShareData"],
  344. configAppMessage)
  345. }
  346. }
  347. }
  348. }
  349. </script>
  350. <style lang="scss" scoped>
  351. page {
  352. background-color: #a3a3a3 !important;
  353. height: 100% !important;
  354. }
  355. .canvas {
  356. position: relative;
  357. }
  358. .distribution-posters {
  359. width: 100%;
  360. height: 100%;
  361. display: flex;
  362. flex-direction: column;
  363. justify-content: center;
  364. align-items: center;
  365. }
  366. .distribution-posters swiper {
  367. width: 100%;
  368. height: 1000rpx;
  369. position: relative;
  370. margin-top: 40rpx;
  371. }
  372. .distribution-posters .slide-image {
  373. width: 100%;
  374. height: 100%;
  375. margin: 0 auto;
  376. border-radius: 15rpx;
  377. }
  378. .distribution-posters .slide-image.active {
  379. transform: none;
  380. transition: all 0.2s ease-in 0s;
  381. }
  382. .distribution-posters .slide-image.quiet {
  383. transform: scale(0.8333333);
  384. transition: all 0.2s ease-in 0s;
  385. }
  386. .distribution-posters .keep {
  387. font-size: 30rpx;
  388. color: #fff;
  389. width: 600rpx;
  390. height: 80rpx;
  391. border-radius: 50rpx;
  392. text-align: center;
  393. line-height: 80rpx;
  394. margin: 38rpx auto;
  395. }
  396. .distribution-posters .preserve {
  397. color: #fff;
  398. text-align: center;
  399. margin-top: 38rpx;
  400. }
  401. .distribution-posters .preserve .line {
  402. width: 100rpx;
  403. height: 1px;
  404. background-color: #fff;
  405. }
  406. .distribution-posters .preserve .tip {
  407. margin: 0 30rpx;
  408. }
  409. </style>