index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="farm-card-page">
  3. <custom-header name="种植方案"></custom-header>
  4. <div class="system-generated">
  5. <div class="tip-box">
  6. <el-icon size="18"><CircleCheckFilled /></el-icon>
  7. <span>系统已生成多套方案,请选择最佳方案</span>
  8. </div>
  9. <tab-list
  10. type="light"
  11. v-model="active"
  12. :tabs="tabs"
  13. @change="handleTabChange"
  14. class="tabs-list"
  15. />
  16. <farm-work-plan-timeline
  17. class="timeline-wrap"
  18. pageType="plant"
  19. :containerId="containerId"
  20. :disableClick="true"
  21. />
  22. </div>
  23. </div>
  24. <div class="custom-bottom-fixed-btns">
  25. <div class="bottom-btn primary-btn" @click="handleConfirmPlan">确认方案</div>
  26. </div>
  27. <tip-popup
  28. v-model:show="showFarmPopup"
  29. type="success"
  30. text="农场创建成功"
  31. buttonText="分享微信"
  32. @confirm="handleShareFarm"
  33. @handleClickOverlay="handleClickOverlay"
  34. />
  35. </template>
  36. <script setup>
  37. import customHeader from "@/components/customHeader.vue";
  38. import { onActivated, ref, onDeactivated } from "vue";
  39. import { useRoute, useRouter } from "vue-router";
  40. import { Tab, Tabs } from "vant";
  41. import FarmWorkPlanTimeline from "@/components/pageComponents/FarmWorkPlanTimeline.vue";
  42. import wx from "weixin-js-sdk";
  43. import tabList from "@/components/pageComponents/TabList.vue";
  44. import tipPopup from "@/components/popup/tipPopup.vue";
  45. import { ElMessage } from "element-plus";
  46. const router = useRouter();
  47. const route = useRoute();
  48. const tabs = ref([]);
  49. const active = ref(null);
  50. const containerId = ref(null);
  51. const handleTabChange = (id, item) => {
  52. containerId.value = item.containerId;
  53. active.value = id;
  54. };
  55. onActivated(() => {
  56. containerId.value = route.query.containerId;
  57. getListMySchemes();
  58. });
  59. onDeactivated(() => {
  60. active.value = null;
  61. containerId.value = null;
  62. });
  63. const getListMySchemes = () => {
  64. VE_API.home.listMySchemes({containerId: route.query.containerId}).then(({ data }) => {
  65. if (data.length) {
  66. tabs.value = data || [];
  67. const index = data.findIndex((item) => item.containerId == route.query.containerId);
  68. active.value = route.query.schemeId || data[index].id;
  69. containerId.value = route.query.containerId;
  70. }
  71. });
  72. };
  73. const showFarmPopup = ref(false);
  74. const shareData = ref({});
  75. const handleConfirmPlan = () => {
  76. // 从路由参数中获取农场数据
  77. let geomValue = route.query.geom;
  78. // 处理 geom 参数,可能是 JSON 字符串或数组
  79. if (typeof geomValue === "string") {
  80. try {
  81. // 尝试解析 JSON 字符串
  82. const parsed = JSON.parse(geomValue);
  83. if (Array.isArray(parsed)) {
  84. geomValue = parsed;
  85. }
  86. } catch (e) {
  87. // 如果不是 JSON 字符串,保持原值
  88. console.warn("geom 参数解析失败,使用原值:", e);
  89. }
  90. }
  91. const farmParams = {
  92. ...route.query,
  93. containerId: containerId.value,
  94. schemeId: active.value,
  95. geom: geomValue,
  96. defaultFarm: Boolean(route.query.defaultFarm),
  97. agriculturalCreate: route.query.agriculturalCreate * 1,
  98. };
  99. // 验证必填字段
  100. if (
  101. !farmParams.wkt ||
  102. !farmParams.speciesId ||
  103. !farmParams.containerId ||
  104. !farmParams.address ||
  105. !farmParams.mu ||
  106. !farmParams.name ||
  107. !farmParams.fzr ||
  108. !farmParams.tel
  109. ) {
  110. ElMessage.error("农场信息不完整,请返回重新填写");
  111. return;
  112. }
  113. delete farmParams.from;
  114. // 调用创建农场接口
  115. VE_API.farm
  116. .saveFarm(farmParams)
  117. .then((res) => {
  118. if (res.code === 0) {
  119. shareData.value = res.data;
  120. ElMessage.success("创建成功");
  121. setTimeout(() => {
  122. handleClickOverlay();
  123. }, 1000);
  124. } else {
  125. ElMessage.error(res.msg || "创建失败");
  126. }
  127. })
  128. .catch((err) => {
  129. console.error("创建农场失败:", err);
  130. ElMessage.error("创建失败,请稍后重试");
  131. });
  132. };
  133. const handleShareFarm = () => {
  134. const query = {
  135. agriculturalStoreId: shareData.value.agriculturalStoreId,
  136. farmId: shareData.value.id,
  137. speciesName: route.query.speciesName,
  138. containerId: shareData.value.containerId,
  139. };
  140. wx.miniProgram.navigateTo({
  141. url: `/pages/subPages/share_page/index?pageParams=${JSON.stringify(query)}&type=shareFarm`,
  142. });
  143. };
  144. const handleClickOverlay = () => {
  145. // 根据 from 参数跳转回原页面
  146. const fromPage = route.query.from;
  147. if (fromPage) {
  148. router.replace(`/${fromPage}`);
  149. } else {
  150. // 如果没有 from 参数,默认跳转到首页
  151. router.replace("/home");
  152. }
  153. };
  154. </script>
  155. <style scoped lang="scss">
  156. .farm-card-page {
  157. width: 100%;
  158. height: 100vh;
  159. background: #f5f7fb;
  160. .system-generated {
  161. padding: 17px 10px 10px;
  162. height: calc(100vh - 150px);
  163. .tip-box {
  164. display: flex;
  165. align-items: center;
  166. justify-content: center;
  167. padding: 10px;
  168. background: rgba(33, 153, 248, 0.1);
  169. border-radius: 8px;
  170. color: #2199f8;
  171. box-sizing: border-box;
  172. margin-bottom: 10px;
  173. span {
  174. margin-left: 5px;
  175. }
  176. }
  177. .tabs-list {
  178. margin-bottom: 10px;
  179. }
  180. .timeline-wrap{
  181. height: calc(100vh - 150px - 50px);
  182. }
  183. }
  184. .tabs-wrap {
  185. ::v-deep {
  186. .van-tabs__line {
  187. width: 24px;
  188. height: 4px;
  189. }
  190. .van-tab {
  191. width: 100px;
  192. flex: none;
  193. }
  194. .van-tabs__nav {
  195. justify-content: center;
  196. }
  197. }
  198. }
  199. }
  200. .custom-bottom-fixed-btns {
  201. justify-content: center;
  202. .bottom-btn {
  203. padding: 10px 40px;
  204. }
  205. }
  206. </style>