active-people.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // 调用预约API
  2. const { museumApi } = require('../../../utils/api.js');
  3. Page({
  4. data: {
  5. selectedDate: '',
  6. title: '', // 活动标题
  7. activityId: 0, // 活动ID,type=2时需要
  8. type: 1, // 预约类型:1-普通预约,2-活动预约
  9. visitors: [], // 参观人列表
  10. showExistingPage: false,
  11. showSuccessModal: false,
  12. isFormValid: false,
  13. idTypes: [{value: 1, name: '身份证'}],
  14. existingVisitors: [] // 我的联系人列表
  15. },
  16. onLoad(options) {
  17. this.addNewVisitor();
  18. // 获取页面参数
  19. if (options.date) {
  20. this.setData({
  21. selectedDate: options.date
  22. });
  23. }
  24. // 获取活动标题
  25. if (options.title) {
  26. this.setData({
  27. title: decodeURIComponent(options.title)
  28. });
  29. }
  30. // 保存activityId和type用于后续提交
  31. if (options.activityId) {
  32. this.setData({
  33. activityId: parseInt(options.activityId)
  34. });
  35. }
  36. if (options.type) {
  37. this.setData({
  38. type: parseInt(options.type)
  39. });
  40. }
  41. // 加载已有参观人数据
  42. this.loadExistingVisitors();
  43. },
  44. // 新增参观人
  45. addNewVisitor() {
  46. const visitors = this.data.visitors;
  47. visitors.unshift({
  48. id: 0, // 新增参观人ID为0
  49. name: '',
  50. phone: '',
  51. cardType: this.data.idTypes[0].value, // 证件类型
  52. idCard: '', // 证件号码
  53. idType: this.data.idTypes[0].name, // 用于显示
  54. idTypeIndex: 0,
  55. idCard: '', // 用于显示
  56. nameError: '',
  57. phoneError: '',
  58. idNumberError: '',
  59. isFromContacts: false // 标识是否来自联系人
  60. });
  61. this.setData({
  62. visitors: visitors
  63. });
  64. this.checkFormValid();
  65. },
  66. // 删除参观人
  67. removeVisitor(e) {
  68. const index = e.currentTarget.dataset.index;
  69. const visitors = this.data.visitors;
  70. visitors.splice(index, 1);
  71. this.setData({
  72. visitors: visitors
  73. });
  74. this.checkFormValid();
  75. },
  76. // 姓名输入
  77. onNameInput(e) {
  78. const index = e.currentTarget.dataset.index;
  79. const value = e.detail.value;
  80. const visitors = this.data.visitors;
  81. visitors[index].name = value;
  82. this.setData({
  83. visitors: visitors
  84. });
  85. },
  86. // 电话输入
  87. onPhoneInput(e) {
  88. const index = e.currentTarget.dataset.index;
  89. const value = e.detail.value;
  90. const visitors = this.data.visitors;
  91. visitors[index].phone = value;
  92. this.setData({
  93. visitors: visitors
  94. });
  95. },
  96. // 证件号码输入
  97. onIdNumberInput(e) {
  98. const index = e.currentTarget.dataset.index;
  99. const value = e.detail.value;
  100. const visitors = this.data.visitors;
  101. visitors[index].idCard = value;
  102. visitors[index].idCard = value; // 同步更新API字段
  103. this.setData({
  104. visitors: visitors
  105. });
  106. },
  107. // 证件类型选择
  108. onIdTypeChange(e) {
  109. const index = e.currentTarget.dataset.index;
  110. const value = e.detail.value;
  111. const visitors = this.data.visitors;
  112. const selectedIdType = this.data.idTypes[value];
  113. visitors[index].idTypeIndex = value;
  114. visitors[index].idType = selectedIdType.name;
  115. visitors[index].cardType = selectedIdType.value; // 同步更新API字段
  116. this.setData({
  117. visitors: visitors
  118. });
  119. },
  120. // 验证姓名
  121. validateName(e) {
  122. const index = e.currentTarget.dataset.index;
  123. const visitors = this.data.visitors;
  124. const visitor = visitors[index];
  125. if (!visitor.name.trim()) {
  126. visitor.nameError = '请输入姓名';
  127. } else if (visitor.name.length < 2) {
  128. visitor.nameError = '姓名至少2个字符';
  129. } else {
  130. visitor.nameError = '';
  131. }
  132. this.setData({
  133. visitors: visitors
  134. });
  135. this.checkFormValid();
  136. },
  137. // 验证电话
  138. validatePhone(e) {
  139. const index = e.currentTarget.dataset.index;
  140. const visitors = this.data.visitors;
  141. const visitor = visitors[index];
  142. const phoneRegex = /^1[3-9]\d{9}$/;
  143. if (!visitor.phone) {
  144. visitor.phoneError = '请输入电话号码';
  145. } else if (!phoneRegex.test(visitor.phone)) {
  146. visitor.phoneError = '请输入正确的11位手机号码';
  147. } else {
  148. visitor.phoneError = '';
  149. }
  150. this.setData({
  151. visitors: visitors
  152. });
  153. this.checkFormValid();
  154. },
  155. // 验证证件号码
  156. validateIdNumber(e) {
  157. const index = e.currentTarget.dataset.index;
  158. const visitors = this.data.visitors;
  159. const visitor = visitors[index];
  160. const idRegex = /^[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]$/;
  161. if (!visitor.idCard) {
  162. visitor.idNumberError = '请输入证件号码';
  163. } else if (!idRegex.test(visitor.idCard)) {
  164. visitor.idNumberError = '请输入正确的18位身份证号码';
  165. } else {
  166. visitor.idNumberError = '';
  167. }
  168. this.setData({
  169. visitors: visitors
  170. });
  171. this.checkFormValid();
  172. },
  173. // 检查表单是否有效
  174. checkFormValid() {
  175. const visitors = this.data.visitors;
  176. if (visitors.length === 0) {
  177. this.setData({ isFormValid: false });
  178. return;
  179. }
  180. const isValid = visitors.every(visitor =>
  181. visitor.name &&
  182. visitor.phone &&
  183. visitor.idCard &&
  184. !visitor.nameError &&
  185. !visitor.phoneError &&
  186. !visitor.idNumberError
  187. );
  188. this.setData({ isFormValid: isValid });
  189. },
  190. // 加载已有参观人数据
  191. loadExistingVisitors() {
  192. museumApi.getMyVisitors()
  193. .then(response => {
  194. console.log('获取参观人列表成功:', response);
  195. if (response && response.length > 0) {
  196. const existingVisitors = response.map(visitor => ({
  197. id: visitor.id,
  198. name: visitor.name,
  199. idCard: visitor.idCard,
  200. phone: visitor.phone,
  201. selected: false
  202. }));
  203. this.setData({ existingVisitors });
  204. }
  205. })
  206. .catch(error => {
  207. console.error('获取参观人列表失败:', error);
  208. // 如果接口失败,可以使用模拟数据
  209. this.setData({
  210. existingVisitors: [
  211. {
  212. name: '周明明',
  213. idCard: '210112196705041430',
  214. phone: '18416573665',
  215. selected: false
  216. },
  217. {
  218. name: '李明',
  219. idCard: '621124199504251508',
  220. phone: '13902376115',
  221. selected: false
  222. }
  223. ]
  224. });
  225. });
  226. },
  227. // 显示已有参观人
  228. showExistingVisitors() {
  229. // 每次显示前重新加载数据
  230. this.loadExistingVisitors();
  231. this.setData({
  232. showExistingPage: true
  233. });
  234. },
  235. // 切换已有参观人选择状态
  236. toggleExistingVisitor(e) {
  237. const index = e.currentTarget.dataset.index;
  238. const existingVisitors = this.data.existingVisitors;
  239. existingVisitors[index].selected = !existingVisitors[index].selected;
  240. this.setData({
  241. existingVisitors: existingVisitors
  242. });
  243. },
  244. // 确认选择已有参观人
  245. confirmExistingVisitors() {
  246. const selectedVisitors = this.data.existingVisitors.filter(visitor => visitor.selected);
  247. const visitors = this.data.visitors;
  248. selectedVisitors.forEach(existingVisitor => {
  249. // if (visitors.length < 5) {
  250. visitors.unshift({
  251. id: existingVisitor.id, // 使用联系人的真实ID
  252. name: existingVisitor.name,
  253. phone: existingVisitor.phone,
  254. cardType: this.data.idTypes[0].value, // 证件类型
  255. idCard: existingVisitor.idCard, // 证件号码
  256. idType: this.data.idTypes[0].name, // 用于显示
  257. idTypeIndex: 0,
  258. idCard: existingVisitor.idCard, // 用于显示
  259. nameError: '',
  260. phoneError: '',
  261. idNumberError: '',
  262. isFromContacts: true // 标识来自联系人
  263. });
  264. // }
  265. });
  266. // 重置选择状态
  267. const existingVisitors = this.data.existingVisitors;
  268. existingVisitors.forEach(visitor => {
  269. visitor.selected = false;
  270. });
  271. this.setData({
  272. visitors: visitors,
  273. existingVisitors: existingVisitors,
  274. showExistingPage: false
  275. });
  276. this.checkFormValid();
  277. },
  278. // 提交预约
  279. submitReservation() {
  280. if (!this.data.isFormValid) {
  281. wx.showToast({
  282. title: '请完善所有参观人信息',
  283. icon: 'none'
  284. });
  285. return;
  286. }
  287. wx.showLoading({
  288. title: '提交中...',
  289. mask: true
  290. });
  291. // 构建API参数
  292. const requestData = {
  293. activityId: this.data.activityId || 0,
  294. appointmentSlotsId: this.data.appointmentSlotsId || 0,
  295. appointmentTime: this.data.selectedDate,
  296. type: this.data.type,
  297. visitors: this.data.visitors.map(visitor => ({
  298. cardType: this.data.idTypes[visitor.idTypeIndex].value,
  299. id: visitor.id, // 新增参观人为0,联系人为真实ID
  300. idCard: visitor.idCard,
  301. name: visitor.name,
  302. phone: visitor.phone
  303. }))
  304. };
  305. console.log('提交预约参数:', requestData);
  306. museumApi.submitReservation(requestData)
  307. .then(response => {
  308. console.log('预约提交成功:', response);
  309. this.setData({
  310. showSuccessModal: true
  311. });
  312. wx.hideLoading();
  313. })
  314. .catch(error => {
  315. wx.hideLoading();
  316. console.error('预约提交失败:', error);
  317. wx.showToast({
  318. title: error.message || '预约失败,请重试',
  319. icon: 'none'
  320. });
  321. });
  322. },
  323. // 关闭成功弹窗
  324. closeSuccessModal() {
  325. this.setData({
  326. showSuccessModal: false
  327. });
  328. // 跳转到我的预约页面
  329. wx.navigateTo({
  330. url: '/pages/user/my-preview/index'
  331. })
  332. },
  333. // 阻止事件冒泡
  334. stopPropagation() {
  335. // 空函数,用于阻止事件冒泡
  336. }
  337. });