邃芒智像(Uniapp : WX、TT、H5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

119 lines
3.4 KiB

  1. <style lang="scss">
  2. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  3. @import "uview-plus/index.scss";
  4. </style>
  5. <script setup>
  6. import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
  7. import { userInfoModules } from "@/store/modules/userInfo";
  8. import { loginApi } from "./api/login";
  9. const userInfoModulesPinia = userInfoModules();
  10. onLaunch(() => {
  11. console.log("App Launch!");
  12. // 获取平台及系统信息
  13. uni.getSystemInfo({
  14. success: function (res) {
  15. let uniPlatform = "";
  16. let osName = "";
  17. if (res.uniPlatform == "web") {
  18. uniPlatform = 1;
  19. } else if (res.uniPlatform == "mp-weixin") {
  20. uniPlatform = 2;
  21. } else if (res.uniPlatform == "mp-toutiao") {
  22. uniPlatform = 3;
  23. }
  24. if (res.osName == "windows") {
  25. osName = 1;
  26. } else if (res.osName == "ios") {
  27. osName = 2;
  28. } else if (res.osName == "android") {
  29. osName = 3;
  30. } else if (res.osName == "mac") {
  31. osName = 4;
  32. } else if (res.osName == "linux") {
  33. osName = 5;
  34. }
  35. userInfoModulesPinia.platForm = uniPlatform; // 平台
  36. userInfoModulesPinia.hostSystem = osName; // 系统
  37. console.log(res.uniPlatform, uniPlatform, "PlatForm");
  38. console.log(res.osName, osName, "System");
  39. },
  40. });
  41. // 微信登录授权
  42. if (userInfoModulesPinia.platForm != 1) {
  43. uni.login({
  44. provider: "weixin", // 使用微信登录授权
  45. success: async (res) => {
  46. console.log(res);
  47. if (res.code) {
  48. console.log(res.code);
  49. try {
  50. uni.showLoading({
  51. title: "加载中...",
  52. mask: true,
  53. });
  54. const data = {
  55. appId: "wx75cf14e3a0d45821",
  56. code: res.code,
  57. };
  58. const res2 = await loginApi(data);
  59. userInfoModulesPinia.openId = res2.data.openId;
  60. userInfoModulesPinia.token = res2.data.token;
  61. console.log(userInfoModulesPinia.openId, "获取openid");
  62. uni.hideLoading();
  63. if (res2.data.token) {
  64. uni.redirectTo({
  65. url: "/pages/home/index",
  66. });
  67. } else {
  68. uni.redirectTo({
  69. url: "/pages/login/index",
  70. });
  71. uni.showToast({
  72. title: "登录失败,请重试",
  73. icon: "none",
  74. });
  75. }
  76. } catch (error) {
  77. // uni.redirectTo({
  78. // url: "pages/index/index",
  79. // });
  80. uni.showToast({
  81. title: "登录失败,请重试",
  82. icon: "none",
  83. });
  84. }
  85. } else {
  86. // uni.redirectTo({
  87. // url: "pages/index/index",
  88. // });
  89. uni.showToast({
  90. title: "登录失败,请重试",
  91. icon: "none",
  92. });
  93. }
  94. },
  95. fail: (err) => {
  96. // uni.redirectTo({
  97. // url: "pages/index/index",
  98. // });
  99. uni.showToast({
  100. title: "登录失败,请重试",
  101. icon: "none",
  102. });
  103. },
  104. });
  105. }
  106. });
  107. onShow(() => {
  108. console.log("App Show!");
  109. });
  110. onHide(() => {
  111. console.log("App Hide!");
  112. });
  113. </script>
  114. <style>
  115. /*每个页面公共css */
  116. </style>