index.vue 11 KB

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