wechat.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { wxLogin,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. wxLogin({source:1}).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. // uni.showModal({
  17. // title: '提示2',
  18. // content: '这是一个模态弹窗',
  19. // })
  20. // if(!platformAgent().isWechat){
  21. // return false;
  22. // }
  23. // if(isIOS() && page != "home"){
  24. // return false;
  25. // }
  26. let url = getShareUrl(window.location.href);
  27. getShareConfig({
  28. url: url
  29. }).then((res)=>{
  30. if(res.status){
  31. wx.config(res.data);
  32. }else{
  33. console.log("获取wx分享配置失败");
  34. }
  35. });
  36. }
  37. export function setShareData(data){
  38. // if(!platformAgent().isWechat){
  39. // return false;
  40. // }
  41. let url = getShareUrl(window.location.href);
  42. wx.ready(function () {
  43. wx.updateAppMessageShareData({
  44. title: data.title, // 分享标题
  45. desc: data.desc || '', // 分享描述
  46. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  47. imgUrl: data.imgUrl, // 分享图标
  48. success: function () {}
  49. });
  50. wx.updateTimelineShareData({
  51. title: data.title, // 分享标题
  52. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  53. imgUrl: data.imgUrl, // 分享图标
  54. success: function () {}
  55. });
  56. });
  57. }