expansion.vue 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
<template>
	<view class="container">

		<view class="form-card">
			<view class="form-content">
				<u-form :model="formData" ref="form" :rules="rules" @submit="handleFormSubmit" labelPosition="top"
					labelWidth="200">
					<!-- 拓客活动 -->
					<u-form-item label="拓客活动" prop="eventId" required>
						<u-picker :show="showEventPicker" :columns="[eventList]" @confirm="onEventConfirm"
							@cancel="showEventPicker = false" @close="showEventPicker = false"></u-picker>
						<view class="picker-trigger" @click="showEventPicker = true">
							<text class="picker-text">{{ selectedEventName || '请选择拓客活动' }}</text>
							<u-icon name="arrow-down" size="16" color="#6a9c6a"></u-icon>
						</view>
					</u-form-item>

					<!-- 顾客姓名 -->
					<u-form-item label="顾客姓名" prop="customerName" required>
						<u-input v-model="formData.customerName" placeholder="请输入顾客姓名" border="none"
							:customStyle="inputStyle"></u-input>
					</u-form-item>

					<!-- 电话号码 -->
					<u-form-item label="电话号码" prop="customerPhone" required>
						<u-input v-model="formData.customerPhone" placeholder="请输入电话号码" type="number" border="none"
							:customStyle="inputStyle"></u-input>
					</u-form-item>

					<!-- 支付方式 -->
					<u-form-item label="支付方式" prop="paymentMethod" required>
						<u-picker :show="showPaymentPicker" :columns="[paymentOptions]" @confirm="onPaymentConfirm"
							@cancel="showPaymentPicker = false" @close="showPaymentPicker = false"></u-picker>
						<view class="picker-trigger" @click="showPaymentPicker = true">
							<text class="picker-text">{{ formData.paymentMethod || '请选择支付方式' }}</text>
							<u-icon name="arrow-down" size="16" color="#6a9c6a"></u-icon>
						</view>
					</u-form-item>

					<!-- 是否加微信 -->
					<u-form-item label="是否加微信" prop="isAddWeChat" required>
						<u-picker :show="showWeChatPicker" :columns="[weChatOptions]" @confirm="onWeChatConfirm"
							@cancel="showWeChatPicker = false" @close="showWeChatPicker = false"></u-picker>
						<view class="picker-trigger" @click="showWeChatPicker = true">
							<text class="picker-text">{{ formData.isAddWeChat || '请选择是否加微信' }}</text>
							<u-icon name="arrow-down" size="16" color="#6a9c6a"></u-icon>
						</view>
					</u-form-item>

					<!-- 备注 -->
					<u-form-item label="备注" prop="remarks">
						<u-textarea v-model="formData.remarks" placeholder="请输入备注信息" border="none"
							:customStyle="textareaStyle" :height="120"></u-textarea>
					</u-form-item>

					<!-- 提交按钮 -->
					<u-button type="primary" :loading="loading" :disabled="loading"
						@click="loading?null:handleFormSubmit()" :customStyle="buttonStyle">
						{{ loading ? '提交中...' : '保存' }}
					</u-button>
				</u-form>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				loading: false,
				userInfo: null,
				currentEventId: null,
				eventList: [],
				formData: {
					eventId: '',
					customerName: '',
					customerPhone: '',
					paymentMethod: '微信',
					isAddWeChat: '否',
					remarks: ''
				},
				// 选择器相关
				showEventPicker: false,
				showPaymentPicker: false,
				showWeChatPicker: false,
				selectedEventName: '',
				// 选择器选项
				paymentOptions: [{
						text: '微信',
						value: '微信'
					},
					{
						text: '支付宝',
						value: '支付宝'
					},
					{
						text: '现金',
						value: '现金'
					},
					{
						text: '银行转账',
						value: '银行转账'
					}
				],
				weChatOptions: [{
						text: '是',
						value: '是'
					},
					{
						text: '否',
						value: '否'
					}
				],
				// 表单验证规则
				rules: {
					eventId: [{
						required: true,
						message: '请选择拓客活动',
						trigger: 'change'
					}],
					customerName: [{
						required: true,
						message: '请输入顾客姓名',
						trigger: 'blur'
					}],
					customerPhone: [
						{
							required: true,
							message: '请输入电话号码',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								const phone = String(value || '').trim()
								if (!phone) {
									callback(new Error('请输入电话号码'))
								} else if (!/^1[3-9]\d{9}$/.test(phone)) {
									callback(new Error('请输入正确的手机号码'))
								} else {
									callback()
								}
							},
							trigger: ['blur', 'change']
						}
					],
					paymentMethod: [{
						required: true,
						message: '请选择支付方式',
						trigger: 'change'
					}],
					isAddWeChat: [{
						required: true,
						message: '请选择是否加微信',
						trigger: 'change'
					}]
				},
				// 样式配置
				inputStyle: {
					backgroundColor: '#f9fff9',
					border: '3rpx solid #c8e6c9',
					borderRadius: '20rpx',
					padding: '24rpx',
					fontSize: '28rpx',
					color: '#2e7d32'
				},
				textareaStyle: {
					backgroundColor: '#f9fff9',
					border: '3rpx solid #c8e6c9',
					borderRadius: '20rpx',
					padding: '32rpx',
					fontSize: '28rpx',
					color: '#2e7d32'
				},
				buttonStyle: {
					width: '100%',
					height: '80rpx',
					borderRadius: '24rpx',
					fontSize: '32rpx',
					fontWeight: '600',
					letterSpacing: '2rpx',
					background: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
					boxShadow: '0 8rpx 32rpx rgba(67, 233, 123, 0.3)',
					marginTop: '64rpx',
					borderColor: 'rgba(67, 233, 123, 0.3)'
				}
			}
		},

		onLoad() {
			this.initializePage()
		},

		methods: {
			// 初始化页面
			async initializePage() {
				try {
					// 获取当前登录用户
					this.userInfo = uni.getStorageSync('userInfo')
					if (!this.userInfo || Object.keys(this.userInfo).length === 0) {
						uni.reLaunch({
							url: '/pages/login/login'
						})
						return
					}
					console.log('用户信息:', this.userInfo)

					// 设置默认时间
					this.setDefaultDateTime()

					// 获取当前活动
					await this.getCurrentEvent()
				} catch (error) {
					console.error('页面初始化失败:', error)
					uni.showToast({
						title: '页面初始化失败,请刷新重试',
						icon: 'none',
						duration: 3000
					})
				}
			},

			// 设置默认时间
			setDefaultDateTime() {
				// 设置默认支付方式
				this.formData.paymentMethod = '微信'
				// 设置默认是否加微信
				this.formData.isAddWeChat = '否'
			},

			// 获取当前活动
			async getCurrentEvent() {
				try {
					const res = await this.API.getCurrentEvent(this.userInfo.userId)
					if (res.code === 200 && res.data && res.data.length > 0) {
						// 填充活动下拉选项,适配 u-picker 格式
						this.eventList = res.data.map(event => ({
							text: event.EventName,
							value: event.EventId
						}))

						// 默认选择第一个活动
						if (res.data.length > 0) {
							this.currentEventId = res.data[0].EventId
							this.formData.eventId = this.currentEventId
							this.selectedEventName = res.data[0].EventName
							console.log('当前活动ID:', this.currentEventId)
						}
					} else {
						console.warn('未找到当前活动')
					}
				} catch (error) {
					console.error('获取当前活动失败:', error)
				}
			},

			// 表单验证
			async validateForm() {
				const errors = []

				// 验证必填字段
				if (!this.formData.eventId) {
					errors.push('请选择拓客活动')
				}

				if (!this.formData.customerName.trim()) {
					errors.push('请输入顾客姓名')
				}

				const phoneNumber = this.formData.customerPhone.trim()
				if (!phoneNumber) {
					errors.push('请输入电话号码')
				} else {
					// 验证手机号格式
					const phoneRegex = /^1[3-9]\d{9}$/
					if (!phoneRegex.test(phoneNumber)) {
						errors.push('请输入正确的手机号码')
					}
				}

				if (!this.formData.paymentMethod) {
					errors.push('请选择支付方式')
				}
				if (!this.formData.isAddWeChat) {
					errors.push('请选择是否加微信')
				}

				return errors
			},

			// 收集表单数据
			collectFormData() {
				return {
					expansionTime: null,
					customerName: this.formData.customerName.trim(),
					customerPhone: this.formData.customerPhone.trim(),
					buyNumber: 1,
					paymentMethod: this.formData.paymentMethod,
					isAddWeChat: this.formData.isAddWeChat,
					remarks: this.formData.remarks.trim(),
					expansionUserId: this.userInfo.userId,
					eventId: this.formData.eventId
				}
			},

			// 选择器确认方法
			onEventConfirm(e) {
				const {
					value
				} = e
				this.formData.eventId = value[0].value
				this.selectedEventName = value[0].text
				this.showEventPicker = false
			},

			onPaymentConfirm(e) {
				const {
					value
				} = e
				this.formData.paymentMethod = value[0].value
				this.showPaymentPicker = false
			},

			onWeChatConfirm(e) {
				const {
					value
				} = e
				this.formData.isAddWeChat = value[0].value
				this.showWeChatPicker = false
			},

			// 处理表单提交
			async handleFormSubmit() {
				try {
					// 先走 uview 自带必填等校验
					await this.$refs.form.validate()

					// 再做一层自定义业务校验(尤其是手机号)
					const errors = await this.validateForm()
					if (errors && errors.length > 0) {
						uni.showToast({
							title: errors[0],
							icon: 'none',
							duration: 2000
						})
						return
					}

					// 收集表单数据
					const formData = this.collectFormData()
					console.log('表单数据:', formData)

					await this.submitExpansion(formData)
				} catch (error) {
					console.log('表单验证失败:', error)
				}
			},

			// 提交扩展邀约数据
			async submitExpansion(data) {
				try {
					uni.showLoading({
						title: '正在提交...'
					})
					this.loading = true
					const res1 = await this.API.getEvent(data.eventId)
					let pushUrl = res1.data && res1.data.pushUrl ? res1.data.pushUrl : ''
					// console.error(pushUrl)
					// return
					const res = await this.API.submitExpansion(data)
					// console.error(res)
					// return
					if (res.code === 200 && res.data && res.data.entity) {
						let tkinfo = res.data

						// return
						// 发送微信通知
						await this.sendWeChatNotification(tkinfo, data, pushUrl)

					} else {
						uni.hideLoading()
						uni.showToast({
							title: `添加失败:${res.data?.Message || '未知错误'}`,
							icon: 'none',
							duration: 3000
						})
						this.loading = false
					}
				} catch (error) {
					console.error('提交失败:', error)
					uni.hideLoading()
					uni.showToast({
						title: '网络错误,请稍后重试',
						icon: 'none',
						duration: 3000
					})
					this.loading = false
				}
			},

			// 发送微信通知
			async sendWeChatNotification(tkinfo, data, pushUrl) {
				try {
					// 格式化时间
					const formatTime = (timestamp) => {
						const date = new Date(timestamp)
						return uni.$u.timeFormat(date, 'yyyy/mm/dd hh:MM')
					}

					const datanew = {
						"webhookUrl": pushUrl ||
							'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=581c22a6-cb67-42e5-8c76-b8e90052e188',
						// "webhookUrl": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=496f1add-122b-43fc-9e38-0ca79c48b33f",
						// 测试地址
						// "webhookUrl":"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6f8686ec-5011-4c1d-bae9-d82a2a2f4d83",
						"content": `🎉 拓客记录
⏩门店:${tkinfo.storeinfo?.Dm || '未知'}
⏩拓客人员:${this.userInfo.userName || '未知'}
⏩战队:${tkinfo.entity?.TeamName || '未知'}
⏩顾客姓名:${data.customerName}
⏩电话号码:${data.customerPhone}
⏩购买张数:${data.buyNumber}
⏩拓客时间:${formatTime(tkinfo.entity?.ExpansionTime)}
⏩支付方式:${data.paymentMethod}
⏩是否加微信:${data.isAddWeChat}
⏩备注:${data.remarks || ''}`
					}

					// 发送通知
					await this.API.sendWeChatNotification(datanew)
					uni.hideLoading()
					uni.showToast({
						title: '拓客数据提交成功!',
						icon: 'success',
						duration: 2000
					})
					this.loading = false
					// 清空表单
					this.clearForm()
				} catch (error) {
					console.error('发送微信通知失败:', error)
				}
			},

			// 清空表单
			clearForm() {
				try {
					// 重置表单
					this.formData = {
						eventId: this.currentEventId || '',
						customerName: '',
						customerPhone: '',
						paymentMethod: '微信',
						isAddWeChat: '否',
						remarks: ''
					}

					// 重置选择器显示名称
					if (this.currentEventId && this.eventList.length > 0) {
						const currentEvent = this.eventList.find(event => event.value === this.currentEventId)
						this.selectedEventName = currentEvent ? currentEvent.text : ''
					} else {
						this.selectedEventName = ''
					}

					// 清除表单验证
					this.$refs.form && this.$refs.form.clearValidate()

					console.log('表单已清空')
				} catch (error) {
					console.error('清空表单失败:', error)
				}
			},


		}
	}
