All Projects → Chooin → Wechat Spa

Chooin / Wechat Spa

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

Labels

Projects that are alternatives of or similar to Wechat Spa

Wechatdc
微信点餐SPA,基于Vue2.0,vuex开发,mock支持,跨端debug支持 项目预览地址:https://oneflying.github.io/wechatdc/
Stars: ✭ 22 (-93.47%)
Mutual labels:  wechat, spa
Wechat
微信SDK的golang实现,短小精悍,同时兼容【企业号/服务号/订阅号/小程序】
Stars: ✭ 330 (-2.08%)
Mutual labels:  wechat
Docs
Java知识总结:MySQL实战45讲,多线程和JVM知识总结,,SpringBoot,SpringCloud,Storm系列,微信小程序开发,ELK,《JAVA核心技术36讲笔记》,《深入理解JVM虚拟机笔记》,《高性能MySQL笔记》,《数据结构与算法》等等
Stars: ✭ 308 (-8.61%)
Mutual labels:  wechat
Youxiang
获取淘宝优惠券、京东优惠券、拼多多(多多客)优惠券、苏宁易购优惠券、唯品会优惠券,通过接入淘宝联盟、京东联盟、拼多多(多多进宝)、苏宁联盟(苏宁推客)、唯品会及其对应的开放平台,获取优惠商品图片和对应商品信息,利用微信机器人推送到指定群聊。
Stars: ✭ 313 (-7.12%)
Mutual labels:  wechat
Falconmessenger
🌟🌟🌟🌟🌟 Falcon Messenger is a Fast and Beautiful cloud-based messaging app. With iOS and IPadOS Support. Available on the App Store.
Stars: ✭ 310 (-8.01%)
Mutual labels:  wechat
Vbot
💬The best wechat robot base on web api!
Stars: ✭ 3,301 (+879.53%)
Mutual labels:  wechat
Jumpjumphelper
用 PHP 玩微信跳一跳
Stars: ✭ 304 (-9.79%)
Mutual labels:  wechat
Spa
A webapp framework for routing control and view transitions
Stars: ✭ 331 (-1.78%)
Mutual labels:  spa
Wechat Mini Shop
微信小程序商城,微信小程序微店,接口基于FaShop
Stars: ✭ 328 (-2.67%)
Mutual labels:  wechat
Wechat App Issues
💥 微信小程序踩坑集合
Stars: ✭ 318 (-5.64%)
Mutual labels:  wechat
Vuefront
VueFront Core. Turn your old-fashioned CMS website in to a SPA & PWA in 5 minutes
Stars: ✭ 316 (-6.23%)
Mutual labels:  spa
Weflow
A web developer workflow tool by WeChat team based on tmt-workflow, with cross-platform supported and environment ready.
Stars: ✭ 3,225 (+856.97%)
Mutual labels:  wechat
Eshoponcontainers
Cross-platform .NET sample microservices and container based application that runs on Linux Windows and macOS. Powered by .NET 6, Docker Containers and Azure Kubernetes Services. Supports Visual Studio, VS for Mac and CLI based environments with Docker CLI, dotnet CLI, VS Code or any other code editor.
Stars: ✭ 19,397 (+5655.79%)
Mutual labels:  spa
We Swiper
✨ 微信小程序触摸内容滑动解决方案we-swiper
Stars: ✭ 311 (-7.72%)
Mutual labels:  wechat
Wechaty Getting Started
A Starter Project Template for Wechaty works out-of-the-box
Stars: ✭ 330 (-2.08%)
Mutual labels:  wechat
Wepy Mall
微信小程序--基于wepy 商城(微店)微信小程序 欢迎学习交流
Stars: ✭ 3,224 (+856.68%)
Mutual labels:  wechat
Wemall
wemall7 开源版本 (不含商城)
Stars: ✭ 315 (-6.53%)
Mutual labels:  wechat
Tnwx
TNWX: TypeScript + Node.js + WeiXin 微信系开发脚手架,支持微信公众号、微信支付、微信小游戏、微信小程序、企业微信/企业号。最最最重要的是能快速的集成至任何 Node.js 框架(Express、Nest、Egg、Koa 等)
Stars: ✭ 319 (-5.34%)
Mutual labels:  wechat
Wechatbotxposed
微信回复机器人,Xposed模块
Stars: ✭ 335 (-0.59%)
Mutual labels:  wechat
Shorthand
shorthand微信公众号开源,基于Node.js和Vue
Stars: ✭ 331 (-1.78%)
Mutual labels:  wechat

微信端单页面应用(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].