index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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="viewTerms">
  67. <view class="left">
  68. <text>用户协议</text>
  69. </view>
  70. <text class="arrow">></text>
  71. </view>
  72. <view class="function-item" @click="viewPrivacy">
  73. <view class="left">
  74. <text>隐私政策</text>
  75. </view>
  76. <text class="arrow">></text>
  77. </view>
  78. <!-- <view class="function-item" @click="navigateToSettings">
  79. <view class="left">
  80. <text>系统设置</text>
  81. </view>
  82. <text class="arrow">></text>
  83. </view> -->
  84. <view v-if="isLogin" class="function-item" @click="handleLogout">
  85. <view class="left">
  86. <!-- <text class="function-icon">退</text> -->
  87. <text>退出登录</text>
  88. </view>
  89. <text class="arrow">></text>
  90. </view>
  91. </view>
  92. </view>
  93. </view>
  94. </template>
  95. <script setup>
  96. import { ref, reactive } from 'vue'
  97. import { onShow } from '@dcloudio/uni-app'
  98. import { logout, webLogout } from '@/api/services/connect.js'
  99. import { countUserPlots } from '@/api/services/field.js'
  100. import storage from "@/utils/storage.js"
  101. // 查看用户协议
  102. const viewTerms = () => {
  103. uni.navigateTo({
  104. url: '/pages/login/terms'
  105. })
  106. }
  107. // 查看隐私政策
  108. const viewPrivacy = () => {
  109. uni.navigateTo({
  110. url: '/pages/login/privacy'
  111. })
  112. }
  113. // 响应式数据
  114. const plotInfo = reactive({
  115. total: 0,
  116. active: 0,
  117. idle: 0
  118. })
  119. const serviceList = ref([
  120. // {
  121. // name: '农资商城',
  122. // iconText: '商',
  123. // iconSvg: '/static/icons/mall.png',
  124. // path: '/pages/service/mall'
  125. // },
  126. {
  127. name: '农品交易',
  128. iconText: '售',
  129. iconSvg: '/static/icons/sales.png',
  130. path: '/pages/service/sales'
  131. },
  132. {
  133. name: '农事记录',
  134. iconText: '录',
  135. iconSvg: '/static/icons/activity-active.png',
  136. path: '/pages/activity/index'
  137. },
  138. {
  139. name: '设备管理',
  140. iconText: '备',
  141. iconSvg: '/static/icons/device-active.png',
  142. path: '/pages/device/index'
  143. },
  144. // {
  145. // name: '专家咨询',
  146. // iconText: '诊',
  147. // iconSvg: '/static/icons/expert.png',
  148. // path: '/pages/service/expert'
  149. // },
  150. // {
  151. // name: '绿色认证',
  152. // iconText: '证',
  153. // iconSvg: '/static/icons/certification.png',
  154. // path: '/pages/service/certification'
  155. // },
  156. // {
  157. // name: '保险接入',
  158. // iconText: '保',
  159. // iconSvg: '/static/icons/insurance.png',
  160. // path: '/pages/service/insurance'
  161. // }
  162. ])
  163. const userInfo = reactive({
  164. nickName: '',
  165. id: '',
  166. avatar: '',
  167. sex: ''
  168. })
  169. const isLogin = ref(false)
  170. // 方法
  171. const userPlots = () => {
  172. countUserPlots().then((res => {
  173. if (res.data.code == 200) {
  174. const { plotsTotal, inUseCount, leiSureCount } = res.data.data
  175. plotInfo.total = plotsTotal
  176. plotInfo.active = inUseCount
  177. plotInfo.idle = leiSureCount
  178. } else {
  179. console.error("统计地块数量失败!")
  180. }
  181. }))
  182. }
  183. // 检查登录状态
  184. const checkLoginStatus = () => {
  185. console.log("执行Show")
  186. if (storage.getHasLogin()) {
  187. isLogin.value = true
  188. // 获取用户信息
  189. const storedUserInfo = storage.getUserInfo()
  190. console.log("执行Show", storedUserInfo)
  191. if (storedUserInfo) {
  192. userInfo.nickName = storedUserInfo.username || '游客'
  193. userInfo.avatar = storedUserInfo.avatar || '/static/icons/user_icon.png'
  194. userInfo.sex = storedUserInfo.sex
  195. }
  196. } else {
  197. isLogin.value = false
  198. }
  199. }
  200. const navigateToLogin = () => {
  201. uni.navigateTo({
  202. url: '/pages/login/index'
  203. })
  204. }
  205. const navigateToPlots = () => {
  206. if (!storage.getHasLogin()) {
  207. uni.showModal({
  208. title: '提示',
  209. content: '您还未登录,请先登录',
  210. confirmText: '去登录',
  211. cancelText: '取消',
  212. success: function(res) {
  213. if (res.confirm) {
  214. uni.navigateTo({
  215. url: '/pages/login/index'
  216. });
  217. }
  218. },
  219. });
  220. return;
  221. }
  222. uni.navigateTo({
  223. url: '/pages/plots/list'
  224. })
  225. }
  226. const navigateToService = (item) => {
  227. if (!storage.getHasLogin()) {
  228. uni.showModal({
  229. title: '提示',
  230. content: '您还未登录,请先登录',
  231. confirmText: '去登录',
  232. cancelText: '取消',
  233. success: function(res) {
  234. if (res.confirm) {
  235. uni.navigateTo({
  236. url: '/pages/login/index'
  237. });
  238. }
  239. },
  240. });
  241. return;
  242. }
  243. uni.navigateTo({ url: item.path })
  244. }
  245. const handleContact = () => {
  246. uni.makePhoneCall({
  247. phoneNumber: '13379508760' // 替换为实际的客服电话
  248. })
  249. }
  250. const navigateToAbout = () => {
  251. uni.navigateTo({
  252. url: '/pages/about/index'
  253. })
  254. }
  255. const navigateToSettings = () => {
  256. uni.navigateTo({
  257. url: '/pages/settings/index'
  258. })
  259. }
  260. // 使用授权服务处理登出
  261. const handleLogout = () => {
  262. uni.showModal({
  263. title: '提示',
  264. content: '确认退出登录?',
  265. success: (res) => {
  266. if (res.confirm) {
  267. webLogout().then(res => {
  268. console.log("tuichu", res)
  269. storage.setAccessToken("")
  270. storage.setUserInfo({})
  271. storage.setHasLogin(false)
  272. storage.removeCurrentPlot()
  273. storage.removeUserPlots()
  274. isLogin.value = false
  275. userInfo.nickname = '游客'
  276. userInfo.id = ''
  277. userInfo.avatar = ''
  278. plotInfo.total = 0
  279. plotInfo.active = 0
  280. plotInfo.idle = 0
  281. })
  282. }
  283. }
  284. })
  285. }
  286. // 生命周期钩子
  287. onShow(() => {
  288. checkLoginStatus()
  289. if (isLogin.value) {
  290. userPlots()
  291. }
  292. })
  293. </script>
  294. <style>
  295. .container {
  296. min-height: 100vh;
  297. background-color: #f5f5f5;
  298. padding: 20rpx;
  299. }
  300. .user-card {
  301. background-color: #4CAF50;
  302. border-radius: 16rpx;
  303. padding: 30rpx;
  304. margin-bottom: 20rpx;
  305. }
  306. .user-header {
  307. display: flex;
  308. align-items: center;
  309. }
  310. .avatar {
  311. width: 120rpx;
  312. height: 120rpx;
  313. border-radius: 60rpx;
  314. /* border: 4rpx solid #fff; */
  315. }
  316. .user-detail {
  317. margin-left: 20rpx;
  318. color: #fff;
  319. }
  320. .nickname {
  321. font-size: 32rpx;
  322. font-weight: bold;
  323. margin-bottom: 10rpx;
  324. display: block;
  325. }
  326. .user-id {
  327. font-size: 24rpx;
  328. opacity: 0.8;
  329. }
  330. .info-card {
  331. background-color: #fff;
  332. border-radius: 16rpx;
  333. padding: 30rpx;
  334. margin-bottom: 20rpx;
  335. }
  336. .card-title {
  337. display: flex;
  338. justify-content: space-between;
  339. align-items: center;
  340. margin-bottom: 30rpx;
  341. font-size: 32rpx;
  342. font-weight: bold;
  343. }
  344. .more {
  345. color: #666;
  346. font-size: 24rpx;
  347. font-weight: normal;
  348. }
  349. .plot-info {
  350. display: flex;
  351. justify-content: space-around;
  352. }
  353. .plot-item {
  354. text-align: center;
  355. }
  356. .number {
  357. font-size: 36rpx;
  358. font-weight: bold;
  359. color: #4CAF50;
  360. display: block;
  361. }
  362. .label {
  363. font-size: 24rpx;
  364. color: #666;
  365. margin-top: 10rpx;
  366. display: block;
  367. }
  368. .service-grid {
  369. display: grid;
  370. grid-template-columns: repeat(4, 1fr);
  371. gap: 30rpx;
  372. padding: 10rpx 0;
  373. }
  374. .service-item {
  375. display: flex;
  376. flex-direction: column;
  377. align-items: center;
  378. }
  379. .service-icon {
  380. width: 88rpx;
  381. height: 88rpx;
  382. background-color: #E8F5E9;
  383. border-radius: 16rpx;
  384. display: flex;
  385. align-items: center;
  386. justify-content: center;
  387. margin-bottom: 16rpx;
  388. }
  389. .service-icon text {
  390. font-size: 32rpx;
  391. color: #4CAF50;
  392. font-weight: bold;
  393. }
  394. .icon-svg {
  395. width: 48rpx;
  396. height: 48rpx;
  397. display: block;
  398. }
  399. .service-name {
  400. font-size: 28rpx;
  401. color: #666;
  402. text-align: center;
  403. }
  404. .function-list {
  405. width: 100%;
  406. }
  407. .function-item {
  408. display: flex;
  409. justify-content: space-between;
  410. align-items: center;
  411. padding: 30rpx 0;
  412. border-bottom: 1rpx solid #f5f5f5;
  413. }
  414. .function-item:last-child {
  415. border-bottom: none;
  416. }
  417. .function-item .left {
  418. display: flex;
  419. align-items: center;
  420. }
  421. .function-icon {
  422. width: 44rpx;
  423. height: 44rpx;
  424. background-color: #E8F5E9;
  425. color: #4CAF50;
  426. font-size: 28rpx;
  427. font-weight: bold;
  428. border-radius: 8rpx;
  429. display: flex;
  430. align-items: center;
  431. justify-content: center;
  432. margin-right: 20rpx;
  433. }
  434. .function-item text {
  435. font-size: 28rpx;
  436. color: #333;
  437. }
  438. .arrow {
  439. color: #999;
  440. font-size: 24rpx;
  441. }
  442. .login-text {
  443. font-size: 32rpx;
  444. font-weight: bold;
  445. color: #fff;
  446. background-color: rgba(255, 255, 255, 0.2);
  447. padding: 10rpx 30rpx;
  448. border-radius: 30rpx;
  449. }
  450. </style>