simple.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <view class="content">
  3. <view class="header">
  4. <view class="back-btn" @tap="goBack">
  5. <text class="back-icon">←</text>
  6. <text class="back-text">返回</text>
  7. </view>
  8. <text class="page-title">AI旅游规划</text>
  9. </view>
  10. <!-- 表单区域 -->
  11. <view class="form-container">
  12. <view class="form-group">
  13. <text class="form-label">目的地</text>
  14. <input class="form-input" v-model="formData.destination" placeholder="输入旅游目的地,如:保定" />
  15. </view>
  16. <view class="form-group">
  17. <text class="form-label">行程天数</text>
  18. <slider class="form-slider" :min="1" :max="10" :step="1" :value="formData.days" @change="onDaysChange" show-value />
  19. </view>
  20. <view class="form-group">
  21. <text class="form-label">预算金额</text>
  22. <slider class="form-slider" :min="500" :max="10000" :step="500" :value="formData.budget" @change="onBudgetChange" show-value />
  23. <text class="slider-value">{{ formData.budget }}元</text>
  24. </view>
  25. <view class="form-group">
  26. <text class="form-label">季节</text>
  27. <view class="tag-container">
  28. <view
  29. v-for="(season, index) in seasons"
  30. :key="index"
  31. class="tag"
  32. :class="{ active: formData.season === season }"
  33. @tap="formData.season = season"
  34. >
  35. {{ season }}
  36. </view>
  37. </view>
  38. </view>
  39. <view class="form-group">
  40. <text class="form-label">偏好</text>
  41. <view class="tag-container">
  42. <view
  43. v-for="(preference, index) in preferences"
  44. :key="index"
  45. class="tag"
  46. :class="{ active: formData.preferences.includes(preference) }"
  47. @tap="togglePreference(preference)"
  48. >
  49. {{ preference }}
  50. </view>
  51. </view>
  52. </view>
  53. <view class="form-group">
  54. <text class="form-label">交通方式</text>
  55. <view class="tag-container">
  56. <view
  57. v-for="(transport, index) in transportations"
  58. :key="index"
  59. class="tag"
  60. :class="{ active: formData.transportation === transport }"
  61. @tap="formData.transportation = transport"
  62. >
  63. {{ transport }}
  64. </view>
  65. </view>
  66. </view>
  67. <view class="form-group">
  68. <text class="form-label">住宿类型</text>
  69. <view class="tag-container">
  70. <view
  71. v-for="(accom, index) in accommodations"
  72. :key="index"
  73. class="tag"
  74. :class="{ active: formData.accommodation === accom }"
  75. @tap="formData.accommodation = accom"
  76. >
  77. {{ accom }}
  78. </view>
  79. </view>
  80. </view>
  81. <view class="form-group checkbox-group">
  82. <label class="checkbox-label">
  83. <switch :checked="formData.includeTransportation" @change="formData.includeTransportation = $event.detail.value" />
  84. <text>包含交通费用</text>
  85. </label>
  86. <label class="checkbox-label">
  87. <switch :checked="formData.includeMeals" @change="formData.includeMeals = $event.detail.value" />
  88. <text>包含餐饮费用</text>
  89. </label>
  90. <label class="checkbox-label">
  91. <switch :checked="formData.includeTickets" @change="formData.includeTickets = $event.detail.value" />
  92. <text>包含门票费用</text>
  93. </label>
  94. </view>
  95. <view class="action-buttons">
  96. <view class="reset-btn" @tap="resetForm">重置</view>
  97. <view class="submit-btn" @tap="generatePlan">生成行程</view>
  98. </view>
  99. </view>
  100. </view>
  101. </template>
  102. <script>
  103. import { generateAIPlan, saveAIPlan } from '@/pages/api/ai.js';
  104. export default {
  105. data() {
  106. return {
  107. formData: {
  108. userId: 'user_123', // 模拟用户ID
  109. destination: '保定',
  110. days: 3,
  111. budget: 3000,
  112. season: '春季',
  113. preferences: ['景点', '美食'],
  114. transportation: '公共交通',
  115. accommodation: '经济型酒店',
  116. includeTransportation: true,
  117. includeMeals: true,
  118. includeTickets: true
  119. },
  120. seasons: ['春季', '夏季', '秋季', '冬季', '四季皆宜'],
  121. preferences: ['景点', '美食', '文化', '购物', '自然', '摄影', '历史', '休闲'],
  122. transportations: ['公共交通', '自驾', '步行', '共享单车', '出租车'],
  123. accommodations: ['经济型酒店', '舒适型酒店', '豪华酒店', '民宿', '青旅']
  124. }
  125. },
  126. methods: {
  127. goBack() {
  128. uni.navigateBack();
  129. },
  130. onDaysChange(e) {
  131. this.formData.days = e.detail.value;
  132. },
  133. onBudgetChange(e) {
  134. this.formData.budget = e.detail.value;
  135. },
  136. togglePreference(preference) {
  137. const index = this.formData.preferences.indexOf(preference);
  138. if (index > -1) {
  139. // 已选中,则移除
  140. this.formData.preferences.splice(index, 1);
  141. } else {
  142. // 未选中,则添加
  143. if (this.formData.preferences.length < 5) { // 最多选5个
  144. this.formData.preferences.push(preference);
  145. } else {
  146. uni.showToast({
  147. title: '最多选择5个偏好',
  148. icon: 'none'
  149. });
  150. }
  151. }
  152. },
  153. resetForm() {
  154. this.formData = {
  155. userId: 'user_123',
  156. destination: '保定',
  157. days: 3,
  158. budget: 3000,
  159. season: '春季',
  160. preferences: ['景点', '美食'],
  161. transportation: '公共交通',
  162. accommodation: '经济型酒店',
  163. includeTransportation: true,
  164. includeMeals: true,
  165. includeTickets: true
  166. };
  167. },
  168. generatePlan() {
  169. // 表单验证
  170. if (!this.formData.destination) {
  171. uni.showToast({
  172. title: '请输入目的地',
  173. icon: 'none'
  174. });
  175. return;
  176. }
  177. if (this.formData.preferences.length === 0) {
  178. uni.showToast({
  179. title: '请至少选择一个偏好',
  180. icon: 'none'
  181. });
  182. return;
  183. }
  184. // 显示加载中
  185. uni.showLoading({
  186. title: '正在生成行程...'
  187. });
  188. // 调用AI生成行程
  189. generateAIPlan(this.formData)
  190. .then(res => {
  191. uni.hideLoading();
  192. console.log('AI响应:', res);
  193. if (res && res.data) {
  194. // 保存生成的行程
  195. saveAIPlan(this.formData)
  196. .then(saveRes => {
  197. console.log('保存行程成功:', saveRes);
  198. // 跳转到行程详情页
  199. uni.navigateTo({
  200. url: `/pages/travel-detail/index?planId=${saveRes.data.planId}`
  201. });
  202. })
  203. .catch(err => {
  204. console.error('保存行程失败:', err);
  205. uni.showModal({
  206. title: '保存失败',
  207. content: '行程已生成但保存失败,请重试',
  208. showCancel: false
  209. });
  210. });
  211. } else {
  212. uni.showModal({
  213. title: '生成失败',
  214. content: '无法生成行程,请稍后再试',
  215. showCancel: false
  216. });
  217. }
  218. })
  219. .catch(err => {
  220. uni.hideLoading();
  221. console.error('生成行程失败:', err);
  222. uni.showModal({
  223. title: '生成失败',
  224. content: '无法连接服务器,请检查网络连接',
  225. showCancel: false
  226. });
  227. });
  228. }
  229. }
  230. }
  231. </script>
  232. <style>
  233. .content {
  234. display: flex;
  235. flex-direction: column;
  236. padding: 30rpx;
  237. background-color: #f7f9fc;
  238. min-height: 100vh;
  239. }
  240. .header {
  241. display: flex;
  242. align-items: center;
  243. margin-bottom: 40rpx;
  244. }
  245. .back-btn {
  246. display: flex;
  247. align-items: center;
  248. margin-right: 20rpx;
  249. }
  250. .back-icon {
  251. font-size: 40rpx;
  252. color: #3a9eeb;
  253. margin-right: 5rpx;
  254. }
  255. .back-text {
  256. font-size: 28rpx;
  257. color: #3a9eeb;
  258. }
  259. .page-title {
  260. font-size: 36rpx;
  261. font-weight: bold;
  262. color: #333;
  263. flex: 1;
  264. text-align: center;
  265. margin-right: 60rpx;
  266. }
  267. .form-container {
  268. background-color: #fff;
  269. border-radius: 20rpx;
  270. padding: 30rpx;
  271. box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.05);
  272. }
  273. .form-group {
  274. margin-bottom: 30rpx;
  275. }
  276. .form-label {
  277. font-size: 28rpx;
  278. color: #333;
  279. margin-bottom: 15rpx;
  280. display: block;
  281. font-weight: 600;
  282. }
  283. .form-input {
  284. width: 100%;
  285. height: 80rpx;
  286. background-color: #f5f7fa;
  287. border-radius: 10rpx;
  288. padding: 0 20rpx;
  289. font-size: 28rpx;
  290. color: #333;
  291. }
  292. .form-slider {
  293. margin: 20rpx 0;
  294. }
  295. .slider-value {
  296. font-size: 26rpx;
  297. color: #666;
  298. text-align: right;
  299. }
  300. .tag-container {
  301. display: flex;
  302. flex-wrap: wrap;
  303. margin: 0 -10rpx;
  304. }
  305. .tag {
  306. padding: 10rpx 20rpx;
  307. background-color: #f5f7fa;
  308. border-radius: 30rpx;
  309. margin: 10rpx;
  310. font-size: 26rpx;
  311. color: #666;
  312. }
  313. .tag.active {
  314. background-color: #3a9eeb;
  315. color: #fff;
  316. }
  317. .checkbox-group {
  318. display: flex;
  319. flex-direction: column;
  320. }
  321. .checkbox-label {
  322. display: flex;
  323. align-items: center;
  324. margin-bottom: 20rpx;
  325. font-size: 28rpx;
  326. }
  327. .checkbox-label text {
  328. margin-left: 10rpx;
  329. }
  330. .action-buttons {
  331. display: flex;
  332. justify-content: space-between;
  333. margin-top: 50rpx;
  334. }
  335. .reset-btn, .submit-btn {
  336. width: 45%;
  337. height: 80rpx;
  338. border-radius: 40rpx;
  339. display: flex;
  340. align-items: center;
  341. justify-content: center;
  342. font-size: 30rpx;
  343. font-weight: bold;
  344. }
  345. .reset-btn {
  346. background-color: #f5f7fa;
  347. color: #666;
  348. }
  349. .submit-btn {
  350. background-color: #3a9eeb;
  351. color: #fff;
  352. }
  353. </style>