initial.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. console.log(store);
  52. const code = node.process.NODE_ENV === 'development' ? '123' : getCode()
  53. return LoginUser(code).then(e => {
  54. // PVsum
  55. console.log(e);
  56. let userInfo = {
  57. token: e.token,
  58. name: e.person.name,
  59. phone: e.person.phone,
  60. personId: e.person.personId,
  61. }
  62. store.commit('SET_USER_INFO', userInfo)
  63. let location = window.location.href
  64. pv({ addr: location })
  65. hideLoading()
  66. return;
  67. })
  68. }
  69. function hideLoading () {
  70. document.getElementsByClassName('page-loading-wrapper')[0].style = "display: none"
  71. }
  72. /**
  73. * 获取 code
  74. * @returns
  75. */
  76. export function getCode () {
  77. const matched = /[?&]*code=([^&]+)/.exec(location.search)
  78. if (matched) {
  79. return decodeURIComponent(matched[1])
  80. }
  81. }
  82. /**
  83. * 跳转授权页面
  84. */
  85. export function redirect (force) {
  86. if (process.env.NODE_ENV === 'development') return;
  87. const originCode = localStorage.getItem('wxcode');
  88. const queryCode = getCode();
  89. localStorage.setItem('wxcode', queryCode)
  90. if (force || !queryCode || queryCode === originCode) {
  91. const local = encodeURIComponent(location.origin + location.pathname)
  92. 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`
  93. window.location.href = url;
  94. }
  95. }