电速宝
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. const app = getApp();
  2. const api = require('../../../api/index.js');
  3. // 全局常量定义 -()-()-()-
  4. const PHONE_REG = /^1[3-9]\d{9}$/; // 手机号正则
  5. const EMPTY_REGION_TIP = '请选择省/市/区'; // 地区空提示
  6. Page({
  7. data: {
  8. formData: {
  9. receiver: '',
  10. phone: '',
  11. province: '', // 省份名称(原生组件返回)
  12. city: '', // 城市名称(原生组件返回)
  13. district: '', // 区县名称(原生组件返回)
  14. provincecode: '', // 省份编码(原生组件返回)
  15. citycode: '', // 城市编码(原生组件返回)
  16. districtcode: '', // 区县编码(原生组件返回)
  17. addressDetails: '',
  18. isDefault: false,
  19. poiName: '',
  20. latitude: '',
  21. longitude: '',
  22. addressId:''
  23. },
  24. regionValue: [], // 原生选择器绑定值(['省', '市', '区'])
  25. customItem: EMPTY_REGION_TIP, // 选择器默认提示
  26. isSubmitting: false // 防止重复提交
  27. },
  28. onLoad(options) {
  29. console.log(options);
  30. if (wx.getStorageSync("editAddress")) {
  31. const parsedAddress = wx.getStorageSync("editAddress") || {};
  32. console.log(parsedAddress);
  33. this.setData({
  34. formData:parsedAddress
  35. })
  36. }
  37. wx.removeStorageSync("editAddress"); // 用完立即删除
  38. // 初始化:优先使用定位获取当前地区(可选)
  39. this.getLocationRegion();
  40. // 支持编辑模式
  41. this.initEditMode(options);
  42. },
  43. onShow(){
  44. },
  45. /**
  46. * 初始化编辑模式(从地址列表传递数据)
  47. */
  48. initEditMode(options) {
  49. console.log(options);
  50. if (options.address) {
  51. try {
  52. const address = JSON.parse(decodeURIComponent(options.address));
  53. this.setData({
  54. formData: { ...this.data.formData, ...address },
  55. regionValue: [address.province, address.city, address.district]
  56. });
  57. } catch (err) {
  58. console.error('编辑地址数据解析失败:', err);
  59. wx.showToast({ title: '地址数据异常', icon: 'none' });
  60. }
  61. }
  62. },
  63. /**
  64. * 获取当前定位地区(优化初始体验)
  65. */
  66. getLocationRegion() {
  67. wx.getLocation({
  68. type: 'gcj02',
  69. success: (res) => {
  70. // 逆地理编码:通过经纬度获取省市区信息
  71. wx.request({
  72. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=VRGBZ-ZFRHB-SKIUP-NHHJ3-TXGJT-ZIFG3`,
  73. success: (geoRes) => {
  74. if (geoRes.data.status === 0) {
  75. const { province, city, district, adcode } = geoRes.data.result.ad_info;
  76. const regionValue = [province, city, district];
  77. this.setData({
  78. regionValue,
  79. 'formData.province': province,
  80. 'formData.city': city,
  81. 'formData.district': district,
  82. 'formData.provincecode': adcode.slice(0, 2) + '0000', // 省份编码(前2位+0000)
  83. 'formData.citycode': adcode.slice(0, 4) + '00', // 城市编码(前4位+00)
  84. 'formData.districtcode': adcode, // 区县编码
  85. 'formData.latitude': res.latitude,
  86. 'formData.longitude': res.longitude
  87. });
  88. }
  89. }
  90. });
  91. },
  92. fail: () => {
  93. console.warn('获取定位失败,使用默认选择器');
  94. // 定位失败不影响,用户可手动选择
  95. }
  96. });
  97. },
  98. /**
  99. * 省市区选择器变更事件(完全原生实现)
  100. */
  101. handlePickerChange(e) {
  102. const { value, code } = e.detail; // 原生组件返回:value=名称数组,code=编码数组
  103. const [province, city, district] = value;
  104. const [provincecode, citycode, districtcode] = code;
  105. // 直接使用原生返回的名称和编码,无需自定义映射
  106. this.setData({
  107. regionValue: value,
  108. 'formData.province': province,
  109. 'formData.provincecode': provincecode,
  110. 'formData.city': city,
  111. 'formData.citycode': citycode,
  112. 'formData.district': district,
  113. 'formData.districtcode': districtcode
  114. });
  115. console.log('选中省市区:', {
  116. name: `${province}-${city}-${district}`,
  117. code: `${provincecode}-${citycode}-${districtcode}`
  118. });
  119. },
  120. /**
  121. * 输入框事件(保持原有限制)
  122. */
  123. handleInput(e) {
  124. const { key } = e.currentTarget.dataset;
  125. let { value } = e.detail;
  126. // 针对性输入限制
  127. switch (key) {
  128. case 'receiver':
  129. value = value.trim().slice(0, 10); // 收货人最多10个字
  130. break;
  131. case 'phone':
  132. value = value.replace(/\D/g, ''); // 只保留数字
  133. break;
  134. case 'addressDetails':
  135. value = value.trim().slice(0, 50); // 详细地址最多50个字
  136. break;
  137. }
  138. this.setData({
  139. [`formData.${key}`]: value
  140. });
  141. },
  142. /**
  143. * 开关切换事件
  144. */
  145. handleSwitchChange(e) {
  146. const isDefault = e.detail.value;
  147. this.setData({
  148. 'formData.isDefault': isDefault
  149. });
  150. if (isDefault) {
  151. wx.showToast({
  152. title: '将设为默认收货地址',
  153. icon: 'none',
  154. duration: 1500
  155. });
  156. }
  157. },
  158. /**
  159. * 跳转地图选址
  160. */
  161. navigateToMapSelect() {
  162. const { formData } = this.data;
  163. console.log(formData);
  164. const params = {
  165. province: formData.province,
  166. city: formData.city,
  167. district: formData.district
  168. };
  169. wx.navigateTo({
  170. url: `/package-order/pages/mapSelect/index?params=${encodeURIComponent(JSON.stringify(params))}`
  171. });
  172. },
  173. /**
  174. * 表单提交(保持原有校验)
  175. */
  176. formSubmit(e) {
  177. const { formData, isSubmitting } = this.data;
  178. // 防止重复提交
  179. if (isSubmitting) return;
  180. console.log(formData);
  181. // 完整表单校验
  182. if (!formData.receiver.trim()) {
  183. wx.showToast({ title: '请输入收货人姓名', icon: 'none' });
  184. return;
  185. }
  186. if (!PHONE_REG.test(formData.phone)) {
  187. wx.showToast({ title: '请输入正确的手机号码', icon: 'none' });
  188. return;
  189. }
  190. if (!formData.province || !formData.city || !formData.district) {
  191. wx.showToast({ title: '请选择所在地区', icon: 'none' });
  192. return;
  193. }
  194. if (!formData.addressDetails.trim()) {
  195. wx.showToast({ title: '请输入详细地址', icon: 'none' });
  196. return;
  197. }
  198. if (formData.poiName=='') {
  199. formData.poiName = formData.addressDetails
  200. formData.addressDetails = formData.province+formData.city+formData.district+formData.addressDetails
  201. }
  202. // 开始提交
  203. this.setData({ isSubmitting: true });
  204. // 模拟接口请求(实际项目替换为真实接口)
  205. if (formData.addressId=='') {
  206. api.request(`/sysaddress/addaddress`, 'post',formData,{ isPublic: false })
  207. .then((data) => {
  208. console.log(data.data);
  209. if (data.code==200) {
  210. // wx.navigateTo({
  211. // url: '/package-order/pages/address/index',
  212. // })
  213. wx.navigateBack({
  214. delta: 1,
  215. })
  216. this.setData({ isSubmitting: false });
  217. }
  218. })
  219. .catch((err) => {
  220. this.setData({ isSubmitting: false });
  221. console.error('请求失败:', err);
  222. });
  223. }else{
  224. api.request(`/sysaddress/updateaddress`, 'post',formData,{ isPublic: false })
  225. .then((data) => {
  226. console.log(data.data);
  227. if (data.code==200) {
  228. // wx.navigateTo({
  229. // url: '/package-order/pages/address/index',
  230. // })
  231. wx.navigateBack({
  232. delta: 1,
  233. })
  234. this.setData({ isSubmitting: false });
  235. }
  236. })
  237. .catch((err) => {
  238. this.setData({ isSubmitting: false });
  239. console.error('请求失败:', err);
  240. });
  241. }
  242. }
  243. });