index.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { Component } from "react";
  2. import { View, Image, Text, Input } from "@tarojs/components";
  3. import Taro from "@tarojs/taro";
  4. import "./index.less";
  5. import iconOne from "../../../images/calculator/iconOne.png";
  6. import iconTwo from "../../../images/calculator/iconTwo.png";
  7. import iconThree from "../../../images/calculator/iconThree.png";
  8. import {
  9. getCalculatorData,
  10. getCalculatorSearch,
  11. } from "../../../service/activity";
  12. export default class Index extends Component {
  13. state = {
  14. wishList: [],
  15. wish: "", // 心愿
  16. price: "", // 价格
  17. };
  18. componentDidMount() {
  19. this.getCalculator(); // 计算捡漏
  20. }
  21. // 计算捡漏
  22. getCalculator = async () => {
  23. const res = await getCalculatorData();
  24. this.setState({
  25. wishList: res,
  26. });
  27. };
  28. // 搜索捡漏
  29. getCalculatorSearch = async () => {
  30. if (!this.state.wish.trim()) {
  31. Taro.showToast({
  32. title: "请输入您想要捡漏的物品或服务",
  33. icon: "none",
  34. });
  35. return;
  36. }
  37. if (!this.state.price.trim()) {
  38. Taro.showToast({
  39. title: "请输入您预期的价格",
  40. icon: "none",
  41. });
  42. return;
  43. }
  44. Taro.showLoading({
  45. title: "正在计算...",
  46. });
  47. try {
  48. const res = await getCalculatorSearch({
  49. keyword: this.state.wish,
  50. money: this.state.price,
  51. });
  52. Taro.navigateTo({
  53. url: `/pages/calculatorSub/calculatorResult/index?isSearch=${
  54. res.is_search
  55. }&&list=${encodeURIComponent(JSON.stringify(res.goods_list))}`,
  56. });
  57. } finally {
  58. Taro.hideLoading();
  59. }
  60. };
  61. // 心愿输入框改变
  62. handleWishChange = (e) => {
  63. this.setState({
  64. wish: e.detail.value,
  65. });
  66. };
  67. // 价格输入框改变
  68. handlePriceChange = (e) => {
  69. if (e.detail.value < 1 && e.detail.value.trim() !== "") {
  70. e.detail.value = 1;
  71. }
  72. this.setState({
  73. price: e.detail.value,
  74. });
  75. };
  76. render() {
  77. return (
  78. <View className="index">
  79. <Image
  80. className="top-img"
  81. src="https://yushi.tos-cn-beijing.volces.com/calculator/wishTopImg.png"
  82. />
  83. {/* 底部 */}
  84. <View className="bottom-box">
  85. <Image
  86. mode="heightFix"
  87. className="bg-img"
  88. src="https://yushi.tos-cn-beijing.volces.com/calculator/bottomImg.png"
  89. />
  90. {/* 输入内容区域 */}
  91. <View className="input-box">
  92. <View className="input-box-title">
  93. <Image className="title-icon" src={iconOne} />
  94. <Text className="title-text">您想要的物品或服务</Text>
  95. </View>
  96. <View className="input-box-content">
  97. <View className="input-box-content-box">
  98. <Input
  99. value={this.state.wish}
  100. onInput={this.handleWishChange}
  101. className="input-box-content-input"
  102. placeholder="请详细描述需求"
  103. />
  104. </View>
  105. <View className="input-box-content-text">
  106. 例: 输入机票不如输入从深圳到北京的机票
  107. </View>
  108. </View>
  109. </View>
  110. <View className="input-box">
  111. <View className="input-box-title">
  112. <Image className="title-icon" src={iconTwo} />
  113. <Text className="title-text">您预期的价格</Text>
  114. </View>
  115. <View className="input-box-content">
  116. <View className="input-box-content-box">
  117. <Input
  118. value={this.state.price}
  119. onInput={this.handlePriceChange}
  120. type="number"
  121. min="1"
  122. className="input-box-content-input"
  123. placeholder="价格最低不能少于1元"
  124. />
  125. </View>
  126. <View className="input-box-content-text">
  127. 注意:我们是认真的!
  128. </View>
  129. </View>
  130. </View>
  131. <View onClick={this.getCalculatorSearch} className="button-box">
  132. <Image
  133. className="button-icon"
  134. src="https://yushi.tos-cn-beijing.volces.com/calculator/buttonTwo.png"
  135. />
  136. <View className="button-text-box">
  137. <Text className="button-text">捡漏计算</Text>
  138. </View>
  139. </View>
  140. {/* 底部捡漏列表 */}
  141. <View className="bottom-list-box">
  142. <View className="bottom-list-box-title">
  143. <Image className="title-icon" src={iconThree} />
  144. <Text className="title-text">TA们捡了哪些漏</Text>
  145. </View>
  146. <View className="bottom-list-box-content">
  147. {this.state.wishList.length > 0 &&
  148. this.state.wishList.map((item, index) => (
  149. <View key={index} className="bottom-list-box-content-item">
  150. TA想要“{item.jianlou_title}”,捡漏{item.reserve_price}元
  151. </View>
  152. ))}
  153. </View>
  154. </View>
  155. </View>
  156. </View>
  157. );
  158. }
  159. }