share-with-system.uvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view direction="vertical" style="flex:1;">
  4. <!-- #endif -->
  5. <view id="viewshot">
  6. <button class="button" @click="shareText()">分享文本</button>
  7. <button class="button" @click="shareLink()">分享链接</button>
  8. <button class="button" @click="sharePrivateImg()">分享单个本地图片</button>
  9. <button class="button" @click="sharePrivateImgs()">分享多个本地图片</button>
  10. <button class="button" @click="shareAll()">分享链接、文本、一张图片</button>
  11. <button class="button" @click="sharePrivateVideo()">分享video文件(单个)</button>
  12. <button class="button" @click="sharePrivateVideos()">分享video文件(多个)</button>
  13. <button class="button" @click="sharePrivateAudio()">分享Audio文件(单个)</button>
  14. <button class="button" @click="sharePrivateAudios()">分享Audio文件(多个)</button>
  15. <button class="button" @click="sharePrivateFile()">分享文件(单个)</button>
  16. <button class="button" @click="sharePrivateFiles()">分享文件(多个)</button>
  17. <button class="button" @click="sharePubImg()">选择图片并分享</button>
  18. <button class="button" @click="sharePubMedias()">选择video并分享</button>
  19. <button class="button" @click="shareSnapShot()">指定view截图并分享</button>
  20. <button class="button" type="warn" @click="sharePrivateErrorImg()">分享单个本地图片(错误路径)</button>
  21. <button class="button" type="warn" @click="sharePrivateErrorImgs()">分享多个本地图片(含有错误路径)</button>
  22. <button class="button" type="warn" @click="sharePrivateErrorVideos()">分享Video文件(错误路径)</button>
  23. <button class="button" type="warn" @click="sharePrivateErrorAudios()">分享Audio文件(错误路径)</button>
  24. <button class="button" type="warn" @click="sharePrivateErrorFiles()">分享文件(错误路径)</button>
  25. </view>
  26. </scroll-view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. summary: '欢迎使用hello uniapp-x'
  33. }
  34. },
  35. methods: {
  36. sharePrivateErrorAudios() {
  37. uni.hideToast()
  38. const path1 : string = "/static/test-audio/ForElise.mp3";
  39. const path2 : string = "/static/test-audio/ForElise.mp32";
  40. uni.shareWithSystem({
  41. audioPaths: [path1, path2],
  42. type:'audio',
  43. success() {
  44. console.log('Shared----------------------------success')
  45. },
  46. fail(res) {
  47. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  48. uni.showToast({
  49. icon: "error",
  50. title: "errorCode=" + res.errCode
  51. })
  52. },
  53. complete(_) {
  54. }
  55. })
  56. },
  57. sharePrivateErrorFiles() {
  58. uni.hideToast()
  59. const path1 : string = "/static/filemanager1/to.zip";
  60. const path2 : string = "/static/filemanager/11.txt.br";
  61. uni.shareWithSystem({
  62. filePaths: [path1, path2],
  63. type:'file',
  64. success(_) {
  65. console.log('Shared----------------------------success')
  66. },
  67. fail(res) {
  68. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  69. uni.showToast({
  70. icon: "error",
  71. title: "errorCode=" + res.errCode
  72. })
  73. },
  74. complete(_) {
  75. }
  76. })
  77. },
  78. sharePrivateFile() {
  79. uni.hideToast()
  80. const path1 : string = "/static/filemanager/to.zip";
  81. uni.shareWithSystem({
  82. filePaths: [path1],
  83. type:'file',
  84. success(_) {
  85. console.log('Shared----------------------------success')
  86. },
  87. fail(res) {
  88. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  89. uni.showToast({
  90. icon: "error",
  91. title: "errorCode=" + res.errCode
  92. })
  93. },
  94. complete(_) {
  95. }
  96. })
  97. },
  98. sharePrivateFiles() {
  99. uni.hideToast()
  100. const path1 : string = "/static/filemanager/to.zip";
  101. const path2 : string = "/static/filemanager/1.txt.br";
  102. uni.shareWithSystem({
  103. filePaths: [path1, path2],
  104. type:'file',
  105. success(_) {
  106. console.log('Shared----------------------------success')
  107. },
  108. fail(res) {
  109. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  110. uni.showToast({
  111. icon: "error",
  112. title: "errorCode=" + res.errCode
  113. })
  114. },
  115. complete(_) {
  116. }
  117. })
  118. },
  119. sharePrivateAudio() {
  120. uni.hideToast()
  121. const path1 : string = "/static/test-audio/ForElise.mp3";
  122. uni.shareWithSystem({
  123. audioPaths: [path1],
  124. type:'audio',
  125. success(_) {
  126. console.log('Shared----------------------------success')
  127. },
  128. fail(res) {
  129. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  130. uni.showToast({
  131. icon: "error",
  132. title: "errorCode=" + res.errCode
  133. })
  134. },
  135. complete(_) {
  136. }
  137. })
  138. },
  139. sharePrivateAudios() {
  140. uni.hideToast()
  141. const path1 : string = "/static/test-audio/ForElise.mp3";
  142. const path2 : string = "/static/test-audio/ForElise.mp3";
  143. uni.shareWithSystem({
  144. audioPaths: [path1, path2],
  145. type:'audio',
  146. success(_) {
  147. console.log('Shared----------------------------success')
  148. },
  149. fail(res) {
  150. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  151. uni.showToast({
  152. icon: "error",
  153. title: "errorCode=" + res.errCode
  154. })
  155. },
  156. complete(_) {
  157. }
  158. })
  159. },
  160. sharePrivateErrorVideos() {
  161. uni.hideToast()
  162. const path1 : string = "/static/test-video/10second-demo.mp4";
  163. const path2 : string = "/static/test-video/10second-demo1.mp4";
  164. uni.shareWithSystem({
  165. videoPaths: [path1, path2],
  166. type:'video',
  167. success(_) {
  168. console.log('Shared----------------------------success')
  169. },
  170. fail(res) {
  171. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  172. uni.showToast({
  173. icon: "error",
  174. title: "errorCode=" + res.errCode
  175. })
  176. },
  177. complete(_) {
  178. }
  179. })
  180. },
  181. sharePrivateVideo() {
  182. uni.hideToast()
  183. const path1 : string = "/static/test-video/10second-demo.mp4";
  184. uni.shareWithSystem({
  185. videoPaths: [path1],
  186. type:'video',
  187. success(_) {
  188. console.log('Shared----------------------------success')
  189. },
  190. fail(res) {
  191. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  192. uni.showToast({
  193. icon: "error",
  194. title: "errorCode=" + res.errCode
  195. })
  196. },
  197. complete(_) {
  198. }
  199. })
  200. },
  201. sharePrivateVideos() {
  202. uni.hideToast()
  203. const path1 : string = "/static/test-video/10second-demo.mp4";
  204. const path2 : string = "/static/test-video/10second-demo.mp4";
  205. uni.shareWithSystem({
  206. videoPaths: [path1, path2],
  207. type:'video',
  208. success(_) {
  209. console.log('Shared----------------------------success')
  210. },
  211. fail(res) {
  212. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  213. uni.showToast({
  214. icon: "error",
  215. title: "errorCode=" + res.errCode
  216. })
  217. },
  218. complete(_) {
  219. }
  220. })
  221. },
  222. sharePubMedias() {
  223. uni.hideToast()
  224. uni.chooseVideo({
  225. success(res) {
  226. uni.shareWithSystem({
  227. videoPaths: [res.tempFilePath],
  228. type:'video',
  229. success(_) {
  230. console.log('Shared----------------------------success')
  231. },
  232. fail(res) {
  233. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  234. uni.showToast({
  235. icon: "error",
  236. title: "errorCode=" + res.errCode
  237. })
  238. },
  239. complete(_) {
  240. }
  241. })
  242. }
  243. })
  244. },
  245. shareText() {
  246. uni.hideToast()
  247. uni.shareWithSystem({
  248. summary: this.summary,
  249. type:'text',
  250. success(_) {
  251. console.log('Shared----------------------------success')
  252. },
  253. fail(res) {
  254. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  255. uni.showToast({
  256. icon: "error",
  257. title: "errorCode=" + res.errCode
  258. })
  259. },
  260. complete(_) {
  261. }
  262. })
  263. },
  264. shareLink() {
  265. uni.hideToast()
  266. uni.shareWithSystem({
  267. type:'text',
  268. href: 'https://uniapp.dcloud.io',
  269. success(_) {
  270. console.log('Shared----------------------------success')
  271. },
  272. fail(res) {
  273. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  274. uni.showToast({
  275. icon: "error",
  276. title: "errorCode=" + res.errCode
  277. })
  278. },
  279. complete(_) {
  280. }
  281. })
  282. },
  283. sharePrivateImg() {
  284. uni.hideToast()
  285. const imageSrc : string = "/static/test-image/logo.gif";
  286. uni.shareWithSystem({
  287. type:'image',
  288. imageUrl: imageSrc,
  289. success(_) {
  290. console.log('Shared----------------------------success')
  291. // 分享完成,请注意此时不一定是成功分享
  292. },
  293. fail(res) {
  294. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  295. uni.showToast({
  296. icon: "error",
  297. title: "errorCode=" + res.errCode
  298. })
  299. // 分享失败
  300. },
  301. complete(_) {
  302. }
  303. })
  304. },
  305. sharePrivateErrorImg() {
  306. uni.hideToast()
  307. const imageSrc : string = "/static/test-image/logo.jpg11";
  308. uni.shareWithSystem({
  309. imageUrl: imageSrc,
  310. type:'image',
  311. success(_) {
  312. console.log('Shared----------------------------success')
  313. // 分享完成,请注意此时不一定是成功分享
  314. },
  315. fail(res) {
  316. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  317. uni.showToast({
  318. icon: "error",
  319. title: "errorCode=" + res.errCode
  320. })
  321. // 分享失败
  322. },
  323. complete(_) {
  324. }
  325. })
  326. },
  327. sharePrivateImgs() {
  328. uni.hideToast()
  329. const errorImageSrc1 : string = "/static/test-image/logo.gif";
  330. const errorImageSrc2 : string = "/static/test-image/logo.png";
  331. const imageSrc : string = "/static/test-image/logo.jpg";
  332. let imageUrlList : string[] = new Array()
  333. imageUrlList.push(errorImageSrc2)
  334. imageUrlList.push(imageSrc)
  335. // imageUrlList.push(errorImageSrc1)
  336. uni.shareWithSystem({
  337. imagePaths: imageUrlList,
  338. type:'image',
  339. success(_) {
  340. console.log('Shared----------------------------success')
  341. // 分享完成,请注意此时不一定是成功分享
  342. },
  343. fail(res) {
  344. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  345. uni.showToast({
  346. icon: "error",
  347. title: "errorCode=" + res.errCode
  348. })
  349. },
  350. complete(_) {
  351. }
  352. })
  353. },
  354. sharePrivateErrorImgs() {
  355. uni.hideToast()
  356. const errorImageSrc1 : string = "/static/test-image/logo.jpg1";
  357. const errorImageSrc2 : string = "/static/test-image/logo.jpg3";
  358. const imageSrc : string = "/static/test-image/logo.jpg";
  359. let imageUrlList : string[] =new Array()
  360. imageUrlList.push(imageSrc)
  361. imageUrlList.push(errorImageSrc1)
  362. imageUrlList.push(errorImageSrc2)
  363. uni.shareWithSystem({
  364. imagePaths: imageUrlList,
  365. type:'image',
  366. success(_) {
  367. console.log('Shared----------------------------success')
  368. // 分享完成,请注意此时不一定是成功分享
  369. },
  370. fail(res) {
  371. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  372. uni.showToast({
  373. icon: "error",
  374. title: "errorCode=" + res.errCode
  375. })
  376. },
  377. complete(_) {
  378. }
  379. })
  380. },
  381. shareAll() {
  382. uni.hideToast()
  383. const imageSrc : string = "/static/test-video/fast-forward.png";
  384. let imageUrlList : string[] = new Array()
  385. imageUrlList.push(imageSrc)
  386. uni.shareWithSystem({
  387. summary: this.summary,
  388. href: 'https://uniapp.dcloud.io',
  389. imagePaths: imageUrlList,
  390. type:'image',
  391. success(_) {
  392. console.log('Shared----------------------------success')
  393. // 分享完成,请注意此时不一定是成功分享
  394. },
  395. fail(res) {
  396. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  397. uni.showToast({
  398. icon: "error",
  399. title: "errorCode=" + res.errCode
  400. })
  401. },
  402. complete(_) {
  403. }
  404. })
  405. },
  406. sharePubImg() {
  407. uni.hideToast()
  408. uni.chooseImage({
  409. count: 3,
  410. sourceType: ['camera', 'album'],
  411. success(e) {
  412. console.log(e)
  413. console.log(JSON.stringify(e))
  414. uni.shareWithSystem({
  415. // summary: "aa",
  416. // href: 'https://uniapp.dcloud.io',
  417. imagePaths: e.tempFilePaths,
  418. type:'image',
  419. success(_) {
  420. console.log('Shared----------------------------success')
  421. // 分享完成,请注意此时不一定是成功分享
  422. },
  423. fail(res) {
  424. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  425. uni.showToast({
  426. icon: "error",
  427. title: "errorCode=" + res.errCode
  428. })
  429. },
  430. complete(_) {
  431. }
  432. })
  433. }
  434. })
  435. },
  436. shareSnapShot() {
  437. uni.hideToast()
  438. uni.getElementById("viewshot")?.takeSnapshot(
  439. {
  440. success: function (res) {
  441. uni.shareWithSystem({
  442. imageUrl: res.tempFilePath,
  443. type:'image',
  444. success(_) {
  445. console.log('Shared----------------------------success')
  446. // 分享完成,请注意此时不一定是成功分享
  447. },
  448. fail(res) {
  449. console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
  450. uni.showToast({
  451. icon: "error",
  452. title: "errorCode=" + res.errCode
  453. })
  454. },
  455. complete(_) {
  456. }
  457. })
  458. },
  459. fail: function (res) {
  460. console.log(res)
  461. uni.showToast({
  462. icon: 'error',
  463. title: '截图失败'
  464. })
  465. }
  466. }
  467. )
  468. },
  469. }
  470. }
  471. </script>
  472. <style>
  473. .button {
  474. margin-left: 30px;
  475. margin-right: 30px;
  476. margin-bottom: 15px;
  477. }
  478. </style>