wechath5.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // #ifdef H5
  2. import weixin from "@/js_sdk/jweixin-module";
  3. import {
  4. isAndroid
  5. } from "./tools"
  6. import {
  7. getJsconfig,
  8. getCodeUrl,
  9. wechatLogin,
  10. wechatBindLogin
  11. } from '@/api/app'
  12. import store from '../store'
  13. import Cache from './cache'
  14. class Wechath5 {
  15. //获取微信配置url
  16. signLink() {
  17. if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
  18. window.entryUrl = location.href.split('#')[0]
  19. }
  20. return isAndroid() ? location.href.split('#')[0] : window.entryUrl;
  21. }
  22. //微信sdk配置
  23. config() {
  24. return new Promise((resolve) => {
  25. getJsconfig().then(res => {
  26. if (res.code == 1) {
  27. let config = res.data.config
  28. weixin.config({
  29. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  30. appId: config.appId, // 必填,公众号的唯一标识
  31. timestamp: config.timestamp, // 必填,生成签名的时间戳
  32. nonceStr: config.nonceStr, // 必填,生成签名的随机串
  33. signature: config.signature, // 必填,签名
  34. jsApiList: config.jsApiList // 必填,需要使用的JS接口列表
  35. });
  36. resolve()
  37. }
  38. })
  39. })
  40. }
  41. //获取微信登录url
  42. getWxUrl() {
  43. getCodeUrl().then(res => {
  44. if (res.code == 1) {
  45. location.href = res.data.url
  46. }
  47. })
  48. }
  49. //微信授权
  50. authLogin(code) {
  51. return new Promise((resolve, reject) => {
  52. wechatLogin({
  53. code
  54. })
  55. .then(res => {
  56. if(res.code == 1) {
  57. store.commit("login", {
  58. token: res.data.token
  59. });
  60. resolve(res.data);
  61. }
  62. })
  63. });
  64. }
  65. //微信H5绑定授权
  66. authLoginWx(code) {
  67. return new Promise((resolve, reject) => {
  68. wechatBindLogin({
  69. code
  70. })
  71. .then(res => {
  72. if(res.code == 1) {
  73. store.commit("login", {
  74. token: res.data.token
  75. });
  76. resolve(res.data);
  77. }
  78. })
  79. });
  80. }
  81. //微信分享
  82. share(option) {
  83. weixin.ready(() => {
  84. const {
  85. shareTitle,
  86. shareLink,
  87. shareImage,
  88. shareDesc
  89. } = option
  90. weixin.updateTimelineShareData({
  91. title: shareTitle, // 分享标题
  92. link: shareLink, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  93. imgUrl: shareImage, // 分享图标
  94. success: function(res) {
  95. // 设置成功
  96. }
  97. });
  98. // 发送给好友
  99. weixin.updateAppMessageShareData({
  100. title: shareTitle, // 分享标题
  101. link: shareLink, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  102. imgUrl: shareImage, // 分享图标
  103. desc: shareDesc,
  104. success: function(res) {
  105. // 设置成功
  106. }
  107. });
  108. // 发送到tx微博
  109. weixin.onMenuShareWeibo({
  110. title: shareTitle, // 分享标题
  111. link: shareLink, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  112. imgUrl: shareImage, // 分享图标
  113. desc: shareDesc,
  114. success: function(res) {
  115. // 设置成功
  116. }
  117. })
  118. })
  119. }
  120. wxPay(opt) {
  121. return new Promise((reslove, reject) => {
  122. weixin.ready(() => {
  123. weixin.chooseWXPay({
  124. timestamp: opt.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  125. nonceStr: opt.nonceStr, // 支付签名随机串,不长于 32 位
  126. package: opt.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  127. signType: opt.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  128. paySign: opt.paySign, // 支付签名
  129. success: (res) => {
  130. reslove()
  131. },
  132. cancel: (res) => {
  133. reject()
  134. },
  135. fail: (res) => {
  136. reject()
  137. },
  138. });
  139. });
  140. })
  141. }
  142. getWxAddress() {
  143. return new Promise((reslove, reject) => {
  144. weixin.ready(() => {
  145. weixin.openAddress({
  146. success: (res) => {
  147. reslove(res)
  148. },
  149. })
  150. })
  151. })
  152. }
  153. }
  154. export default new Wechath5()
  155. // #endif