index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <view :style="colorStyle">
  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 spreadData" :key="index" class="img-list">
  7. <swiper-item class="aaa">
  8. <div class="box" ref="bill" :class="swiperIndex == index ? 'active' : 'quiet'">
  9. <view class="user-msg">
  10. <view class="user-code">
  11. <image class="canvas" :style="{height:hg+'px'}" :src="posterImage[index]"
  12. v-if="posterImage[index]"></image>
  13. <canvas class="canvas" :style="{height:hg+'px'}" :canvas-id="'myCanvas'+ index"
  14. v-else></canvas>
  15. </view>
  16. </view>
  17. </div>
  18. <!-- <image :src="item.wap_poster" class="slide-image" :class="swiperIndex == index ? 'active' : 'quiet'" mode='aspectFill' /> -->
  19. </swiper-item>
  20. </block>
  21. </swiper>
  22. <!-- #ifndef H5 -->
  23. <view class='keep bg-color' @click='savePosterPathMp(posterImage[swiperIndex])'>保存海报</view>
  24. <!-- #endif -->
  25. <!-- #ifndef MP || APP-PLUS -->
  26. <div class="preserve acea-row row-center-wrapper">
  27. <div class="line"></div>
  28. <div class="tip">长按保存图片</div>
  29. <div class="line"></div>
  30. </div>
  31. <!-- #endif -->
  32. </view>
  33. <home v-if="navigation"></home>
  34. <view class="qrimg">
  35. <zb-code ref="qrcode" :show="codeShow" :cid="cid" :val="val" :size="size" :unit="unit"
  36. :background="background" :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconsize"
  37. :onval="onval" :loadMake="loadMake" @result="qrR" />
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import zbCode from '@/components/zb-code/zb-code.vue'
  43. import {
  44. getUserInfo,
  45. spreadBanner,
  46. userShare,
  47. routineCode,
  48. spreadMsg,
  49. imgToBase
  50. } from '@/api/user.js';
  51. import {
  52. toLogin
  53. } from '@/libs/login.js';
  54. import {
  55. mapGetters
  56. } from "vuex";
  57. import home from '@/components/home';
  58. import {
  59. TOKENNAME,
  60. HTTP_REQUEST_URL
  61. } from '@/config/app.js';
  62. import colors from '@/mixins/color.js';
  63. export default {
  64. components: {
  65. home,
  66. zbCode
  67. },
  68. mixins:[colors],
  69. data() {
  70. return {
  71. imgUrls: [],
  72. indicatorDots: false,
  73. posterImageStatus: true,
  74. circular: false,
  75. autoplay: false,
  76. interval: 3000,
  77. duration: 500,
  78. swiperIndex: 0,
  79. spreadList: [],
  80. userInfo: {},
  81. poster: '',
  82. isAuto: false, //没有授权的不会自动授权
  83. isShowAuth: false, //是否隐藏授权
  84. spreadData: [{}], //新海报数据
  85. nickName: "",
  86. siteName: "",
  87. mpUrl: "",
  88. canvasImageUrl: '',
  89. posterImage: [],
  90. //二维码参数
  91. codeShow: false,
  92. cid: '1',
  93. ifShow: true,
  94. val: "", // 要生成的二维码值
  95. size: 200, // 二维码大小
  96. unit: 'upx', // 单位
  97. background: '#FFF', // 背景色
  98. foreground: '#000', // 前景色
  99. pdground: '#000', // 角标色
  100. icon: '', // 二维码图标
  101. iconsize: 40, // 二维码图标大小
  102. lv: 3, // 二维码容错级别 , 一般不用设置,默认就行
  103. onval: true, // val值变化时自动重新生成二维码
  104. loadMake: true, // 组件加载完成后自动生成二维码
  105. src: '', // 二维码生成后的图片地址或base64
  106. codeSrc: "",
  107. wd: 0,
  108. hg: 0,
  109. qrcode: ""
  110. };
  111. },
  112. computed: mapGetters({
  113. 'isLogin': 'isLogin',
  114. 'userData': 'userInfo',
  115. 'uid': 'uid'
  116. }),
  117. watch: {
  118. isLogin: {
  119. handler: function(newV, oldV) {
  120. if (newV) {
  121. this.userSpreadBannerList();
  122. }
  123. },
  124. deep: true
  125. },
  126. userData: {
  127. handler: function(newV, oldV) {
  128. if (newV) {
  129. this.$set(this, 'userInfo', newV);
  130. }
  131. },
  132. deep: true
  133. }
  134. },
  135. async onReady() {
  136. if (this.isLogin) {
  137. this.val = `${HTTP_REQUEST_URL}?spread=${this.uid}`
  138. // #ifdef MP
  139. await this.spreadMsg()
  140. // #endif
  141. getUserInfo().then(res=>{
  142. this.userInfo = res.data
  143. })
  144. } else {
  145. toLogin();
  146. }
  147. },
  148. onShow() {
  149. uni.removeStorageSync('form_type_cart');
  150. this.$nextTick(() => {
  151. let selector = uni.createSelectorQuery().select('.aaa');
  152. selector.fields({
  153. size: true
  154. }, data => {
  155. this.wd = data.width
  156. this.hg = data.height
  157. }).exec();
  158. })
  159. },
  160. /**
  161. * 用户点击右上角分享
  162. */
  163. // #ifdef MP
  164. onShareAppMessage() {
  165. return {
  166. title: this.userInfo.nickname + '-分销海报',
  167. imageUrl: this.spreadList[0],
  168. path: '/pages/index/index?spread=' + this.userInfo.uid,
  169. };
  170. },
  171. // #endif
  172. methods: {
  173. async qrR(res) {
  174. this.codeSrc = await res
  175. // #ifdef H5 || APP-PLUS
  176. this.spreadMsg()
  177. // #endif
  178. },
  179. //获取图片
  180. async spreadMsg() {
  181. let res = await spreadMsg()
  182. this.spreadData = res.data.spread
  183. this.nickName = res.data.nickname
  184. this.siteName = res.data.site_name
  185. uni.showLoading({
  186. title: '海报生成中',
  187. mask: true
  188. });
  189. for (let i = 0; i < res.data.spread.length; i++) {
  190. let that = this
  191. let arr2 = [];
  192. let img = await this.imgToBase(res.data.spread[i].pic);
  193. let followCode = await res.data.qrcode?this.imgToBase(res.data.qrcode):'';
  194. // #ifdef H5
  195. arr2 = [followCode || this.codeSrc, img]
  196. // #endif
  197. // #ifdef MP
  198. await this.routineCode();
  199. img = await this.downloadFilestoreImage(res.data.spread[i].pic);
  200. arr2 = [this.mpUrl, img]
  201. // #endif
  202. // #ifdef APP-PLUS
  203. img = await this.downloadFilestoreImage(res.data.spread[i].pic);
  204. arr2 = [this.codeSrc, img]
  205. // #endif
  206. this.$nextTick(function(){
  207. that.$util.userPosterCanvas(arr2, res.data.nickname, res.data.site_name, i, this.wd, this.hg, (
  208. tempFilePath) => {
  209. that.$set(that.posterImage, i, tempFilePath);
  210. // #ifdef MP
  211. if(!that.posterImage.length){
  212. return that.$util.Tips({
  213. title: '小程序二维码需要发布正式版后才能获取到'
  214. });
  215. }
  216. // #endif
  217. });
  218. })
  219. }
  220. uni.hideLoading();
  221. },
  222. // #ifdef MP
  223. async routineCode() {
  224. let res = await routineCode()
  225. this.mpUrl = await this.downloadFilestoreImage(res.data.url);
  226. },
  227. // #endif
  228. async imgToBase(url) {
  229. let res = await imgToBase({
  230. image: url
  231. })
  232. return res.data.image
  233. },
  234. // 授权关闭
  235. authColse: function(e) {
  236. this.isShowAuth = e
  237. },
  238. bindchange(e) {
  239. let spreadList = this.spreadList;
  240. this.swiperIndex = e.detail.current;
  241. },
  242. // #ifdef MP
  243. savePosterPathMp(url) {
  244. let that = this;
  245. uni.getSetting({
  246. success(res) {
  247. if (!res.authSetting['scope.writePhotosAlbum']) {
  248. uni.authorize({
  249. scope: 'scope.writePhotosAlbum',
  250. success() {
  251. uni.saveImageToPhotosAlbum({
  252. filePath: url,
  253. success: function(res) {
  254. that.$util.Tips({
  255. title: '保存成功',
  256. icon: 'success'
  257. });
  258. },
  259. fail: function(res) {
  260. that.$util.Tips({
  261. title: '保存失败'
  262. });
  263. }
  264. });
  265. }
  266. });
  267. } else {
  268. uni.saveImageToPhotosAlbum({
  269. filePath: url,
  270. success: function(res) {
  271. that.$util.Tips({
  272. title: '保存成功',
  273. icon: 'success'
  274. });
  275. },
  276. fail: function(res) {
  277. that.$util.Tips({
  278. title: '保存失败'
  279. });
  280. }
  281. });
  282. }
  283. }
  284. });
  285. },
  286. // #endif
  287. // #ifdef APP-PLUS
  288. savePosterPathMp(url) {
  289. let that = this;
  290. uni.saveImageToPhotosAlbum({
  291. filePath: url,
  292. success: function(res) {
  293. that.$util.Tips({
  294. title: '保存成功',
  295. icon: 'success'
  296. });
  297. },
  298. fail: function(res) {
  299. that.$util.Tips({
  300. title: '保存失败'
  301. });
  302. }
  303. });
  304. },
  305. // #endif
  306. //图片转符合安全域名路径
  307. downloadFilestoreImage(url) {
  308. return new Promise((resolve, reject) => {
  309. let that = this;
  310. uni.downloadFile({
  311. url: url,
  312. success: function(res) {
  313. resolve(res.tempFilePath);
  314. },
  315. fail: function() {
  316. return that.$util.Tips({
  317. title: ''
  318. });
  319. }
  320. });
  321. })
  322. },
  323. setShareInfoStatus: function() {
  324. if (this.$wechat.isWeixin()) {
  325. if (this.isLogin) {
  326. getUserInfo().then(res => {
  327. let configAppMessage = {
  328. desc: '分销海报',
  329. title: res.data.nickname + '-分销海报',
  330. link: '/pages/index/index?spread=' + res.data.uid,
  331. imgUrl: this.spreadList[0]
  332. };
  333. this.$wechat.wechatEvevt(["updateAppMessageShareData", "updateTimelineShareData"],
  334. configAppMessage)
  335. });
  336. } else {
  337. toLogin();
  338. }
  339. }
  340. },
  341. userSpreadBannerList: function() {
  342. let that = this;
  343. uni.showLoading({
  344. title: '获取中',
  345. mask: true,
  346. })
  347. spreadBanner().then(res => {
  348. uni.hideLoading();
  349. that.$set(that, 'spreadList', res.data);
  350. that.$set(that, 'poster', res.data[0].poster);
  351. // #ifdef H5
  352. that.setShareInfoStatus();
  353. // #endif
  354. }).catch(err => {
  355. uni.hideLoading();
  356. });
  357. }
  358. }
  359. }
  360. </script>
  361. <style lang="scss">
  362. page {
  363. background-color: #a3a3a3 !important;
  364. }
  365. .canvas {
  366. width: 100%;
  367. // height: 550px;
  368. }
  369. .box {
  370. width: 100%;
  371. height: 100%;
  372. position: relative;
  373. border-radius: 18rpx;
  374. overflow: hidden;
  375. .user-msg {
  376. position: absolute;
  377. width: 100%;
  378. height: 100%;
  379. display: flex;
  380. align-items: center;
  381. justify-content: center;
  382. .user-code {
  383. width: 100%;
  384. // height: 100%;
  385. display: flex;
  386. align-items: center;
  387. justify-content: center;
  388. justify-content: space-between;
  389. image {
  390. width: 100%;
  391. }
  392. }
  393. }
  394. }
  395. .img-list {
  396. margin-right: 40px;
  397. }
  398. .distribution-posters swiper {
  399. width: 100%;
  400. height: 1000rpx;
  401. position: relative;
  402. margin-top: 40rpx;
  403. }
  404. .distribution-posters .slide-image {
  405. width: 100%;
  406. height: 100%;
  407. margin: 0 auto;
  408. border-radius: 15rpx;
  409. }
  410. .distribution-posters /deep/.active {
  411. transform: none;
  412. transition: all 0.2s ease-in 0s;
  413. }
  414. .distribution-posters /deep/ .quiet {
  415. transform: scale(0.8333333);
  416. transition: all 0.2s ease-in 0s;
  417. }
  418. .distribution-posters .keep {
  419. font-size: 30rpx;
  420. color: #fff;
  421. width: 600rpx;
  422. height: 80rpx;
  423. border-radius: 50rpx;
  424. text-align: center;
  425. line-height: 80rpx;
  426. margin: 38rpx auto;
  427. }
  428. .distribution-posters .preserve {
  429. color: #fff;
  430. text-align: center;
  431. margin-top: 38rpx;
  432. }
  433. .distribution-posters .preserve .line {
  434. width: 100rpx;
  435. height: 1px;
  436. background-color: #fff;
  437. }
  438. .distribution-posters .preserve .tip {
  439. margin: 0 30rpx;
  440. }
  441. </style>