visitor.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <!-- 访客记录 -->
  3. <view class="container">
  4. <template v-if="!isEmployeeLogin">
  5. <!-- 未登录状态 -->
  6. <view class="no-login">
  7. <!-- <image src="/static/images/no-login.png" mode="aspectFit" class="no-login-image"></image>-->
  8. <text class="no-login-text">请使用员工账号登录</text>
  9. <button class="login-btn" @click="goToLogin">授权登录</button>
  10. </view>
  11. </template>
  12. <template v-else>
  13. <!-- Visit list -->
  14. <scroll-view scroll-y class="visit-list" @scrolltolower="onReachBottom">
  15. <template v-if="visitList.length > 0">
  16. <view class="visit-item" v-for="(item, index) in visitList" :key="index" @click="goToDetail(item)">
  17. <view class="visit-date">
  18. <text class="date-num">{{item.date}}</text>
  19. <text class="date-month">{{item.year}}</text>
  20. </view>
  21. <view class="visit-content">
  22. <view class="visit-header">
  23. <view class="visitor-info">
  24. <text class="visitor-label">对接人 </text>
  25. <text class="visitor-name">{{item.employeeName}}</text>
  26. </view>
  27. <text class="visit-status" :class="getStatusClass(item.status)">{{item.status}}</text>
  28. </view>
  29. <view class="visit-desc">{{item.visitReason}}</view>
  30. <view class="visit-time">
  31. <u-icon name="clock" size="12" color="#999"></u-icon>
  32. <text>{{item.time}}</text>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- Loading more -->
  37. <u-loadmore :status="loadMoreStatus" />
  38. </template>
  39. <template v-else>
  40. <!-- 暂无数据 -->
  41. <view class="flex-items-plus">
  42. <image src="../../static/images/empty.png" class="empty "></image>
  43. </view>
  44. <view class="font28 font-gray flex-items-plus">
  45. 数据为空
  46. </view>
  47. </template>
  48. </scroll-view>
  49. </template>
  50. </view>
  51. </template>
  52. <script>
  53. import {
  54. mapGetters
  55. } from 'vuex';
  56. import {
  57. getVisitList,
  58. wxLogin
  59. } from '@/config/api.js';
  60. export default {
  61. data() {
  62. return {
  63. user: {},
  64. visitStatus: '0',
  65. visitList: [],
  66. loadMoreStatus: 'loadmore', // loadmore, loading, nomore
  67. params: {
  68. current: 1,
  69. size: 10000,
  70. },
  71. hasMore: true,
  72. isEmployeeLogin: false // 是否是员工登录
  73. };
  74. },
  75. computed: {
  76. ...mapGetters(['isLogin'])
  77. },
  78. onLoad() {
  79. this.checkEmployeeLogin();
  80. },
  81. onShow() {
  82. console.log('onShow-----', this.isEmployeeLogin);
  83. // 每次显示页面时检查登录状态
  84. this.visitList = []
  85. this.checkEmployeeLogin();
  86. if (this.isEmployeeLogin) {
  87. this.getList();
  88. }
  89. // 监听刷新列表事件
  90. uni.$on('refreshVisitList', () => {
  91. this.refresh();
  92. });
  93. },
  94. onUnload() {
  95. // 移除事件监听
  96. // uni.$off('refreshVisitList');
  97. },
  98. onPullDownRefresh() {
  99. this.refresh()
  100. uni.stopPullDownRefresh();
  101. },
  102. methods: {
  103. // 检查员工登录状态
  104. checkEmployeeLogin() {
  105. const user = uni.getStorageSync('user');
  106. console.log('检查员工登录状态,user:', user);
  107. if (user && Object.keys(user).length > 0) {
  108. // user 缓存有值,已登录
  109. this.isEmployeeLogin = true;
  110. this.user = user;
  111. } else {
  112. // user 缓存没有值,未登录
  113. this.isEmployeeLogin = false;
  114. this.user = {};
  115. }
  116. },
  117. // Refresh list
  118. refresh() {
  119. this.visitLi
  120. st = []
  121. // this.params.current = 1
  122. this.hasMore = true
  123. this.loadMoreStatus = 'loadmore'
  124. this.getList()
  125. },
  126. // Handle reaching bottom of scroll
  127. onReachBottom() {
  128. // if (!this.hasMore || this.loadMoreStatus === 'loading') return
  129. //
  130. // this.params.current++
  131. // this.getList(true)
  132. },
  133. // Get status class for styling
  134. getStatusClass(status) {
  135. const statusMap = {
  136. '待访问': 'status-pending-visit',
  137. '待审核': 'status-pending-review',
  138. '已拒绝': 'status-rejected'
  139. }
  140. return statusMap[status] || 'status-default'
  141. },
  142. // 获取访客列表
  143. async getList(loadMore = false) {
  144. if (!loadMore) {
  145. uni.showLoading({
  146. title: '加载中...'
  147. });
  148. }
  149. this.loadMoreStatus = 'loading'
  150. try {
  151. // 调用接口获取数据
  152. const user = uni.getStorageSync('user');
  153. let data = {}
  154. // if (user.userId=='1') {
  155. //
  156. // }else{
  157. // data.userId = user.userId
  158. // }
  159. // if (this.visitStatus) {
  160. //
  161. // }
  162. const response = await getVisitList(
  163. {
  164. userId: user.userId, // 从缓存获取用户ID
  165. // visitStatus: this.visitStatus // 传递状态参数
  166. }
  167. );
  168. console.log('访客列表接口返回:', response);
  169. if (response && response.code === 200) {
  170. // 处理返回的数据
  171. const statusMap = {
  172. '0': '待访问',
  173. '1': '已访问',
  174. '2': '待审核',
  175. '3': '已拒绝'
  176. };
  177. const list = response.rows.map(item => {
  178. // 解析创建时间
  179. const createTime = item.createTime || item.visitDate;
  180. return {
  181. id: item.id,
  182. employeeName: item.empName, // 对接人(员工姓名)
  183. visitReason: item.visitReson || item.visitReason, // 兼容字段拼写问题
  184. status: statusMap[item.visitStatus] || '未知', // 状态
  185. visitDate: item.visitDate, // 访问时间
  186. visitorName: item.visitPerson, // 访客姓名
  187. visDept: item.visDept, // 部门
  188. visitorPhone: item.visitorTel, // 访客电话
  189. visitorNum: item.visitorNum, // 访客人数
  190. accPerson: item.accPerson, // 随行人员(JSON字符串)
  191. createTime: createTime,
  192. // visitorCompany: item.visDept, // 访客单位
  193. // 格式化显示用的字段
  194. date: this.$u.timeFormat(createTime, 'dd'),
  195. year: this.$u.timeFormat(createTime, 'yyyy/mm'),
  196. time: this.$u.timeFormat(createTime, 'hh:MM')
  197. };
  198. });
  199. if (loadMore) {
  200. this.visitList = [...this.visitList, ...list];
  201. } else {
  202. this.visitList = list;
  203. }
  204. // 根据返回的数据判断是否还有更多数据
  205. // 这里简单处理,如果返回的数据少于每页数量,说明没有更多了
  206. this.hasMore = response.rows.length >= this.params.size;
  207. this.loadMoreStatus = this.hasMore ? 'loadmore' : 'nomore';
  208. } else {
  209. uni.showToast({
  210. title: response.msg || '获取数据失败',
  211. icon: 'none'
  212. });
  213. this.loadMoreStatus = 'nomore';
  214. }
  215. } catch (error) {
  216. console.error('获取访客列表失败:', error);
  217. uni.showToast({
  218. title: '获取数据失败',
  219. icon: 'none'
  220. });
  221. this.loadMoreStatus = 'nomore';
  222. } finally {
  223. if (!loadMore) {
  224. uni.hideLoading();
  225. }
  226. }
  227. },
  228. // 跳转到详情页面
  229. goToDetail(item) {
  230. // 将完整对象数据存储到缓存,用于详情页读取
  231. uni.setStorageSync('visitDetailData', item);
  232. uni.navigateTo({
  233. url: `/pagesA/task/detail?id=${item.id}`
  234. });
  235. },
  236. // 跳转到登录页面
  237. async goToLogin() {
  238. try {
  239. // 1. 尝试微信授权登录
  240. const loginRes = await new Promise((resolve, reject) => {
  241. uni.login({
  242. provider: 'weixin',
  243. success: (res) => {
  244. if (res.code) {
  245. resolve(res); // 成功获取 code
  246. } else {
  247. reject(new Error('未获取到微信授权码'));
  248. }
  249. },
  250. fail: (err) => {
  251. reject(new Error('微信授权失败'));
  252. }
  253. });
  254. });
  255. // 2. 调用微信登录接口
  256. const wxRes = await wxLogin({
  257. code: loginRes.code
  258. });
  259. console.log('微信登录成功:', wxRes);
  260. if (wxRes.openid) {
  261. uni.setStorageSync('openid', wxRes.openid);
  262. }
  263. // 3. 登录成功后的逻辑(如存储用户信息、跳转首页等)
  264. if (wxRes && wxRes.message === "登录成功" && wxRes.user) {
  265. uni.setStorageSync('user', wxRes.data);
  266. if (wxRes.user) {
  267. uni.setStorageSync('user', wxRes.user);
  268. }
  269. // 更新store状态
  270. this.$store.commit('isLogin', true);
  271. this.isEmployeeLogin = true
  272. this.getList(true)
  273. // uni.switchTab({
  274. // url: '/pages/tabbar/home' // 替换为实际首页路径
  275. // });
  276. } else {
  277. throw new Error('微信登录接口返回异常');
  278. }
  279. } catch (error) {
  280. console.error('登录失败:', error);
  281. // 4. 微信授权或登录失败,跳转到手机号登录页
  282. uni.navigateTo({
  283. url: '/pagesA/public/phone-login'
  284. });
  285. }
  286. }
  287. }
  288. };
  289. </script>
  290. <style lang="scss" scoped>
  291. .container {
  292. min-height: 100vh;
  293. background-color: #f5f5f5;
  294. .no-login {
  295. display: flex;
  296. flex-direction: column;
  297. align-items: center;
  298. justify-content: center;
  299. padding-top: 30vh;
  300. .no-login-image {
  301. width: 240rpx;
  302. height: 240rpx;
  303. margin-bottom: 40rpx;
  304. }
  305. .no-login-text {
  306. font-size: 32rpx;
  307. color: #999;
  308. margin-bottom: 60rpx;
  309. }
  310. .login-btn {
  311. width: 320rpx;
  312. height: 88rpx;
  313. background: #FF6B00;
  314. border-radius: 44rpx;
  315. color: #fff;
  316. font-size: 32rpx;
  317. font-weight: 500;
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. }
  322. }
  323. }
  324. .visit-list {
  325. height: 100vh;
  326. }
  327. .visit-item {
  328. background-color: #fff;
  329. margin: 16rpx 20rpx;
  330. border-radius: 16rpx;
  331. padding: 24rpx;
  332. display: flex;
  333. align-items: flex-start;
  334. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  335. }
  336. .visit-date {
  337. display: flex;
  338. flex-direction: column;
  339. align-items: center;
  340. margin-right: 24rpx;
  341. min-width: 80rpx;
  342. .date-num {
  343. font-size: 48rpx;
  344. font-weight: bold;
  345. color: #333;
  346. line-height: 1;
  347. }
  348. .date-month {
  349. font-size: 24rpx;
  350. color: #999;
  351. margin-top: 4rpx;
  352. }
  353. }
  354. .visit-content {
  355. flex: 1;
  356. .visit-header {
  357. display: flex;
  358. justify-content: space-between;
  359. align-items: center;
  360. margin-bottom: 12rpx;
  361. .visitor-info {
  362. display: flex;
  363. align-items: center;
  364. .visitor-label {
  365. margin-right: 10rpx;
  366. font-size: 32rpx;
  367. color: #333;
  368. font-weight: normal;
  369. }
  370. .visitor-name {
  371. font-size: 36rpx;
  372. color: #333;
  373. font-weight: bold;
  374. }
  375. }
  376. .visit-status {
  377. font-size: 28rpx;
  378. padding: 4rpx 12rpx;
  379. border-radius: 12rpx;
  380. &.status-pending-visit {
  381. color: #4CAF50;
  382. background-color: rgba(76, 175, 80, 0.1);
  383. }
  384. &.status-pending-review {
  385. color: #2196F3;
  386. background-color: rgba(33, 150, 243, 0.1);
  387. }
  388. &.status-rejected {
  389. color: #F44336;
  390. background-color: rgba(244, 67, 54, 0.1);
  391. }
  392. }
  393. }
  394. .visit-desc {
  395. color: #666;
  396. font-size: 28rpx;
  397. line-height: 1.4;
  398. margin-bottom: 12rpx;
  399. }
  400. .visit-time {
  401. display: flex;
  402. align-items: center;
  403. gap: 8rpx;
  404. text {
  405. color: #999;
  406. font-size: 24rpx;
  407. }
  408. }
  409. }
  410. </style>