import { LoginUser, pv } from './api' import store from '../store' const jsApiList = [ 'updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ] function initSDK (url) { request(`https://api.h5.njyunzhi.com/mp/jssdk?url=${encodeURIComponent(url)}`).then((res) => { window.wx.config({ debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: res.appId, // 必填,公众号的唯一标识 timestamp: res.timestamp, // 必填,生成签名的时间戳 nonceStr: res.nonceStr, // 必填,生成签名的随机串 signature: res.signature, // 必填,签名 jsApiList // 必填,需要使用的JS接口列表 }) }) } // Vue.use(Vuex) /** * 分享 * @param {*} opt */ export function share (opt) { const { origin, pathname, search } = window.location // const defaultLink = origin + pathname const defaultImg = `${origin}${pathname}images/share.png` const link = origin + pathname + search // opt.link || defaultLink const imgUrl = opt.imgUrl || defaultImg initSDK(link) window.wx.ready(function () { jsApiList.map((apiName) => { const fn = window.wx[apiName] if (typeof fn === 'function') { fn({ title: opt.title || '开启干洗护理新时代', // 分享标题 link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl, // 分享图标 desc: opt.desc || '即刻尊享' }) } }) }) } export function Login () { const code = node.process.NODE_ENV === 'development' ? '123' : getCode() return LoginUser(code).then(e => { // PVsum console.log(e); let userInfo = { token: e.token, name: e.person.name, phone: e.person.phone, personId: e.person.personId, } store.commit('SET_USER_INFO', userInfo) let location = window.location.href pv({ addr: location }) hideLoading() return; }) } function hideLoading () { document.getElementsByClassName('page-loading-wrapper')[0].style = "display: none" } /** * 获取 code * @returns */ export function getCode () { const matched = /[?&]*code=([^&]+)/.exec(location.search) if (matched) { return decodeURIComponent(matched[1]) } } /** * 跳转授权页面 */ export function redirect (force) { if (process.env.NODE_ENV === 'development') return; const originCode = localStorage.getItem('wxcode'); const queryCode = getCode(); localStorage.setItem('wxcode', queryCode) if (force || !queryCode || queryCode === originCode) { const local = encodeURIComponent(location.origin + location.pathname) 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` window.location.href = url; } }