visitor.vue 11 KB

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