share-boxN.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <view class="share-content" :style="{'width':viewWidth+'px'}">
  3. <view class="share-inner" style="width: 100%;">
  4. <view class="share-item" :style="{'width':viewWidth/viewCount+'px'}" @click="wxShare(0)">
  5. <image class="image" src="/static/images/icon_wx.png" mode=""></image>
  6. <text class="text">微信</text>
  7. </view>
  8. <view class="share-item" :style="{'width':viewWidth/viewCount+'px'}" @click="wxShare(1)">
  9. <image class="image" src="/static/images/icon_pyq.png" mode=""></image>
  10. <text class="text">朋友圈</text>
  11. </view>
  12. <view class="share-item" :style="{'width':viewWidth/viewCount+'px'}" @click="qwShare()">
  13. <image class="image" src="/static/images/icon_qwshare.png" mode=""></image>
  14. <text class="text">发给同事</text>
  15. </view>
  16. <view class="share-item" :style="{'width':viewWidth/viewCount+'px'}" @click="copyUrl()" v-if="operate.indexOf('copyUrl') > -1">
  17. <image class="image" src="/static/images/icon_copy_link.png" mode=""></image>
  18. <text class="text">复制链接</text>
  19. </view>
  20. <view class="share-item tsjy" :style="{'width':viewWidth/viewCount+'px'}" @click="goToFeedback()">
  21. <image class="image" src="/static/image/my/tsjy.png" mode=""></image>
  22. <text class="text">举报</text>
  23. </view>
  24. </view>
  25. <text class="cancel-btn" @click="closeShare">取消</text>
  26. </view>
  27. </template>
  28. <script>
  29. // import {shareCourse} from '@/api/course'
  30. // import { shareVideo } from '@/api/shortvideo'
  31. import { getQwCompanyList } from '@/api/common.js'
  32. import { registerQW } from '@/utils/common.js'
  33. export default {
  34. name: "share-box",
  35. props: {
  36. shareItem: {
  37. type: Object,
  38. default: function() {
  39. return {
  40. imageUrl: "",
  41. title: "",
  42. url: "",
  43. summary: url,
  44. courseId: 0,
  45. videoId: 0,
  46. isMini: false,
  47. compressImage: 0, // 是否需要压缩
  48. showReport:false,
  49. }; // type 0:课程 1视频
  50. }
  51. },
  52. operate: {
  53. type: Array,
  54. default:['WXSceneSession','WXSceneTimeline','copyUrl']
  55. }
  56. },
  57. data() {
  58. return {
  59. viewWidth: 0,
  60. viewCount:4.0,
  61. companyList:[]
  62. };
  63. },
  64. mounted() {
  65. const systemInfo = uni.getSystemInfoSync();
  66. this.viewWidth = systemInfo.screenWidth;
  67. if(this.shareItem.showReport){
  68. this.viewCount=5.2;
  69. }
  70. },
  71. methods: {
  72. wxShare(type) {
  73. if (this.isEmpty(this.shareItem.title)) {
  74. uni.showToast({
  75. title: "数据未加载",
  76. icon: "none",
  77. position: 'top',
  78. duration: 2000
  79. })
  80. return;
  81. }
  82. // #ifdef APP-PLUS
  83. if (!plus.runtime.isAgreePrivacy()) {
  84. uni.showToast({
  85. title: "请同意隐私政策",
  86. icon: "none",
  87. position: 'top',
  88. duration: 2000
  89. })
  90. return;
  91. }
  92. // #endif
  93. let that = this;
  94. let sceneStr = type == 0 ? "WXSceneSession" : "WXSceneTimeline";
  95. // 仅支持分享微信小程序到微信聊天界面,想进入朋友圈需改为分享图片方式,在图片中包含小程序码
  96. if (sceneStr == 'WXSceneTimeline' || !this.shareItem.isMini) {
  97. uni.share({
  98. provider: "weixin",
  99. scene: sceneStr,
  100. type: 0,
  101. imageUrl: this.shareItem.imageUrl, //分享封面图片
  102. title: this.shareItem.title, // 分享标题
  103. href: this.shareItem.url, //跳转链接
  104. summary: this.shareItem.summary, // 分享内容文字
  105. success: function(res) {
  106. that.doShare();
  107. uni.showToast({
  108. title: "分享成功",
  109. icon: 'none'
  110. });
  111. },
  112. fail: function(err) {
  113. // 此处是调起微信分享失败的回调
  114. },
  115. complete: (e) => {
  116. console.log("WXSceneTimeline", e)
  117. }
  118. });
  119. } else {
  120. if(this.shareItem.compressImage == 1) {
  121. uni.downloadFile({
  122. url: this.shareItem.imageUrl, //仅为示例,并非真实接口地址。
  123. success: (images)=> {
  124. uni.compressImage({
  125. src: images.tempFilePath,
  126. quality: 40,
  127. width: '50%',
  128. height: '50%',
  129. success: res => {
  130. this.shareMini(res.tempFilePath,sceneStr)
  131. },
  132. fail: function(err) {
  133. // 此处是压缩失败
  134. }
  135. })
  136. }
  137. });
  138. } else {
  139. this.shareMini(this.shareItem.imageUrl)
  140. }
  141. }
  142. },
  143. shareMini(imageUrl,sceneStr) {
  144. const that = this
  145. uni.share({
  146. provider: "weixin",
  147. scene: sceneStr,
  148. type: 5,
  149. imageUrl: imageUrl, //分享封面图片
  150. title: this.shareItem.title, // 分享标题
  151. //summary: this.shareItem.summary, // 分享内容文字
  152. miniProgram: {
  153. id: "gh_7a6a32e5ef61",
  154. path: this.shareItem.path,
  155. type: 0, //0-正式版; 1-测试版; 2-体验版。 默认值为0。
  156. webUrl: "http://uniapp.dcloud.io"
  157. },
  158. success: function(res) {
  159. that.doShare();
  160. uni.showToast({
  161. title: "分享成功",
  162. icon: 'none'
  163. });
  164. },
  165. fail: function(err) {
  166. // 此处是调起微信分享失败的回调
  167. },
  168. complete: (e) => {
  169. console.log("WXSceneSession", e)
  170. }
  171. });
  172. },
  173. doShare() {
  174. if (!this.shareItem.isMini) {
  175. if (this.shareItem.courseId > 0) {
  176. this.shareCourse();
  177. }
  178. }
  179. if(this.shareItem.videoId > 0) {
  180. this.shareVideo();
  181. }
  182. },
  183. isEmpty(obj) {
  184. if (typeof obj == "undefined" || obj == null || obj == "") {
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. },
  190. shareCourse() {
  191. shareCourse(this.shareItem.courseId).then(res => {
  192. },
  193. rej => {}
  194. );
  195. },
  196. shareVideo() {
  197. shareVideo({videoId:this.shareItem.videoId})
  198. },
  199. copyUrl() {
  200. uni.setClipboardData({
  201. data: this.shareItem.url,
  202. success: () => {
  203. uni.showToast({
  204. title: '复制成功',
  205. icon: 'none'
  206. });
  207. },
  208. fail: () => {
  209. uni.showToast({
  210. title: '复制失败',
  211. icon: 'none'
  212. });
  213. }
  214. });
  215. },
  216. closeShare() {
  217. return this.$emit('closeShare');
  218. },
  219. showToast(title) {
  220. uni.showToast({
  221. icon: 'none',
  222. title: title,
  223. duration: 3000
  224. });
  225. },
  226. qwShare() {
  227. if (this.isEmpty(this.shareItem.title)) {
  228. uni.showToast({
  229. title: "数据未加载",
  230. icon: "none",
  231. position: 'top',
  232. duration: 2000
  233. })
  234. return;
  235. }
  236. // #ifdef APP-PLUS
  237. if (!plus.runtime.isAgreePrivacy()) {
  238. uni.showToast({
  239. title: "请同意隐私政策",
  240. icon: "none",
  241. position: 'top',
  242. duration: 2000
  243. })
  244. return;
  245. }
  246. // #endif
  247. this.getQwCompanyList()
  248. },
  249. getQwCompanyList(){
  250. let token = uni.getStorageSync('CompanyUserToken');
  251. if (!token) {
  252. uni.showToast({
  253. title: "请先登录销售账号",
  254. icon: 'none'
  255. });
  256. return
  257. }
  258. uni.showLoading()
  259. getQwCompanyList().then(res=>{
  260. uni.hideLoading()
  261. if(res.code == 200&&res.qwCompanyList&&res.qwCompanyList.length > 0) {
  262. this.companyList = res.qwCompanyList
  263. if(this.companyList.length==1) {
  264. this.initregisterQW(0)
  265. } else {
  266. uni.showActionSheet({
  267. itemList: this.companyList.map(item=>item.corpName),
  268. success: (res)=> {
  269. console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
  270. this.initregisterQW(res.tapIndex)
  271. },
  272. fail: (res)=> {
  273. console.log(res.errMsg);
  274. }
  275. });
  276. }
  277. } else {
  278. uni.showToast({
  279. title: "暂无可分享企业微信账号",
  280. icon: 'none'
  281. });
  282. }
  283. }).catch(()=>{
  284. uni.hideLoading()
  285. })
  286. },
  287. // 初始化注册到企业微信并分享
  288. initregisterQW(index) {
  289. getApp().globalData.shareSchema = this.companyList[index].yjfyySchema; //应用跳转标识,显示在具体应用下的 Schema字段
  290. getApp().globalData.shareCorpId = this.companyList[index].corpId; //企业唯一标识。创建企业后显示在,我的企业 CorpID字段
  291. getApp().globalData.shareAgentid = this.companyList[index].yjfyyAgentId; //应用
  292. getApp().globalData.shareAppid_gh = this.companyList[index].yjfyyAppId;
  293. registerQW((installed) => {
  294. this.doShare();
  295. this.$emit('closeShare')
  296. },this.shareItem)
  297. },
  298. goToFeedback(){
  299. uni.navigateTo({
  300. url: "/pages/user/feedback"
  301. });
  302. },
  303. }
  304. }
  305. </script>
  306. <style scoped lang="scss">
  307. .share-content {
  308. //border-radius: 40upx 40upx 0px 0px;
  309. width: 100%;
  310. height: auto; /* 改为自适应高度 */
  311. padding-bottom: 20rpx; /* 增加底部间距 */
  312. background: #fff;
  313. }
  314. .share-inner {
  315. padding: 20rpx 0; /* 增加上下内边距 */
  316. width: 100%;
  317. flex: 1;
  318. height: auto; /* 改为自适应高度 */
  319. flex-direction: row;
  320. flex-wrap: wrap;
  321. display: flex; /* 确保弹性布局生效 */
  322. }
  323. .share-item {
  324. /* width: 20%; 注释掉这行 */
  325. flex-direction: column;
  326. align-items: center;
  327. justify-content: center;
  328. height: 180rpx; /* 增加高度 */
  329. box-sizing: border-box;
  330. //flex: 1; /* 添加这个属性使5个项等宽 */
  331. min-width: 0; /* 防止内容溢出 */
  332. }
  333. .share-item .image {
  334. width: 80rpx;
  335. height: 80rpx;
  336. margin-top: 10rpx; /* 减少顶部间距 */
  337. }
  338. .share-item .text {
  339. font-size: 28rpx;
  340. font-weight: 500;
  341. color: #111111;
  342. height: auto; /* 改为自适应高度 */
  343. margin-top: 10rpx; /* 减少间距 */
  344. text-align: center; /* 文字居中 */
  345. white-space: nowrap; /* 防止文字换行 */
  346. }
  347. .tsjy .image {
  348. width: 48rpx;
  349. height: 48rpx;
  350. margin-top: 22rpx; /* 减少顶部间距 */
  351. }
  352. .tsjy .text {
  353. width: auto; /* 改为自适应宽度 */
  354. height: auto; /* 改为自适应高度 */
  355. margin-top: 30rpx; /* 减少间距 */
  356. }
  357. .cancel-btn {
  358. height: 90rpx; /* 增加高度 */
  359. line-height: 90rpx; /* 垂直居中 */
  360. text-align: center;
  361. font-size: 32upx;
  362. font-family: PingFang SC;
  363. font-weight: 500;
  364. color: #111111;
  365. border-top: 2upx solid #E8E8E8;
  366. background-color: #FFF;
  367. }
  368. </style>