visitor.vue 11 KB

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