initial.js 3.5KB

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