index copy.vue 8.6 KB

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