index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. } else {
  151. toLogin();
  152. }
  153. this.$nextTick(() => {
  154. let selector = uni.createSelectorQuery().select('.aaa');
  155. selector.fields({
  156. size: true
  157. }, data => {
  158. this.wd = data.width
  159. this.hg = data.height
  160. }).exec();
  161. })
  162. },
  163. onShow() {
  164. },
  165. onHide() {
  166. uni.hideLoading();
  167. },
  168. /**
  169. * 用户点击右上角分享
  170. */
  171. // #ifdef MP
  172. onShareAppMessage() {
  173. return {
  174. title: this.userInfo.nickname + '-' + this.$t(`分销海报`),
  175. imageUrl: this.spreadList[0],
  176. path: '/pages/index/index?spread=' + this.userInfo.uid,
  177. };
  178. },
  179. // #endif
  180. methods: {
  181. getUser() {
  182. getUserInfo().then(res => {
  183. this.userInfo = res.data
  184. })
  185. },
  186. onLoadFun: function(e) {
  187. this.$set(this, 'userInfo', e);
  188. this.userSpreadBannerList();
  189. },
  190. qrR(res) {
  191. this.codeSrc = res
  192. this.spreadMsgs()
  193. },
  194. //获取图片
  195. async spreadMsgs() {
  196. let res = await spreadMsg()
  197. this.spreadData = res.data.spread
  198. this.nickName = res.data.nickname
  199. this.siteName = res.data.site_name
  200. // #ifdef MP
  201. this.qrcode = await this.imgToBase(res.data.qrcode)
  202. // #endif
  203. // #ifdef MP
  204. await this.routineCode()
  205. let mpUrl = await this.downloadFilestoreImage(this.mpUrl)
  206. // #endif
  207. uni.showLoading({
  208. title: this.$t(`海报生成中`),
  209. mask: true
  210. });
  211. for (let i = 0; i < res.data.spread.length; i++) {
  212. let that = this
  213. let arr2, img
  214. // #ifdef MP
  215. arr2 = [mpUrl, await this.downloadFilestoreImage(res.data.spread[i].pic)]
  216. // #endif
  217. // #ifdef H5
  218. img = await this.imgToBase(res.data.spread[i].pic, res.data.qrcode)
  219. arr2 = [img.code || this.codeSrc, img.image]
  220. // #endif
  221. // #ifdef APP-PLUS
  222. img = await this.imgToBase(res.data.spread[i].pic, res.data.qrcode)
  223. arr2 = [img.code || this.codeSrc, res.data.spread[i].pic]
  224. // #endif
  225. that.$util.userPosterCanvas(arr2, res.data.nickname, res.data.site_name, i, this
  226. .wd,
  227. this.hg, (
  228. tempFilePath) => {
  229. that.$set(that.posterImage, i, tempFilePath);
  230. // #ifdef MP
  231. if (!that.posterImage.length) {
  232. return that.$util.Tips({
  233. title: that.$t(`小程序二维码需要发布正式版后才能获取到`)
  234. });
  235. }
  236. // #endif
  237. });
  238. }
  239. uni.hideLoading();
  240. },
  241. downloadImg() {
  242. uni.saveImageToPhotosAlbum({
  243. filePath: this.posterImage[this.swiperIndex],
  244. success: function() {}
  245. });
  246. },
  247. async routineCode() {
  248. let res = await routineCode()
  249. this.mpUrl = res.data.url
  250. },
  251. async imgToBase(url, code) {
  252. let res = await imgToBase({
  253. image: url,
  254. code: code
  255. })
  256. return res.data
  257. },
  258. // 二维码生成
  259. codeImg() {
  260. // http://当前域名+"?spread="+用户uid
  261. },
  262. // 授权关闭
  263. authColse: function(e) {
  264. this.isShowAuth = e
  265. },
  266. bindchange(e) {
  267. let spreadList = this.spreadList;
  268. this.swiperIndex = e.detail.current;
  269. // this.$set(this, 'poster', spreadList[e.detail.current].poster);
  270. },
  271. // #ifdef MP
  272. savePosterPathMp(url) {
  273. let that = this;
  274. uni.getSetting({
  275. success(res) {
  276. if (!res.authSetting['scope.writePhotosAlbum']) {
  277. uni.authorize({
  278. scope: 'scope.writePhotosAlbum',
  279. success() {
  280. uni.saveImageToPhotosAlbum({
  281. filePath: url,
  282. success: function(res) {
  283. that.$util.Tips({
  284. title: that.$t(`保存成功`),
  285. icon: 'success'
  286. });
  287. },
  288. fail: function(res) {
  289. that.$util.Tips({
  290. title: that.$t(`保存失败`),
  291. });
  292. }
  293. });
  294. }
  295. });
  296. } else {
  297. uni.saveImageToPhotosAlbum({
  298. filePath: url,
  299. success: function(res) {
  300. that.$util.Tips({
  301. title: that.$t(`保存成功`),
  302. icon: 'success'
  303. });
  304. },
  305. fail: function(res) {
  306. that.$util.Tips({
  307. title: that.$t(`保存失败`),
  308. });
  309. }
  310. });
  311. }
  312. }
  313. });
  314. },
  315. // #endif
  316. // #ifdef APP-PLUS
  317. savePosterPathMp(url) {
  318. let that = this;
  319. uni.saveImageToPhotosAlbum({
  320. filePath: url,
  321. success: function(res) {
  322. that.$util.Tips({
  323. title: that.$t(`保存成功`),
  324. icon: 'success'
  325. });
  326. },
  327. fail: function(res) {
  328. that.$util.Tips({
  329. title: that.$t(`保存失败`),
  330. });
  331. }
  332. });
  333. },
  334. // #endif
  335. //图片转符合安全域名路径
  336. downloadFilestoreImage(url) {
  337. return new Promise((resolve, reject) => {
  338. let that = this;
  339. uni.downloadFile({
  340. url: url,
  341. success: function(res) {
  342. resolve(res.tempFilePath);
  343. },
  344. fail: function() {
  345. return that.$util.Tips({
  346. title: ''
  347. });
  348. }
  349. });
  350. })
  351. },
  352. setShareInfoStatus: function() {
  353. if (this.$wechat.isWeixin()) {
  354. if (this.isLogin) {
  355. getUserInfo().then(res => {
  356. let configAppMessage = {
  357. desc: this.$t(`分销海报`),
  358. title: res.data.nickname + '-' + this.$t(`分销海报`),
  359. link: '/pages/index/index?spread=' + res.data.uid,
  360. imgUrl: this.spreadList[0]
  361. };
  362. this.$wechat.wechatEvevt(["updateAppMessageShareData", "updateTimelineShareData"],
  363. configAppMessage)
  364. });
  365. } else {
  366. toLogin();
  367. }
  368. }
  369. },
  370. userSpreadBannerList: function() {
  371. let that = this;
  372. uni.showLoading({
  373. title: that.$t(`获取中`),
  374. mask: true,
  375. })
  376. spreadBanner().then(res => {
  377. uni.hideLoading();
  378. that.$set(that, 'spreadList', res.data);
  379. that.$set(that, 'poster', res.data[0].poster);
  380. // #ifdef H5
  381. that.setShareInfoStatus();
  382. // #endif
  383. }).catch(err => {
  384. uni.hideLoading();
  385. });
  386. }
  387. }
  388. }
  389. </script>
  390. <style lang="scss">
  391. page {
  392. background-color: #a3a3a3 !important;
  393. }
  394. .canvas {
  395. width: 100%;
  396. // height: 550px;
  397. }
  398. .box {
  399. width: 100%;
  400. height: 100%;
  401. position: relative;
  402. border-radius: 18rpx;
  403. overflow: hidden;
  404. .user-msg {
  405. position: absolute;
  406. width: 100%;
  407. height: 100%;
  408. display: flex;
  409. align-items: center;
  410. justify-content: center;
  411. .user-code {
  412. width: 100%;
  413. // height: 100%;
  414. display: flex;
  415. align-items: center;
  416. justify-content: center;
  417. justify-content: space-between;
  418. image {
  419. width: 100%;
  420. }
  421. }
  422. }
  423. }
  424. .img-list {
  425. margin-right: 40px;
  426. }
  427. .distribution-posters swiper {
  428. width: 100%;
  429. height: 1000rpx;
  430. position: relative;
  431. margin-top: 40rpx;
  432. }
  433. .distribution-posters .slide-image {
  434. width: 100%;
  435. height: 100%;
  436. margin: 0 auto;
  437. border-radius: 15rpx;
  438. }
  439. .distribution-posters /deep/.active {
  440. transform: none;
  441. transition: all 0.2s ease-in 0s;
  442. }
  443. .distribution-posters /deep/ .quiet {
  444. transform: scale(0.8333333);
  445. transition: all 0.2s ease-in 0s;
  446. }
  447. .distribution-posters .keep {
  448. font-size: 30rpx;
  449. color: #fff;
  450. width: 600rpx;
  451. height: 80rpx;
  452. border-radius: 50rpx;
  453. text-align: center;
  454. line-height: 80rpx;
  455. margin: 38rpx auto;
  456. }
  457. .distribution-posters .preserve {
  458. color: #fff;
  459. text-align: center;
  460. margin-top: 38rpx;
  461. }
  462. .distribution-posters .preserve .line {
  463. width: 100rpx;
  464. height: 1px;
  465. background-color: #fff;
  466. }
  467. .distribution-posters .preserve .tip {
  468. margin: 0 30rpx;
  469. }
  470. </style>