IOriens / ioriens.github.io

https://Junjie.xyz
12 stars 2 forks source link

代码片段 #16

Closed IOriens closed 6 years ago

IOriens commented 6 years ago

有用或没用的代码片段,写着玩

百度地图封装

function getBMapQueryString(type, opts, baseType = "place") {
  let queryOpts = {
    ak: "xxxxxxxxxxxxxx",
    output: "json",
    ...opts
  };

  let queryUrl = Object.keys(queryOpts).reduce((pre, curr) => {
    return `${pre}&${curr}=${queryOpts[curr]}`;
  }, "");

  return `https://api.map.baidu.com/${baseType}/v2/${type}?${queryUrl}`;
}

使用

let location = `${lat},${lng}`
let url = getBMapQueryString('search', {
  query: '地铁',
  location,
  page_size: 2,
  radius: 1000
})
let url2 = getBMapQueryString(
  '',
  {
    // query: "地铁",
    location,
    page_size: 2,
    pois: 1
  },
  'geocoder'
)

美团外卖自动选店

先加载几页店铺,然后随便打开一个,注意,打开连接的操作可能会被浏览器拦截

function loadMoreAndSelectOne (step) {
  console.log(step)
  if (step < 3) {
    // 加载店铺
    $('.loading').click()
    // 频繁点击会被警告
    setTimeout(() => {
      loadMoreAndSelectOne(step + 1)
    }, 2500)
  } else {
    // 打开店铺
    $($('.rest-li')[Math.floor($('.rest-li').length * Math.random())])
      .find('.rest-atag')
      .click()
  }
}

loadMoreAndSelectOne(0)