index.vue 11 KB

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