achao преди 2 години
родител
ревизия
f21660f64c
променени са 5 файла, в които са добавени 239 реда и са изтрити 1 реда
  1. 0 1
      .gitignore
  2. 7 0
      xiaochengxu/components/lb-picker/utils.js
  3. 141 0
      xiaochengxu/js_sdk/district.js
  4. 52 0
      xiaochengxu/js_sdk/filterCity.js
  5. 39 0
      xiaochengxu/util/util.js

+ 0 - 1
.gitignore

@@ -72,7 +72,6 @@ unimall.log.*.tmp
 ### xcx ###
 .map/
 /unpackage/
-*.js
 *.wxss
 *.json
 *.wxml

+ 7 - 0
xiaochengxu/components/lb-picker/utils.js

@@ -0,0 +1,7 @@
+export function isObject (val) {
+  return Object.prototype.toString.call(val) === '[object Object]'
+}
+
+export function getIndicatorHeight () {
+  return Math.round(uni.getSystemInfoSync().screenWidth / (750 / 100))
+}

+ 141 - 0
xiaochengxu/js_sdk/district.js

@@ -0,0 +1,141 @@
+import twData from './taiwan.json'
+import { formatCity } from './filterCity'
+export default {
+	/**
+	 * 按省的城市编码排序
+	 */
+	orderByCityCode(a, b) {
+	  return parseInt(a.value) - parseInt(b.value)
+	},
+	/**
+	 * 自定义app数据格式
+	 * @param {Array} addressList 高德地图api返回的城市数据
+	 * @param {boolean} noFilter 是否过滤省市区
+	 * @param {boolean} allProvice 是否显示全省
+	 * @param {boolean} allCity 是否显示全市
+	 * @param {boolean} allCountry 是否显示全国
+	 */
+	buildAppJSONData({
+		addressList, 
+		noFilter = false,
+		allProvince = false,
+		allCity = false,
+		allCountry = false,
+	}) {
+	  let array = []
+	  get(addressList, array, noFilter)
+		
+		array.forEach(item => {
+		  if (item.value === '710000') {
+		    if (item.children.length === 0) {
+		      item.children = twData
+		    }
+		  }
+		})
+		array.sort(this.orderByCityCode) // 加入台湾区域,并按省份城市代码排序
+		
+		const specialCitys = ['北京', '上海', '天津', '重庆', '香港', '澳门']
+		if (array.length > 0) {
+			array.forEach(item => {
+				if (allCity) {
+					item.children.forEach(city => {
+						city.children.unshift({
+							value: '0', label: '全市', children: []
+						})
+					})
+				}
+				if (allProvince) {
+		      if (specialCitys.indexOf(item.label) === -1) {
+		        item.children.unshift({
+		        	value: '0', label: '全省', children: [
+		        		{value: '0', label: '全省', children: []}
+		        	]
+		        })
+		      }
+				}
+			})
+			if (allCountry) {
+				array.unshift({
+					value: '0', label: '全国', children: [
+						{value: '0', label: '全国', children: [
+							{value: '0', label: '全国', children: []}
+						]}
+					]
+				})
+			}
+		}
+	  return array
+		
+	  function get(districts, children, noFilter) {
+	    districts.forEach((item, index) => {
+	      children.push({
+	        value: noFilter === true ? item.adcode : item.name,
+	        label: noFilter === true ? formatCity(item.name) : item.name
+	      })
+	      if (item.districts.length != 0) {
+	        children[children.length - 1].children = []
+	        if (item.adcode === item.districts[0].adcode) {
+	          children[children.length - 1].children = [
+	            {
+	              value: noFilter === true ? item.adcode : item.name,
+	              label: noFilter === true ? formatCity(item.name) : item.name
+	            }
+	          ]      
+	        } else {
+	          get(item.districts, children[children.length - 1].children, noFilter)
+	        }
+	      } else {
+	        // 特别处理香港,市区统一
+	        if (parseInt(item.adcode) > 810000 && parseInt(item.adcode) < 820000) {
+	          children[children.length - 1].children = [
+	            {
+	             value: noFilter === true ? item.adcode : item.name,
+	             label: noFilter === true ? formatCity(item.name) : item.name
+	            }
+	          ]
+	        }
+	        // 特别处理澳门,市区统一
+	        if (parseInt(item.adcode) > 820000 && parseInt(item.adcode) < 830000) {
+	          children[children.length - 1].children = [
+	            {
+	              value: noFilter === true ? item.adcode : item.name,
+	              label: noFilter === true ? formatCity(item.name) : item.name
+	            }
+	          ]
+	        }
+	        // 特别处理台湾
+	        if (parseInt(item.adcode) === 710000) {
+	          children[children.length - 1].children = []
+	        }
+	      }
+	    })
+	  }
+	},
+	getGaoDeData(key) {
+		if (key) {
+			return new Promise((resolve, reject) => {
+				uni.request({
+					method: 'get',
+					url: `https://restapi.amap.com/v3/config/district?subdistrict=3&key=${key}`,
+					success: res => {
+						if (res.statusCode === 200) {
+							// 构造匹配app,otms框架的城市JSON文件
+							// const data = this.buildAppJSONData(, false, false)
+							resolve(res.data.districts[0].districts)
+						} else {
+							reject(res)
+						}
+					},
+					fail: res => {
+						reject(res)
+					}
+				})
+			})
+		} else {
+			uni.showModal({
+				title: '提示',
+				content: '请输入高德key'
+			})
+		}
+	}
+}

