h5WxLogin.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_login = require("../../api/login.js");
  4. const common_assets = require("../../common/assets.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: "处理中..."
  28. });
  29. let that = this;
  30. var data = { code: this.code };
  31. api_login.loginByWeChat(data).then(
  32. (res) => {
  33. common_vendor.index.hideLoading();
  34. if (res.code == 200) {
  35. common_vendor.index.setStorageSync("AppToken", res.token);
  36. common_vendor.index.setStorageSync("userInfo", JSON.stringify(res.user));
  37. common_vendor.index.$emit("refreshIM");
  38. that.goToLanch();
  39. } else {
  40. common_vendor.index.showToast({ title: res.msg, icon: "none" });
  41. }
  42. },
  43. (err) => {
  44. }
  45. );
  46. },
  47. getWechatCode() {
  48. if (isWechat) {
  49. let appid = "wx9ea36eecd281bcd3";
  50. let code = this.getUrlCode().code;
  51. let local = window.location.href;
  52. if (code == null || code === "") {
  53. 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";
  54. } else {
  55. this.code = code;
  56. }
  57. }
  58. },
  59. getUrlCode() {
  60. var url = location.search;
  61. var theRequest = new Object();
  62. if (url.indexOf("?") != -1) {
  63. var str = url.substr(1);
  64. var strs = str.split("&");
  65. for (var i = 0; i < strs.length; i++) {
  66. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  67. }
  68. }
  69. console.log(theRequest);
  70. return theRequest;
  71. },
  72. goToLanch() {
  73. let beforLoginUrl = common_vendor.index.getStorageSync("beforLoginPage");
  74. console.log("beforLoginUrl:" + beforLoginUrl);
  75. common_vendor.index.reLaunch({
  76. url: beforLoginUrl
  77. });
  78. }
  79. }
  80. };
  81. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  82. return {
  83. a: common_assets._imports_0$6,
  84. b: common_vendor.o(($event) => $options.loginByMp())
  85. };
  86. }
  87. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  88. wx.createPage(MiniProgramPage);