index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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: "/authentication",
  167. });
  168. // list.unshift({
  169. // title: "认证农资",
  170. // path: "/register?identity=NZ",
  171. // });
  172. }
  173. return list;
  174. });
  175. // 花园信息项数据
  176. const gardenInfoItems = ref([
  177. { num: "--", label: "服务次数" },
  178. { num: "--", label: "累计客户" },
  179. { num: "--", label: "服务果园" },
  180. ]);
  181. onActivated(() => {
  182. if (curRole.value !== 0) {
  183. getStatistics();
  184. }
  185. });
  186. const getStatistics = () => {
  187. VE_API.z_agricultural_store.statistics().then(({ data }) => {
  188. gardenInfoItems.value[0].num = data.serviceCount || "--";
  189. gardenInfoItems.value[1].num = data.customerCount || "--";
  190. gardenInfoItems.value[2].num = data.serviceFarmCount || "--";
  191. });
  192. };
  193. // 处理网格项点击
  194. const handleGridClick = (item) => {
  195. if (item.path) {
  196. router.push(item.path);
  197. }
  198. };
  199. // 处理单元格项点击
  200. const handleCellClick = (item) => {
  201. if (item.path) {
  202. if (item.path === "/logout") {
  203. // 退出登录逻辑
  204. console.log("退出登录");
  205. // 这里可以添加退出登录的具体逻辑
  206. } else {
  207. router.push(item.path);
  208. }
  209. }
  210. };
  211. const changeToggle = () => {
  212. show.value = true;
  213. };
  214. const handleUserInfoClick = () => {
  215. wx.miniProgram.navigateTo({
  216. url: `/pages/subPages/user_info/index?route_path=/mine`,
  217. });
  218. };
  219. </script>
  220. <style lang="scss" scoped>
  221. .mine-index {
  222. width: 100%;
  223. height: 100vh;
  224. box-sizing: border-box;
  225. padding: 20px 16px;
  226. background: url("@/assets/img/mine/mine-bg.png") no-repeat center center / 100% 100%;
  227. .mine-header {
  228. width: 100%;
  229. display: flex;
  230. align-items: center;
  231. justify-content: space-between;
  232. .user-info-box {
  233. display: flex;
  234. align-items: center;
  235. .avatar {
  236. border: 1px solid #fff;
  237. margin-right: 12px;
  238. }
  239. .user-info {
  240. font-size: 20px;
  241. .user-name {
  242. font-weight: 500;
  243. display: flex;
  244. align-items: center;
  245. .score {
  246. font-size: 14px;
  247. color: #2199f8;
  248. margin-left: 6px;
  249. font-weight: 500;
  250. }
  251. }
  252. .user-day {
  253. font-size: 12px;
  254. color: rgba(0, 0, 0, 0.5);
  255. }
  256. }
  257. }
  258. .code-icon {
  259. width: 21px;
  260. height: 21px;
  261. img {
  262. width: 100%;
  263. height: 100%;
  264. }
  265. }
  266. .switch-role-btn {
  267. margin-right: -16px;
  268. display: flex;
  269. align-items: center;
  270. justify-content: center;
  271. padding: 5px 10px 6px 12px;
  272. color: #fff;
  273. background: #2199f8;
  274. border-radius: 25px 0 0 25px;
  275. gap: 5px;
  276. font-size: 13px;
  277. }
  278. }
  279. .mine-content {
  280. margin-top: 20px;
  281. .garden-info {
  282. display: flex;
  283. align-items: center;
  284. justify-content: space-between;
  285. border-radius: 8px;
  286. padding: 6px 0;
  287. background-color: rgba(255, 255, 255, 0.5);
  288. margin-bottom: 10px;
  289. .item {
  290. display: flex;
  291. flex: 1;
  292. flex-direction: column;
  293. align-items: center;
  294. justify-content: center;
  295. color: #999999;
  296. font-size: 13px;
  297. .num {
  298. font-size: 16px;
  299. color: #000;
  300. margin-bottom: 2px;
  301. }
  302. }
  303. }
  304. .grid-group {
  305. display: flex;
  306. align-items: center;
  307. .grid-item {
  308. background-color: #fff;
  309. border-radius: 14px;
  310. padding: 10px 0 10px 10px;
  311. color: #000;
  312. font-size: 16px;
  313. flex: 1;
  314. .grid-title {
  315. display: flex;
  316. align-items: center;
  317. font-weight: 500;
  318. margin-bottom: 6px;
  319. span {
  320. margin-right: 1px;
  321. }
  322. }
  323. .grid-desc {
  324. font-size: 12px;
  325. color: rgba(0, 0, 0, 0.2);
  326. }
  327. }
  328. .grid-item + .grid-item {
  329. margin-left: 10px;
  330. }
  331. }
  332. .cell-group {
  333. .cell-item {
  334. margin-top: 10px;
  335. background-color: #fff;
  336. border-radius: 14px;
  337. padding: 13px 10px;
  338. display: flex;
  339. align-items: center;
  340. justify-content: space-between;
  341. color: #000;
  342. font-size: 16px;
  343. }
  344. }
  345. }
  346. }
  347. </style>