index.vue 12 KB

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