index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. gridItems.value = [
  118. {
  119. title: "我的主页",
  120. desc: "查看农场列表",
  121. path: "/my_farm",
  122. },
  123. {
  124. title: "我的消息",
  125. desc: "查看未读信息",
  126. path: "/message",
  127. },
  128. {
  129. title: "信息化档案",
  130. desc: "查看历史农事",
  131. path: "/archives",
  132. },
  133. ];
  134. });
  135. // 单元格项数据 - 根据角色动态显示
  136. const cellItems = computed(() => {
  137. let list = [
  138. {
  139. title: "系统提醒",
  140. path: "/message",
  141. },
  142. {
  143. title: "种植方案",
  144. path: "/plan?pageType=plant",
  145. },
  146. {
  147. title: "服务记录",
  148. path: "/service_records",
  149. },
  150. {
  151. title: "报价维护",
  152. path: "/offer_price",
  153. },
  154. {
  155. title: "服务维护",
  156. path: "/service_manage",
  157. },
  158. {
  159. title: "团队管理",
  160. path: "/team_manage",
  161. },
  162. ]
  163. if(curRole.value === 0) {
  164. list.unshift({
  165. title: "认证农资",
  166. path: "/register?identity=NZ",
  167. });
  168. }
  169. return list;
  170. });
  171. // 花园信息项数据
  172. const gardenInfoItems = ref([
  173. { num: "--", label: "服务次数" },
  174. { num: "--", label: "累计客户" },
  175. { num: "--", label: "服务果园" },
  176. ]);
  177. onActivated(() => {
  178. if (curRole.value !== 0) {
  179. getStatistics();
  180. }
  181. });
  182. const getStatistics = () => {
  183. VE_API.z_agricultural_store.statistics().then(({ data }) => {
  184. gardenInfoItems.value[0].num = data.serviceCount || "--";
  185. gardenInfoItems.value[1].num = data.customerCount || "--";
  186. gardenInfoItems.value[2].num = data.serviceFarmCount || "--";
  187. });
  188. };
  189. // 处理网格项点击
  190. const handleGridClick = (item) => {
  191. if (item.path) {
  192. router.push(item.path);
  193. }
  194. };
  195. // 处理单元格项点击
  196. const handleCellClick = (item) => {
  197. if (item.path) {
  198. if (item.path === "/logout") {
  199. // 退出登录逻辑
  200. console.log("退出登录");
  201. // 这里可以添加退出登录的具体逻辑
  202. } else {
  203. router.push(item.path);
  204. }
  205. }
  206. };
  207. const changeToggle = () => {
  208. show.value = true;
  209. };
  210. const handleUserInfoClick = () => {
  211. wx.miniProgram.navigateTo({
  212. url: `/pages/subPages/user_info/index?route_path=/mine`,
  213. });
  214. };
  215. </script>
  216. <style lang="scss" scoped>
  217. .mine-index {
  218. width: 100%;
  219. height: 100vh;
  220. box-sizing: border-box;
  221. padding: 20px 16px;
  222. background: url("@/assets/img/mine/mine-bg.png") no-repeat center center / 100% 100%;
  223. .mine-header {
  224. width: 100%;
  225. display: flex;
  226. align-items: center;
  227. justify-content: space-between;
  228. .user-info-box {
  229. display: flex;
  230. align-items: center;
  231. .avatar {
  232. border: 1px solid #fff;
  233. margin-right: 12px;
  234. }
  235. .user-info {
  236. font-size: 20px;
  237. .user-name {
  238. font-weight: 500;
  239. display: flex;
  240. align-items: center;
  241. .score {
  242. font-size: 14px;
  243. color: #2199f8;
  244. margin-left: 6px;
  245. font-weight: 500;
  246. }
  247. }
  248. .user-day {
  249. font-size: 12px;
  250. color: rgba(0, 0, 0, 0.5);
  251. }
  252. }
  253. }
  254. .code-icon {
  255. width: 21px;
  256. height: 21px;
  257. img {
  258. width: 100%;
  259. height: 100%;
  260. }
  261. }
  262. .switch-role-btn {
  263. margin-right: -16px;
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. padding: 5px 10px 6px 12px;
  268. color: #fff;
  269. background: #2199f8;
  270. border-radius: 25px 0 0 25px;
  271. gap: 5px;
  272. font-size: 13px;
  273. }
  274. }
  275. .mine-content {
  276. margin-top: 20px;
  277. .garden-info {
  278. display: flex;
  279. align-items: center;
  280. justify-content: space-between;
  281. border-radius: 8px;
  282. padding: 6px 0;
  283. background-color: rgba(255, 255, 255, 0.5);
  284. margin-bottom: 10px;
  285. .item {
  286. display: flex;
  287. flex: 1;
  288. flex-direction: column;
  289. align-items: center;
  290. justify-content: center;
  291. color: #999999;
  292. font-size: 13px;
  293. .num {
  294. font-size: 16px;
  295. color: #000;
  296. margin-bottom: 2px;
  297. }
  298. }
  299. }
  300. .grid-group {
  301. display: flex;
  302. align-items: center;
  303. .grid-item {
  304. background-color: #fff;
  305. border-radius: 14px;
  306. padding: 10px 0 10px 10px;
  307. color: #000;
  308. font-size: 16px;
  309. flex: 1;
  310. .grid-title {
  311. display: flex;
  312. align-items: center;
  313. font-weight: 500;
  314. margin-bottom: 6px;
  315. span {
  316. margin-right: 1px;
  317. }
  318. }
  319. .grid-desc {
  320. font-size: 12px;
  321. color: rgba(0, 0, 0, 0.2);
  322. }
  323. }
  324. .grid-item + .grid-item {
  325. margin-left: 10px;
  326. }
  327. }
  328. .cell-group {
  329. .cell-item {
  330. margin-top: 10px;
  331. background-color: #fff;
  332. border-radius: 14px;
  333. padding: 13px 10px;
  334. display: flex;
  335. align-items: center;
  336. justify-content: space-between;
  337. color: #000;
  338. font-size: 16px;
  339. }
  340. }
  341. }
  342. }
  343. </style>