index.vue 11 KB

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