| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- const app = getApp();
- const api = require('../../../api/index.js');
-
- // 全局常量定义 -()-()-()-
- const PHONE_REG = /^1[3-9]\d{9}$/; // 手机号正则
- const EMPTY_REGION_TIP = '请选择省/市/区'; // 地区空提示
-
- Page({
- data: {
- formData: {
- receiver: '',
- phone: '',
- province: '', // 省份名称(原生组件返回)
- city: '', // 城市名称(原生组件返回)
- district: '', // 区县名称(原生组件返回)
- provincecode: '', // 省份编码(原生组件返回)
- citycode: '', // 城市编码(原生组件返回)
- districtcode: '', // 区县编码(原生组件返回)
- addressDetails: '',
- isDefault: false,
- poiName: '',
- latitude: '',
- longitude: '',
- addressId:''
- },
- regionValue: [], // 原生选择器绑定值(['省', '市', '区'])
- customItem: EMPTY_REGION_TIP, // 选择器默认提示
- isSubmitting: false // 防止重复提交
- },
-
- onLoad(options) {
- console.log(options);
- if (wx.getStorageSync("editAddress")) {
- const parsedAddress = wx.getStorageSync("editAddress") || {};
- console.log(parsedAddress);
- this.setData({
- formData:parsedAddress
- })
- }
-
- wx.removeStorageSync("editAddress"); // 用完立即删除
- // 初始化:优先使用定位获取当前地区(可选)
- this.getLocationRegion();
- // 支持编辑模式
- this.initEditMode(options);
- },
- onShow(){
-
- },
-
- /**
- * 初始化编辑模式(从地址列表传递数据)
- */
- initEditMode(options) {
- console.log(options);
- if (options.address) {
- try {
- const address = JSON.parse(decodeURIComponent(options.address));
- this.setData({
- formData: { ...this.data.formData, ...address },
- regionValue: [address.province, address.city, address.district]
- });
- } catch (err) {
- console.error('编辑地址数据解析失败:', err);
- wx.showToast({ title: '地址数据异常', icon: 'none' });
- }
- }
- },
-
- /**
- * 获取当前定位地区(优化初始体验)
- */
- getLocationRegion() {
- wx.getLocation({
- type: 'gcj02',
- success: (res) => {
- // 逆地理编码:通过经纬度获取省市区信息
- wx.request({
- url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=VRGBZ-ZFRHB-SKIUP-NHHJ3-TXGJT-ZIFG3`,
- success: (geoRes) => {
- if (geoRes.data.status === 0) {
- const { province, city, district, adcode } = geoRes.data.result.ad_info;
- const regionValue = [province, city, district];
- this.setData({
- regionValue,
- 'formData.province': province,
- 'formData.city': city,
- 'formData.district': district,
- 'formData.provincecode': adcode.slice(0, 2) + '0000', // 省份编码(前2位+0000)
- 'formData.citycode': adcode.slice(0, 4) + '00', // 城市编码(前4位+00)
- 'formData.districtcode': adcode, // 区县编码
- 'formData.latitude': res.latitude,
- 'formData.longitude': res.longitude
- });
- }
- }
- });
- },
- fail: () => {
- console.warn('获取定位失败,使用默认选择器');
- // 定位失败不影响,用户可手动选择
- }
- });
- },
-
- /**
- * 省市区选择器变更事件(完全原生实现)
- */
- handlePickerChange(e) {
- const { value, code } = e.detail; // 原生组件返回:value=名称数组,code=编码数组
- const [province, city, district] = value;
- const [provincecode, citycode, districtcode] = code;
-
- // 直接使用原生返回的名称和编码,无需自定义映射
- this.setData({
- regionValue: value,
- 'formData.province': province,
- 'formData.provincecode': provincecode,
- 'formData.city': city,
- 'formData.citycode': citycode,
- 'formData.district': district,
- 'formData.districtcode': districtcode
- });
-
- console.log('选中省市区:', {
- name: `${province}-${city}-${district}`,
- code: `${provincecode}-${citycode}-${districtcode}`
- });
- },
-
- /**
- * 输入框事件(保持原有限制)
- */
- handleInput(e) {
- const { key } = e.currentTarget.dataset;
- let { value } = e.detail;
-
- // 针对性输入限制
- switch (key) {
- case 'receiver':
- value = value.trim().slice(0, 10); // 收货人最多10个字
- break;
- case 'phone':
- value = value.replace(/\D/g, ''); // 只保留数字
- break;
-
- case 'addressDetails':
- value = value.trim().slice(0, 50); // 详细地址最多50个字
- break;
- }
-
- this.setData({
- [`formData.${key}`]: value
- });
- },
- /**
- * 开关切换事件
- */
- handleSwitchChange(e) {
- const isDefault = e.detail.value;
- this.setData({
- 'formData.isDefault': isDefault
- });
-
- if (isDefault) {
- wx.showToast({
- title: '将设为默认收货地址',
- icon: 'none',
- duration: 1500
- });
- }
- },
-
- /**
- * 跳转地图选址
- */
- navigateToMapSelect() {
- const { formData } = this.data;
- console.log(formData);
- const params = {
- province: formData.province,
- city: formData.city,
- district: formData.district
- };
-
- wx.navigateTo({
- url: `/package-order/pages/mapSelect/index?params=${encodeURIComponent(JSON.stringify(params))}`
- });
- },
-
- /**
- * 表单提交(保持原有校验)
- */
- formSubmit(e) {
- const { formData, isSubmitting } = this.data;
-
- // 防止重复提交
- if (isSubmitting) return;
- console.log(formData);
-
- // 完整表单校验
- if (!formData.receiver.trim()) {
- wx.showToast({ title: '请输入收货人姓名', icon: 'none' });
- return;
- }
-
- if (!PHONE_REG.test(formData.phone)) {
- wx.showToast({ title: '请输入正确的手机号码', icon: 'none' });
- return;
- }
-
- if (!formData.province || !formData.city || !formData.district) {
- wx.showToast({ title: '请选择所在地区', icon: 'none' });
- return;
- }
-
- if (!formData.addressDetails.trim()) {
- wx.showToast({ title: '请输入详细地址', icon: 'none' });
- return;
- }
- if (formData.poiName=='') {
- formData.poiName = formData.addressDetails
- formData.addressDetails = formData.province+formData.city+formData.district+formData.addressDetails
- }
- // 开始提交
- this.setData({ isSubmitting: true });
- // 模拟接口请求(实际项目替换为真实接口)
- if (formData.addressId=='') {
- api.request(`/sysaddress/addaddress`, 'post',formData,{ isPublic: false })
- .then((data) => {
- console.log(data.data);
- if (data.code==200) {
- // wx.navigateTo({
- // url: '/package-order/pages/address/index',
- // })
- wx.navigateBack({
- delta: 1,
- })
- this.setData({ isSubmitting: false });
-
- }
- })
- .catch((err) => {
- this.setData({ isSubmitting: false });
-
- console.error('请求失败:', err);
- });
- }else{
- api.request(`/sysaddress/updateaddress`, 'post',formData,{ isPublic: false })
- .then((data) => {
- console.log(data.data);
- if (data.code==200) {
- // wx.navigateTo({
- // url: '/package-order/pages/address/index',
- // })
- wx.navigateBack({
- delta: 1,
- })
- this.setData({ isSubmitting: false });
-
- }
- })
- .catch((err) => {
- this.setData({ isSubmitting: false });
-
- console.error('请求失败:', err);
- });
- }
-
- }
- });
|