index.vue 12 KB

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