wuyi 1 год назад
Родитель
Сommit
acc2fc2761
1 измененных файлов с 34 добавлено и 19 удалено
  1. 34 19
      src/rcs-number/impl/cloud214.service.ts

+ 34 - 19
src/rcs-number/impl/cloud214.service.ts

@@ -22,28 +22,43 @@ export class cloud214 extends GetNumberService {
     }
 
     async getNumber(country: string): Promise<GetNumberResponse> {
-        const res = await axiosInstance.get('phoneInfo', {
-            params: {
-                userid: this.userid,
-                pid,
-                token: this.token,
-                country: country.toUpperCase()
-            }
-        })
-        let result = res.data
-        if (result.code !== '1001') {
-            throw new Error(result.message)
+        const requestPhoneInfo = async () => {
+            const res = await axiosInstance.get('phoneInfo', {
+                params: {
+                    userid: this.userid,
+                    pid,
+                    token: this.token,
+                    country: country.toUpperCase()
+                }
+            })
+            return res.data
         }
 
-        const data = result.data
-        const number = data.phone
+        const requests = Array(20)
+            .fill(null)
+            .map(() => requestPhoneInfo())
+
+        try {
+            const result = await Promise.any(requests.map(async (request) => {
+                const res = await request
+                if (res.code === '1001') {
+                    return res
+                }
+                throw new Error(res.message)
+            }))
 
-        return {
-            number: number,
-            orderId: data.orderId,
-            operatorCode: '',
-            operatorName: '',
-            rawResponse: result
+            const data = result.data
+            const number = data.phone
+
+            return {
+                number: number,
+                orderId: data.orderId,
+                operatorCode: '',
+                operatorName: '',
+                rawResponse: result
+            }
+        } catch (e) {
+            throw new Error(`Failed to get a valid number: ${e.message}`)
         }
     }