All Projects → chooin → wechat-spa

chooin / wechat-spa

Licence: other
🎉 微信端单页面应用(SPA)常见问题汇总及解决方案

Projects that are alternatives of or similar to wechat-spa

Cofoundry.Samples.SPASite
A simple single page application using Cofoundry as a headless CMS
Stars: ✭ 30 (-91.1%)
Mutual labels:  spa
overlooker
Frontend performance profiling tool
Stars: ✭ 21 (-93.77%)
Mutual labels:  spa
webviewhs
🌐 A Haskell binding to the webview library created by Serge Zaitsev.
Stars: ✭ 109 (-67.66%)
Mutual labels:  spa
latelier
L'atelier, a project management tool
Stars: ✭ 74 (-78.04%)
Mutual labels:  spa
jQuery-SPA
jquery单页应用开发骨架
Stars: ✭ 20 (-94.07%)
Mutual labels:  spa
RSSR
React Server Side Rendering boilerplate with Authentication structure
Stars: ✭ 41 (-87.83%)
Mutual labels:  spa
componentjs
ComponentJS -- Powerful run-time Component System for structuring HTML5-based Rich Clients
Stars: ✭ 83 (-75.37%)
Mutual labels:  spa
xRoute
一个小型的前端路由库✈️
Stars: ✭ 36 (-89.32%)
Mutual labels:  spa
dumber
A dumb JavasScript bundler for Single Page Application, dumber than you and me.
Stars: ✭ 21 (-93.77%)
Mutual labels:  spa
super-simple-dockerized-spa
Illustrating how to wrap a single page application in docker.
Stars: ✭ 25 (-92.58%)
Mutual labels:  spa
vuetube
Video resources that will help you to improve your Vue skills
Stars: ✭ 54 (-83.98%)
Mutual labels:  spa
kontent-sample-app-react
Sample React SPA utilizing the Kontent Delivery API to fetch content.
Stars: ✭ 45 (-86.65%)
Mutual labels:  spa
dowels
🔨 a tiny but powerful javascript library that performs client-side routing, templating, and REST API communication to help you get your single-page web applications running in seconds
Stars: ✭ 13 (-96.14%)
Mutual labels:  spa
spring-boot-react-blog
Token-based blog application using spring boot, react and jwt.
Stars: ✭ 132 (-60.83%)
Mutual labels:  spa
mo360-ftk
MO360 Frontend Toolkit: A toolkit for single page applications (SPA) based on React and Typescript that allows to extract single features into microfrontends.
Stars: ✭ 54 (-83.98%)
Mutual labels:  spa
SvelteScaling
Does Svelte scale?
Stars: ✭ 21 (-93.77%)
Mutual labels:  spa
imgalign
Webapplication for image stitching and aligning
Stars: ✭ 162 (-51.93%)
Mutual labels:  spa
wordpress
Free PWA & SPA for Wordpress & Woocommerce
Stars: ✭ 103 (-69.44%)
Mutual labels:  spa
brn
The idea of this project is to design and make a web-application (with scientist cooperation) which would contained series of special audio trainings to support people with central auditory skills deficit to allow them to train them to listen better.
Stars: ✭ 41 (-87.83%)
Mutual labels:  spa
stimulus todomvc
[WIP] An implementation of TodoMVC using Ruby on Rails and StimulusJS
Stars: ✭ 14 (-95.85%)
Mutual labels:  spa

微信端单页面应用(SPA)常见问题汇总及解决方案

非常重要:

路由启用 hash 模式,hash 务必是 #,如:https://example.com/#/home/index

采用 history 模式,页面路由改变后无法复制出改变后的 URL 地址

参数使用 ? 的形式获取,如:https://example.com/#/product/detail?id=1

不采用 ? 的形式获取参数则需要配置很多支付安全目录

新建一个页面用于获取 wechat_openid、token 等操作,用户第一次进入 SPA 项目后的都需要跳转到 auth.html 页面,如:在根目录 static 文件夹下新建 auth.html,微信授权时序图

