gaoluyang
6 天以前 7a0ffb0048adeda9ebfbca1d0b525eb224c173e3
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
<template>
  <view class="client-visit-detail">
    <PageHeader title="客户拜访详情" @back="goBack" />
    
    <u-form @submit="handleSignIn" ref="formRef" label-width="110" input-align="right" error-message-align="right">
      <!-- 客户信息 -->
      <u-cell-group title="客户信息">
        <u-form-item label="客户名称" prop="customerName" required border-bottom>
          <u-input
            v-model="form.customerName"
            placeholder="请输入客户名称"
            readonly
          />
        </u-form-item>
        <u-form-item label="联系人" prop="contactPerson" border-bottom>
          <u-input
            v-model="form.contactPerson"
            placeholder="请输入联系人"
            readonly
          />
        </u-form-item>
        <u-form-item label="联系电话" prop="contactPhone" border-bottom>
          <u-input
            v-model="form.contactPhone"
            placeholder="请输入联系电话"
            readonly
          />
        </u-form-item>
      </u-cell-group>
 
      <!-- 拜访信息 -->
      <u-cell-group title="拜访信息">
        <u-form-item label="拜访目的" prop="visitPurpose" required border-bottom>
          <u-input
            v-model="form.visitPurpose"
            placeholder="请输入拜访目的"
          />
        </u-form-item>
        <u-form-item label="拜访时间" prop="visitTime" required border-bottom>
          <u-input
            v-model="form.visitTime"
            placeholder="请选择拜访时间"
            readonly
            @click="showTimePicker"
          />
        </u-form-item>
        <u-form-item label="拜访地点" prop="visitLocation" required border-bottom>
          <u-input
            v-model="form.visitLocation"
            placeholder="请输入拜访地点"
          >
            <template #suffix>
              <u-icon name="map" @click.stop="getCurrentLocation" class="location-icon" />
            </template>
          </u-input>
        </u-form-item>
      </u-cell-group>
 
      <!-- 备注信息 -->
      <u-cell-group title="备注信息">
        <u-form-item label="备注" prop="remark" border-bottom>
          <u-textarea
            v-model="form.remark"
            placeholder="请输入备注信息"
            :maxlength="200"
            count
            :autoHeight="true"
          />
        </u-form-item>
      </u-cell-group>
 
      <!-- 提交按钮 -->
      <view class="footer-btns">
        <u-button class="cancel-btn" @click="goBack">取消</u-button>
        <u-button class="sign-btn" type="primary" @click="handleSignIn" :loading="loading">签到</u-button>
      </view>
    </u-form>
 
    <!-- 时间选择器 -->
    <u-popup v-model="showTime" mode="bottom">
      <u-datetime-picker
        v-model="currentTime"
        title="选择时间"
        @confirm="onTimeConfirm"
        @cancel="showTime = false"
      />
    </u-popup>
  </view>
</template>
 
<script setup>
// 替换 toast 方法
const showToast = (message) => {
  uni.showToast({
    title: message,
    icon: 'none'
  })
}
 
import { ref, onMounted } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import PageHeader from '@/components/PageHeader.vue'
import { clientVisitSignIn } from '@/api/cooperativeOffice/clientVisit'
import useUserStore from "@/store/modules/user"
import dayjs from "dayjs"
 
const userStore = useUserStore()
 
// 表单数据
const form = ref({
  customerName: '',
  contact: '',
  contactPhone: '',
  visitingPeople: '',
  purposeVisit: '',
  purposeDate: '',
  visitAddress: '',
  latitude: '',
  longitude: '',
  locationAddress: '',
  remark: ''
})
 
// 页面状态
const loading = ref(false)
const formRef = ref(null)
 
// 时间相关
const currentTime = ref(new Date())
const showTime = ref(false)
 
// 返回上一页
const goBack = () => {
  uni.navigateBack()
}
 
// 显示时间选择器
const showTimePicker = () => {
  showTime.value = true
}
 
// 确认时间选择
const onTimeConfirm = ({ selectedValues }) => {
  form.value.purposeDate = selectedValues.join('-')
  currentTime.value = selectedValues.join('-')
  showTime.value = false
}
 
