index.vue 11 KB

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