解决需要配置很多支付安全目录的问题(网上很多资料都说在支付页面添加 ?,如:https://example.com/?#/payment/index?order_id=1,这样会使路由很混乱,我不建议你采用添加 ? 的形式去解决支付问题)

Nginx 配置

add_header "Cache-Control" "no-cache, private";

解决 window.location.href 跳转页面被浏览器缓存的问题

涉及调用 JS-SDK 的页面都得重新配置 wx.config()

你懂的~

微信授权时序图:

is_auth:is_auth 存在说明已经经过 auth.html 页面跳转

安装和使用微信 JS-SDK

方法 1 (推荐)

在入口 index.html 文件引入微信的 JS-SDK 文件,webpack 配置参考:中文 / English

index.html

<script src="//res.wx.qq.com/open/js/jweixin-1.2.2.js"></script>

webpack.config.js

externals: {
  wx: 'wx'
}

如何使用:

import wx from 'wx'

wx.ready(() => {
  console.log('Hello Wechat!')
})

方法 2

如何安装:

yarn add weixin-js-sdk
#
npm install weixin-js-sdk --save

如何使用:

import wx from 'weixin-js-sdk'

wx.ready(() => {
  console.log('Hello Wechat!')
})

标题更新

在切换页面路由之后需在 body 里面添加 iframe,随后移除掉 iframe 即可,代码如下

// iPhone,iPod,iPad 下无法更新标题
if (/ip(hone|od|ad)/i.test(window.navigator.userAgent)) {
  let iframe = document.createElement('iframe')
  iframe.style.display = 'none'
  iframe.src = '/favicon.ico'
  iframe.onload = () => {
    setTimeout(() => {
      iframe.remove()
    }, 10)
  }
  document.body.appendChild(iframe)
}

微信分享

  1. 分享配置都正确,进入链接后页面显示不对

解决方案: 在分享的地址后面添加一个随机字符串,如:https://example.com/#/product/detail?id=1&share_at=${Date.now()}

微信分享参考代码:

import wx from 'wx'
import axios from 'axios'

const share = ({
  title,
  desc,
  fullPath,
  imgUrl
}) => {
  let link = fullPath.indexOf('?') > -1
    ? `https://example.com/#${fullPath}&share_at=${Date.now()}`
    : `https://example.com/#${fullPath}?share_at=${Date.now()}`
  wx.showAllNonBaseMenuItem()
  wx.onMenuShareTimeline({
    title,
    link,
    imgUrl
  })
  wx.onMenuShareAppMessage({
    title,
    desc,
    link,
    imgUrl
  })
}

const $_wechat = () => {
  return new Promise((resolve, reject) => {
    // 获取服务端微信配置信息
    axios.get('https://api.example.com/v1/wechat/config', {
      params: {
        url: window.location.href.split('#')[0]
      }
    }).then(res => {
      wx.config({
        debug: false,
        appId: res.data.appId,
        timestamp: res.data.timestamp,
        nonceStr: res.data.nonceStr,
        signature: res.data.signature,
        jsApiList: [
          'onMenuShareTimeline',
          'onMenuShareAppMessage'
        ]
      })
      wx.ready(() => { // 配置 wx.config 成功
        resolve({
          wx,
          share
        })
      })
    }).catch(() => {
      reject(new Error('微信签名接口异常'))
    })
  })
}

// 调用分享
$_wechat().then(res => {
  res.share({ // 配置分享
    title: 'wechat-spa',
    desc: 'Wechat SPA',
    fullPath: '/home/index',
    imgUrl: 'https://www.baidu.com/img/bd_logo1.png'
  })
}).catch(_ => {
  console.warn(_.message)
})

微信支付

  1. 支付安全目录,iOS 识别支付安全目录路径规则是进入 SPA 应用的第一个页面所对应的 URL
第一次进入的 URL iOS 获取到的安全目录
https://example.com/#/home/index https://example.com/#/home/index
https://example.com/#/me/index https://example.com/#/me/index
https://example.com/#/product/index https://example.com/#/product/index

这样我们要配置很多的安全目录路径,但微信平台仅允许设置3个安全目录路径,直接进入 SPA 应用的页面是行不通的

解决思路: 用户都得先进入 SPA 应用的根目录 https://example.com/,然后通过 SPA 应用路由提供的钩子重定向到自己想要访问的页面,微信授权时序图

白屏

微信支付后立即跳转到其他页面有一定几率出现白屏(长按屏幕可以复制出文字或图片地址),解决方案:

// 延迟跳转即可解决
setTimeout(() => {
  window.location.replace('/payment/success') // 跳转逻辑
}, 500)

微信内置浏览器的 bug,图片无法批量上传也可以通过 setTimeout 方法解决

问题反馈

如内容有误请反馈给我,谢谢

有什么问题可以加我好友,大家一起交流

QQ:465353876

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].