share.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-title">分享内容</view>
  6. <view class="uni-textarea">
  7. <textarea class="textarea" v-model="shareText" />
  8. </view>
  9. <view class="uni-title">分享图片:</view>
  10. <view class="uni-uploader" style="padding:15rpx; background:#FFF;">
  11. <view class="uni-uploader__input-box" v-if="!image" @tap="chooseImage"></view>
  12. <image class="uni-uploader__img" v-if="image" :src="image"></image>
  13. </view>
  14. <!-- #ifdef APP-PLUS -->
  15. <view class="uni-title">分享类型:</view>
  16. <view>
  17. <radio-group @change="radioChange">
  18. <label class="radio">
  19. <radio value="1" checked="true"/>文字
  20. </label>
  21. <label class="radio">
  22. <radio value="2" />图片
  23. </label>
  24. <label class="radio">
  25. <radio value="0" />图文
  26. </label>
  27. <label class="radio">
  28. <radio value="5" />小程序
  29. </label>
  30. </radio-group>
  31. </view>
  32. <view class="uni-btn-v uni-common-mt" v-if="providerList.length > 0">
  33. <block v-for="(value,index) in providerList" :key="index">
  34. <button type="primary" v-if="value" :disabled="isDisableButton(value)" @tap="share(value)">{{value.name}}</button>
  35. </block>
  36. </view>
  37. <!-- #endif -->
  38. <!-- #ifdef MP || QUICKAPP-WEBVIEW -->
  39. <view class="uni-btn-v uni-common-mt">
  40. <button type="primary" open-type="share">分享</button>
  41. </view>
  42. <!-- #endif -->
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. title: 'share',
  51. shareText: 'uni-app可以同时发布成原生App、小程序、H5,邀请你一起体验!',
  52. href:"https://uniapp.dcloud.io",
  53. image: '',
  54. shareType:1,
  55. providerList: []
  56. }
  57. },
  58. computed:{
  59. isDisableButton() {
  60. return function(item) {
  61. if(this.shareType === 0 && item.id === 'qq'){
  62. return true;
  63. }
  64. if(this.shareType === 5 && item.name !== '分享到微信好友'){
  65. return true;
  66. }
  67. return false;
  68. }
  69. }
  70. },
  71. onShareAppMessage() {
  72. return {
  73. title: this.shareText ? this.shareText : "欢迎体验uni-app",
  74. path: '/pages/tabBar/component/component',
  75. imageUrl:this.image ? this.image : 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b6304f00-5168-11eb-bd01-97bc1429a9ff.png'
  76. }
  77. },
  78. onUnload:function(){
  79. this.shareText='uni-app可以同时发布成原生App、小程序、H5,邀请你一起体验!',
  80. this.href = 'https://uniapp.dcloud.io',
  81. this.image='';
  82. },
  83. onLoad: function () {
  84. uni.getProvider({
  85. service: 'share',
  86. success: (e) => {
  87. console.log('success', e);
  88. let data = []
  89. for (let i = 0; i < e.provider.length; i++) {
  90. switch (e.provider[i]) {
  91. case 'weixin':
  92. data.push({
  93. name: '分享到微信好友',
  94. id: 'weixin',
  95. sort:0
  96. })
  97. data.push({
  98. name: '分享到微信朋友圈',
  99. id: 'weixin',
  100. type:'WXSenceTimeline',
  101. sort:1
  102. })
  103. break;
  104. case 'sinaweibo':
  105. data.push({
  106. name: '分享到新浪微博',
  107. id: 'sinaweibo',
  108. sort:2
  109. })
  110. break;
  111. case 'qq':
  112. data.push({
  113. name: '分享到QQ',
  114. id: 'qq',
  115. sort:3
  116. })
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. this.providerList = data.sort((x,y) => {
  123. return x.sort - y.sort
  124. });
  125. },
  126. fail: (e) => {
  127. console.log('获取分享通道失败', e);
  128. uni.showModal({
  129. content:'获取分享通道失败',
  130. showCancel:false
  131. })
  132. }
  133. });
  134. },
  135. methods: {
  136. async share(e) {
  137. console.log('分享通道:'+ e.id +'; 分享类型:' + this.shareType);
  138. if(!this.shareText && (this.shareType === 1 || this.shareType === 0)){
  139. uni.showModal({
  140. content:'分享内容不能为空',
  141. showCancel:false
  142. })
  143. return;
  144. }
  145. if(!this.image && (this.shareType === 2 || this.shareType === 0)){
  146. uni.showModal({
  147. content:'分享图片不能为空',
  148. showCancel:false
  149. })
  150. return;
  151. }
  152. let shareOPtions = {
  153. provider: e.id,
  154. scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏
  155. type: this.shareType,
  156. success: (e) => {
  157. console.log('success', e);
  158. uni.showModal({
  159. content: '已分享',
  160. showCancel:false
  161. })
  162. },
  163. fail: (e) => {
  164. console.log('fail', e)
  165. uni.showModal({
  166. content: e.errMsg,
  167. showCancel:false
  168. })
  169. },
  170. complete:function(){
  171. console.log('分享操作结束!')
  172. }
  173. }
  174. switch (this.shareType){
  175. case 0:
  176. shareOPtions.summary = this.shareText;
  177. shareOPtions.imageUrl = this.image;
  178. shareOPtions.title = '欢迎体验uniapp';
  179. shareOPtions.href = 'https://uniapp.dcloud.io';
  180. break;
  181. case 1:
  182. shareOPtions.summary = this.shareText;
  183. break;
  184. case 2:
  185. shareOPtions.imageUrl = this.image;
  186. break;
  187. case 5:
  188. shareOPtions.imageUrl = this.image ? this.image : 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b6304f00-5168-11eb-bd01-97bc1429a9ff.png'
  189. shareOPtions.title = '欢迎体验uniapp';
  190. shareOPtions.miniProgram = {
  191. id:'gh_33446d7f7a26',
  192. path:'/pages/tabBar/component/component',
  193. webUrl:'https://uniapp.dcloud.io',
  194. type:0
  195. };
  196. break;
  197. default:
  198. break;
  199. }
  200. if(shareOPtions.type === 0 && plus.os.name === 'iOS'){//如果是图文分享,且是ios平台,则压缩图片
  201. shareOPtions.imageUrl = await this.compress();
  202. }
  203. if(shareOPtions.type === 1 && shareOPtions.provider === 'qq'){//如果是分享文字到qq,则必须加上href和title
  204. shareOPtions.href = 'https://uniapp.dcloud.io';
  205. shareOPtions.title = '欢迎体验uniapp';
  206. }
  207. uni.share(shareOPtions);
  208. },
  209. radioChange(e){
  210. console.log('type:' + e.detail.value);
  211. this.shareType = parseInt(e.detail.value);
  212. },
  213. chooseImage() {
  214. uni.chooseImage({
  215. count: 1,
  216. sourceType: ['album', 'camera'],
  217. sizeType: ['compressed', 'original'],
  218. success: (res) => {
  219. this.image = res.tempFilePaths[0];
  220. },
  221. fail: (err) => {
  222. // #ifdef MP
  223. uni.getSetting({
  224. success: (res) => {
  225. let authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
  226. if (!authStatus) {
  227. uni.showModal({
  228. title: '授权失败',
  229. content: 'Hello uni-app需要从您的相机或相册获取图片,请在设置界面打开相关权限',
  230. success: (res) => {
  231. if (res.confirm) {
  232. uni.openSetting()
  233. }
  234. }
  235. })
  236. }
  237. }
  238. })
  239. // #endif
  240. }
  241. })
  242. },
  243. compress(){//压缩图片 图文分享要求分享图片大小不能超过20Kb
  244. console.log('开始压缩');
  245. let img = this.image;
  246. return new Promise((res) => {
  247. var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
  248. console.log('after' + localPath);
  249. // 压缩size
  250. plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
  251. entry.file((file) => {// 可通过entry对象操作图片
  252. console.log('getFile:' + JSON.stringify(file));
  253. if(file.size > 20480) {// 压缩后size 大于20Kb
  254. plus.zip.compressImage({
  255. src: img,
  256. dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
  257. width: '10%',
  258. height: '10%',
  259. quality: 1,
  260. overwrite: true
  261. }, (event) => {
  262. console.log('success zip****' + event.size);
  263. let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
  264. res(newImg);
  265. }, function(error) {
  266. uni.showModal({
  267. content:'分享图片太大,需要请重新选择图片!',
  268. showCancel:false
  269. })
  270. });
  271. }
  272. });
  273. }, (e) => {
  274. console.log('Resolve file URL failed: ' + e.message);
  275. uni.showModal({
  276. content:'分享图片太大,需要请重新选择图片!',
  277. showCancel:false
  278. })
  279. });
  280. })
  281. }
  282. }
  283. }
  284. </script>
  285. <style>
  286. </style>