product_list.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { kIsAndroid, kIsIOS } from "@/utils/tools";
  2. import { View, Text, Image } from "@tarojs/components";
  3. import { useEffect, useState } from "react";
  4. import { FlatList, Pressable } from "react-native";
  5. import Purchases from 'react-native-purchases'
  6. import { useSelector } from "react-redux";
  7. import './product_list.scss'
  8. import { ColorType } from "@/context/themes/color";
  9. import { payInfo } from "@/services/user";
  10. import Taro from "@tarojs/taro";
  11. let useNavigation;
  12. if (process.env.TARO_ENV == 'rn') {
  13. useNavigation = require("@react-navigation/native").useNavigation
  14. }
  15. export default function ProductList() {
  16. const [packages, setPackages] = useState([]);
  17. const user = useSelector((state: any) => state.user);
  18. // - State for displaying an overlay view
  19. const [isPurchasing, setIsPurchasing] = useState(false);
  20. const [errorCode,setErrorCode] = useState<any>(-1)
  21. let navigation;
  22. if (useNavigation) {
  23. navigation = useNavigation()
  24. }
  25. useEffect(() => {
  26. console.log(user)
  27. if (process.env.TARO_ENV == 'rn') {
  28. if (kIsIOS) {
  29. Purchases.configure({
  30. apiKey: 'appl_FNFYDLwHZlXzqrKJFlErWXUHGwx',
  31. appUserID: user.id
  32. })
  33. }
  34. else if (kIsAndroid) {
  35. Purchases.configure({
  36. apiKey: 'goog_cyJSYOsnZpNqsUbCsHSdhqQdQwe',
  37. appUserID: user.id
  38. })
  39. }
  40. getProducts();
  41. // console.log('aaaa')
  42. // setTimeout(() => {
  43. // // getProducts();
  44. // hi()
  45. // }, 2000)
  46. }
  47. }, [])
  48. async function getProducts() {
  49. try {
  50. const offerings = await Purchases.getOfferings();
  51. console.log('product list offerings', offerings)
  52. console.log('product list detail', (offerings as any).current.availablePackages[1])
  53. if (offerings.current !== null && offerings.current.availablePackages.length !== 0) {
  54. setPackages((offerings as any).current.availablePackages);
  55. }
  56. } catch (e) {
  57. console.log(e.message)
  58. }
  59. }
  60. async function hi() {
  61. try {
  62. // 启动购买流程
  63. const purchaseResult = await Purchases.purchaseProduct('pro_lifelong_test');
  64. console.log(purchaseResult);
  65. // 检查购买是否成功
  66. // setIsSubscribed(purchaseResult.purchaserInfo.activeSubscriptions.length > 0);
  67. } catch (error) {
  68. console.error('Purchase error:', error);
  69. }
  70. }
  71. async function pay(item: any) {
  72. setIsPurchasing(true);
  73. try {
  74. const { customerInfo, productIdentifier, transaction } = await Purchases.purchasePackage(item);
  75. payInfo({
  76. customerInfo,
  77. productIdentifier,
  78. transaction
  79. }).then(res => {
  80. global.paySuccess()
  81. if (process.env.TARO_ENV == 'rn') {
  82. navigation.pop(1)
  83. }
  84. else {
  85. Taro.navigateBack({
  86. delta: 1
  87. })
  88. }
  89. });
  90. console.log('result', customerInfo)
  91. } catch (e) {
  92. debugger
  93. setErrorCode(e.code)
  94. if (e.code === Purchases.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR) {
  95. }
  96. } finally {
  97. setIsPurchasing(false);
  98. }
  99. }
  100. return <View>
  101. <View style={{ height: 40 }} />
  102. {
  103. packages.map((item, index) => {
  104. return <View className="product_item" key={index} onClick={() => pay(item)}>
  105. <Text className="product_title" style={{ color: ColorType.fast }}>{(item as any).product.title}</Text>
  106. <Text style={{ color: '#fff', fontSize: 16, marginTop: 5, marginBottom: 5 }}>{(item as any).product.description}</Text>
  107. <View style={{ flexDirection: 'row', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
  108. <Text className="product_price">{(item as any).product.priceString}</Text>
  109. <Image style={{ width: 24, height: 24 }} src={require('@/assets/images/arrow3.png')} />
  110. </View>
  111. </View>
  112. })
  113. }
  114. {/* <FlatList
  115. data={packages}
  116. renderItem={({ item }) => <View className="product_item" onClick={() => pay(item)}>
  117. <Text className="product_title" style={{ color: ColorType.fast }}>{(item as any).product.title}</Text>
  118. <Text style={{ color: '#fff', fontSize: 16, marginTop: 5, marginBottom: 5 }}>{(item as any).product.description}</Text>
  119. <View style={{ flexDirection: 'row', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
  120. <Text className="product_price">{(item as any).product.priceString}</Text>
  121. <Image style={{width:24,height:24}} src={require('@/assets/images/arrow3.png')} />
  122. </View>
  123. </View>}
  124. keyExtractor={(item) => (item as any).identifier}
  125. /> */}
  126. {
  127. isPurchasing && <Text style={{ color: '#fff', fontSize: 40 }}>正在付款</Text>
  128. }
  129. {
  130. errorCode!=-1 && <Text style={{ color: '#fff', fontSize: 40 }}>出错了,错误代码{errorCode}</Text>
  131. }
  132. </View>
  133. }