// 微信授权跳转
export const wxAuthRedirect = () => {
  const appId = 'wx8f2aa0defc700688' // 替换实际值
  const redirectUri = encodeURIComponent(window.location.origin + '/addUser')
  const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`

  // 清除可能存在的旧code
  const cleanUrl = window.location.href.split('?')[0]
  window.history.replaceState({}, document.title, cleanUrl)

  window.location.href = authUrl
}

// 解析URL参数
export const getUrlParam = (name) => {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`)
  const match = window.location.search.substr(1).match(reg)
  return match ? decodeURIComponent(match[2]) : null
}
