h5WxLogin.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. var common_vendor = require("../../common/vendor.js");
  3. var api_login = require("../../api/login.js");
  4. require("../../common/request.js");
  5. const isWechat = () => {
  6. return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
  7. };
  8. const _sfc_main = {
  9. data() {
  10. return {
  11. code: null
  12. };
  13. },
  14. onLoad(option) {
  15. this.getWechatCode();
  16. },
  17. onUnload() {
  18. },
  19. mounted() {
  20. },
  21. methods: {
  22. loginByMp() {
  23. if (this.code == null) {
  24. return;
  25. }
  26. common_vendor.index.showLoading({
  27. title: "\u5904\u7406\u4E2D..."
  28. });
  29. let that = this;
  30. var data = { code: this.code };
  31. api_login.loginByWeChat(data).then((res) => {
  32. common_vendor.index.hideLoading();
  33. if (res.code == 200) {
  34. common_vendor.index.setStorageSync("AppToken", res.token);
  35. common_vendor.index.setStorageSync("userInfo", JSON.stringify(res.user));
  36. common_vendor.index.$emit("refreshIM");
  37. that.goToLanch();
  38. } else {
  39. common_vendor.index.showToast({ title: res.msg, icon: "none" });
  40. }
  41. }, (err) => {
  42. });
  43. },
  44. getWechatCode() {
  45. if (isWechat) {
  46. let appid = "wx9ea36eecd281bcd3";
  47. let code = this.getUrlCode().code;
  48. let local = window.location.href;
  49. if (code == null || code === "") {
  50. window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + encodeURIComponent(local) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
  51. } else {
  52. this.code = code;
  53. }
  54. }
  55. },
  56. getUrlCode() {
  57. var url = location.search;
  58. var theRequest = new Object();
  59. if (url.indexOf("?") != -1) {
  60. var str = url.substr(1);
  61. var strs = str.split("&");
  62. for (var i = 0; i < strs.length; i++) {
  63. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  64. }
  65. }
  66. console.log(theRequest);
  67. return theRequest;
  68. },
  69. goToLanch() {
  70. let beforLoginUrl = common_vendor.index.getStorageSync("beforLoginPage");
  71. console.log("beforLoginUrl:" + beforLoginUrl);
  72. common_vendor.index.reLaunch({
  73. url: beforLoginUrl
  74. });
  75. }
  76. }
  77. };
  78. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  79. return {
  80. a: common_vendor.o(($event) => $options.loginByMp())
  81. };
  82. }
  83. var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "C:/Users/Administrator/Desktop/\u9879\u76EE/\u76F4\u64AD/liveH5-v3/pages/auth/h5WxLogin.vue"]]);
  84. wx.createPage(MiniProgramPage);