index-list.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import CustomPage from '../../../base/CustomPage'
  2. CustomPage({
  3. onShareAppMessage() {
  4. return {
  5. title: 'index-list',
  6. path: 'packageExtend/pages/extend/index-list/index-list'
  7. }
  8. },
  9. onLoad() {
  10. this.getCitys()
  11. },
  12. getCitys() {
  13. const db = wx.cloud.database({
  14. env: 'release-b86096'
  15. })
  16. const mapCity = db.collection('mapCity')
  17. const _this = this
  18. mapCity.doc('6af880a55eb9574b008b78aa53a48405').get({
  19. success(re) {
  20. console.log(re)
  21. const cities = re.data.cities
  22. // 按拼音排序
  23. cities.sort((c1, c2) => {
  24. const pinyin1 = c1.pinyin.join('')
  25. const pinyin2 = c2.pinyin.join('')
  26. return pinyin1.localeCompare(pinyin2)
  27. })
  28. // 添加首字母
  29. const map = new Map()
  30. for (const city of cities) {
  31. const alpha = city.pinyin[0].charAt(0).toUpperCase()
  32. if (!map.has(alpha)) map.set(alpha, [])
  33. map.get(alpha).push({name: city.fullname})
  34. }
  35. const keys = []
  36. for (const key of map.keys()) {
  37. keys.push(key)
  38. }
  39. keys.sort()
  40. const list = []
  41. for (const key of keys) {
  42. list.push({
  43. alpha: key,
  44. subItems: map.get(key)
  45. })
  46. }
  47. _this.setData({list})
  48. }
  49. })
  50. }
  51. })