index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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: '13379508760' // 替换为实际的客服电话
  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. webLogout().then(res => {
  200. console.log("tuichu", res)
  201. storage.setAccessToken("")
  202. storage.setUserInfo({})
  203. storage.setHasLogin(false)
  204. storage.removeCurrentPlot()
  205. storage.removeUserPlots()
  206. isLogin.value = false
  207. userInfo.nickname = '游客'
  208. userInfo.id = ''
  209. userInfo.avatar = ''
  210. plotInfo.total = 0
  211. plotInfo.active = 0
  212. plotInfo.idle = 0
  213. })
  214. }
  215. }
  216. })
  217. }
  218. // 生命周期钩子
  219. onShow(() => {
  220. checkLoginStatus()
  221. if (isLogin.value) {
  222. userPlots()
  223. }
  224. })
  225. </script>
  226. <style>
  227. .container {
  228. min-height: 100vh;
  229. background-color: #f5f5f5;
  230. padding: 20rpx;
  231. }
  232. .user-card {
  233. background-color: #4CAF50;
  234. border-radius: 16rpx;
  235. padding: 30rpx;
  236. margin-bottom: 20rpx;
  237. }
  238. .user-header {
  239. display: flex;
  240. align-items: center;
  241. }
  242. .avatar {
  243. width: 120rpx;
  244. height: 120rpx;
  245. border-radius: 60rpx;
  246. /* border: 4rpx solid #fff; */
  247. }
  248. .user-detail {
  249. margin-left: 20rpx;
  250. color: #fff;
  251. }
  252. .nickname {
  253. font-size: 32rpx;
  254. font-weight: bold;
  255. margin-bottom: 10rpx;
  256. display: block;
  257. }
  258. .user-id {
  259. font-size: 24rpx;
  260. opacity: 0.8;
  261. }
  262. .info-card {
  263. background-color: #fff;
  264. border-radius: 16rpx;
  265. padding: 30rpx;
  266. margin-bottom: 20rpx;
  267. }
  268. .card-title {
  269. display: flex;
  270. justify-content: space-between;
  271. align-items: center;
  272. margin-bottom: 30rpx;
  273. font-size: 32rpx;
  274. font-weight: bold;
  275. }
  276. .more {
  277. color: #666;
  278. font-size: 24rpx;
  279. font-weight: normal;
  280. }
  281. .plot-info {
  282. display: flex;
  283. justify-content: space-around;
  284. }
  285. .plot-item {
  286. text-align: center;
  287. }
  288. .number {
  289. font-size: 36rpx;
  290. font-weight: bold;
  291. color: #4CAF50;
  292. display: block;
  293. }
  294. .label {
  295. font-size: 24rpx;
  296. color: #666;
  297. margin-top: 10rpx;
  298. display: block;
  299. }
  300. .service-grid {
  301. display: grid;
  302. grid-template-columns: repeat(4, 1fr);
  303. gap: 30rpx;
  304. padding: 10rpx 0;
  305. }
  306. .service-item {
  307. display: flex;
  308. flex-direction: column;
  309. align-items: center;
  310. }
  311. .service-icon {
  312. width: 88rpx;
  313. height: 88rpx;
  314. background-color: #E8F5E9;
  315. border-radius: 16rpx;
  316. display: flex;
  317. align-items: center;
  318. justify-content: center;
  319. margin-bottom: 16rpx;
  320. }
  321. .service-icon text {
  322. font-size: 32rpx;
  323. color: #4CAF50;
  324. font-weight: bold;
  325. }
  326. .icon-svg {
  327. width: 48rpx;
  328. height: 48rpx;
  329. display: block;
  330. }
  331. .service-name {
  332. font-size: 28rpx;
  333. color: #666;
  334. text-align: center;
  335. }
  336. .function-list {
  337. width: 100%;
  338. }
  339. .function-item {
  340. display: flex;
  341. justify-content: space-between;
  342. align-items: center;
  343. padding: 30rpx 0;
  344. border-bottom: 1rpx solid #f5f5f5;
  345. }
  346. .function-item:last-child {
  347. border-bottom: none;
  348. }
  349. .function-item .left {
  350. display: flex;
  351. align-items: center;
  352. }
  353. .function-icon {
  354. width: 44rpx;
  355. height: 44rpx;
  356. background-color: #E8F5E9;
  357. color: #4CAF50;
  358. font-size: 28rpx;
  359. font-weight: bold;
  360. border-radius: 8rpx;
  361. display: flex;
  362. align-items: center;
  363. justify-content: center;
  364. margin-right: 20rpx;
  365. }
  366. .function-item text {
  367. font-size: 28rpx;
  368. color: #333;
  369. }
  370. .arrow {
  371. color: #999;
  372. font-size: 24rpx;
  373. }
  374. .login-text {
  375. font-size: 32rpx;
  376. font-weight: bold;
  377. color: #fff;
  378. background-color: rgba(255, 255, 255, 0.2);
  379. padding: 10rpx 30rpx;
  380. border-radius: 30rpx;
  381. }
  382. </style>