前言 最近搞了个 h5 需要调用企业微信的 jssdk 结果却怎么也调用不起来 -.-
代码结构 我的 uniapp 代码是利用uniapp
官方的colorui
模版结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 ├── App .vue ├── Readme .md ├── api ├── colorui ├── components ├── main .js ├── manifest .json ├── pages ├── pages .json ├── static ├── unpackage ├── pluign │ └── wxsdk .js └── utils
挖坑过程描述 我看企微 sdk 里面官方的例子是用的script
标签去进行 sdk 的引入。没有提供amd
等方式引入的例子。于是把文档保存在plugin
文件夹下,用import
的形式进行引入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <script > import wx from "@/plugin/wxsdk.js" ; import { getJsSdk } from "@/api/wx.js" ; export default { onLaunch: function ( ) { getJsSdk().then((res ) => { wx.config({ beta: true , debug: true , appId: res.corpid, timestamp: res.timestamp, nonceStr: res.nonceStr, signature: res.signature, jsApiList: ["sendChatMessage" ], }); }); wx.ready(() => console .log("wx ready" )); wx.error(err => console .log("wx error" , err)); }, };</script >
运行后提示微信的sdk文件S.title no defined
。然后看了下,发现S
是undefined
那我去做了个容错处理。
重新跑。结果没有跑ready
方法,也没有跑error
方法。然后心想是不是环境问题。换到微信开发者工具跑,没有打印日志。好吧,那我搞去企微上运行。结果也是不行。
但是我用打印wx.config
是有这个方法的,证明已经引入成功了的。而且debug
模式也开了。结果什么错误成功信息也没有出现。
解决办法 最后想有没有办法用script
标签的方式进行引入,引入文件。
发现在manifest.json
文件下的h5配置
tab内有一个index.html模版路径
的配置下。于是在项目根目录下添加index.html
文件,并写入以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <!DOCTYPE html > <html lang ="zh-CN" > <head > <meta charset ="utf-8" > <meta http-equiv ="X-UA-Compatible" content ="IE=edge" > <meta name ="viewport" content ="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <title > <%= htmlWebpackPlugin.options.title %> </title > <link rel ="stylesheet" href ="<%= BASE_URL %>static/index.<%= VUE_APP_INDEX_CSS_HASH %>.css" /> <script type ="text/javascript" src ="https://res.wx.qq.com/open/js/jweixin-1.2.0.js" > </script > </head > <body > <noscript > <strong > Please enable JavaScript to continue.</strong > </noscript > <div id ="app" > </div > </body > </html >
终于可以了。