useShare.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { img, isWeixinBrowser, currRoute, currShareRoute } from '@/utils/common'
  2. import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
  3. import { getShareInfo } from '@/app/api/diy';
  4. import useSystemStore from '@/stores/system';
  5. // #ifdef H5
  6. import wechat from '@/utils/wechat'
  7. // #endif
  8. export const useShare = () => {
  9. let wechatOptions: any = {};
  10. let weappOptions: any = {};
  11. const systemStore = useSystemStore()
  12. const wechatInit = () => {
  13. if (!isWeixinBrowser()) return;
  14. // 初始化sdk
  15. wechat.init();
  16. }
  17. // 微信公众号分享
  18. const wechatShare = () => {
  19. if (!isWeixinBrowser()) return;
  20. wechat.share(wechatOptions);
  21. }
  22. const getQuery = () => {
  23. let query: any = currShareRoute().params;
  24. let wap_member_id = uni.getStorageSync('wap_member_id');
  25. if (wap_member_id) {
  26. query.mid = wap_member_id;
  27. }
  28. let queryStr = [];
  29. for (let key in query) {
  30. queryStr.push(key + '=' + query[key]);
  31. }
  32. return queryStr
  33. }
  34. const setShare = (options: any = {}) => {
  35. if (currRoute() == '' || currRoute().indexOf('app/pages/index/close') != -1 || currRoute().indexOf('app/pages/index/nosite') != -1) return;
  36. let queryStr = getQuery();
  37. let h5Link = '';
  38. // #ifdef H5
  39. h5Link = location.origin + location.pathname + (queryStr.length > 0 ? '?' + queryStr.join('&') : '');
  40. wechatOptions = {
  41. link: h5Link
  42. }
  43. // #endif
  44. // #ifdef APP-PLUS
  45. h5Link = systemStore.site.wap_url + currShareRoute().path + (queryStr.length > 0 ? '?' + queryStr.join('&') : '');
  46. wechatOptions = {
  47. link: h5Link
  48. }
  49. // #endif
  50. weappOptions = {
  51. path: '/' + currRoute() + (queryStr.length > 0 ? '?' + queryStr.join('&') : ''),
  52. query: queryStr.join('&'),
  53. }
  54. if (options && Object.keys(options).length) {
  55. if (options.wechat) {
  56. wechatOptions.title = options.wechat.title || ''
  57. wechatOptions.link = options.wechat.link || h5Link
  58. wechatOptions.desc = options.wechat.desc || ''
  59. wechatOptions.imgUrl = options.wechat.url ? img(options.wechat.url) : ''
  60. // wechatOptions.success = options.wechat.callback || null;
  61. // useSystemStore().shareCallback = options.wechat.callback || null;
  62. // #ifdef H5
  63. wechatShare()
  64. // #endif
  65. }
  66. if (options.weapp) {
  67. weappOptions.title = options.weapp.title || ''
  68. if (options.weapp.path) weappOptions.path = options.weapp.path
  69. weappOptions.imageUrl = options.weapp.url ? img(options.weapp.url) : ''
  70. useSystemStore().shareCallback = options.weapp.callback || null;
  71. // #ifdef MP-WEIXIN
  72. uni.setStorageSync('weappOptions', weappOptions)
  73. // #endif
  74. }
  75. }
  76. getShareInfo({
  77. route: '/' + currRoute(),
  78. params: JSON.stringify(currShareRoute().params)
  79. }).then((res: any) => {
  80. let data = res.data;
  81. let wechat = data.wechat;
  82. if (wechat) {
  83. wechatOptions.title = wechat.title
  84. wechatOptions.desc = wechat.desc
  85. wechatOptions.imgUrl = wechat.url ? img(wechat.url) : ''
  86. } else {
  87. wechatOptions.title = document ? document.title : ''
  88. wechatOptions.desc = ''
  89. }
  90. // #ifdef H5
  91. wechatShare()
  92. // #endif
  93. let weapp = data.weapp;
  94. if (weapp) {
  95. weappOptions.title = weapp.title
  96. weappOptions.imageUrl = weapp.url ? img(weapp.url) : ''
  97. }
  98. // #ifdef MP
  99. if(!weappOptions.title && !weappOptions.imageUrl){
  100. uni.setStorageSync('weappOptions', {})
  101. return;
  102. }
  103. uni.setStorageSync('weappOptions', weappOptions)
  104. // #endif
  105. useSystemStore().$patch((state) => {
  106. state.shareOptions = {
  107. wechatOptions,
  108. weappOptions
  109. }
  110. })
  111. })
  112. useSystemStore().$patch((state) => {
  113. state.shareOptions = {
  114. wechatOptions,
  115. weappOptions
  116. }
  117. })
  118. }
  119. // 小程序分享,分享给好友
  120. const shareApp = (options = {}) => {
  121. // #ifdef MP
  122. return onShareAppMessage(() => {
  123. let config: any = uni.getStorageSync('weappOptions')
  124. if (!config) config = {}
  125. if (systemStore.shareCallback) systemStore.shareCallback();
  126. return {
  127. ...config,
  128. ...options
  129. }
  130. })
  131. // #endif
  132. // #ifdef APP-PLUS
  133. const weappOptions = systemStore.shareOptions.weappOptions
  134. const wechatOptions = systemStore.shareOptions.wechatOptions
  135. if (!weappOptions.title && !wechatOptions.title) return
  136. const shareOptions: any = {}
  137. if (weappOptions.title && systemStore.appConfig.weapp_original) {
  138. shareOptions.type = 5
  139. shareOptions.title = weappOptions.title
  140. shareOptions.imageUrl = weappOptions.imageUrl
  141. shareOptions.miniProgram = {
  142. id: systemStore.appConfig.weapp_original,
  143. path: weappOptions.path,
  144. type: 0,
  145. webUrl: wechatOptions.link
  146. }
  147. } else {
  148. shareOptions.type = 0
  149. shareOptions.href = wechatOptions.link
  150. shareOptions.title = wechatOptions.title
  151. shareOptions.summary = wechatOptions.desc
  152. shareOptions.imageUrl = wechatOptions.imgUrl
  153. }
  154. uni.share({
  155. provider: "weixin",
  156. scene: "WXSceneSession",
  157. success: () => {
  158. if (systemStore.shareCallback) systemStore.shareCallback();
  159. },
  160. ...shareOptions
  161. });
  162. // #endif
  163. }
  164. // 小程序分享,分享到朋友圈
  165. const shareTime = (options = {}) => {
  166. // #ifdef MP
  167. return onShareTimeline(() => {
  168. let config: any = uni.getStorageSync('weappOptions')
  169. if (!config) config = {}
  170. if (systemStore.shareCallback) systemStore.shareCallback();
  171. return {
  172. ...config,
  173. ...options
  174. }
  175. })
  176. // #endif
  177. // #ifdef APP-PLUS
  178. const wechatOptions = systemStore.shareOptions.wechatOptions
  179. if (wechatOptions.title) {
  180. uni.share({
  181. provider: "weixin",
  182. scene: "WXSceneTimeline",
  183. type: 0,
  184. href: wechatOptions.link,
  185. title: wechatOptions.title,
  186. summary: wechatOptions.desc,
  187. imageUrl: wechatOptions.imgUrl,
  188. success: () => {
  189. if (systemStore.shareCallback) systemStore.shareCallback();
  190. }
  191. });
  192. }
  193. // #endif
  194. }
  195. // 禁用当前页面的分享功能(同时支持小程序和公众号)
  196. const disableShare = () => {
  197. // 公众号(H5)禁用分享
  198. // #ifdef H5
  199. if (isWeixinBrowser()) {
  200. // 确保SDK初始化后再禁用
  201. wechat.init(() => {
  202. wechat.disableShare();
  203. });
  204. }
  205. // #endif
  206. // 小程序禁用分享
  207. // #ifdef MP-WEIXIN
  208. // 隐藏分享菜单(转发给朋友、朋友圈)
  209. uni.hideShareMenu({
  210. menus: ['shareAppMessage', 'shareTimeline'],
  211. success: () => {
  212. console.log('小程序分享已禁用');
  213. },
  214. fail: (err) => {
  215. console.error('小程序禁用分享失败:', err);
  216. }
  217. });
  218. // 覆盖分享方法,返回空对象
  219. onShareAppMessage(() => ({}));
  220. onShareTimeline(() => ({}));
  221. // #endif
  222. };
  223. return {
  224. wechatInit: wechatInit,
  225. setShare: setShare,
  226. onShareAppMessage: shareApp,
  227. onShareTimeline: shareTime,
  228. disableShare: disableShare,
  229. }
  230. }