ソースを参照

获取店铺详情

cr 4 年 前
コミット
eaf1a06ada
共有2 個のファイルを変更した40 個の追加1 個の削除を含む
  1. 20 1
      app/controller/home.js
  2. 20 0
      app/service/get_shop_info.js

+ 20 - 1
app/controller/home.js

@@ -17,7 +17,26 @@ class HomeController extends Controller {
17
  */
17
  */
18
   async getShopInfo() {
18
   async getShopInfo() {
19
     const { ctx } = this;
19
     const { ctx } = this;
20
-    ctx.body = 'hi, getShopInfo';
20
+    const query = this.ctx.query;
21
+    const url = query.url;
22
+
23
+    console.log(url);
24
+
25
+    const data = new ResultModel();
26
+    if (url) {
27
+      ctx.service.getShopInfo.getData(url)
28
+        .then(res => {
29
+          saveFile('shop.json', JSON.stringify(res, null, 4), 'shopInfo/');
30
+          console.log('店铺获取成功')
31
+        });
32
+      data.success = true;
33
+      data.message = '等待回调';
34
+    } else {
35
+      data.success = false;
36
+      data.message = '未传入店铺url';
37
+    }
38
+
39
+    ctx.body = JSON.stringify(data);
21
   }
40
   }
22
 
41
 
23
 
42
 

+ 20 - 0
app/service/get_shop_info.js

@@ -8,6 +8,26 @@ class getShopInfoService extends Service {
8
   async getData(url) {
8
   async getData(url) {
9
     return new Promise(async (resolve, reject) => {
9
     return new Promise(async (resolve, reject) => {
10
 
10
 
11
+      let driver = new webdriver.Builder()
12
+        .forBrowser('chrome')
13
+        .build();
14
+
15
+        let shopInfo = {};
16
+
17
+        await driver.get(url);
18
+        await driver.wait(webdriver.until.elementLocated(webdriver.By.className('j_Username')), 100000, '超时未登录');
19
+  
20
+        await driver.sleep(1000)
21
+
22
+        // title
23
+        let titleEle = await driver.findElement(webdriver.By.xpath('//*[@id="shopExtra"]/div[1]/a/strong'));
24
+        shopInfo.title = await titleEle.getText();
25
+
26
+        // address
27
+        let addressEle = await driver.findElement(webdriver.By.xpath('//*[@id="ks-component1646"]/div/div/div/div[2]/ul/li[4]/div'));
28
+        shopInfo.address = await addressEle.getText();
29
+
30
+        resolve(shopInfo);
11
     });
31
     });
12
   }
32
   }
13
 
33