You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

396 lines
9.1 KiB

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