sharebox.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <template>
  2. <view :class="[AppTheme]">
  3. <u-popup :show="ifshare" mode="bottom">
  4. <view class="sharebox">
  5. <view class="shareitem_title">分享到:</view>
  6. <view class="shareitem_list">
  7. <!-- #ifdef MP-WEIXIN -->
  8. <view class="shareitem" v-for="(item,index) in shareitemlist" :key="index" @click="sharemsg(item)">
  9. <u-icon v-if="item.provider!='weixin'" labelPos="bottom" :label="item.title" labelSize="12"
  10. size="40" :name="reqimg(item.imgsrc,item.type)"></u-icon>
  11. <button v-else class="separate" plain="true" data-name="shareBtn" open-type="share">
  12. <u-icon labelPos="bottom" :label="item.title" labelSize="12" size="40"
  13. :name="reqimg(item.imgsrc,item.type)"></u-icon>
  14. </button>
  15. </view>
  16. <!-- #endif -->
  17. <!-- #ifndef MP-WEIXIN -->
  18. <view class="shareitem" v-for="(item,index) in shareitemlist" :key="index" @click="sharemsg(item)">
  19. <u-icon labelPos="bottom" :label="item.title" labelSize="12" size="40"
  20. :name="reqimg(item.imgsrc,item.type)"></u-icon>
  21. </view>
  22. <!-- #endif -->
  23. </view>
  24. <view class="sharecanle" @click="colosesharebox()">取消</view>
  25. </view>
  26. </u-popup>
  27. <u-popup :show="showalert" mode="center">
  28. <view class="dAlert" v-show="showalert">
  29. <view class="wrapperAlert" @click="closeModal">
  30. <view class="wrapperAlertBox" @click.stop="" @touchmove.stop.prevent="">
  31. <view class="vueDAlertWrapper" style="border-radius: 20rpx;">
  32. <view style="width: 100%;height: 50rpx;"></view>
  33. <view class="vueDAlertWrapperTop" style="justify-content: center;display: flex;">
  34. <u--image :src="sharedata.imageUrl" mode="aspectFit" width="600rpx" height="900rpx">
  35. <template v-slot:loading>
  36. <u-loading-icon color="#afafaf"></u-loading-icon>
  37. </template>
  38. </u--image>
  39. </view>
  40. <view class="vueDAlertWrapperBtn" @click.stop="saveImage()">
  41. 长安海报进行保存或者分享
  42. </view>
  43. <view style="justify-content: center;display: flex;" @click="closeModal()">
  44. <u-icon name="close-circle" color="#ffffff" size="28"></u-icon>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </u-popup>
  51. </view>
  52. </template>
  53. <script>
  54. // #ifdef H5
  55. import FileSaver from 'file-saver'
  56. import wxchar from '@/js/wxchar.js'
  57. // #endif
  58. export default {
  59. props: ["data"],
  60. data() {
  61. return {
  62. depng: '@/static/imgs/del.png',
  63. showalert: false,
  64. primary: this.$theme.primary,
  65. settingFile: getApp().globalData.siteinfo,
  66. sharedata: [],
  67. ifshare: false,
  68. shareitemlist: [
  69. // {
  70. // title: '微信好友',
  71. // provider: 'weixin',
  72. // type: 1,
  73. // imgsrc: 'weixin.png'
  74. // },
  75. // {
  76. // title: 'QQ好友',
  77. // provider: 'qq',
  78. // type: 1,
  79. // imgsrc: 'qq.png'
  80. // },
  81. // {
  82. // title: 'QQ空间',
  83. // provider: 'qq',
  84. // type: 1,
  85. // imgsrc: 'qqkjian.png'
  86. // },
  87. // {
  88. // title: '复制链接',
  89. // provider: 'copyhref',
  90. // type: 1,
  91. // imgsrc: 'href.png'
  92. // },
  93. {
  94. title: '保存图片',
  95. provider: 'saveimg',
  96. type: 2,
  97. imgsrc: 'download'
  98. }
  99. ]
  100. }
  101. },
  102. created() {
  103. let that = this;
  104. if (that.data) {
  105. that.sharedata = that.data
  106. } else {
  107. that.sharedata = that.$config.sharedata
  108. }
  109. // #ifdef H5
  110. let wxdata = {
  111. title: that.sharedata.title,
  112. desc: that.sharedata.title,
  113. link: that.sharedata.path,
  114. imageUrl: that.sharedata.imageUrl,
  115. };
  116. wxchar.initWeixinShareConfig(wxdata);
  117. that.shareitemlist = [{
  118. title: '复制链接',
  119. provider: 'copyhref',
  120. type: 1,
  121. imgsrc: 'href.png'
  122. },
  123. {
  124. title: '保存图片',
  125. provider: 'saveimg',
  126. type: 2,
  127. imgsrc: 'download'
  128. }
  129. ]
  130. // #endif
  131. // #ifdef APP-PLUS
  132. that.shareitemlist = [{
  133. title: '微信好友',
  134. provider: 'weixin',
  135. type: 1,
  136. imgsrc: 'weixin.png'
  137. },
  138. {
  139. title: '微信朋友圈',
  140. provider: 'weixin',
  141. type: 1,
  142. imgsrc: 'pyq.png'
  143. }, {
  144. title: '复制链接',
  145. provider: 'copyhref',
  146. type: 1,
  147. imgsrc: 'href.png'
  148. },
  149. {
  150. title: '保存图片',
  151. provider: 'saveimg',
  152. type: 2,
  153. imgsrc: 'download'
  154. }
  155. ]
  156. // #endif
  157. },
  158. methods: {
  159. //保存图片到相册
  160. saveImage() {
  161. //判断用户授权
  162. var that = this
  163. uni.getSetting({
  164. success(res) {
  165. console.log('获取用户权限', res.authSetting)
  166. if (Object.keys(res.authSetting).length > 0) {
  167. //判断是否有相册权限
  168. if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
  169. that.authorize();
  170. } else {
  171. if (!res.authSetting['scope.writePhotosAlbum']) {
  172. that.authorize();
  173. }
  174. }
  175. } else {
  176. return
  177. }
  178. }
  179. })
  180. that.saveImg(that.sharedata.imageUrl);
  181. },
  182. authorize() {
  183. let scope = 'scope.writePhotosAlbum'
  184. return new Promise((resolve, reject) => {
  185. wx.getSetting({
  186. success: res => {
  187. if (!res.authSetting[scope]) {
  188. wx.authorize({
  189. scope,
  190. success: resolve,
  191. fail: () => wx.showModal({
  192. content: R.string
  193. .prompt_authorize_photos, // 请允许小程序使用相册权限
  194. success: () => wx.openSetting({
  195. success: res => {
  196. if (res.authSetting[
  197. scope]) {
  198. resolve(res
  199. .authSetting)
  200. } else {
  201. wx.showModal({
  202. content: R
  203. .string
  204. .prompt_authorize_cancel
  205. }) // 未允许使用该权限
  206. reject(res.authSetting)
  207. }
  208. }
  209. })
  210. })
  211. })
  212. } else {
  213. resolve(res.authSetting)
  214. }
  215. }
  216. })
  217. })
  218. },
  219. closeModal() {
  220. this.showalert = false;
  221. },
  222. // 引入图片
  223. reqimg(imgsrc, type = 1) {
  224. if (type == 2) {
  225. return imgsrc;
  226. } else {
  227. return this.settingFile.root_img + '/static/app/imgs/farm/' + imgsrc;
  228. }
  229. },
  230. //打开
  231. opensharebox(data = null) {
  232. let that = this;
  233. if (data) {
  234. that.sharedata = data;
  235. }
  236. this.ifshare = true;
  237. // #ifdef H5
  238. let wxdata = {
  239. title: that.sharedata.title,
  240. desc: that.sharedata.title,
  241. link: that.sharedata.paht,
  242. imageUrl: that.sharedata.imageUrl,
  243. };
  244. wxchar.initWeixinShareConfig(wxdata);
  245. // #endif
  246. console.log('that.sharedata', that.sharedata)
  247. },
  248. // 关闭分享窗口
  249. colosesharebox() {
  250. this.ifshare = false;
  251. },
  252. // 分享
  253. sharemsg(item) {
  254. let that = this
  255. that.colosesharebox()
  256. switch (item.provider) {
  257. //微信分支
  258. case 'weixin':
  259. // #ifndef MP-WEIXIN
  260. let scene = ''
  261. //判定是发送到微信好友还有朋友圈
  262. if (item.title === '微信好友') {
  263. scene = 'WXSceneSession'
  264. } else {
  265. scene = 'WXSceneTimeline'
  266. }
  267. uni.share({
  268. provider: item.provider,
  269. type: 0,
  270. scene: scene,
  271. title: that.sharedata.title,
  272. imageUrl: that.sharedata.imageUrl,
  273. summary: that.sharedata.title,
  274. href: that.sharedata.path,
  275. success: function(res) {
  276. uni.showToast({
  277. title: '分享成功',
  278. icon: 'none',
  279. position: 'top'
  280. })
  281. },
  282. fail: function(err) {
  283. uni.showToast({
  284. title: '分享失败',
  285. icon: 'none',
  286. position: 'top'
  287. })
  288. }
  289. });
  290. // #endif
  291. break;
  292. case 'qq':
  293. uni.share({
  294. provider: item.provider,
  295. type: 2,
  296. title: that.sharedata.title,
  297. imageUrl: that.sharedata.imageUrl,
  298. href: that.sharedata.path,
  299. success: function(res) {
  300. uni.showToast({
  301. title: '分享成功',
  302. icon: 'none',
  303. position: 'top'
  304. })
  305. },
  306. fail: function(err) {
  307. uni.showToast({
  308. title: '分享失败',
  309. icon: 'none',
  310. position: 'top'
  311. })
  312. }
  313. });
  314. break;
  315. case 'copyhref':
  316. var context = that.sharedata.title + "\r\n-------------\r\n打开链接:" + that.sharedata.tourl
  317. console.log('context', context)
  318. uni.setClipboardData({
  319. data: context,
  320. success() {
  321. uni.showToast({
  322. title: '已复制到剪贴板',
  323. icon: 'none',
  324. position: 'top'
  325. })
  326. }
  327. })
  328. break;
  329. case 'saveimg':
  330. // #ifdef H5
  331. let isWeixin = that.isWeiXin();
  332. console.log('isWeixin', isWeixin)
  333. if (isWeixin) {
  334. this.showalert = true;
  335. } else {
  336. FileSaver.saveAs(that.sharedata.imageUrl, 'saveimg.jpg');
  337. }
  338. // #endif
  339. // #ifndef H5
  340. that.saveImg(that.sharedata.imageUrl);
  341. // #endif
  342. break;
  343. }
  344. },
  345. saveImg(url) {
  346. const that = this;
  347. console.log('url',url)
  348. uni.downloadFile({
  349. url: url,
  350. success: res => {
  351. if (res.statusCode === 200) {
  352. uni.saveImageToPhotosAlbum({
  353. filePath: res.tempFilePath,
  354. success: function() {
  355. uni.showToast({
  356. title: '保存下载成功',
  357. icon: 'none',
  358. position: 'top'
  359. })
  360. },
  361. fail: function() {
  362. uni.showToast({
  363. title: '保存失败,请稍后重试',
  364. icon: 'none',
  365. position: 'top'
  366. })
  367. }
  368. });
  369. } else {
  370. uni.showToast({
  371. title: '下载失败',
  372. icon: 'none',
  373. position: 'top'
  374. })
  375. }
  376. }
  377. });
  378. }
  379. }
  380. }
  381. </script>
  382. <style lang="scss" scoped>
  383. .separate {
  384. // opacity: 0;
  385. border: 0px;
  386. }
  387. .wrapperAlert {
  388. z-index: 12000;
  389. position: fixed;
  390. top: 0;
  391. left: 0;
  392. right: 0;
  393. bottom: 100;
  394. transition: background-color .15s linear;
  395. display: flex;
  396. align-items: center;
  397. justify-content: center;
  398. .vueDAlertWrapperBtn {
  399. padding: 30rpx;
  400. justify-content: center;
  401. display: flex;
  402. color: #FFFFFF;
  403. }
  404. .wrapperAlertBox {
  405. width: 90%;
  406. border-radius: 15rpx;
  407. .wrapperAlertBoxTitle {
  408. width: 100%;
  409. height: 30rpx;
  410. display: flex;
  411. justify-content: center;
  412. align-items: center;
  413. color: #762cd9;
  414. }
  415. .wrapperAlertBoxCenter {
  416. width: 100%;
  417. height: 135rpx;
  418. display: flex;
  419. justify-content: center;
  420. align-items: center;
  421. color: #C0C0C0;
  422. color: red;
  423. }
  424. .wrapperAlertBoxBit {
  425. width: 100%;
  426. height: 1rpx;
  427. background-color: #f2f2f2;
  428. }
  429. .wrapperAlertBoxFooter {
  430. width: 100%;
  431. height: 100rpx;
  432. display: flex;
  433. justify-content: space-around;
  434. align-items: center;
  435. .wrapperAlertBoxFooterBox {
  436. width: 290rpx;
  437. height: 115rpx;
  438. display: flex;
  439. justify-content: center;
  440. align-items: center;
  441. }
  442. .wrapperAlertBoxFooterBit {
  443. width: 1rpx;
  444. height: 40rpx;
  445. background: #f2f2f2;
  446. }
  447. }
  448. }
  449. }
  450. .sharebox {
  451. background-color: #FFFFFF;
  452. border-radius: 30rpx 30rpx 0 0;
  453. width: calc(100% - 50rpx);
  454. padding: 20rpx 25rpx 0 25rpx;
  455. .shareitem_title {
  456. color: #999;
  457. font-size: 25rpx;
  458. }
  459. .shareitem_list {
  460. display: flex;
  461. justify-content: flex-start;
  462. flex-wrap: wrap;
  463. border-bottom: 1rpx solid #f3f3f3;
  464. .shareitem {
  465. width: 28%;
  466. margin: 20rpx 2.5%;
  467. text-align: center;
  468. .shareitem_thu {
  469. width: 80rpx;
  470. height: 80rpx;
  471. }
  472. .shareitem_text {
  473. color: #666;
  474. font-size: 22rpx;
  475. margin-top: 15rpx;
  476. }
  477. }
  478. }
  479. .sharecanle {
  480. width: 100%;
  481. font-size: 32rpx;
  482. text-align: center;
  483. color: #777;
  484. height: 100rpx;
  485. line-height: 100rpx;
  486. }
  487. }
  488. </style>