+ 52 - 0
xiaochengxu/js_sdk/filterCity.js

@@ -0,0 +1,52 @@
+/**
+ * 过滤城市字段
+ * 去掉省市区自治区等文字,匹配tms的地址库,兼容老数据 
+ */
+
+const cityReg = /省|市|自治区|自治州|自治县|特别行政区|壮族|回族|维吾尔/g
+
+const areaReg = /回族自治县|满族自治县|满族蒙古族自治县|蒙古族自治县|蒙古自治州|朝鲜族自治州|朝鲜族自治县|畲族自治县|土家族自治县|土家族苗族自治州|侗族自治县|苗族侗族自治县|苗族侗族自治州|苗族自治县|瑶族自治县|壮族瑶族自治县|各族自治县|毛南族自治县|仫佬族自治县|黎族自治县|黎族苗族自治县|苗族土家族自治县|土家族苗族自治县|羌族自治县|彝族自治县|彝族自治州|藏族自治县|藏族羌族自治州|仡佬族苗族自治县|布依族苗族自治州|苗族布依族自治县|布依族苗族自治县|布依族苗族自治州|水族自治县|彝族回族苗族自治县|傈僳族自治州|独龙族怒族自治县|白族普米族自治县|哈尼族彝族自治州|苗族瑶族傣族自治县|傣族自治州|彝族傣族自治县|哈尼族彝族傣族自治县|白族自治州|彝族自治县|彝族回族自治县|彝族自治县|彝族自治州|藏族自治州|傈僳族自治县|纳西族自治县|壮族苗族自治州|彝族哈尼族拉祜族自治县|哈尼族彝族自治县|傣族彝族自治县|哈尼族彝族自治县|哈尼族自治县|拉祜族自治县|傣族拉祜族佤族自治县|佤族自治县|傣族佤族自治县|拉祜族佤族布朗族傣族自治县|回族彝族自治县|彝族苗族自治县|傣族景颇族自治州|哈萨克族自治县|裕固族自治县|保安族东乡族撒拉族自治县|回族土族自治县|撒拉族自治县|土族自治县|蒙古族藏族自治州|蒙古族藏族自治州直辖|哈萨克自治县|哈萨克自治州|锡伯自治县|塔吉克自治县|柯尔克孜自治州/g
+
+const countyReg = /区|县|经济开发区/g
+
+export const formatCity = (name) => {
+  var tempName = name
+  if (tempName.length > 3) {
+    if (tempName.indexOf('城区') > -1) {
+      tempName = tempName.replace('城区', '')
+    }
+    if (tempName.indexOf('地区') > -1) {
+      tempName = tempName.replace('地区', '')
+    }
+  }
+  if (tempName.length > 2) {
+    if (tempName.indexOf('和田') === -1) {
+      tempName = tempName.replace(areaReg, "")
+      if (tempName.indexOf('市') > -1 && tempName.indexOf('市') !== tempName.length - 1) { // 市不是出现在最后一位,不过滤
+        if (tempName.indexOf('市市') > -1) { // 如果出现两个市,如津市市,只去掉第一个市字
+          tempName = tempName.replace('市', "")
+        } else {
+          console.log('保留市字:' + tempName)
+        }
+      } else {
+        tempName = tempName.replace(cityReg, "")
+      }
+      // 过滤剩余的x族自治县
+      tempName = tempName.replace('族自治县', "")
+      // 过滤区县
+      if (tempName.length > 2) {
+        if (tempName.indexOf('县区') > -1) {
+          tempName = tempName.replace('区', "")
+        } else {
+          if (tempName.length > 3 && tempName.indexOf('新区') > -1) {
+            console.log('保留新区:' + tempName)
+          } else {
+            tempName = tempName.replace(countyReg, "")
+          }
+        }
+        // tempName = tempName.replace(countyReg, "")
+      }
+    }
+  }
+  return tempName
+}

+ 39 - 0
xiaochengxu/util/util.js

@@ -0,0 +1,39 @@
+
+// 日期格式化1
+export function parseTime(time, pattern) {
+  if (arguments.length === 0) {
+    return null
+  }
+  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
+  let date
+  if (typeof time === 'object') {
+    date = time
+  } else {
+    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
+      time = parseInt(time)
+    }
+    if ((typeof time === 'number') && (time.toString().length === 10)) {
+      time = time * 1000
+    }
+    date = new Date(time)
+  }
+  const formatObj = {
+    y: date.getFullYear(),
+    m: date.getMonth() + 1,
+    d: date.getDate(),
+    h: date.getHours(),
+    i: date.getMinutes(),
+    s: date.getSeconds(),
+    a: date.getDay()
+  }
+  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
+    let value = formatObj[key]
+    // Note: getDay() returns 0 on Sunday
+    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
+    if (result.length > 0 && value < 10) {
+      value = '0' + value
+    }
+    return value || 0
+  })
+  return time_str
+}