wechat.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { wechatLogin,getShareConfig } from './http';
  2. import { getShareUrl,isIOS,platformAgent } from './utils';
  3. import wx from "weixin-js-sdk";
  4. export function login(){
  5. return new Promise((resolve, reject)=>{
  6. wechatLogin().then(res=>{
  7. if(res.status == 1){
  8. location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${res.data.appid}&redirect_uri=${res.data.url}/pages/public/login&response_type=code&scope=snsapi_userinfo&state=${res.data.state}#wechat_redirect`;
  9. }
  10. }).catch(err=>{
  11. reject(err);
  12. });
  13. });
  14. }
  15. export function shareConfig(page){
  16. if(!platformAgent().isWechat){
  17. return false;
  18. }
  19. if(isIOS() && page != "home"){
  20. return false;
  21. }
  22. let url = getShareUrl(window.location.href);
  23. getShareConfig({
  24. url: url
  25. }).then((res)=>{
  26. if(res.status){
  27. wx.config(res.data);
  28. }else{
  29. console.log("获取wx分享配置失败");
  30. }
  31. });
  32. }
  33. export function setShareData(data){
  34. if(!platformAgent().isWechat){
  35. return false;
  36. }
  37. let url = getShareUrl(window.location.href);
  38. wx.ready(function () {
  39. wx.updateAppMessageShareData({
  40. title: data.title, // 分享标题
  41. desc: data.desc || '', // 分享描述
  42. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  43. imgUrl: data.imgUrl, // 分享图标
  44. success: function () {}
  45. });
  46. wx.updateTimelineShareData({
  47. title: data.title, // 分享标题
  48. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  49. imgUrl: data.imgUrl, // 分享图标
  50. success: function () {}
  51. });
  52. });
  53. }