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