wx-js-sdk-register.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. declare const wx: any
  2. declare const WeixinJSBridge: any
  3. // 官方文档
  4. // https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
  5. // 参考1: https://github.com/wxt365/JWeixinApi/blob/master/jweixinapi.js
  6. // 参考2: https://my.oschina.net/u/2405644/blog/492396
  7. class WeiXinSDK {
  8. env = {
  9. // 微信JSSDK版本
  10. version: '1.6.0',
  11. isWeiXinBrowser: navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
  12. }
  13. config = {
  14. // 是否开启调试
  15. WeiXinDebugMode: false,
  16. // 当前url(发送到后端进行注册的,包含参数)
  17. configUrl: location.href.split('#')[0],
  18. // 分享配置文件中的url
  19. shareUrl: location.href.split('?')[0],
  20. configData: {
  21. // 公众号的唯一标识
  22. appId: '',
  23. // 生成签名的时间戳
  24. timestamp: '',
  25. // 生成签名的随机串
  26. nonceStr: '',
  27. // 签名,见附录1
  28. signature: ''
  29. },
  30. jsApiList: [
  31. // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
  32. 'updateAppMessageShareData',
  33. // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
  34. 'updateTimelineShareData',
  35. // 获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口
  36. // 'onMenuShareWeibo',
  37. // 图片预览
  38. // 'imagePreview',
  39. // 获取网络状态接口
  40. // 'getNetworkType',
  41. // 获取地理位置接口
  42. // 'getLocation',
  43. // 使用微信内置地图<查看>位置接口
  44. // 'openLocation',
  45. // 批量隐藏功能按钮接口
  46. // 'hideMenuItems',
  47. // 批量显示功能按钮接口
  48. // 'showMenuItems',
  49. // 隐藏所有非基础按钮接口
  50. // 'hideAllNonBaseMenuItem',
  51. // 显示所有功能按钮接口
  52. // 'showAllNonBaseMenuItem',
  53. // 调起微信扫一扫接口
  54. // 'scanQRCode',
  55. // 关闭当前网页窗口接口
  56. 'closeWindow',
  57. // 发起一个微信支付请求
  58. 'chooseWXPay'
  59. ]
  60. }
  61. currentLocation = {
  62. // 纬度,浮点数,范围为90 ~ -90
  63. latitude: 0,
  64. // 经度,浮点数,范围为180 ~ -180
  65. longitude: 0,
  66. // 速度,以米/每秒计
  67. speed: 0,
  68. // 位置精度
  69. accuracy: 0
  70. }
  71. defaultShareInfo = {
  72. // 分享标题
  73. title: '剑鱼标讯',
  74. // 分享描述
  75. desc: '全国招标信息免费看,不遮挡',
  76. // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  77. link: this.config.shareUrl,
  78. // 分享图标
  79. imgUrl: 'https://cdn-ali.jianyu360.com/images/appext/fixed-sm.jpg'
  80. }
  81. constructor (data: object) {
  82. if (!this.env.isWeiXinBrowser) return
  83. this.initSignature(data)
  84. }
  85. initSignature (data: object) {
  86. this.setConfigData(data)
  87. this.setConfig()
  88. }
  89. // 将constructor中传入的值赋值到全局
  90. setConfigData (data: any) {
  91. for (const key in this.config.configData) {
  92. const d = this.config.configData as any
  93. d[key] = data[key]
  94. }
  95. }
  96. // wxJSSDK注册
  97. setConfig () {
  98. wx.config({
  99. // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  100. debug: this.config.WeiXinDebugMode,
  101. // // 必填,公众号的唯一标识
  102. // appId: data.appId,
  103. // // 必填,生成签名的时间戳
  104. // timestamp: data.timestamp,
  105. // // 必填,生成签名的随机串
  106. // nonceStr: data.nonceStr,
  107. // // 必填,签名,见附录1
  108. // signature: data.signature,
  109. ...this.config.configData,
  110. // 必填,需要使用的JS接口列表
  111. jsApiList: this.config.jsApiList
  112. })
  113. }
  114. // 将分享数据整理
  115. getShareInfo (data: any) {
  116. // 如果是null或者空对象
  117. if (!data || (data && Object.keys(data).length === 0)) {
  118. return this.defaultShareInfo
  119. } else {
  120. const shareInfo = JSON.parse(JSON.stringify(this.defaultShareInfo))
  121. for (const key in shareInfo) {
  122. if (data[key]) {
  123. shareInfo[key] = data[key]
  124. }
  125. }
  126. return shareInfo
  127. }
  128. }
  129. // 检查微信api是否可用
  130. checkJsApi (apiList: any = [], callback?: Function) {
  131. apiList = apiList.length === 0 ? this.config.jsApiList : apiList
  132. wx.checkJsApi({
  133. // 需要检测的JS接口列表
  134. jsApiList: apiList,
  135. success: (res: any) => {
  136. // 以键值对的形式返回,可用的api值true,不可用为false
  137. // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  138. console.log(res)
  139. callback && callback(res)
  140. }
  141. })
  142. }
  143. ready (callback?: Function) {
  144. wx.ready(() => {
  145. if (typeof callback === 'function') {
  146. // callback && callback.call(this)
  147. callback && callback()
  148. }
  149. })
  150. }
  151. // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
  152. shareToFriendAndQQ (options: any = {}) {
  153. // 如果不传配置,就使用默认的配置
  154. const config = this.getShareInfo(options.config)
  155. // 执行分享
  156. this.ready(() => {
  157. wx.updateAppMessageShareData({
  158. ...config,
  159. // title: config.title,
  160. // desc: config.desc,
  161. // link: config.link,
  162. // imgUrl: config.imgUrl,
  163. success: () => {
  164. // 用户确认分享后执行的回调函数
  165. options && options.success && options.success()
  166. },
  167. cancel: () => {
  168. // 用户取消分享后执行的回调函数
  169. options && options.cancel && options.cancel()
  170. },
  171. fail: () => {
  172. // 接口调用失败时执行的回调函数
  173. options && options.fail && options.fail()
  174. },
  175. complete: () => {
  176. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  177. options && options.complete && options.complete()
  178. }
  179. })
  180. })
  181. }
  182. // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
  183. shareToFriendsAndQZone (options: any = {}) {
  184. // 如果不传配置,就使用默认的配置
  185. const config = this.getShareInfo(options.config)
  186. // 执行分享
  187. this.ready(() => {
  188. wx.updateTimelineShareData({
  189. ...config,
  190. // title: config.title,
  191. // desc: config.desc,
  192. // link: config.link,
  193. // imgUrl: config.imgUrl,
  194. success: () => {
  195. // 用户确认分享后执行的回调函数
  196. options && options.success && options.success()
  197. },
  198. cancel: () => {
  199. // 用户取消分享后执行的回调函数
  200. options && options.cancel && options.cancel()
  201. },
  202. fail: () => {
  203. // 接口调用失败时执行的回调函数
  204. options && options.fail && options.fail()
  205. },
  206. complete: () => {
  207. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  208. options && options.complete && options.complete()
  209. }
  210. })
  211. })
  212. }
  213. // 自定义“分享到微博”
  214. shareToWeiBo (options: any = {}) {
  215. // 如果不传配置,就使用默认的配置
  216. const config = this.getShareInfo(options.config)
  217. // 执行分享
  218. wx.onMenuShareWeibo({
  219. ...config,
  220. // title: config.title,
  221. // desc: config.desc,
  222. // link: config.link,
  223. // imgUrl: config.imgUrl,
  224. success: () => {
  225. // 用户确认分享后执行的回调函数
  226. options && options.success && options.success()
  227. },
  228. cancel: () => {
  229. // 用户取消分享后执行的回调函数
  230. options && options.cancel && options.cancel()
  231. },
  232. fail: () => {
  233. // 接口调用失败时执行的回调函数
  234. options && options.fail && options.fail()
  235. },
  236. complete: () => {
  237. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  238. options && options.complete && options.complete()
  239. }
  240. })
  241. }
  242. // 微信js-sdk支付
  243. chooseWXPay (options: any) {
  244. // 执行支付
  245. wx.chooseWXPay({
  246. // appId: this.config.configData.appId,
  247. ...options.config,
  248. // timestamp: config.timestamp,
  249. // nonceStr: config.nonceStr,
  250. // package: config.package,
  251. // signType: config.signType,
  252. // paySign: config.paySign,
  253. success: () => {
  254. // 用户确认支付后执行的回调函数
  255. options && options.success && options.success()
  256. },
  257. cancel: () => {
  258. // 用户取消支付后执行的回调函数
  259. options && options.cancel && options.cancel()
  260. },
  261. fail: () => {
  262. // 接口调用失败时执行的回调函数
  263. options && options.fail && options.fail()
  264. },
  265. complete: () => {
  266. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  267. options && options.complete && options.complete()
  268. }
  269. })
  270. }
  271. // https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
  272. chooseWXPayForWeiXinJSBridge (options: any) {
  273. // 调用WeixinJSBridge支付
  274. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  275. appId: options.config.appId,
  276. timeStamp: options.config.timestamp,
  277. nonceStr: options.config.nonceStr,
  278. package: options.config.package,
  279. signType: options.config.signType,
  280. paySign: options.config.paySign
  281. }, (res: any) => {
  282. if (res.err_msg === 'get_brand_wcpay_request:ok') {
  283. // 使用以上方式判断前端返回,微信团队郑重提示:
  284. // res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  285. options && options.success && options.success()
  286. } else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
  287. options && options.cancel && options.cancel()
  288. } else {
  289. options && options.fail && options.fail()
  290. }
  291. })
  292. }
  293. closeWindow () {
  294. wx.closeWindow()
  295. }
  296. hideOptionMenu () {
  297. wx.hideOptionMenu()
  298. }
  299. showOptionMenu () {
  300. wx.showOptionMenu()
  301. }
  302. hideMenuItems (menuList = []) {
  303. // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  304. wx.hideMenuItems({
  305. menuList
  306. })
  307. }
  308. showMenuItems (menuList = []) {
  309. // 要显示的菜单项,所有menu项见附录3
  310. wx.showMenuItems({
  311. menuList
  312. })
  313. }
  314. hideAllNonBaseMenuItem () {
  315. wx.hideAllNonBaseMenuItem()
  316. }
  317. showAllNonBaseMenuItem () {
  318. wx.showAllNonBaseMenuItem()
  319. }
  320. getNetworkType (options: any) {
  321. wx.getNetworkType({
  322. success: (res: any) => {
  323. // 用户确认支付后执行的回调函数
  324. // 返回网络类型2g,3g,4g,wifi
  325. options && options.success && options.success(res)
  326. },
  327. cancel: () => {
  328. // 用户取消支付后执行的回调函数
  329. options && options.cancel && options.cancel()
  330. },
  331. fail: () => {
  332. // 接口调用失败时执行的回调函数
  333. options && options.fail && options.fail()
  334. },
  335. complete: () => {
  336. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  337. options && options.complete && options.complete()
  338. }
  339. })
  340. }
  341. /**
  342. * 调起微信Native的图片播放组件。
  343. * 这里必须对参数进行强检测,如果参数不合法,直接会导致微信客户端crash
  344. *
  345. * @param {String} curSrc 当前播放的图片地址
  346. * @param {Array} srcList 图片地址列表
  347. */
  348. imagePreview (curSrc: string, srcList: Array<string>) {
  349. if (!curSrc || !srcList || srcList.length === 0) {
  350. return
  351. }
  352. wx.previewImage({
  353. // 当前显示图片的http地址
  354. current: curSrc,
  355. // 需要预览的图片http地址列表
  356. urls: srcList
  357. })
  358. }
  359. getLocation (options: any) {
  360. wx.getLocation({
  361. // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  362. type: options.type === undefined ? 'wgs84' : options.type,
  363. success: (res: any) => {
  364. // for (const key in this.currentLocation) {
  365. // this.currentLocation[key] = res[key]
  366. // }
  367. this.currentLocation = res
  368. options && options.success && options.success(res)
  369. },
  370. cancel: (res: any) => {
  371. // 用户取消支付后执行的回调函数
  372. options && options.cancel && options.cancel(res)
  373. },
  374. fail: (res: any) => {
  375. // 接口调用失败时执行的回调函数
  376. options && options.fail && options.fail(res)
  377. },
  378. complete: () => {
  379. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  380. options && options.complete && options.complete()
  381. }
  382. })
  383. }
  384. openLocation (data: object) {
  385. wx.openLocation({
  386. ...data
  387. // latitude: 0, // 纬度,浮点数,范围为90 ~ -90
  388. // longitude: 0, // 经度,浮点数,范围为180 ~ -180。
  389. // name: '', // 位置名
  390. // address: '', // 地址详情说明
  391. // scale: 1, // 地图缩放级别,整形值,范围从1~28。默认为最大
  392. // infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
  393. })
  394. }
  395. // 调起微信扫一扫接口
  396. scanQRCode (options: any) {
  397. wx.scanQRCode({
  398. needResult: options.needResult === undefined ? 0 : options.needResult, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
  399. scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  400. success: (res: any) => {
  401. // 当needResult 为 1 时,扫码返回的结果
  402. console.log(res.resultStr)
  403. options && options.success && options.success(res)
  404. },
  405. cancel: (res: any) => {
  406. // 用户取消支付后执行的回调函数
  407. options && options.cancel && options.cancel(res)
  408. },
  409. fail: (res: any) => {
  410. // 接口调用失败时执行的回调函数
  411. options && options.fail && options.fail(res)
  412. },
  413. complete: () => {
  414. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  415. options && options.complete && options.complete()
  416. }
  417. })
  418. }
  419. }
  420. export default WeiXinSDK