开始

image.png

image.png
image.png
D:\demos\uni-app\hello-uni-app2022\pages\API\share
image.png

如果是小程序端
image.png
重点看这段
image.png
整个复制过来image.png

  1. data() {
  2. return {
  3. title: 'share',
  4. shareText: 'uni-app可以同时发布成原生App、小程序、H5,邀请你一起体验!',
  5. href:"https://uniapp.dcloud.io",
  6. image: '',
  7. shareType:1,
  8. providerList: []
  9. }
  10. },

image.png
复制onLoad方法
image.png

  1. onLoad: function () {
  2. uni.getProvider({
  3. service: 'share',
  4. success: (e) => {
  5. console.log('success', e);
  6. let data = []
  7. for (let i = 0; i < e.provider.length; i++) {
  8. switch (e.provider[i]) {
  9. case 'weixin':
  10. data.push({
  11. name: '分享到微信好友',
  12. id: 'weixin',
  13. sort:0
  14. })
  15. data.push({
  16. name: '分享到微信朋友圈',
  17. id: 'weixin',
  18. type:'WXSenceTimeline',
  19. sort:1
  20. })
  21. break;
  22. case 'sinaweibo':
  23. data.push({
  24. name: '分享到新浪微博',
  25. id: 'sinaweibo',
  26. sort:2
  27. })
  28. break;
  29. case 'qq':
  30. data.push({
  31. name: '分享到QQ',
  32. id: 'qq',
  33. sort:3
  34. })
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. this.providerList = data.sort((x,y) => {
  41. return x.sort - y.sort
  42. });
  43. },
  44. fail: (e) => {
  45. console.log('获取分享通道失败', e);
  46. uni.showModal({
  47. content:'获取分享通道失败',
  48. showCancel:false
  49. })
  50. }
  51. });
  52. },

增加两个自定义属性
image.png

image.png

  1. onLoad() {
  2. uni.getProvider({
  3. service: 'share',
  4. success: (e) => {
  5. let data = []
  6. for (let i = 0; i < e.provider.length; i++) {
  7. switch (e.provider[i]) {
  8. case 'weixin':
  9. data.push({
  10. name: '微信好友',
  11. id: 'weixin',
  12. zicon:"weixin",
  13. zclass:"wx",
  14. sort:0
  15. })
  16. data.push({
  17. name: '朋友圈',
  18. id: 'weixin',
  19. type:'WXSenceTimeline',
  20. zicon:"weixin",
  21. zclass:"pyq",
  22. sort:1
  23. })
  24. break;
  25. case 'sinaweibo':
  26. data.push({
  27. name: '新浪微博',
  28. id: 'sinaweibo',
  29. zicon:"xinlangweibo",
  30. zclass:"wb",
  31. sort:2
  32. })
  33. break;
  34. case 'qq':
  35. data.push({
  36. name: 'QQ好友',
  37. id: 'qq',
  38. zicon:"QQ",
  39. zclass:"qq",
  40. sort:3
  41. })
  42. break;
  43. default:
  44. break;
  45. }
  46. }
  47. this.providerList = data.sort((x,y) => {
  48. return x.sort - y.sort
  49. });
  50. },
  51. fail: (e) => {
  52. console.log('获取分享通道失败', e);
  53. uni.showModal({
  54. content:'获取分享通道失败',
  55. showCancel:false
  56. })
  57. }
  58. });
  59. },

image.png

image.png
复制share方法
image.png
复制压缩图片的方法
image.png
给每一个循环增加事件
image.png

image.png

image.png
qq好友
image.png

image.png

本节代码

more-shar.vue组件

  1. <template>
  2. <view class="animated fadeIn faster" v-if="show">
  3. <view class="more-share-model" @tap="togle"></view>
  4. <view class="more-share">
  5. <view class="more-share-title u-f-ajc">分享到</view>
  6. <scroll-view scroll-x class="more-share-body">
  7. <block v-for="(val,index) in providerList" :key="index">
  8. <view class="more-share-item"
  9. hover-class="more-share-hover"
  10. @tap="share(val)">
  11. <view class="icon iconfont u-f-ajc"
  12. :class="['icon-'+val.zicon,'more-share-'+val.zclass]"></view>
  13. <view>{{val.name}}</view>
  14. </view>
  15. </block>
  16. </scroll-view>
  17. <view class="more-share-bottom u-f-ajc" hover-class="more-share-hover" @tap="togle">取消</view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. props:{
  24. show:Boolean
  25. },
  26. data() {
  27. return {
  28. shareText: 'uni-app可以同时发布成原生App、小程序、H5,邀请你一起体验!',
  29. href:"https://uniapp.dcloud.io",
  30. image: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png',
  31. shareType:1,//1文字 2图片 0图文 5小程序
  32. providerList: []
  33. }
  34. },
  35. onLoad() {
  36. uni.getProvider({
  37. service: 'share',
  38. success: (e) => {
  39. let data = []
  40. for (let i = 0; i < e.provider.length; i++) {
  41. switch (e.provider[i]) {
  42. case 'weixin':
  43. data.push({
  44. name: '微信好友',
  45. id: 'weixin',
  46. zicon:"weixin",
  47. zclass:"wx",
  48. sort:0
  49. })
  50. data.push({
  51. name: '朋友圈',
  52. id: 'weixin',
  53. type:'WXSenceTimeline',
  54. zicon:"weixin",
  55. zclass:"pyq",
  56. sort:1
  57. })
  58. break;
  59. case 'sinaweibo':
  60. data.push({
  61. name: '新浪微博',
  62. id: 'sinaweibo',
  63. zicon:"xinlangweibo",
  64. zclass:"wb",
  65. sort:2
  66. })
  67. break;
  68. case 'qq':
  69. data.push({
  70. name: 'QQ好友',
  71. id: 'qq',
  72. zicon:"QQ",
  73. zclass:"qq",
  74. sort:3
  75. })
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. this.providerList = data.sort((x,y) => {
  82. return x.sort - y.sort
  83. });
  84. },
  85. fail: (e) => {
  86. console.log('获取分享通道失败', e);
  87. uni.showModal({
  88. content:'获取分享通道失败',
  89. showCancel:false
  90. })
  91. }
  92. });
  93. },
  94. methods:{
  95. togle(){
  96. this.$emit('togle')
  97. },
  98. async share(e) {
  99. console.log('分享通道:'+ e.id +'; 分享类型:' + this.shareType);
  100. if(!this.shareText && (this.shareType === 1 || this.shareType === 0)){
  101. uni.showModal({
  102. content:'分享内容不能为空',
  103. showCancel:false
  104. })
  105. return;
  106. }
  107. if(!this.image && (this.shareType === 2 || this.shareType === 0)){
  108. uni.showModal({
  109. content:'分享图片不能为空',
  110. showCancel:false
  111. })
  112. return;
  113. }
  114. let shareOPtions = {
  115. provider: e.id,
  116. scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏
  117. type: this.shareType,
  118. success: (e) => {
  119. console.log('success', e);
  120. uni.showModal({
  121. content: '分享成功',
  122. showCancel:false
  123. })
  124. },
  125. fail: (e) => {
  126. console.log('fail', e)
  127. uni.showModal({
  128. content: e.errMsg,
  129. showCancel:false
  130. })
  131. },
  132. complete:function(){
  133. console.log('分享操作结束!')
  134. }
  135. }
  136. switch (this.shareType){
  137. case 0:
  138. shareOPtions.summary = this.shareText;
  139. shareOPtions.imageUrl = this.image;
  140. shareOPtions.title = '欢迎体验uniapp';
  141. shareOPtions.href = 'https://uniapp.dcloud.io';
  142. break;
  143. case 1:
  144. shareOPtions.summary = this.shareText;
  145. break;
  146. case 2:
  147. shareOPtions.imageUrl = this.image;
  148. break;
  149. case 5:
  150. shareOPtions.imageUrl = this.image ? this.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
  151. shareOPtions.title = '欢迎体验uniapp';
  152. shareOPtions.miniProgram = {
  153. id:'gh_33446d7f7a26',
  154. path:'/pages/tabBar/component/component',
  155. webUrl:'https://uniapp.dcloud.io',
  156. type:0
  157. };
  158. break;
  159. default:
  160. break;
  161. }
  162. if(shareOPtions.type === 0 && plus.os.name === 'iOS'){//如果是图文分享,且是ios平台,则压缩图片
  163. shareOPtions.imageUrl = await this.compress();
  164. }
  165. if(shareOPtions.type === 1 && shareOPtions.provider === 'qq'){//如果是分享文字到qq,则必须加上href和title
  166. shareOPtions.href = 'https://uniapp.dcloud.io';
  167. shareOPtions.title = '欢迎体验uniapp';
  168. }
  169. uni.share(shareOPtions);
  170. },
  171. compress(){//压缩图片 图文分享要求分享图片大小不能超过20Kb
  172. console.log('开始压缩');
  173. let img = this.image;
  174. return new Promise((res) => {
  175. var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
  176. console.log('after' + localPath);
  177. // 压缩size
  178. plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
  179. entry.file((file) => {// 可通过entry对象操作图片
  180. console.log('getFile:' + JSON.stringify(file));
  181. if(file.size > 20480) {// 压缩后size 大于20Kb
  182. plus.zip.compressImage({
  183. src: img,
  184. dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
  185. width: '10%',
  186. height: '10%',
  187. quality: 1,
  188. overwrite: true
  189. }, (event) => {
  190. console.log('success zip****' + event.size);
  191. let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
  192. res(newImg);
  193. }, function(error) {
  194. uni.showModal({
  195. content:'分享图片太大,需要请重新选择图片!',
  196. showCancel:false
  197. })
  198. });
  199. }
  200. });
  201. }, (e) => {
  202. console.log('Resolve file URL failed: ' + e.message);
  203. uni.showModal({
  204. content:'分享图片太大,需要请重新选择图片!',
  205. showCancel:false
  206. })
  207. });
  208. })
  209. }
  210. }
  211. }
  212. </script>
  213. <style scoped>
  214. /* 分享 */
  215. .more-share-model{
  216. background:rgba(51, 51, 51, 0.49);
  217. position: fixed;
  218. top: 0;
  219. left: 0;
  220. right: 0;
  221. bottom: 0;
  222. z-index: 100;
  223. }
  224. .more-share{
  225. position: fixed;
  226. z-index: 110;
  227. bottom: 0;
  228. left: 0;
  229. right: 0;
  230. background: #FFFFFF;
  231. }
  232. .more-share-title,.more-share-bottom{
  233. font-size: 32upx;
  234. padding: 25upx;
  235. }
  236. .more-share-body{
  237. white-space: nowrap;
  238. width: 100%;
  239. height: 200upx;
  240. border-bottom: 1upx solid #EEEEEE;
  241. display: flex!important;
  242. line-height: 200upx!important;
  243. }
  244. .more-share-item{
  245. width: 25%;
  246. display: inline-flex;
  247. justify-content: center;
  248. align-items: center;
  249. flex-direction: column;
  250. height: 100%;
  251. }
  252. .more-share-item>view:first-child{
  253. width: 100upx;
  254. height: 100upx;
  255. border-radius: 100%;
  256. font-size: 55upx;
  257. color: #FFFFFF;
  258. }
  259. .more-share-item>view:last-child{
  260. color: #7A7A7A;
  261. }
  262. .more-share-hover{
  263. background: #EEEEEE;
  264. }
  265. .more-share-wx{
  266. background: #2AD19B;
  267. }
  268. .more-share-pyq{
  269. background: #514D4C;
  270. }
  271. .more-share-wb{
  272. background: #EE5E5E;
  273. }
  274. .more-share-qq{
  275. background: #4A73BA;
  276. }
  277. </style>

结束