index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { Toast, Notify } from 'vant'
  2. import Clipboard from "clipboard";
  3. import { getWxToken, openError } from "@/utils/auth";
  4. import { getCache } from '@/utils/auth'
  5. import { getWxConfigInfo } from '../api'
  6. import router from '@/router'
  7. import wx from "weixin-js-sdk";
  8. //判断环境/登录状态
  9. export function isWxEnv(type, data) {
  10. return new Promise((resolve, reject) => {
  11. if (isWeChatMobile()) {
  12. if (getWxToken() && localStorage.getItem('wx_user_info')) {
  13. Toast("微信环境已授权");
  14. resolve()
  15. } else {
  16. Toast.fail("微信环境未授权");
  17. reject(1)
  18. doWeixinLogin(type, data);
  19. }
  20. } else {
  21. Toast.fail("请在手机微信中打开浏览");
  22. reject(2)
  23. // if (getWxToken() && localStorage.getItem('wx_user_info')) {
  24. // resolve()
  25. // } else {
  26. // Toast.fail("请在手机微信中打开浏览");
  27. // reject(2)
  28. // }
  29. }
  30. })
  31. }
  32. export function isWeChat() {
  33. var ua = navigator.userAgent.toLowerCase();
  34. return /micromessenger/.test(ua);
  35. }
  36. export function isWeChatPC() {
  37. var ua = navigator.userAgent.toLowerCase();
  38. // 你可以根据PC版微信浏览器的特性来添加更多的判断条件
  39. // 例如,某些PC版微信浏览器可能包含特定的关键字或版本信息
  40. return /micromessenger/.test(ua) && /windows/.test(ua) && !/mobile/.test(ua);
  41. }
  42. export function isWeChatMobile() {
  43. var ua = navigator.userAgent.toLowerCase();
  44. return /micromessenger/.test(ua) && /mobile/.test(ua);
  45. }
  46. // if (isWeChat()) {
  47. // if (isWeChatPC()) {
  48. // console.log('这是PC版微信');
  49. // } else if (isWeChatMobile()) {
  50. // console.log('这是手机版微信');
  51. // } else {
  52. // console.log('无法确定是PC版还是手机版微信');
  53. // }
  54. // } else {
  55. // console.log('这不是微信浏览器');
  56. // }
  57. //请求微信授权
  58. export function doWeixinLogin(type, data) {
  59. // let defaultUrl = 'https://3g954g5149.picp.vip/wx/mp/auth/wx65f4dde5ec7c31e7?marketing=1'
  60. // let dealerUrl = 'https://3g954g5149.picp.vip/wx/mp/auth/wx65f4dde5ec7c31e7?marketing=2&pid='
  61. // let timinalUrl = 'https://3g954g5149.picp.vip/wx/mp/auth/wx65f4dde5ec7c31e7?marketing=3&expendCode='
  62. let defaultUrl = `${process.env.BASE_API}/wx/mp/auth/wx65f4dde5ec7c31e7?marketing=1`
  63. let dealerUrl = `${process.env.BASE_API}/wx/mp/auth/wx65f4dde5ec7c31e7?marketing=2&pid=`
  64. let timinalUrl = `${process.env.BASE_API}/wx/mp/auth/wx65f4dde5ec7c31e7?marketing=3&expendCode=`
  65. if (getCache("isAndroid")) {
  66. if (type == 'pid' && data) {
  67. window.open(`${dealerUrl}${data}`)
  68. } else if (type == 'code' && data) {
  69. window.open(`${timinalUrl}${data}`)
  70. } else {
  71. window.open(`${defaultUrl}`)
  72. }
  73. } else {
  74. if (type == 'pid' && data) {
  75. window.location.href = `${dealerUrl}${data}`;
  76. } else if (type == 'code' && data) {
  77. window.location.href = `${timinalUrl}${data}`;
  78. } else {
  79. window.location.href = `${defaultUrl}`;
  80. }
  81. }
  82. }
  83. //复制(event必须为点击元素)
  84. export function copyLink(event) {
  85. let that = this;
  86. //这里是复制目标的类名
  87. let clipboard = new Clipboard(event, {
  88. text: function (trigger) {
  89. return trigger.getAttribute("aria-label");
  90. }
  91. });
  92. clipboard.on("success", function (e) {
  93. e.clearSelection(); //清除选中的文字的选择状态
  94. that.$toast.success("复制成功~");
  95. });
  96. clipboard.on("error", function (e) {
  97. console.error(e);
  98. });
  99. }
  100. export function setWxConfig(shareInfo) {
  101. let info
  102. if(shareInfo){
  103. info = shareInfo
  104. }else{
  105. info = {
  106. title:'功道云知识平台',// 分享标题
  107. desc:'听完觉得确实不错,忍不住要推荐~',// 分享描述
  108. link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  109. imgUrl: 'https://oa.g107.com/m/static/images/logo.png', // 分享图标
  110. }
  111. }
  112. console.log(info)
  113. let data = {
  114. url:window.location.href
  115. }
  116. getWxConfigInfo(data).then(config => {
  117. wx.config({
  118. appId: config.appId,
  119. timestamp: config.timestamp,
  120. nonceStr: config.nonceStr,
  121. signature: config.signature,
  122. jsApiList: ['updateAppMessageShareData','updateTimelineShareData']
  123. })
  124. wx.ready(() => {
  125. wx.updateAppMessageShareData({
  126. title: info.title, // 分享标题
  127. desc: info.desc, // 分享描述
  128. link: info.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  129. imgUrl: info.imgUrl, // 分享图标
  130. success: function () {
  131. // 设置成功
  132. console.log("分享成功");
  133. },
  134. });
  135. wx.updateTimelineShareData({
  136. title: info.title, // 分享标题
  137. link: info.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  138. imgUrl: info.imgUrl, // 分享图标
  139. success: function () {
  140. // 设置成功
  141. console.log("分享成功");
  142. },
  143. });
  144. });
  145. }).catch(err=>{
  146. console.log(err)
  147. })
  148. }