wx-js-sdk-register.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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) {
  91. for (const key in this.config.configData) {
  92. this.config.configData[key] = data[key]
  93. }
  94. }
  95. // wxJSSDK注册
  96. setConfig () {
  97. wx.config({
  98. // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  99. debug: this.config.WeiXinDebugMode,
  100. // // 必填,公众号的唯一标识
  101. // appId: data.appId,
  102. // // 必填,生成签名的时间戳
  103. // timestamp: data.timestamp,
  104. // // 必填,生成签名的随机串
  105. // nonceStr: data.nonceStr,
  106. // // 必填,签名,见附录1
  107. // signature: data.signature,
  108. ...this.config.configData,
  109. // 必填,需要使用的JS接口列表
  110. jsApiList: this.config.jsApiList
  111. })
  112. }
  113. // 将分享数据整理
  114. getShareInfo (data) {
  115. // 如果是null或者空对象
  116. if (!data || (data && Object.keys(data).length === 0)) {
  117. return this.defaultShareInfo
  118. } else {
  119. const shareInfo = JSON.parse(JSON.stringify(this.defaultShareInfo))
  120. for (const key in shareInfo) {
  121. if (data[key]) {
  122. shareInfo[key] = data[key]
  123. }
  124. }
  125. return shareInfo
  126. }
  127. }
  128. // 检查微信api是否可用
  129. checkJsApi (apiList: any = [], callback?: Function) {
  130. apiList = apiList.length === 0 ? this.config.jsApiList : apiList
  131. wx.checkJsApi({
  132. // 需要检测的JS接口列表
  133. jsApiList: apiList,
  134. success: res => {
  135. // 以键值对的形式返回,可用的api值true,不可用为false
  136. // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  137. console.log(res)
  138. callback && callback(res)
  139. }
  140. })
  141. }
  142. ready (callback?: Function) {
  143. wx.ready(() => {
  144. if (typeof callback === 'function') {
  145. // callback && callback.call(this)
  146. callback && callback()
  147. }
  148. })
  149. }
  150. // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
  151. shareToFriendAndQQ (options: any = {}) {
  152. // 如果不传配置,就使用默认的配置
  153. const config = this.getShareInfo(options.config)
  154. // 执行分享
  155. this.ready(() => {
  156. wx.updateAppMessageShareData({
  157. ...config,
  158. // title: config.title,
  159. // desc: config.desc,
  160. // link: config.link,
  161. // imgUrl: config.imgUrl,
  162. success: () => {
  163. // 用户确认分享后执行的回调函数
  164. options && options.success && options.success()
  165. },
  166. cancel: () => {
  167. // 用户取消分享后执行的回调函数
  168. options && options.cancel && options.cancel()
  169. },
  170. fail: () => {
  171. // 接口调用失败时执行的回调函数
  172. options && options.fail && options.fail()
  173. },
  174. complete: () => {
  175. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  176. options && options.complete && options.complete()
  177. }
  178. })
  179. })
  180. }
  181. // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
  182. shareToFriendsAndQZone (options: any = {}) {
  183. // 如果不传配置,就使用默认的配置
  184. const config = this.getShareInfo(options.config)
  185. // 执行分享
  186. this.ready(() => {
  187. wx.updateTimelineShareData({
  188. ...config,
  189. // title: config.title,
  190. // desc: config.desc,
  191. // link: config.link,
  192. // imgUrl: config.imgUrl,
  193. success: () => {
  194. // 用户确认分享后执行的回调函数
  195. options && options.success && options.success()
  196. },
  197. cancel: () => {
  198. // 用户取消分享后执行的回调函数
  199. options && options.cancel && options.cancel()
  200. },
  201. fail: () => {
  202. // 接口调用失败时执行的回调函数
  203. options && options.fail && options.fail()
  204. },
  205. complete: () => {
  206. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  207. options && options.complete && options.complete()
  208. }
  209. })
  210. })
  211. }
  212. // 自定义“分享到微博”
  213. shareToWeiBo (options: any = {}) {
  214. // 如果不传配置,就使用默认的配置
  215. const config = this.getShareInfo(options.config)
  216. // 执行分享
  217. wx.onMenuShareWeibo({
  218. ...config,
  219. // title: config.title,
  220. // desc: config.desc,
  221. // link: config.link,
  222. // imgUrl: config.imgUrl,
  223. success: () => {
  224. // 用户确认分享后执行的回调函数
  225. options && options.success && options.success()
  226. },
  227. cancel: () => {
  228. // 用户取消分享后执行的回调函数
  229. options && options.cancel && options.cancel()
  230. },
  231. fail: () => {
  232. // 接口调用失败时执行的回调函数
  233. options && options.fail && options.fail()
  234. },
  235. complete: () => {
  236. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  237. options && options.complete && options.complete()
  238. }
  239. })
  240. }
  241. // 微信js-sdk支付
  242. chooseWXPay (options: any) {
  243. // 执行支付
  244. wx.chooseWXPay({
  245. // appId: this.config.configData.appId,
  246. ...options.config,
  247. // timestamp: config.timestamp,
  248. // nonceStr: config.nonceStr,
  249. // package: config.package,
  250. // signType: config.signType,
  251. // paySign: config.paySign,
  252. success: () => {
  253. // 用户确认支付后执行的回调函数
  254. options && options.success && options.success()
  255. },
  256. cancel: () => {
  257. // 用户取消支付后执行的回调函数
  258. options && options.cancel && options.cancel()
  259. },
  260. fail: () => {
  261. // 接口调用失败时执行的回调函数
  262. options && options.fail && options.fail()
  263. },
  264. complete: () => {
  265. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  266. options && options.complete && options.complete()
  267. }
  268. })
  269. }
  270. // https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
  271. chooseWXPayForWeiXinJSBridge (options: any) {
  272. // 调用WeixinJSBridge支付
  273. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  274. appId: options.config.appId,
  275. timeStamp: options.config.timestamp,
  276. nonceStr: options.config.nonceStr,
  277. package: options.config.package,
  278. signType: options.config.signType,
  279. paySign: options.config.paySign
  280. }, (res) => {
  281. if (res.err_msg === 'get_brand_wcpay_request:ok') {
  282. // 使用以上方式判断前端返回,微信团队郑重提示:
  283. // res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  284. options && options.success && options.success()
  285. } else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
  286. options && options.cancel && options.cancel()
  287. } else {
  288. options && options.fail && options.fail()
  289. }
  290. })
  291. }
  292. closeWindow () {
  293. wx.closeWindow()
  294. }
  295. hideOptionMenu () {
  296. wx.hideOptionMenu()
  297. }
  298. showOptionMenu () {
  299. wx.showOptionMenu()
  300. }
  301. hideMenuItems (menuList = []) {
  302. // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  303. wx.hideMenuItems({
  304. menuList
  305. })
  306. }
  307. showMenuItems (menuList = []) {
  308. // 要显示的菜单项,所有menu项见附录3
  309. wx.showMenuItems({
  310. menuList
  311. })
  312. }
  313. hideAllNonBaseMenuItem () {
  314. wx.hideAllNonBaseMenuItem()
  315. }
  316. showAllNonBaseMenuItem () {
  317. wx.showAllNonBaseMenuItem()
  318. }
  319. getNetworkType (options: any) {
  320. wx.getNetworkType({
  321. success: (res) => {
  322. // 用户确认支付后执行的回调函数
  323. // 返回网络类型2g,3g,4g,wifi
  324. options && options.success && options.success(res)
  325. },
  326. cancel: () => {
  327. // 用户取消支付后执行的回调函数
  328. options && options.cancel && options.cancel()
  329. },
  330. fail: () => {
  331. // 接口调用失败时执行的回调函数
  332. options && options.fail && options.fail()
  333. },
  334. complete: () => {
  335. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  336. options && options.complete && options.complete()
  337. }
  338. })
  339. }
  340. /**
  341. * 调起微信Native的图片播放组件。
  342. * 这里必须对参数进行强检测,如果参数不合法,直接会导致微信客户端crash
  343. *
  344. * @param {String} curSrc 当前播放的图片地址
  345. * @param {Array} srcList 图片地址列表
  346. */
  347. imagePreview (curSrc: string, srcList: Array<string>) {
  348. if (!curSrc || !srcList || srcList.length === 0) {
  349. return
  350. }
  351. wx.previewImage({
  352. // 当前显示图片的http地址
  353. current: curSrc,
  354. // 需要预览的图片http地址列表
  355. urls: srcList
  356. })
  357. }
  358. getLocation (options: any) {
  359. wx.getLocation({
  360. // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  361. type: options.type === undefined ? 'wgs84' : options.type,
  362. success: (res) => {
  363. // for (const key in this.currentLocation) {
  364. // this.currentLocation[key] = res[key]
  365. // }
  366. this.currentLocation = res
  367. options && options.success && options.success(res)
  368. },
  369. cancel: (res) => {
  370. // 用户取消支付后执行的回调函数
  371. options && options.cancel && options.cancel(res)
  372. },
  373. fail: (res) => {
  374. // 接口调用失败时执行的回调函数
  375. options && options.fail && options.fail(res)
  376. },
  377. complete: () => {
  378. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  379. options && options.complete && options.complete()
  380. }
  381. })
  382. }
  383. openLocation (data: object) {
  384. wx.openLocation({
  385. ...data
  386. // latitude: 0, // 纬度,浮点数,范围为90 ~ -90
  387. // longitude: 0, // 经度,浮点数,范围为180 ~ -180。
  388. // name: '', // 位置名
  389. // address: '', // 地址详情说明
  390. // scale: 1, // 地图缩放级别,整形值,范围从1~28。默认为最大
  391. // infoUrl: '' // 在查看位置界面底部显示的超链接,可点击跳转
  392. })
  393. }
  394. // 调起微信扫一扫接口
  395. scanQRCode (options: any) {
  396. wx.scanQRCode({
  397. needResult: options.needResult === undefined ? 0 : options.needResult, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
  398. scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  399. success: (res) => {
  400. // 当needResult 为 1 时,扫码返回的结果
  401. console.log(res.resultStr)
  402. options && options.success && options.success(res)
  403. },
  404. cancel: (res) => {
  405. // 用户取消支付后执行的回调函数
  406. options && options.cancel && options.cancel(res)
  407. },
  408. fail: (res) => {
  409. // 接口调用失败时执行的回调函数
  410. options && options.fail && options.fail(res)
  411. },
  412. complete: () => {
  413. // 接口调用完成时执行的回调函数,无论成功或失败都会执行。
  414. options && options.complete && options.complete()
  415. }
  416. })
  417. }
  418. }
  419. export default WeiXinSDK