index.vue 13 KB

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