initial.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { LoginUser, pv } from './api'
  2. import store from '../store'
  3. const jsApiList = [
  4. 'updateAppMessageShareData',
  5. 'updateTimelineShareData',
  6. 'onMenuShareTimeline',
  7. 'onMenuShareAppMessage',
  8. 'onMenuShareQQ',
  9. 'onMenuShareWeibo',
  10. 'onMenuShareQZone'
  11. ]
  12. function initSDK (url) {
  13. request(`https://api.h5.njyunzhi.com/mp/jssdk?url=${encodeURIComponent(url)}`).then((res) => {
  14. window.wx.config({
  15. debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  16. appId: res.appId, // 必填,公众号的唯一标识
  17. timestamp: res.timestamp, // 必填,生成签名的时间戳
  18. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  19. signature: res.signature, // 必填,签名
  20. jsApiList // 必填,需要使用的JS接口列表
  21. })
  22. })
  23. }
  24. // Vue.use(Vuex)
  25. /**
  26. * 分享
  27. * @param {*} opt
  28. */
  29. export function share (opt) {
  30. const { origin, pathname, search } = window.location
  31. // const defaultLink = origin + pathname
  32. const defaultImg = `${origin}${pathname}images/share.png`
  33. const link = origin + pathname + search // opt.link || defaultLink
  34. const imgUrl = opt.imgUrl || defaultImg
  35. initSDK(link)
  36. window.wx.ready(function () {
  37. jsApiList.map((apiName) => {
  38. const fn = window.wx[apiName]
  39. if (typeof fn === 'function') {
  40. fn({
  41. title: opt.title || '开启干洗护理新时代', // 分享标题
  42. link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  43. imgUrl, // 分享图标
  44. desc: opt.desc || '即刻尊享'
  45. })
  46. }
  47. })
  48. })
  49. }
  50. export function Login () {
  51. const code = node.process.NODE_ENV === 'development' ? '123' : getCode()
  52. return LoginUser(code).then(e => {
  53. // PVsum
  54. console.log(e);
  55. let userInfo = {
  56. token: e.token,
  57. name: e.person.name,
  58. phone: e.person.phone,
  59. personId: e.person.personId,
  60. }
  61. store.commit('SET_USER_INFO', userInfo)
  62. let location = window.location.href
  63. pv({ addr: location })
  64. hideLoading()
  65. return;
  66. })
  67. }
  68. function hideLoading () {
  69. document.getElementsByClassName('page-loading-wrapper')[0].style = "display: none"
  70. }
  71. /**
  72. * 获取 code
  73. * @returns
  74. */
  75. export function getCode () {
  76. const matched = /[?&]*code=([^&]+)/.exec(location.search)
  77. if (matched) {
  78. return decodeURIComponent(matched[1])
  79. }
  80. }
  81. /**
  82. * 跳转授权页面
  83. */
  84. export function redirect (force) {
  85. if (process.env.NODE_ENV === 'development') return;
  86. const originCode = localStorage.getItem('wxcode');
  87. const queryCode = getCode();
  88. localStorage.setItem('wxcode', queryCode)
  89. if (force || !queryCode || queryCode === originCode) {
  90. const local = encodeURIComponent(location.origin + location.pathname)
  91. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd3bab568bc42d1de&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  92. window.location.href = url;
  93. }
  94. }