// 获取当前位置
const getCurrentLocation = () => {
  uni.showLoading({ title: '获取位置中...' })
  
  uni.getLocation({
    type: 'gcj02',
    success: (res) => {
      form.value.latitude = res.latitude
      form.value.longitude = res.longitude
      
      // 使用逆地理编码获取地址信息
      uni.request({
        url: `https://restapi.amap.com/v3/geocode/regeo?key=c120a5dc69a9f61839f7763e6057005f&location=${res.longitude},${res.latitude}&radius=1000&extensions=all`,
        success: (geoRes) => {
          uni.hideLoading()
          if (geoRes.data.status === '1' && geoRes.data.regeocode) {
            const regeocode = geoRes.data.regeocode
            const address = regeocode.formatted_address
            
            // 优先显示详细地址
            if (address) {
              form.value.visitAddress = address
              showToast('位置获取成功')
            } else {
              // 如果没有详细地址,尝试组合地址信息
              const addressComponent = regeocode.addressComponent
              const combinedAddress = `${addressComponent.province}${addressComponent.city}${addressComponent.district}${addressComponent.township}`
              form.value.visitAddress = combinedAddress
              showToast('位置获取成功')
            }
          } else {
            // API调用成功但没有返回地址信息
            const fallbackAddress = `位置: ${res.latitude.toFixed(4)}, ${res.longitude.toFixed(4)}`
            form.value.visitAddress = fallbackAddress
            showToast('获取到位置,但地址解析失败')
          }
        },
        fail: (err) => {
          uni.hideLoading()
          console.error('逆地理编码失败:', err)
          
          // 逆地理编码失败时,显示简化的位置信息
          const fallbackAddress = `位置: ${res.latitude.toFixed(4)}, ${res.longitude.toFixed(4)}`
          form.value.visitAddress = fallbackAddress
          showToast('位置获取成功,但地址解析失败')
        }
      })
    },
    fail: (err) => {
      uni.hideLoading()
      showToast('获取位置失败,请检查定位权限')
      console.error('获取位置失败:', err)
      
      // 失败时显示错误信息
      form.value.visitAddress = '位置获取失败'
    }
  })
}
 
// 提交签到
const handleSignIn = async () => {
    console.log('form.value----', form.value);
    
  if (!form.value.customerName) {
    showToast('请输入客户名称')
    return
  }
  
  if (!form.value.purposeVisit) {
    showToast('请输入拜访目的')
    return
  }
  
  if (!form.value.purposeDate) {
    showToast('请选择拜访时间')
    return
  }
  if (!form.value.visitAddress) {
    showToast('请获取当前位置')
    return
  }
  
  try {
    loading.value = true
 
    // 使用安全浅拷贝,避免对象展开在某些运行时抛错
    const source = (form.value && typeof form.value === 'object') ? form.value : {}
    const submitData = {}
    Object.keys(source).forEach((k) => {
      submitData[k] = source[k]
    })
 
    console.log('提交数据:', submitData)
    
    const { code } = await clientVisitSignIn(submitData)
    console.log('code----', code);
    
    if (code === 200) {
      showToast('签到成功')
      setTimeout(() => {
        uni.navigateBack()
      }, 500)
    } else {
      loading.value = false
      showToast('签到失败,请重试')
    }
  } catch (e) {
    loading.value = false
    showToast('签到失败,请检查网络连接')
    console.error('签到失败:', e)
  }
}
 
// 初始化页面数据
const initPageData = () => {
  // 设置默认拜访时间为当前时间
  form.value.purposeDate = dayjs().format('YYYY-MM-DD HH:mm:ss')
  currentTime.value = new Date()
  
  // 设置拜访人为当前登录用户的昵称
  form.value.visitingPeople = userStore.nickName || ''
}
 
onMounted(() => {
  initPageData()
})
</script>
 
<style scoped lang="scss">
.client-visit {
  min-height: 100vh;
  background: #f8f9fa;
  padding-bottom: 5rem;
}
 
.footer-btns {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 0.75rem 0;
  box-shadow: 0 -0.125rem 0.5rem rgba(0,0,0,0.05);
  z-index: 1000;
}
 
.cancel-btn {
  font-weight: 400;
  font-size: 1rem;
  color: #666;
  background: #f5f5f5;
  border: 1px solid #ddd;
  width: 45%;
  height: 2.5rem;
  border-radius: 2.5rem 2.5rem 2.5rem 2.5rem;
}
 
.sign-btn {
  font-weight: 500;
  font-size: 1rem;
  color: #fff;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  width: 45%;
  height: 2.5rem;
  border-radius: 2.5rem 2.5rem 2.5rem 2.5rem;
}
 
.location-icon {
  color: #1989fa;
  font-size: 1.2rem;
}
</style>