index.vue 12 KB

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