Guide1Screen.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 moment from "moment";
  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 } = useModel("loadingModel", true);
  77. const { mid, changeGuideStep } = useModel("userModel", true);
  78. const { addClassification, sortClassification } = useModel(
  79. "goodsModel",
  80. true
  81. );
  82. const {
  83. guide1_title1,
  84. guideHome_title2,
  85. guide1_form_1,
  86. guide1_pla_1,
  87. guide1_form_2,
  88. guide1_pla_2,
  89. guide1_form_3,
  90. guide1_pla_3,
  91. guide1_form_4,
  92. guide1_pla_4,
  93. guide1_form_5,
  94. guide1_pla_5,
  95. next,
  96. pass,
  97. passTips,
  98. confirm,
  99. addGoods,
  100. delText,
  101. removeTips,
  102. } = useModel("wordsModel");
  103. const { showDialog } = useModel("dialogModel");
  104. const route = useRoute();
  105. const routeName = route.name;
  106. const [id, changeId] = React.useState("");
  107. const [name, changeName] = React.useState("");
  108. const [amount, changeAmount] = React.useState("");
  109. const [discountAmount, changeDiscountAmount] = React.useState("");
  110. const [inventory, changeInventory] = React.useState(0);
  111. const [week, changeWeek] = React.useState("");
  112. const [startTime, changeStartTime] = React.useState("08:00");
  113. const [endTime, changeEndTime] = React.useState("22:00");
  114. const [introduction, changeIntroduction] = React.useState();
  115. const [defaultValue, ChangeDefaultValue] = React.useState("");
  116. const [goodsSpecification, changeGoodsSpecification] = React.useState([]);
  117. const [img, changeImg] = React.useState();
  118. function getInfo(res) {
  119. changeName(res.name || "");
  120. if (res.discountAmount) {
  121. changeDiscountAmount(res.discountAmount.toString());
  122. }
  123. if (res.amount) {
  124. changeAmount(res.amount.toString());
  125. }
  126. if (res.inventory) {
  127. changeInventory(res.inventory.toString());
  128. }
  129. changeWeek(res.week || "");
  130. changeStartTime(res.startTime || "");
  131. changeEndTime(res.endTime || "");
  132. changeIntroduction(res.introduction || "");
  133. // if (res.img && res.img.length) {
  134. changeImg(res.img);
  135. // }
  136. if (res.week) {
  137. ChangeDefaultValue([
  138. `${moment(res.startTime, "HH:mm:ss").format("HH:mm")}~${moment(
  139. res.endTime,
  140. "HH:mm:ss"
  141. ).format("HH:mm")}`,
  142. res.week,
  143. ]);
  144. }
  145. }
  146. useFocusEffect(
  147. React.useCallback(() => {
  148. changeBackground(theme["color-primary-500"]);
  149. const { params } = route;
  150. if (params) {
  151. if (params.id) {
  152. changeId(params.id);
  153. loading();
  154. httpGet(`/goods/get/${params.id}`).then(res => {
  155. getInfo(res);
  156. });
  157. httpGet(
  158. "/goodsSpecification/byGoodsId",
  159. {
  160. goodsId: params.id,
  161. },
  162. true
  163. ).then(res => {
  164. changeGoodsSpecification(sortClassification(res));
  165. });
  166. }
  167. } else {
  168. getInfo({});
  169. }
  170. }, [])
  171. );
  172. const specify = () => {
  173. return goodsSpecification.map(item => {
  174. return (
  175. <Layout key={item.id} style={styles.lay}>
  176. {item.parent ? (
  177. <Text category="h1">
  178. 添加
  179. {item.name}
  180. </Text>
  181. ) : (
  182. <Text category="c1">
  183. 分类名:
  184. {item.name}
  185. </Text>
  186. )}
  187. <Layout style={styles.money}>
  188. {item.parent ? (
  189. <Text category="h1">¥{item.amount}</Text>
  190. ) : (
  191. <Text category="c1">{item.multiple ? "多选" : "单选"}</Text>
  192. )}
  193. </Layout>
  194. </Layout>
  195. );
  196. });
  197. };
  198. const canNext = React.useMemo(() => {
  199. if (name && amount && inventory && week && startTime && endTime && img) {
  200. return true;
  201. }
  202. return false;
  203. }, [
  204. name,
  205. amount,
  206. discountAmount,
  207. inventory,
  208. week,
  209. startTime,
  210. endTime,
  211. introduction,
  212. img,
  213. ]);
  214. return (
  215. <>
  216. {routeName !== "AddGoods" ? (
  217. <GuideHeaderBar />
  218. ) : (
  219. <NavHeaderBar title={addGoods} />
  220. )}
  221. <ScrollPage>
  222. <Layout style={styles.container}>
  223. {routeName !== "AddGoods" && (
  224. <Card appearance="headFilled">
  225. <Text category="s1">{guide1_title1}</Text>
  226. <Text category="s1">{guideHome_title2}</Text>
  227. </Card>
  228. )}
  229. <Card appearance="formFilled">
  230. {/* 商品名称 */}
  231. <FormInput
  232. label={guide1_form_1}
  233. placeholder={guide1_pla_1}
  234. value={name}
  235. onChange={changeName}
  236. style={{ paddingVertical: 3 }}
  237. />
  238. {/* 商品价格 */}
  239. <FormInput
  240. label={guide1_form_2}
  241. placeholder={guide1_pla_2}
  242. value={amount}
  243. type="money"
  244. onChange={changeAmount}
  245. textAlign="right"
  246. />
  247. {/* 优惠价格 */}
  248. <FormInput
  249. label={guide1_form_3}
  250. placeholder={guide1_pla_3}
  251. value={discountAmount}
  252. type="money"
  253. onChange={changeDiscountAmount}
  254. textAlign="right"
  255. />
  256. {id !== 0 && (
  257. <>
  258. {/* 商品规格 */}
  259. <FormInput
  260. label="商品规格"
  261. type="url"
  262. changePath={() => {
  263. navigation.navigate("GoodsSpecification", {
  264. goodsId: id,
  265. });
  266. }}
  267. textAlign="right"
  268. />
  269. {specify()}
  270. </>
  271. )}
  272. {/* 每日供应数量 */}
  273. <FormInput
  274. label={guide1_form_4}
  275. placeholder={guide1_pla_4}
  276. value={inventory}
  277. type="amount"
  278. onChange={changeInventory}
  279. />
  280. {/* 供应时间 */}
  281. <FormInput
  282. label={guide1_form_5}
  283. type="openTime"
  284. defaultValue={defaultValue}
  285. onChange={(week, startTime, endTime) => {
  286. changeWeek(week);
  287. changeStartTime(startTime);
  288. changeEndTime(endTime);
  289. }}
  290. />
  291. <Layout>
  292. <Textarea
  293. containerStyle={styles.textareaContainer}
  294. style={styles.textarea}
  295. onChangeText={changeIntroduction}
  296. defaultValue={introduction}
  297. maxLength={50}
  298. placeholder={guide1_pla_5}
  299. placeholderTextColor="#B4B4B4"
  300. underlineColorAndroid="transparent"
  301. />
  302. </Layout>
  303. <FormInput
  304. type="img"
  305. value={img}
  306. textAlign="right"
  307. onChange={changeImg}
  308. />
  309. <Layout style={styles.layoutLeft} level="1">
  310. <Button
  311. status="primary"
  312. disabled={!canNext}
  313. onPress={() => {
  314. httpPost(
  315. "/goods/save",
  316. {
  317. id,
  318. name,
  319. amount,
  320. discountAmount,
  321. inventory,
  322. week,
  323. startTime,
  324. endTime,
  325. introduction,
  326. img,
  327. merchantId: mid,
  328. },
  329. { body: "json" },
  330. true
  331. ).then(res => {
  332. if (!id) {
  333. const { params } = route;
  334. const { classifyId } = params || {};
  335. if (classifyId) {
  336. addClassification(classifyId, res.id);
  337. }
  338. }
  339. success(`${id ? "修改" : "添加"}成功`);
  340. if (routeName !== "AddGoods") {
  341. changeGuideStep(2);
  342. } else {
  343. navigation.goBack();
  344. }
  345. });
  346. }}
  347. >
  348. {routeName !== "AddGoods" ? next : confirm}
  349. </Button>
  350. {routeName === "AddGoods" && id !== 0 && (
  351. <Button
  352. appearance="ghost"
  353. status="info"
  354. style={{ marginTop: 10 }}
  355. onPress={() => {
  356. showDialog({
  357. bodyText: removeTips,
  358. cancelable: true,
  359. confirmCallback: () => {
  360. httpPost(`/goods/del/${id}`, {}, {}, true).then(() => {
  361. success("删除成功");
  362. navigation.goBack();
  363. });
  364. },
  365. });
  366. }}
  367. >
  368. {delText}
  369. </Button>
  370. )}
  371. {routeName !== "AddGoods" && (
  372. <Button
  373. style={{ marginTop: 10 }}
  374. appearance="ghost"
  375. status="primary"
  376. onPress={() => {
  377. showDialog({
  378. bodyText: passTips,
  379. cancelable: true,
  380. confirmCallback: () => {
  381. changeGuideStep(2);
  382. },
  383. });
  384. }}
  385. >
  386. {pass}
  387. </Button>
  388. )}
  389. </Layout>
  390. </Card>
  391. {routeName !== "AddGoods" && <ConnectButton />}
  392. </Layout>
  393. </ScrollPage>
  394. </>
  395. );
  396. }