</script>

<style lang="scss" scoped>
	*,
	*::before,
	*::after {
		box-sizing: border-box;
	}

	.container {
		font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
		margin: 0;
		height: 100vh;
		background: linear-gradient(135deg, #e8f5e9 0%, #b2dfdb 100%);
		padding: 40rpx;
		width: 100%;
		box-sizing: border-box;
		overflow-y: scroll;
	}

	.page-title {
		text-align: center;
		color: #388e3c;
		margin-bottom: 36rpx;
		letter-spacing: 4rpx;
		font-size: 36rpx;
		font-weight: bold;
	}

	.form-card {
		background: #fff;
		border-radius: 40rpx;
		box-shadow: 0 16rpx 64rpx 0 rgba(76, 175, 80, 0.15);
		border: 2rpx solid #e8f5e9;
		overflow: hidden;
	}

	.form-content {
		padding: 64rpx;
		box-sizing: border-box;
	}

	.form-group {
		margin-bottom: 48rpx;
	}

	.form-group:last-child {
		margin-bottom: 0;
	}

	label {
		display: block;
		margin-bottom: 20rpx;
		font-weight: 600;
		color: #2e7d32;
		letter-spacing: 1rpx;
		font-size: 28rpx;
	}

	.input-wrapper {
		position: relative;
	}

	.input-icon {
		position: absolute;
		left: 24rpx;
		top: 50%;
		transform: translateY(-50%);
		width: 40rpx;
		height: 40rpx;
		color: #6a9c6a;
	}

	input,
	textarea,
	select,
	.form-select {
		width: 100%;
		padding: 24rpx;
		border: 3rpx solid #c8e6c9;
		border-radius: 20rpx;
		font-size: 28rpx;
		transition: all 0.2s ease;
		background: #f9fff9;
		color: #2e7d32;
		font-family: inherit;
		box-sizing: border-box;
	}

	textarea {
		min-height: 200rpx;
		resize: vertical;
		padding: 32rpx;
		line-height: 1.5;
	}

	select {
		cursor: pointer;
		background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236a9c6a' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
		background-position: right 24rpx center;
		background-repeat: no-repeat;
		background-size: 32rpx;
		padding-right: 80rpx;
		appearance: none;
		-webkit-appearance: none;
		-moz-appearance: none;
	}

	select:focus {
		outline: none;
		border-color: #43a047;
		box-shadow: 0 0 0 6rpx rgba(76, 175, 80, 0.1);
		background-color: #fff;
	}

	input:focus,
	textarea:focus,
	select:focus,
	.form-select:focus {
		outline: none;
		border-color: #43a047;
		box-shadow: 0 0 0 6rpx rgba(76, 175, 80, 0.1);
		background: #fff;
	}

	input:disabled {
		background: #f5f5f5;
		color: #666;
		cursor: not-allowed;
	}



	.btn {
		width: 100%;
		padding: 32rpx 40rpx;
		border: none;
		border-radius: 24rpx;
		font-size: 32rpx;
		font-weight: 600;
		cursor: pointer;
		transition: all 0.3s ease;
		letter-spacing: 2rpx;
		background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
		color: #fff;
		box-shadow: 0 8rpx 32rpx rgba(67, 233, 123, 0.3);
		margin-top: 64rpx;
	}

	.btn:hover {
		box-shadow: 0 12rpx 48rpx rgba(67, 233, 123, 0.4);
		transform: translateY(-4rpx);
	}

	.btn:active {
		transform: translateY(0);
	}

	.picker-trigger {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		padding: 24rpx;
		background: #f9fff9;
		border: 3rpx solid #c8e6c9;
		border-radius: 20rpx;
		font-size: 28rpx;
		color: #2e7d32;
		transition: all 0.2s ease;
		box-sizing: border-box;
	}

	.picker-trigger:active {
		border-color: #43a047;
		box-shadow: 0 0 0 6rpx rgba(76, 175, 80, 0.1);
		background: #fff;
	}

	.picker-text {
		flex: 1;
		color: #2e7d32;
	}


	// @media (max-width: 1040rpx) {
	// 	.container {
	// 		max-width: 100%;
	// 	}

	// 	.form-content {
	// 		padding: 48rpx;
	// 	}

	// 	.form-group {
	// 		margin-bottom: 40rpx;
	// 	}

	// 	.btn {
	// 		margin-top: 48rpx;
	// 	}
	// }
</style>