123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- const { museumApi } = require('../../../utils/api.js');
- Page({
- data: {
- showAddForm: false,
- contactList: [],
- newVisitor: {
- name: '',
- phone: '',
- idType: '身份证',
- idCard: '',
- nameError: '',
- phoneError: '',
- idNumberError: ''
- },
- idTypes: ['身份证']
- },
- onLoad() {
- this.fetchContactList();
- },
- onPullDownRefresh() {
- this.fetchContactList().then(() => {
- wx.stopPullDownRefresh();
- });
- },
- // 显示新增参观人表单
- showAddVisitor() {
- this.setData({ showAddForm: true });
- this.resetForm();
- },
- // 隐藏新增表单
- hideAddForm() {
- this.setData({ showAddForm: false });
- },
- // 重置表单
- resetForm() {
- this.setData({
- newVisitor: {
- name: '',
- phone: '',
- idType: '身份证',
- idCard: '',
- nameError: '',
- phoneError: '',
- idNumberError: ''
- }
- });
- },
- // 表单输入处理
- onNameInput(e) {
- this.setData({
- 'newVisitor.name': e.detail.value,
- 'newVisitor.nameError': ''
- });
- },
- onPhoneInput(e) {
- this.setData({
- 'newVisitor.phone': e.detail.value,
- 'newVisitor.phoneError': ''
- });
- },
- onIdNumberInput(e) {
- this.setData({
- 'newVisitor.idCard': e.detail.value,
- 'newVisitor.idNumberError': ''
- });
- },
- onIdTypeChange(e) {
- this.setData({
- 'newVisitor.idType': this.data.idTypes[e.detail.value]
- });
- },
- // 验证姓名
- validateName() {
- const name = this.data.newVisitor.name.trim();
- if (!name) {
- this.setData({ 'newVisitor.nameError': '请输入姓名' });
- return false;
- }
- if (!/^[\u4e00-\u9fa5a-zA-Z]+$/.test(name)) {
- this.setData({ 'newVisitor.nameError': '姓名只能包含中文和英文字母' });
- return false;
- }
- this.setData({ 'newVisitor.nameError': '' });
- return true;
- },
- // 验证电话号码
- validatePhone() {
- const phone = this.data.newVisitor.phone.trim();
- if (!phone) {
- this.setData({ 'newVisitor.phoneError': '请输入电话号码' });
- return false;
- }
- if (!/^1[3-9]\d{9}$/.test(phone)) {
- this.setData({ 'newVisitor.phoneError': '请输入正确的11位手机号码' });
- return false;
- }
- this.setData({ 'newVisitor.phoneError': '' });
- return true;
- },
- // 验证身份证号码
- validateIdNumber() {
- const idCard = this.data.newVisitor.idCard.trim();
- if (!idCard) {
- this.setData({ 'newVisitor.idNumberError': '请输入证件号码' });
- return false;
- }
- if (!/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(idCard)) {
- this.setData({ 'newVisitor.idNumberError': '请输入正确的18位身份证号码' });
- return false;
- }
- this.setData({ 'newVisitor.idNumberError': '' });
- return true;
- },
- // 确认添加
- confirmAdd() {
- // 验证所有字段
- const isNameValid = this.validateName();
- const isPhoneValid = this.validatePhone();
- const isIdNumberValid = this.validateIdNumber();
- if (!isNameValid || !isPhoneValid || !isIdNumberValid) {
- return;
- }
- // 显示加载提示
- wx.showLoading({
- title: '添加中...'
- });
- this.addContactToServer(this.data.newVisitor)
- .then(() => {
- wx.showToast({
- title: '添加成功',
- icon: 'success'
- });
- // 添加成功后回到联系人列表并刷新数据
- this.setData({ showAddForm: false });
- this.fetchContactList();
- wx.hideLoading();
- })
- .catch(error => {
- wx.hideLoading();
- console.error('添加联系人失败:', error);
- wx.showToast({
- title: '添加失败,请重试',
- icon: 'none'
- });
- });
- },
- // 删除联系人
- editContact(e) {
- const index = e.currentTarget.dataset.index;
- const contact = this.data.contactList[index];
-
- wx.showModal({
- title: '确认删除',
- content: `确定要删除联系人"${contact.name}"吗?`,
- success: (res) => {
- if (res.confirm) {
- this.deleteContactFromServer(contact.id || contact.visitorId, index);
- }
- }
- });
- },
- // API调用 - 删除联系人
- deleteContactFromServer(id, index) {
-
- wx.showLoading({
- title: '删除中...'
- });
-
- museumApi.deleteVisitor(id)
- .then(() => {
- wx.hideLoading();
- wx.showToast({
- title: '删除成功',
- icon: 'success'
- });
- // 删除成功后刷新列表
- this.fetchContactList();
- })
- .catch(error => {
- wx.hideLoading();
- console.error('删除联系人失败:', error);
- wx.showToast({
- title: '删除失败,请重试',
- icon: 'none'
- });
- });
- },
- // API调用 - 添加联系人
- addContactToServer(contact) {
-
- // 构造API参数
- const params = {
- cardType: contact.idType === '身份证' ? '1' : '1', // 暂时都设为身份证类型
- id: 0, // 新增时为0
- idCard: contact.idCard,
- name: contact.name,
- phone: contact.phone
- };
-
- return museumApi.addVisitor(params);
- },
- // 获取联系人列表
- fetchContactList() {
- return museumApi.getMyVisitors()
- .then(response => {
- console.log('获取参观人列表成功:', response);
- // 如果没有数据,使用模拟数据
- const contactList = response || [];
- this.setData({ contactList });
- })
- .catch(error => {
- console.error('获取参观人列表失败:', error);
- // 使用模拟数据
- this.setData({ contactList: this.getMockContactList() });
- });
- },
- });
|