index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <div class="mine-index">
  3. <div class="mine-header">
  4. <div class="user-info-box" @click="handleUserInfoClick">
  5. <el-avatar class="avatar" :size="54" :src="userInfo?.icon" />
  6. <div class="user-info">
  7. <div class="user-name">
  8. <span v-if="curRole === 0">{{ userInfo?.nickname }}</span>
  9. <span v-else>{{ userInfo?.name }}</span>
  10. <span class="score" v-if="curRole !== 0">5.0分</span>
  11. </div>
  12. <div class="user-day">这是您使用飞鸟管家的第{{ daysSinceCreation }}天</div>
  13. </div>
  14. </div>
  15. <div class="code-icon" v-if="curRole === 1">
  16. <img src="@/assets/img/mine/code-icon.png" alt="" />
  17. </div>
  18. <div class="switch-role-btn" v-if="roles && roles.length > 1" @click="changeToggle">
  19. <span>切换身份</span>
  20. <el-icon><Switch /></el-icon>
  21. </div>
  22. </div>
  23. <div class="mine-content">
  24. <div class="garden-info" v-if="curRole !== 0">
  25. <div class="item" v-for="(item, index) in gardenInfoItems" :key="index">
  26. <span class="num">{{ item.num }}</span>
  27. <span>{{ item.label }}</span>
  28. </div>
  29. </div>
  30. <!-- <div class="grid-group">
  31. <div class="grid-item" v-for="(item, index) in gridItems" :key="index" @click="handleGridClick(item)">
  32. <div class="grid-title">
  33. <span>{{ item.title }}</span>
  34. <el-icon><ArrowRight /></el-icon>
  35. </div>
  36. <span class="grid-desc">{{ item.desc }}</span>
  37. </div>
  38. </div> -->
  39. <div class="cell-group">
  40. <div class="cell-item" v-for="(item, index) in cellItems" :key="index" @click="handleCellClick(item)">
  41. <span class="item-title">{{ item.title }}</span>
  42. <el-icon class="item-arrow"><ArrowRight /></el-icon>
  43. </div>
  44. </div>
  45. </div>
  46. <!-- 角色切换 -->
  47. <action-sheet :style="{ bottom: 50 + 'px' }" v-model:show="show" :actions="actions" @select="onSelect" />
  48. </div>
  49. </template>
  50. <script setup>
  51. import { onActivated, ref, computed, onMounted } from "vue";
  52. import { useRouter } from "vue-router";
  53. import { useStore } from "vuex";
  54. import wx from "weixin-js-sdk";
  55. import { ActionSheet } from "vant";
  56. import { SET_USER_CUR_ROLE } from "@/store/modules/app/type";
  57. const store = useStore();
  58. const router = useRouter();
  59. // 0: 农户, 1: 专家, 2:农资农服
  60. const curRole = ref(Number(localStorage.getItem("SET_USER_CUR_ROLE")));
  61. const roles = ref(JSON.parse(store.state.app.roles));
  62. const userInfo = JSON.parse(localStorage.getItem("localUserInfo") || "{}");
  63. // 计算从创建时间到现在经过的天数
  64. const daysSinceCreation = computed(() => {
  65. if (!userInfo?.createTime) {
  66. return 0;
  67. }
  68. try {
  69. // 将创建时间字符串转换为日期对象
  70. const createDate = new Date(userInfo.createTime);
  71. // 获取当前日期
  72. const currentDate = new Date();
  73. // 将两个日期都设置为当天的 00:00:00,只比较日期部分
  74. const createDateOnly = new Date(createDate.getFullYear(), createDate.getMonth(), createDate.getDate());
  75. const currentDateOnly = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
  76. // 计算日期差(毫秒)
  77. const timeDiff = currentDateOnly.getTime() - createDateOnly.getTime();
  78. // 转换为天数
  79. const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
  80. // 加1,因为创建当天算作第1天
  81. return days + 1;
  82. } catch (error) {
  83. console.error("计算天数失败:", error);
  84. return 0;
  85. }
  86. });
  87. // const roles = ref([0,1,2,3])
  88. const actions = ref([]);
  89. // 网格项数据
  90. const gridItems = ref([]);
  91. const objects = [
  92. { name: "农户", id: 0, text: "NH" },
  93. { name: "农资", id: 2, text: "NZ" },
  94. ];
  95. onMounted(() => {
  96. // 仅保留 objects 中存在的角色
  97. actions.value = objects.filter((obj) => roles.value && roles.value.includes(obj.id));
  98. });
  99. const show = ref(false);
  100. // 切换身份角色
  101. const onSelect = async (item) => {
  102. show.value = false;
  103. store.dispatch(`app/${SET_USER_CUR_ROLE}`, item.id);
  104. localStorage.setItem("SET_USER_CUR_ROLE", item.id);
  105. // 保存到 session
  106. try {
  107. await VE_API.mine.saveSessionStore({
  108. val: item.id,
  109. key: "cur_role",
  110. });
  111. } catch (error) {
  112. console.error("保存角色到 session 失败:", error);
  113. }
  114. window.location.reload();
  115. };
  116. onActivated(() => {
  117. // if (curRole.value === 0) {
  118. // gridItems.value = [
  119. // {
  120. // title: "我的农场",
  121. // desc: "查看农场列表",
  122. // path: "/my_farm",
  123. // },
  124. // {
  125. // title: "我的消息",
  126. // desc: "查看未读信息",
  127. // path: "/message",
  128. // },
  129. // {
  130. // title: "农事记录",
  131. // desc: "查看历史农事",
  132. // path: "/farm_records",
  133. // },
  134. // ];
  135. // } else if (curRole.value === 1) {
  136. // gridItems.value = [
  137. // {
  138. // title: "我的主页",
  139. // desc: "查看农场列表",
  140. // path: "/expert_detail",
  141. // },
  142. // {
  143. // title: "我的报价",
  144. // desc: "查看未读信息",
  145. // path: "/message",
  146. // },
  147. // {
  148. // title: "信息化档案",
  149. // desc: "查看历史农事",
  150. // path: "/archives",
  151. // },
  152. // ];
  153. // } else if (curRole.value === 2) {
  154. // gridItems.value = [
  155. // {
  156. // title: "我的主页",
  157. // desc: "查看农场列表",
  158. // path: "/my_farm",
  159. // },
  160. // {
  161. // title: "我的消息",
  162. // desc: "查看未读信息",
  163. // path: "/message",
  164. // },
  165. // {
  166. // title: "信息化档案",
  167. // desc: "查看历史农事",
  168. // path: "/archives",
  169. // },
  170. // ];
  171. // }
  172. gridItems.value = [
  173. {
  174. title: "我的主页",
  175. desc: "查看农场列表",
  176. path: "/my_farm",
  177. },
  178. {
  179. title: "我的消息",
  180. desc: "查看未读信息",
  181. path: "/message",
  182. },
  183. {
  184. title: "信息化档案",
  185. desc: "查看历史农事",
  186. path: "/archives",
  187. },
  188. ];
  189. });
  190. // 单元格项数据 - 根据角色动态显示
  191. const cellItems = computed(() => {
  192. // if (curRole.value === 0) {
  193. // return [
  194. // // {
  195. // // title: "我的农场",
  196. // // path: "/my_farm",
  197. // // },
  198. // {
  199. // title: "认证农资",
  200. // path: "/register?identity=NZ",
  201. // },
  202. // // {
  203. // // title: "退出登录",
  204. // // path: "/logout",
  205. // // },
  206. // ];
  207. // } else {
  208. // return [
  209. // {
  210. // title: "认证农资",
  211. // path: "/register?identity=NZ",
  212. // },
  213. // {
  214. // title: "系统提醒",
  215. // path: "/message",
  216. // },
  217. // {
  218. // title: "种植方案",
  219. // path: "/plan?farmId=101532&pageType=plant",
  220. // },
  221. // {
  222. // title: "服务记录",
  223. // path: "/service_records",
  224. // },
  225. // {
  226. // title: "报价维护",
  227. // path: "/offer_price",
  228. // },
  229. // {
  230. // title: "服务维护",
  231. // path: "/service_manage",
  232. // },
  233. // {
  234. // title: "团队管理",
  235. // path: "/team_manage",
  236. // },
  237. // ];
  238. // }
  239. return [
  240. {
  241. title: "认证农资",
  242. path: "/register?identity=NZ",
  243. },
  244. {
  245. title: "系统提醒",
  246. path: "/message",
  247. },
  248. {
  249. title: "种植方案",
  250. path: "/plan?farmId=101532&pageType=plant",
  251. },
  252. {
  253. title: "服务记录",
  254. path: "/service_records",
  255. },
  256. {
  257. title: "报价维护",
  258. path: "/offer_price",
  259. },
  260. {
  261. title: "服务维护",
  262. path: "/service_manage",
  263. },
  264. {
  265. title: "团队管理",
  266. path: "/team_manage",
  267. },
  268. ];
  269. });
  270. // 花园信息项数据
  271. const gardenInfoItems = ref([
  272. { num: "--", label: "服务次数" },
  273. { num: "--", label: "累计客户" },
  274. { num: "--", label: "服务果园" },
  275. ]);
  276. onActivated(() => {
  277. if (curRole.value !== 0) {
  278. getStatistics();
  279. }
  280. });
  281. const getStatistics = () => {
  282. VE_API.z_agricultural_store.statistics().then(({ data }) => {
  283. gardenInfoItems.value[0].num = data.serviceCount || "--";
  284. gardenInfoItems.value[1].num = data.customerCount || "--";
  285. gardenInfoItems.value[2].num = data.serviceFarmCount || "--";
  286. });
  287. };
  288. // 处理网格项点击
  289. const handleGridClick = (item) => {
  290. if (item.path) {
  291. router.push(item.path);
  292. }
  293. };
  294. // 处理单元格项点击
  295. const handleCellClick = (item) => {
  296. if (item.path) {
  297. if (item.path === "/logout") {
  298. // 退出登录逻辑
  299. console.log("退出登录");
  300. // 这里可以添加退出登录的具体逻辑
  301. } else {
  302. router.push(item.path);
  303. }
  304. }
  305. };
  306. const changeToggle = () => {
  307. show.value = true;
  308. };
  309. const handleUserInfoClick = () => {
  310. wx.miniProgram.navigateTo({
  311. url: `/pages/subPages/user_info/index?route_path=/mine`,
  312. });
  313. };
  314. </script>
  315. <style lang="scss" scoped>
  316. .mine-index {
  317. width: 100%;
  318. height: 100vh;
  319. box-sizing: border-box;
  320. padding: 20px 16px;
  321. background: url("@/assets/img/mine/mine-bg.png") no-repeat center center / 100% 100%;
  322. .mine-header {
  323. width: 100%;
  324. display: flex;
  325. align-items: center;
  326. justify-content: space-between;
  327. .user-info-box {
  328. display: flex;
  329. align-items: center;
  330. .avatar {
  331. border: 1px solid #fff;
  332. margin-right: 12px;
  333. }
  334. .user-info {
  335. font-size: 20px;
  336. .user-name {
  337. font-weight: 500;
  338. display: flex;
  339. align-items: center;
  340. .score {
  341. font-size: 14px;
  342. color: #2199f8;
  343. margin-left: 6px;
  344. font-weight: 500;
  345. }
  346. }
  347. .user-day {
  348. font-size: 12px;
  349. color: rgba(0, 0, 0, 0.5);
  350. }
  351. }
  352. }
  353. .code-icon {
  354. width: 21px;
  355. height: 21px;
  356. img {
  357. width: 100%;
  358. height: 100%;
  359. }
  360. }
  361. .switch-role-btn {
  362. margin-right: -16px;
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. padding: 5px 10px 6px 12px;
  367. color: #fff;
  368. background: #2199f8;
  369. border-radius: 25px 0 0 25px;
  370. gap: 5px;
  371. font-size: 13px;
  372. }
  373. }
  374. .mine-content {
  375. margin-top: 20px;
  376. .garden-info {
  377. display: flex;
  378. align-items: center;
  379. justify-content: space-between;
  380. border-radius: 8px;
  381. padding: 6px 0;
  382. background-color: rgba(255, 255, 255, 0.5);
  383. margin-bottom: 10px;
  384. .item {
  385. display: flex;
  386. flex: 1;
  387. flex-direction: column;
  388. align-items: center;
  389. justify-content: center;
  390. color: #999999;
  391. font-size: 13px;
  392. .num {
  393. font-size: 16px;
  394. color: #000;
  395. margin-bottom: 2px;
  396. }
  397. }
  398. }
  399. .grid-group {
  400. display: flex;
  401. align-items: center;
  402. .grid-item {
  403. background-color: #fff;
  404. border-radius: 14px;
  405. padding: 10px 0 10px 10px;
  406. color: #000;
  407. font-size: 16px;
  408. flex: 1;
  409. .grid-title {
  410. display: flex;
  411. align-items: center;
  412. font-weight: 500;
  413. margin-bottom: 6px;
  414. span {
  415. margin-right: 1px;
  416. }
  417. }
  418. .grid-desc {
  419. font-size: 12px;
  420. color: rgba(0, 0, 0, 0.2);
  421. }
  422. }
  423. .grid-item + .grid-item {
  424. margin-left: 10px;
  425. }
  426. }
  427. .cell-group {
  428. .cell-item {
  429. margin-top: 10px;
  430. background-color: #fff;
  431. border-radius: 14px;
  432. padding: 13px 10px;
  433. display: flex;
  434. align-items: center;
  435. justify-content: space-between;
  436. color: #000;
  437. font-size: 16px;
  438. }
  439. }
  440. }
  441. }
  442. </style>