sharebox.vue 11 KB

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