ChooseAuth.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { View } from "@tarojs/components";
  2. import Buttons from '@components/Buttons'
  3. import Texts from '@components/Texts'
  4. import './ChooseAuth.scss'
  5. import { ButtonType, ComponentStatus, TextType } from "../utils/types";
  6. import Taro, { useReady } from "@tarojs/taro";
  7. import { useDispatch, useSelector } from "react-redux";
  8. import { wxLogin } from "@/store/user";
  9. import { useState } from "react";
  10. export default function Page() {
  11. const dispatch = useDispatch();
  12. const counter = useSelector((state: any) => state.counter.value);
  13. var code = '';
  14. useReady(() => {
  15. Taro.login().then(res => {
  16. code = res.code;
  17. // dispatch(wxLogin({code:res.code}) as any);
  18. })
  19. })
  20. async function login() {
  21. try {
  22. const { userInfo, encryptedData, iv } = await Taro.getUserProfile({
  23. desc: '获取用户信息',
  24. });
  25. dispatch(wxLogin({
  26. code: code,
  27. encryptedData: encryptedData,
  28. iv: iv
  29. }) as any);
  30. setTimeout(() => {
  31. Taro.redirectTo({
  32. url: '/pages/ChooseScenario'
  33. })
  34. }, 1000)
  35. // 在这里处理用户信息
  36. } catch (error) {
  37. console.log('获取用户信息失败:', error);
  38. }
  39. }
  40. function createAccount() {
  41. Taro.navigateTo({
  42. url: '/pages/Auth'
  43. })
  44. }
  45. return <View className="container choose_container">
  46. <Texts text='App name' type={TextType.big} />
  47. <Texts text={counter} />
  48. <View style={{ height: 100 }} />
  49. <Buttons title='WeChat' onClick={login} style={{ width: 289, marginBottom: 30 }} />
  50. <Buttons title='Create account' type={ButtonType.outline} onClick={createAccount} style={{ width: 289, marginBottom: 30 }} />
  51. </View>;
  52. }