index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <view>
  3. <view class='distribution-posters'>
  4. <swiper :indicator-dots="indicatorDots" :autoplay="autoplay" :circular="circular" :interval="interval" :duration="duration"
  5. @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]" v-if="posterImage[index]"></image>
  12. <canvas class="canvas" :style="{height:hg+'px'}" :canvas-id="'myCanvas'+ index" v-else></canvas>
  13. </view>
  14. </view>
  15. </div>
  16. <!-- <image :src="item.wap_poster" class="slide-image" :class="swiperIndex == index ? 'active' : 'quiet'" mode='aspectFill' /> -->
  17. </swiper-item>
  18. </block>
  19. </swiper>
  20. <!-- #ifndef H5 -->
  21. <view class='keep bg-color' @click='savePosterPathMp(posterImage[swiperIndex])'>保存海报</view>
  22. <!-- #endif -->
  23. <!-- #ifndef MP -->
  24. <div class="preserve acea-row row-center-wrapper">
  25. <div class="line"></div>
  26. <div class="tip">长按保存图片</div>
  27. <div class="line"></div>
  28. </div>
  29. <!-- #endif -->
  30. </view>
  31. <!-- #ifdef MP -->
  32. <!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
  33. <!-- #endif -->
  34. <home></home>
  35. <view class="qrimg">
  36. <zb-code ref="qrcode" :show="codeShow" :cid="cid" :val="val" :size="size" :unit="unit" :background="background"
  37. :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconsize" :onval="onval" :loadMake="loadMake"
  38. @result="qrR" />
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import zbCode from '@/components/zb-code/zb-code.vue'
  44. import {
  45. getUserInfo,
  46. spreadBanner,
  47. userShare,
  48. spreadMsg,
  49. imgToBase
  50. } from '@/api/user.js';
  51. import {
  52. toLogin
  53. } from '@/libs/login.js';
  54. import {
  55. mapGetters
  56. } from "vuex";
  57. // #ifdef MP
  58. import authorize from '@/components/Authorize';
  59. // #endif
  60. import home from '@/components/home';
  61. export default {
  62. components: {
  63. // #ifdef MP
  64. authorize,
  65. // #endif
  66. home,
  67. zbCode
  68. },
  69. data() {
  70. return {
  71. imgUrls: [],
  72. indicatorDots: false,
  73. posterImageStatus: true,
  74. circular: false,
  75. autoplay: false,
  76. interval: 3000,
  77. duration: 500,
  78. swiperIndex: 0,
  79. spreadList: [],
  80. userInfo: {},
  81. poster: '',
  82. isAuto: false, //没有授权的不会自动授权
  83. isShowAuth: false, //是否隐藏授权
  84. spreadData: [{}], //新海报数据
  85. nickName: "",
  86. siteName: "",
  87. mpUrl: "",
  88. canvasImageUrl: '',
  89. posterImage: [],
  90. //二维码参数
  91. codeShow: false,
  92. cid: '1',
  93. ifShow: true,
  94. val: "", // 要生成的二维码值
  95. size: 200, // 二维码大小
  96. unit: 'upx', // 单位
  97. background: '#FFF', // 背景色
  98. foreground: '#000', // 前景色
  99. pdground: '#32dbc6', // 角标色
  100. icon: '', // 二维码图标
  101. iconsize: 40, // 二维码图标大小
  102. lv: 3, // 二维码容错级别 , 一般不用设置,默认就行
  103. onval: true, // val值变化时自动重新生成二维码
  104. loadMake: true, // 组件加载完成后自动生成二维码
  105. src: '', // 二维码生成后的图片地址或base64
  106. codeSrc: "",
  107. wd: 225,
  108. hg: 500
  109. };
  110. },
  111. computed: mapGetters({
  112. 'isLogin': 'isLogin',
  113. 'userData': 'userInfo'
  114. }),
  115. watch: {
  116. isLogin: {
  117. handler: function(newV, oldV) {
  118. if (newV) {
  119. this.userSpreadBannerList();
  120. }
  121. },
  122. deep: true
  123. },
  124. userData: {
  125. handler: function(newV, oldV) {
  126. if (newV) {
  127. this.$set(this, 'userInfo', newV);
  128. }
  129. },
  130. deep: true
  131. }
  132. },
  133. async onReady() {
  134. if (this.isLogin) {
  135. // #ifdef H5
  136. let userData = await getUserInfo()
  137. this.val = window.location.host + '?spread=' + userData.data.uid
  138. // #endif
  139. await this.spreadMsg()
  140. // this.longTaop()
  141. } else {
  142. toLogin();
  143. }
  144. },
  145. onShow() {
  146. this.$nextTick(() => {
  147. let selector = uni.createSelectorQuery().select('.aaa');
  148. selector.fields({
  149. size: true
  150. }, data => {
  151. console.log(data)
  152. this.wd = data.width
  153. this.hg = data.height
  154. }).exec();
  155. })
  156. },
  157. /**
  158. * 用户点击右上角分享
  159. */
  160. // #ifdef MP
  161. onShareAppMessage: function() {
  162. userShare();
  163. return {
  164. title: this.userInfo.nickname + '-分销海报',
  165. imageUrl: this.spreadList[0],
  166. path: '/pages/index/index?spid=' + this.userInfo.uid,
  167. };
  168. },
  169. // #endif
  170. methods: {
  171. onLoadFun: function(e) {
  172. this.$set(this, 'userInfo', e);
  173. this.userSpreadBannerList();
  174. },
  175. qrR(res) {
  176. this.codeSrc = res
  177. },
  178. //获取图片
  179. async spreadMsg() {
  180. let rqData = {
  181. // #ifdef MP
  182. type: 'routine',
  183. // #endif
  184. // #ifndef MP
  185. type: 'h5',
  186. // #endif
  187. }
  188. let res = await spreadMsg(rqData)
  189. this.spreadData = res.data.poster
  190. this.nickName = res.data.nickname
  191. this.siteName = res.data.site_name
  192. let codeUrl = "?spread=" + this.userInfo.uid
  193. // #ifdef MP
  194. let mpUrl = await this.downloadFilestoreImage(res.data.qrcode)
  195. // #endif
  196. uni.showLoading({
  197. title: '海报生成中',
  198. mask: true
  199. });
  200. for (let i = 0; i < res.data.poster.length; i++) {
  201. let that = this
  202. let arr2
  203. // #ifdef MP
  204. arr2 = [mpUrl, await this.downloadFilestoreImage(res.data.poster[i].pic)]
  205. // #endif
  206. // #ifdef H5
  207. let img = await this.imgToBase(res.data.poster[i].pic)
  208. arr2 = [res.data.qrcode, img]
  209. // #endif
  210. that.$util.userPosterCanvas(arr2, res.data.nickname, res.data.mark, i, this.wd, this.hg, (tempFilePath) => {
  211. that.$set(that.posterImage, i, tempFilePath);
  212. });
  213. }
  214. uni.hideLoading();
  215. },
  216. downloadImg() {
  217. uni.saveImageToPhotosAlbum({
  218. filePath: this.posterImage[this.swiperIndex],
  219. success: function() {
  220. console.log('save success');
  221. }
  222. });
  223. },
  224. async imgToBase(url) {
  225. let res = await imgToBase({
  226. image: url
  227. })
  228. return res.data.image
  229. },
  230. // 二维码生成
  231. codeImg() {
  232. // http://当前域名+"?spread="+用户uid
  233. },
  234. // 授权关闭
  235. authColse: function(e) {
  236. this.isShowAuth = e
  237. },
  238. bindchange(e) {
  239. let spreadList = this.spreadList;
  240. this.swiperIndex = e.detail.current;
  241. // this.$set(this, 'poster', spreadList[e.detail.current].poster);
  242. },
  243. savePosterPathMp(url) {
  244. let that = this;
  245. uni.getSetting({
  246. success(res) {
  247. if (!res.authSetting['scope.writePhotosAlbum']) {
  248. uni.authorize({
  249. scope: 'scope.writePhotosAlbum',
  250. success() {
  251. uni.saveImageToPhotosAlbum({
  252. filePath: url,
  253. success: function(res) {
  254. that.$util.Tips({
  255. title: '保存成功',
  256. icon: 'success'
  257. });
  258. },
  259. fail: function(res) {
  260. that.$util.Tips({
  261. title: '保存失败'
  262. });
  263. }
  264. });
  265. }
  266. });
  267. } else {
  268. uni.saveImageToPhotosAlbum({
  269. filePath: url,
  270. success: function(res) {
  271. that.$util.Tips({
  272. title: '保存成功',
  273. icon: 'success'
  274. });
  275. },
  276. fail: function(res) {
  277. that.$util.Tips({
  278. title: '保存失败'
  279. });
  280. }
  281. });
  282. }
  283. }
  284. });
  285. },
  286. //图片转符合安全域名路径
  287. downloadFilestoreImage(url) {
  288. return new Promise((resolve, reject) => {
  289. let that = this;
  290. uni.downloadFile({
  291. url: url,
  292. success: function(res) {
  293. resolve(res.tempFilePath);
  294. },
  295. fail: function() {
  296. return that.$util.Tips({
  297. title: ''
  298. });
  299. }
  300. });
  301. })
  302. },
  303. setShareInfoStatus: function() {
  304. if (this.$wechat.isWeixin()) {
  305. if (this.isLogin) {
  306. getUserInfo().then(res => {
  307. let configAppMessage = {
  308. desc: '分销海报',
  309. title: res.data.nickname + '-分销海报',
  310. link: '/pages/index/index?spread=' + res.data.uid,
  311. imgUrl: this.spreadList[0]
  312. };
  313. this.$wechat.wechatEvevt(["updateAppMessageShareData", "updateTimelineShareData"], configAppMessage)
  314. });
  315. } else {
  316. toLogin();
  317. }
  318. }
  319. },
  320. userSpreadBannerList: function() {
  321. let that = this;
  322. uni.showLoading({
  323. title: '获取中',
  324. mask: true,
  325. })
  326. spreadBanner().then(res => {
  327. uni.hideLoading();
  328. that.$set(that, 'spreadList', res.data);
  329. that.$set(that, 'poster', res.data[0].poster);
  330. // #ifdef H5
  331. that.setShareInfoStatus();
  332. // #endif
  333. }).catch(err => {
  334. uni.hideLoading();
  335. });
  336. }
  337. }
  338. }
  339. </script>
  340. <style lang="scss">
  341. page {
  342. background-color: #a3a3a3 !important;
  343. }
  344. .canvas {
  345. width: 100%;
  346. // height: 550px;
  347. }
  348. .box {
  349. width: 100%;
  350. height: 100%;
  351. position: relative;
  352. border-radius: 18rpx;
  353. overflow: hidden;
  354. .user-msg {
  355. position: absolute;
  356. width: 100%;
  357. height: 100%;
  358. display: flex;
  359. align-items: center;
  360. justify-content: center;
  361. .user-code {
  362. width: 100%;
  363. // height: 100%;
  364. display: flex;
  365. align-items: center;
  366. justify-content: center;
  367. justify-content: space-between;
  368. image {
  369. width: 100%;
  370. }
  371. }
  372. }
  373. }
  374. .img-list {
  375. margin-right: 40px;
  376. }
  377. .distribution-posters swiper {
  378. width: 100%;
  379. height: 1000rpx;
  380. position: relative;
  381. margin-top: 40rpx;
  382. }
  383. .distribution-posters .slide-image {
  384. width: 100%;
  385. height: 100%;
  386. margin: 0 auto;
  387. border-radius: 15rpx;
  388. }
  389. .distribution-posters /deep/.active {
  390. transform: none;
  391. transition: all 0.2s ease-in 0s;
  392. }
  393. .distribution-posters /deep/ .quiet {
  394. transform: scale(0.8333333);
  395. transition: all 0.2s ease-in 0s;
  396. }
  397. .distribution-posters .keep {
  398. font-size: 30rpx;
  399. color: #fff;
  400. width: 600rpx;
  401. height: 80rpx;
  402. border-radius: 50rpx;
  403. text-align: center;
  404. line-height: 80rpx;
  405. margin: 38rpx auto;
  406. }
  407. .distribution-posters .preserve {
  408. color: #fff;
  409. text-align: center;
  410. margin-top: 38rpx;
  411. }
  412. .distribution-posters .preserve .line {
  413. width: 100rpx;
  414. height: 1px;
  415. background-color: #fff;
  416. }
  417. .distribution-posters .preserve .tip {
  418. margin: 0 30rpx;
  419. }
  420. </style>