wx.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 获取 code
  3. * @returns
  4. */
  5. export function getCode () {
  6. const matched = /[?&]*code=([^&]+)/.exec(location.search)
  7. if (matched) {
  8. return decodeURIComponent(matched[1])
  9. }
  10. }
  11. /**
  12. * 跳转授权页面
  13. */
  14. export function getRedirectURL (force, code) {
  15. if (import.meta.env.DEV) return;
  16. const originCode = localStorage.getItem('wxcode');
  17. const wxfirst = localStorage.getItem('wxfirst') === null;
  18. localStorage.setItem('wxcode', code)
  19. localStorage.setItem('wxfirst', 'not');
  20. if (force || wxfirst || !code || code === originCode) {
  21. const origin = location.origin + location.pathname
  22. const searchStr = location.search.replace(/code\=[^&]+/, '').replace(/state\=[^&]+/, '').replace(/^&+/g, '').replace(/\?+$/, '')
  23. const local = origin + searchStr + location.hash
  24. console.log('--------------------->')
  25. console.log(JSON.stringify(location))
  26. console.log(local)
  27. console.log('--------------------->')
  28. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd3bab568bc42d1de&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`
  29. }
  30. }
  31. /**
  32. * 初始化微信 SDK
  33. * @param {*} params
  34. * @returns
  35. */
  36. export function initWxSDK(params) {
  37. return new Promise((resolve, reject) => {
  38. wx.config({
  39. beta: true, // 获取微信抬头必须这个
  40. debug: import.meta.env.DEV, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  41. appId: params.appId, // 必填,公众号的唯一标识
  42. timestamp: params.timestamp, // 必填,生成签名的时间戳
  43. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  44. signature: params.signature,// 必填,签名
  45. jsApiList: ['chooseInvoiceTitle'] // 必填,需要使用的JS接口列表
  46. });
  47. wx.ready(() => {
  48. console.log('---微信SDK初始化成功---')
  49. resolve();
  50. window.__initwx = true;
  51. });
  52. wx.error(function(res){
  53. console.log('---微信SDK初始化失败---')
  54. console.error(res);
  55. window.__initwx = false;
  56. reject();
  57. });
  58. });
  59. }
  60. /**
  61. * 获取微信抬头
  62. * @param {*} scene
  63. * @returns
  64. */
  65. export function chooseInvoiceTitle(scene) {
  66. return new Promise((resolve, reject) => {
  67. if (!window.__initwx) {
  68. reject('微信SDK未成功初始化');
  69. return;
  70. }
  71. wx.invoke('chooseInvoiceTitle', { scene }, function (res) {
  72. console.log('----------chooseInvoiceTitle------', res);
  73. if (res.err_msg == 'chooseInvoiceTitle:ok') {
  74. // choose_invoice_title_info 结构如下:
  75. // {
  76. // "type":"0",
  77. // "title":"腾讯科技(深圳)有限公司",
  78. // "taxNumber":"123466789987646131",
  79. // "companyAddress":"深圳市南山区某某路腾讯大厦",
  80. // "telephone":"123456789",
  81. // "bankName":"某某银行",
  82. // "bankAccount":"621111111111290"
  83. // }
  84. resolve(typeof res.choose_invoice_title_info == 'string' ? JSON.parse(res.choose_invoice_title_info) : res.choose_invoice_title_info);
  85. } else {
  86. console.error(res.err_msg);
  87. console.debug(res);
  88. reject(res.err_msg);
  89. }
  90. });
  91. });
  92. }
  93. // 本系统的特殊情况,使用外部服务初始化微信SDK
  94. export function initWxSDK2() {
  95. const link = window.location.origin + window.location.pathname + window.location.search;
  96. const servAPI = '//api.h5.njyunzhi.com/mp/jssdk?url=' + encodeURIComponent(link);
  97. fetch(servAPI).then(res => res.json()).then(res => {
  98. console.log('----------', res.data);
  99. initWxSDK(res.data);
  100. });
  101. }