index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/expert.png',
  112. path: '/pages/service/expert'
  113. },
  114. {
  115. name: '绿色认证',
  116. iconText: '证',
  117. iconSvg: '/static/icons/certification.png',
  118. path: '/pages/service/certification'
  119. },
  120. {
  121. name: '保险接入',
  122. iconText: '保',
  123. iconSvg: '/static/icons/insurance.png',
  124. path: '/pages/service/insurance'
  125. }
  126. ])
  127. const userInfo = reactive({
  128. nickName: '',
  129. id: '',
  130. avatar: '',
  131. sex: ''
  132. })
  133. const isLogin = ref(false)
  134. // 方法
  135. const userPlots = () => {
  136. countUserPlots().then((res => {
  137. if (res.data.code == 200) {
  138. const { plotsTotal, inUseCount, leiSureCount } = res.data.data
  139. plotInfo.total = plotsTotal
  140. plotInfo.active = inUseCount
  141. plotInfo.idle = leiSureCount
  142. } else {
  143. console.error("统计地块数量失败!")
  144. }
  145. }))
  146. }
  147. // 检查登录状态
  148. const checkLoginStatus = () => {
  149. console.log("执行Show")
  150. if (storage.getHasLogin()) {
  151. isLogin.value = true
  152. // 获取用户信息
  153. const storedUserInfo = storage.getUserInfo()
  154. console.log("执行Show", storedUserInfo)
  155. if (storedUserInfo) {
  156. userInfo.nickName = storedUserInfo.username || '游客'
  157. userInfo.avatar = storedUserInfo.avatar || '/static/icons/user_icon.png'
  158. userInfo.sex = storedUserInfo.sex
  159. }
  160. } else {
  161. isLogin.value = false
  162. }
  163. }
  164. const navigateToLogin = () => {
  165. uni.navigateTo({
  166. url: '/pages/login/index'
  167. })
  168. }
  169. const navigateToPlots = () => {
  170. uni.navigateTo({
  171. url: '/pages/plots/list'
  172. })
  173. }
  174. const navigateToService = (item) => {
  175. uni.navigateTo({ url: item.path })
  176. }
  177. const handleContact = () => {
  178. uni.makePhoneCall({
  179. phoneNumber: '400-888-8888' // 替换为实际的客服电话
  180. })
  181. }
  182. const navigateToAbout = () => {
  183. uni.navigateTo({
  184. url: '/pages/about/index'
  185. })
  186. }
  187. const navigateToSettings = () => {
  188. uni.navigateTo({
  189. url: '/pages/settings/index'
  190. })
  191. }
  192. // 使用授权服务处理登出
  193. const handleLogout = () => {
  194. uni.showModal({
  195. title: '提示',
  196. content: '确认退出登录?',
  197. success: (res) => {
  198. if (res.confirm) {
  199. // #ifdef H5
  200. webLogout().then(res => {
  201. console.log("tuichu", res)
  202. storage.setAccessToken("")
  203. storage.setUserInfo({})
  204. storage.setHasLogin(false)
  205. storage.removeCurrentPlot()
  206. storage.removeUserPlots()
  207. isLogin.value = false
  208. userInfo.nickname = '游客'
  209. userInfo.id = ''
  210. userInfo.avatar = ''
  211. plotInfo.total = 0
  212. plotInfo.active = 0
  213. plotInfo.idle = 0
  214. })
  215. // #endif
  216. // #ifdef APP-PLUS || MP-HARMONY
  217. logout().then(() => {
  218. isLogin.value = false
  219. userInfo.nickname = '游客'
  220. userInfo.id = ''
  221. userInfo.avatar = '/static/icons/user_icon'
  222. })
  223. // #endif
  224. }
  225. }
  226. })
  227. }
  228. // 生命周期钩子
  229. onShow(() => {
  230. checkLoginStatus()
  231. if (isLogin.value) {
  232. userPlots()
  233. }
  234. })
  235. </script>
  236. <style>
  237. .container {
  238. min-height: 100vh;
  239. background-color: #f5f5f5;
  240. padding: 20rpx;
  241. }
  242. .user-card {
  243. background-color: #4CAF50;
  244. border-radius: 16rpx;
  245. padding: 30rpx;
  246. margin-bottom: 20rpx;
  247. }
  248. .user-header {
  249. display: flex;
  250. align-items: center;
  251. }
  252. .avatar {
  253. width: 120rpx;
  254. height: 120rpx;
  255. border-radius: 60rpx;
  256. /* border: 4rpx solid #fff; */
  257. }
  258. .user-detail {
  259. margin-left: 20rpx;
  260. color: #fff;
  261. }
  262. .nickname {
  263. font-size: 32rpx;
  264. font-weight: bold;
  265. margin-bottom: 10rpx;
  266. display: block;
  267. }
  268. .user-id {
  269. font-size: 24rpx;
  270. opacity: 0.8;
  271. }
  272. .info-card {
  273. background-color: #fff;
  274. border-radius: 16rpx;
  275. padding: 30rpx;
  276. margin-bottom: 20rpx;
  277. }
  278. .card-title {
  279. display: flex;
  280. justify-content: space-between;
  281. align-items: center;
  282. margin-bottom: 30rpx;
  283. font-size: 32rpx;
  284. font-weight: bold;
  285. }
  286. .more {
  287. color: #666;
  288. font-size: 24rpx;
  289. font-weight: normal;
  290. }
  291. .plot-info {
  292. display: flex;
  293. justify-content: space-around;
  294. }
  295. .plot-item {
  296. text-align: center;
  297. }
  298. .number {
  299. font-size: 36rpx;
  300. font-weight: bold;
  301. color: #4CAF50;
  302. display: block;
  303. }
  304. .label {
  305. font-size: 24rpx;
  306. color: #666;
  307. margin-top: 10rpx;
  308. display: block;
  309. }
  310. .service-grid {
  311. display: grid;
  312. grid-template-columns: repeat(4, 1fr);
  313. gap: 30rpx;
  314. padding: 10rpx 0;
  315. }
  316. .service-item {
  317. display: flex;
  318. flex-direction: column;
  319. align-items: center;
  320. }
  321. .service-icon {
  322. width: 88rpx;
  323. height: 88rpx;
  324. background-color: #E8F5E9;
  325. border-radius: 16rpx;
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. margin-bottom: 16rpx;
  330. }
  331. .service-icon text {
  332. font-size: 32rpx;
  333. color: #4CAF50;
  334. font-weight: bold;
  335. }
  336. .icon-svg {
  337. width: 48rpx;
  338. height: 48rpx;
  339. display: block;
  340. }
  341. .service-name {
  342. font-size: 28rpx;
  343. color: #666;
  344. text-align: center;
  345. }
  346. .function-list {
  347. width: 100%;
  348. }
  349. .function-item {
  350. display: flex;
  351. justify-content: space-between;
  352. align-items: center;
  353. padding: 30rpx 0;
  354. border-bottom: 1rpx solid #f5f5f5;
  355. }
  356. .function-item:last-child {
  357. border-bottom: none;
  358. }
  359. .function-item .left {
  360. display: flex;
  361. align-items: center;
  362. }
  363. .function-icon {
  364. width: 44rpx;
  365. height: 44rpx;
  366. background-color: #E8F5E9;
  367. color: #4CAF50;
  368. font-size: 28rpx;
  369. font-weight: bold;
  370. border-radius: 8rpx;
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. margin-right: 20rpx;
  375. }
  376. .function-item text {
  377. font-size: 28rpx;
  378. color: #333;
  379. }
  380. .arrow {
  381. color: #999;
  382. font-size: 24rpx;
  383. }
  384. .login-text {
  385. font-size: 32rpx;
  386. font-weight: bold;
  387. color: #fff;
  388. background-color: rgba(255, 255, 255, 0.2);
  389. padding: 10rpx 30rpx;
  390. border-radius: 30rpx;
  391. }
  392. </style>