index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <template>
  2. <view class="container">
  3. <!-- 用户信息卡片 -->
  4. <view class="user-card">
  5. <view class="user-header">
  6. <image class="avatar" :src="userInfo.avatar || '/static/icons/user_icon.png'"></image>
  7. <view class="user-detail" v-if="isLogin">
  8. <text class="nickname">{{userInfo.nickName}}</text>
  9. <!-- <text class="user-id">性别: {{userInfo.sex == '0' ? '男' :'女' || '未知'}}</text> -->
  10. </view>
  11. <view class="user-detail" v-else>
  12. <text class="login-text" @click="navigateToLogin">登录/注册</text>
  13. </view>
  14. </view>
  15. </view>
  16. <!-- 地块信息 -->
  17. <view class="info-card">
  18. <view class="card-title">
  19. <text>我的地块</text>
  20. <text class="more" @click="navigateToPlots">查看更多 ></text>
  21. </view>
  22. <view class="plot-info">
  23. <view class="plot-item">
  24. <text class="number">{{ plotInfo.total }}</text>
  25. <text class="label">总地块数</text>
  26. </view>
  27. <view class="plot-item">
  28. <text class="number">{{ plotInfo.active }}</text>
  29. <text class="label">种植中</text>
  30. </view>
  31. <view class="plot-item">
  32. <text class="number">{{ plotInfo.idle }}</text>
  33. <text class="label">空闲</text>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 农业服务 -->
  38. <view class="info-card">
  39. <view class="card-title">
  40. <text>农业服务</text>
  41. </view>
  42. <view class="service-grid">
  43. <view
  44. class="service-item"
  45. v-for="(item, index) in serviceList"
  46. :key="index"
  47. @click="navigateToService(item)"
  48. >
  49. <view class="service-icon">
  50. <image v-if="item.iconSvg" class="icon-svg" :src="item.iconSvg"></image>
  51. <text v-else>{{ item.iconText }}</text>
  52. </view>
  53. <text class="service-name">{{ item.name }}</text>
  54. </view>
  55. </view>
  56. </view>
  57. <!-- 常用功能 -->
  58. <view class="info-card">
  59. <view class="function-list">
  60. <view class="function-item" @click="handleContact">
  61. <view class="left">
  62. <text>联系客服</text>
  63. </view>
  64. <text class="arrow">></text>
  65. </view>
  66. <!-- <view class="function-item" @click="navigateToSettings">
  67. <view class="left">
  68. <text>系统设置</text>
  69. </view>
  70. <text class="arrow">></text>
  71. </view> -->
  72. <view v-if="isLogin" class="function-item" @click="handleLogout">
  73. <view class="left">
  74. <!-- <text class="function-icon">退</text> -->
  75. <text>退出登录</text>
  76. </view>
  77. <text class="arrow">></text>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script setup>
  84. import { ref, reactive } from 'vue'
  85. import { onShow } from '@dcloudio/uni-app'
  86. import { logout, webLogout } from '@/api/services/connect.js'
  87. import { countUserPlots } from '@/api/services/field.js'
  88. import storage from "@/utils/storage.js"
  89. // 响应式数据
  90. const plotInfo = reactive({
  91. total: 0,
  92. active: 0,
  93. idle: 0
  94. })
  95. const serviceList = ref([
  96. // {
  97. // name: '农资商城',
  98. // iconText: '商',
  99. // iconSvg: '/static/icons/mall.png',
  100. // path: '/pages/service/mall'
  101. // },
  102. {
  103. name: '农品交易',
  104. iconText: '售',
  105. iconSvg: '/static/icons/sales.png',
  106. path: '/pages/service/sales'
  107. },
  108. {
  109. name: '农事记录',
  110. iconText: '录',
  111. iconSvg: '/static/icons/activity-active.png',
  112. path: '/pages/activity/index'
  113. },
  114. {
  115. name: '设备管理',
  116. iconText: '备',
  117. iconSvg: '/static/icons/device-active.png',
  118. path: '/pages/device/index'
  119. },
  120. {
  121. name: '专家咨询',
  122. iconText: '诊',
  123. iconSvg: '/static/icons/expert.png',
  124. path: '/pages/service/expert'
  125. },
  126. {
  127. name: '绿色认证',
  128. iconText: '证',
  129. iconSvg: '/static/icons/certification.png',
  130. path: '/pages/service/certification'
  131. },
  132. {
  133. name: '保险接入',
  134. iconText: '保',
  135. iconSvg: '/static/icons/insurance.png',
  136. path: '/pages/service/insurance'
  137. }
  138. ])
  139. const userInfo = reactive({
  140. nickName: '',
  141. id: '',
  142. avatar: '',
  143. sex: ''
  144. })
  145. const isLogin = ref(false)
  146. // 方法
  147. const userPlots = () => {
  148. countUserPlots().then((res => {
  149. if (res.data.code == 200) {
  150. const { plotsTotal, inUseCount, leiSureCount } = res.data.data
  151. plotInfo.total = plotsTotal
  152. plotInfo.active = inUseCount
  153. plotInfo.idle = leiSureCount
  154. } else {
  155. console.error("统计地块数量失败!")
  156. }
  157. }))
  158. }
  159. // 检查登录状态
  160. const checkLoginStatus = () => {
  161. console.log("执行Show")
  162. if (storage.getHasLogin()) {
  163. isLogin.value = true
  164. // 获取用户信息
  165. const storedUserInfo = storage.getUserInfo()
  166. console.log("执行Show", storedUserInfo)
  167. if (storedUserInfo) {
  168. userInfo.nickName = storedUserInfo.username || '游客'
  169. userInfo.avatar = storedUserInfo.avatar || '/static/icons/user_icon.png'
  170. userInfo.sex = storedUserInfo.sex
  171. }
  172. } else {
  173. isLogin.value = false
  174. }
  175. }
  176. const navigateToLogin = () => {
  177. uni.navigateTo({
  178. url: '/pages/login/index'
  179. })
  180. }
  181. const navigateToPlots = () => {
  182. uni.navigateTo({
  183. url: '/pages/plots/list'
  184. })
  185. }
  186. const navigateToService = (item) => {
  187. uni.navigateTo({ url: item.path })
  188. }
  189. const handleContact = () => {
  190. uni.makePhoneCall({
  191. phoneNumber: '13379508760' // 替换为实际的客服电话
  192. })
  193. }
  194. const navigateToAbout = () => {
  195. uni.navigateTo({
  196. url: '/pages/about/index'
  197. })
  198. }
  199. const navigateToSettings = () => {
  200. uni.navigateTo({
  201. url: '/pages/settings/index'
  202. })
  203. }
  204. // 使用授权服务处理登出
  205. const handleLogout = () => {
  206. uni.showModal({
  207. title: '提示',
  208. content: '确认退出登录?',
  209. success: (res) => {
  210. if (res.confirm) {
  211. webLogout().then(res => {
  212. console.log("tuichu", res)
  213. storage.setAccessToken("")
  214. storage.setUserInfo({})
  215. storage.setHasLogin(false)
  216. storage.removeCurrentPlot()
  217. storage.removeUserPlots()
  218. isLogin.value = false
  219. userInfo.nickname = '游客'
  220. userInfo.id = ''
  221. userInfo.avatar = ''
  222. plotInfo.total = 0
  223. plotInfo.active = 0
  224. plotInfo.idle = 0
  225. })
  226. }
  227. }
  228. })
  229. }
  230. // 生命周期钩子
  231. onShow(() => {
  232. checkLoginStatus()
  233. if (isLogin.value) {
  234. userPlots()
  235. }
  236. })
  237. </script>
  238. <style>
  239. .container {
  240. min-height: 100vh;
  241. background-color: #f5f5f5;
  242. padding: 20rpx;
  243. }
  244. .user-card {
  245. background-color: #4CAF50;
  246. border-radius: 16rpx;
  247. padding: 30rpx;
  248. margin-bottom: 20rpx;
  249. }
  250. .user-header {
  251. display: flex;
  252. align-items: center;
  253. }
  254. .avatar {
  255. width: 120rpx;
  256. height: 120rpx;
  257. border-radius: 60rpx;
  258. /* border: 4rpx solid #fff; */
  259. }
  260. .user-detail {
  261. margin-left: 20rpx;
  262. color: #fff;
  263. }
  264. .nickname {
  265. font-size: 32rpx;
  266. font-weight: bold;
  267. margin-bottom: 10rpx;
  268. display: block;
  269. }
  270. .user-id {
  271. font-size: 24rpx;
  272. opacity: 0.8;
  273. }
  274. .info-card {
  275. background-color: #fff;
  276. border-radius: 16rpx;
  277. padding: 30rpx;
  278. margin-bottom: 20rpx;
  279. }
  280. .card-title {
  281. display: flex;
  282. justify-content: space-between;
  283. align-items: center;
  284. margin-bottom: 30rpx;
  285. font-size: 32rpx;
  286. font-weight: bold;
  287. }
  288. .more {
  289. color: #666;
  290. font-size: 24rpx;
  291. font-weight: normal;
  292. }
  293. .plot-info {
  294. display: flex;
  295. justify-content: space-around;
  296. }
  297. .plot-item {
  298. text-align: center;
  299. }
  300. .number {
  301. font-size: 36rpx;
  302. font-weight: bold;
  303. color: #4CAF50;
  304. display: block;
  305. }
  306. .label {
  307. font-size: 24rpx;
  308. color: #666;
  309. margin-top: 10rpx;
  310. display: block;
  311. }
  312. .service-grid {
  313. display: grid;
  314. grid-template-columns: repeat(4, 1fr);
  315. gap: 30rpx;
  316. padding: 10rpx 0;
  317. }
  318. .service-item {
  319. display: flex;
  320. flex-direction: column;
  321. align-items: center;
  322. }
  323. .service-icon {
  324. width: 88rpx;
  325. height: 88rpx;
  326. background-color: #E8F5E9;
  327. border-radius: 16rpx;
  328. display: flex;
  329. align-items: center;
  330. justify-content: center;
  331. margin-bottom: 16rpx;
  332. }
  333. .service-icon text {
  334. font-size: 32rpx;
  335. color: #4CAF50;
  336. font-weight: bold;
  337. }
  338. .icon-svg {
  339. width: 48rpx;
  340. height: 48rpx;
  341. display: block;
  342. }
  343. .service-name {
  344. font-size: 28rpx;
  345. color: #666;
  346. text-align: center;
  347. }
  348. .function-list {
  349. width: 100%;
  350. }
  351. .function-item {
  352. display: flex;
  353. justify-content: space-between;
  354. align-items: center;
  355. padding: 30rpx 0;
  356. border-bottom: 1rpx solid #f5f5f5;
  357. }
  358. .function-item:last-child {
  359. border-bottom: none;
  360. }
  361. .function-item .left {
  362. display: flex;
  363. align-items: center;
  364. }
  365. .function-icon {
  366. width: 44rpx;
  367. height: 44rpx;
  368. background-color: #E8F5E9;
  369. color: #4CAF50;
  370. font-size: 28rpx;
  371. font-weight: bold;
  372. border-radius: 8rpx;
  373. display: flex;
  374. align-items: center;
  375. justify-content: center;
  376. margin-right: 20rpx;
  377. }
  378. .function-item text {
  379. font-size: 28rpx;
  380. color: #333;
  381. }
  382. .arrow {
  383. color: #999;
  384. font-size: 24rpx;
  385. }
  386. .login-text {
  387. font-size: 32rpx;
  388. font-weight: bold;
  389. color: #fff;
  390. background-color: rgba(255, 255, 255, 0.2);
  391. padding: 10rpx 30rpx;
  392. border-radius: 30rpx;
  393. }
  394. </style>