Guide1Screen.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* eslint-disable react/jsx-one-expression-per-line */
  2. /* eslint-disable no-shadow */
  3. /* eslint-disable camelcase */
  4. import * as WebBrowser from "expo-web-browser";
  5. import * as React from "react";
  6. import { StyleSheet } from "react-native";
  7. import { Layout, Text, useTheme, Button, Card } from "@ui-kitten/components";
  8. import { useModel } from "flooks";
  9. import { useFocusEffect, useRoute } from "@react-navigation/native";
  10. import { useCreation, useUnmount, useMount } from "@umijs/hooks";
  11. import Textarea from "react-native-textarea";
  12. import ScrollPage from "../components/ScrollPage";
  13. import FormInput from "../components/FormInput";
  14. import ConnectButton from "../components/ConnectButton";
  15. import GuideHeaderBar from "../components/GuideHeaderBar";
  16. import NavHeaderBar from "../components/NavHeaderBar";
  17. const styles = StyleSheet.create({
  18. container: {
  19. flex: 1,
  20. paddingBottom: 33,
  21. },
  22. tabContent: {
  23. backgroundColor: "#fff",
  24. marginTop: 20,
  25. },
  26. img: {
  27. width: 100,
  28. height: 100,
  29. alignSelf: "center",
  30. },
  31. img2: {
  32. width: 97,
  33. height: 21,
  34. alignSelf: "center",
  35. marginTop: 2,
  36. },
  37. text: {
  38. marginTop: 16,
  39. },
  40. layoutLeft: {
  41. // flexDirection: "row",
  42. paddingVertical: 10,
  43. justifyContent: "center",
  44. alignItems: "center",
  45. },
  46. form: {
  47. paddingHorizontal: 26,
  48. paddingVertical: 20,
  49. },
  50. textareaContainer: {
  51. backgroundColor: "#F0F0F0",
  52. height: 100,
  53. borderRadius: 4,
  54. },
  55. textarea: {
  56. textAlignVertical: "top", // hack android
  57. fontSize: 13,
  58. color: "#333",
  59. paddingHorizontal: 14,
  60. paddingVertical: 10,
  61. height: 100,
  62. },
  63. lay: {
  64. flexDirection: "row",
  65. marginLeft: 120,
  66. marginBottom: 10,
  67. },
  68. money: {
  69. marginLeft: 10,
  70. },
  71. });
  72. export default function Guide1Screen({ navigation }) {
  73. const theme = useTheme();
  74. const { changeBackground } = useModel("barModel");
  75. const { httpGet, httpPost } = useModel("httpModel", true);
  76. const { success, loading, clearLoading } = useModel("loadingModel", true);
  77. const { mid, userInfo, changeGuideStep } = useModel("userModel", true);
  78. const {
  79. addClassification,
  80. editGoodsMap,
  81. saveClassByList,
  82. removeGoodsClassMap,
  83. sortClassification,
  84. } = useModel("goodsModel");
  85. const {
  86. guide1_title1,
  87. guideHome_title2,
  88. guide1_form_1,
  89. guide1_pla_1,
  90. guide1_form_2,
  91. guide1_pla_2,
  92. guide1_form_3,
  93. guide1_pla_3,
  94. guide1_form_4,
  95. guide1_pla_4,
  96. guide1_form_5,
  97. guide1_pla_5,
  98. next,
  99. pass,
  100. passTips,
  101. confirm,
  102. addGoods,
  103. delText,
  104. removeTips,
  105. GVDYKL,
  106. IQYCDU,
  107. } = useModel("wordsModel");
  108. const { showDialog } = useModel("dialogModel");
  109. const route = useRoute();
  110. const [routeName, setRouteName] = React.useState("");
  111. const [id, changeId] = React.useState("NEW");
  112. const [name, changeName] = React.useState("");
  113. const [amount, changeAmount] = React.useState("");
  114. const [discountAmount, changeDiscountAmount] = React.useState("");
  115. const [inventory, changeInventory] = React.useState(0);
  116. const [week, changeWeek] = React.useState("");
  117. const [startTime, changeStartTime] = React.useState("08:00:00");
  118. const [endTime, changeEndTime] = React.useState("18:00:00");
  119. const [introduction, changeIntroduction] = React.useState();
  120. const [goodsSpecification, changeGoodsSpecification] = React.useState([]);
  121. const [img, changeImg] = React.useState();
  122. // 需要批量提交的规格
  123. const postByList = useCreation(() => {
  124. return goodsSpecification.filter(item => {
  125. return !item.id || item.id < 0;
  126. });
  127. }, [goodsSpecification]);
  128. function getInfo(res) {
  129. changeName(res.name || "");
  130. if (res.discountAmount) {
  131. changeDiscountAmount(res.discountAmount.toString());
  132. }
  133. if (res.amount) {
  134. changeAmount(res.amount.toString());
  135. }
  136. if (res.inventory) {
  137. changeInventory(res.inventory.toString());
  138. }
  139. changeWeek(res.week || "");
  140. changeStartTime(res.startTime || "");
  141. changeEndTime(res.endTime || "");
  142. changeIntroduction(res.introduction || "");
  143. // if (res.img && res.img.length) {
  144. changeImg(res.img);
  145. //
  146. }
  147. useMount(() => {
  148. const { params, name } = route;
  149. setRouteName(name);
  150. if (params) {
  151. if (params.id) {
  152. changeId(params.id);
  153. loading();
  154. httpGet(`/goods/get/${params.id}`, {}, true)
  155. .then(res => {
  156. getInfo(res);
  157. changeGoodsSpecification(sortClassification(res.specifications));
  158. })
  159. .finally(() => {
  160. clearLoading();
  161. });
  162. }
  163. } else if (mid) {
  164. console.log(userInfo);
  165. changeWeek(userInfo.week || "");
  166. changeStartTime(userInfo.startTime || "");
  167. changeEndTime(userInfo.endTime || "");
  168. }
  169. });
  170. useUnmount(() => {
  171. removeGoodsClassMap();
  172. });
  173. React.useEffect(() => {
  174. if (editGoodsMap.has(id)) {
  175. changeGoodsSpecification(editGoodsMap.get(id));
  176. }
  177. }, [editGoodsMap.get(id)]);
  178. useFocusEffect(
  179. React.useCallback(() => {
  180. changeBackground(theme["color-primary-500"]);
  181. }, [])
  182. );
  183. const specify = () => {
  184. return goodsSpecification.map((item, index) => {
  185. return (
  186. <Layout key={index} style={styles.lay}>
  187. {item.parent ? (
  188. <Text category="h1">
  189. 添加
  190. {item.name}
  191. </Text>
  192. ) : (
  193. <Text category="c1">
  194. 分类名:
  195. {item.name}
  196. </Text>
  197. )}
  198. <Layout style={styles.money}>
  199. {item.parent ? (
  200. <Text category="h1">¥{item.amount}</Text>
  201. ) : (
  202. <Text category="c1">{item.multiple ? IQYCDU : GVDYKL}</Text>
  203. )}
  204. </Layout>
  205. </Layout>
  206. );
  207. });
  208. };
  209. const canNext = React.useMemo(() => {
  210. if (name && amount && inventory && week && startTime && endTime && img) {
  211. return true;
  212. }
  213. return false;
  214. }, [
  215. name,
  216. amount,
  217. discountAmount,
  218. inventory,
  219. week,
  220. startTime,
  221. endTime,
  222. introduction,
  223. img,
  224. ]);
  225. return (
  226. <>
  227. {routeName !== "AddGoods" ? (
  228. <GuideHeaderBar />
  229. ) : (
  230. <NavHeaderBar title={addGoods} />
  231. )}
  232. <ScrollPage>
  233. <Layout style={styles.container}>
  234. {routeName !== "AddGoods" && (
  235. <Card appearance="headFilled">
  236. <Text category="s1">{guide1_title1}</Text>
  237. <Text category="s1">{guideHome_title2}</Text>
  238. </Card>
  239. )}
  240. <Card appearance="formFilled">
  241. {/* 商品名称 */}
  242. <FormInput
  243. label={guide1_form_1}
  244. placeholder={guide1_pla_1}
  245. value={name}
  246. onChange={changeName}
  247. style={{ paddingVertical: 3 }}
  248. />
  249. {/* 商品价格 */}
  250. <FormInput
  251. label={guide1_form_2}
  252. placeholder={guide1_pla_2}
  253. value={amount}
  254. type="money"
  255. onChange={changeAmount}
  256. textAlign="right"
  257. />
  258. {/* 优惠价格 */}
  259. <FormInput
  260. label={guide1_form_3}
  261. placeholder={guide1_pla_3}
  262. value={discountAmount}
  263. type="money"
  264. onChange={changeDiscountAmount}
  265. textAlign="right"
  266. />
  267. {/* 商品规格 */}
  268. <FormInput
  269. label="商品规格"
  270. type="url"
  271. changePath={() => {
  272. navigation.navigate("GoodsSpecification", {
  273. goodsId: id,
  274. });
  275. }}
  276. textAlign="right"
  277. />
  278. {specify()}
  279. {/* 每日供应数量 */}
  280. <FormInput
  281. label={guide1_form_4}
  282. placeholder={guide1_pla_4}
  283. value={inventory}
  284. type="amount"
  285. onChange={changeInventory}
  286. />
  287. {/* 供应时间 */}
  288. <FormInput
  289. label={guide1_form_5}
  290. type="openTime"
  291. week={week}
  292. startTime={startTime}
  293. endTime={endTime}
  294. onChange={(week, startTime, endTime) => {
  295. changeWeek(week);
  296. changeStartTime(startTime);
  297. changeEndTime(endTime);
  298. }}
  299. />
  300. <Layout>
  301. <Textarea
  302. containerStyle={styles.textareaContainer}
  303. style={styles.textarea}
  304. onChangeText={changeIntroduction}
  305. defaultValue={introduction}
  306. maxLength={50}
  307. placeholder={guide1_pla_5}
  308. placeholderTextColor="#B4B4B4"
  309. underlineColorAndroid="transparent"
  310. />
  311. </Layout>
  312. <FormInput
  313. type="img"
  314. value={img}
  315. textAlign="right"
  316. onChange={changeImg}
  317. />
  318. <Layout style={styles.layoutLeft} level="1">
  319. <Button
  320. status="primary"
  321. disabled={!canNext}
  322. onPress={() => {
  323. loading();
  324. httpPost(
  325. "/goods/save",
  326. {
  327. id: id === "NEW" ? "" : id,
  328. name,
  329. amount,
  330. discountAmount,
  331. inventory,
  332. week,
  333. startTime,
  334. endTime,
  335. introduction,
  336. img,
  337. merchantId: mid,
  338. },
  339. { body: "json" },
  340. true
  341. )
  342. .then(res => {
  343. if (id === "NEW") {
  344. const { params } = route;
  345. const { classifyId } = params || {};
  346. if (classifyId) {
  347. addClassification(classifyId, res.id);
  348. }
  349. }
  350. return saveClassByList(postByList, res.id);
  351. })
  352. .then(() => {
  353. success(`${id !== "NEW" ? "修改" : "添加"}成功`);
  354. if (routeName !== "AddGoods") {
  355. changeGuideStep("2");
  356. } else {
  357. navigation.goBack();
  358. }
  359. });
  360. }}
  361. >
  362. {routeName !== "AddGoods" ? next : confirm}
  363. </Button>
  364. {routeName === "AddGoods" && id !== "NEW" && (
  365. <Button
  366. appearance="ghost"
  367. status="info"
  368. style={{ marginTop: 10 }}
  369. onPress={() => {
  370. showDialog({
  371. bodyText: removeTips,
  372. cancelable: true,
  373. confirmCallback: () => {
  374. httpPost(`/goods/del/${id}`, {}, {}, true).then(() => {
  375. success("删除成功");
  376. navigation.goBack();
  377. });
  378. },
  379. });
  380. }}
  381. >
  382. {delText}
  383. </Button>
  384. )}
  385. {routeName !== "AddGoods" && (
  386. <Button
  387. style={{ marginTop: 10 }}
  388. appearance="ghost"
  389. status="primary"
  390. onPress={() => {
  391. showDialog({
  392. bodyText: passTips,
  393. cancelable: true,
  394. confirmCallback: () => {
  395. changeGuideStep(2);
  396. },
  397. });
  398. }}
  399. >
  400. {pass}
  401. </Button>
  402. )}
  403. </Layout>
  404. </Card>
  405. {routeName !== "AddGoods" && <ConnectButton />}
  406. </Layout>
  407. </ScrollPage>
  408. </>
  409. );
  410. }