邃芒智像(Uniapp : WX、TT、H5)
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

117 rader
3.2 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. uni.login({
  43. provider: "weixin", // 使用微信登录授权
  44. success: async (res) => {
  45. console.log(res);
  46. if (res.code) {
  47. console.log(res.code);
  48. try {
  49. uni.showLoading({
  50. title: "加载中...",
  51. mask: true,
  52. });
  53. const data = {
  54. appId: "wx75cf14e3a0d45821",
  55. code: res.code,
  56. };
  57. const res2 = await loginApi(data);
  58. userInfoModulesPinia.openId = res2.data.openId;
  59. userInfoModulesPinia.token = res2.token;
  60. console.log(userInfoModulesPinia.openId, "获取openid");
  61. uni.hideLoading();
  62. if (res2.token) {
  63. uni.redirectTo({
  64. url: "/pages/home/index",
  65. });
  66. } else {
  67. uni.redirectTo({
  68. url: "/pages/index/index",
  69. });
  70. uni.showToast({
  71. title: "登录失败,请重试",
  72. icon: "none",
  73. });
  74. }
  75. } catch (error) {
  76. // uni.redirectTo({
  77. // url: "pages/index/index",
  78. // });
  79. uni.showToast({
  80. title: "登录失败,请重试",
  81. icon: "none",
  82. });
  83. }
  84. } else {
  85. // uni.redirectTo({
  86. // url: "pages/index/index",
  87. // });
  88. uni.showToast({
  89. title: "登录失败,请重试",
  90. icon: "none",
  91. });
  92. }
  93. },
  94. fail: (err) => {
  95. // uni.redirectTo({
  96. // url: "pages/index/index",
  97. // });
  98. uni.showToast({
  99. title: "登录失败,请重试",
  100. icon: "none",
  101. });
  102. },
  103. });
  104. });
  105. onShow(() => {
  106. console.log("App Show!");
  107. });
  108. onHide(() => {
  109. console.log("App Hide!");
  110. });
  111. </script>
  112. <style>
  113. /*每个页面公共css */
  114. </style>