openks / learn-vue

自定义组件文档
https://openks.github.io/learn-vue
0 stars 0 forks source link

20191216-试题汇总 #131

Open openks opened 4 years ago

openks commented 4 years ago

new与原型链问题

var F = function() {}
Object.prototype.a = function() {
    console.log("a from Object")
}
Function.prototype.b = function() {
    console.log("b from Function")
}
f = new F()
// 请写出一下项的输出结果
F.a() // a from Object
F.b() // b from Function
f.a() // a from Object
f.b() // f.b is not a function

代码格式转换题

let data = [{
    province: "江苏省",
    city: "南京",
    code: '001'
}, {
    province: "江苏省",
    city: "苏州",
    code: '002'
}, {
    province: "浙江省",
    city: "杭州",
    code: '101'
}, {
    province: "浙江省",
    city: "宁波",
    code: '102'
}]
//原数据如上,写个函数把以上代码格式化为如下格式,不要用多重循环
let data2 = [{
    "province": "江苏省",
    "citys": [{
        "name": "南京",
        "code": "001"
    }, {
        "name": "苏州",
        "code": "002"
    }]
}, {
    "province": "浙江省",
    "citys": [{
        "name": "杭州",
        "code": "101"
    }, {
        "name": "宁波",
        "code": "102"
    }]
}]

function dealData(data) {
    let obj = {}
    data.map(ele=>{
        let pname = ele.province
        let el = {
            name: ele.city,
            code: ele.code
        }
        if (!obj[pname]) {
            obj[pname] = []
        }
        obj[pname].push(el)
    }
    )
    let arr = []
    for (k in obj) {
        arr.push({
            province: k,
            citys: obj[k],
        })
    }
   // console.log(JSON.stringify(arr))
}
dealData(data)

多次尝试调取同一接口

支付相关借口很重要,需要及时获取结果 结果可能有三种情况 1.成功 2.失败 3.未知 想要实现逻辑如下: 1-5次每次间隔2秒
6-10次每次间隔1秒
11-20次每次间隔0.5秒
若20次后结果依旧是未知则提示失败

具体实现方案如下

import { Component, Vue } from "vue-property-decorator";
// 可通过配置文件导入
const TimesAndTimeProid = [
  {
    min: 1,
    max: 5,
    time: 2000
  },
  {
    min: 6,
    max: 10,
    time: 1000
  },
  {
    min: 11,
    max: 20,
    time: 500
  }
];

@Component
export default class Test extends Vue {
  resultData = {};

  //直接调用该方法即可
  async tryAndGetResult() {
    // 填充url及相关参数
    let obj = {
      url: "",
      params: {}
    };
    for (let i = 0; i < TimesAndTimeProid.length; i++) {
      const timemObj = TimesAndTimeProid[i];
      for (let j = timemObj.min; j <= timemObj.max; j++) {
        obj.timeout = timemObj.time;
        let tmp1 = await this.getResult(obj);
        if (tmp1) {
          return;
        }
      }
    }
    this.resultData = {
      code: "errorCode",
      msg: "errorMessage"
    };
    return;
  }

  async getResult(args) {
    try {
      let res = await this.axios.get(args.url, {params: args.params}, {
        timeout: args.timeout
      });
      if (res.data.code === "OK" || res.data.code === "ERROR") {
        this.resultData = res.data;
        // 弹出提示等其他后续逻辑
        return true;
      } else {
        return false;
      }
    } catch (error) {
      // 超时及其他情况
      return false;
    }
  }
}
openks commented 4 years ago

上一段多次尝试获取同一接口逻辑有误 用超时时间作为间隔时间不符合要求

该逻辑以请求返回后仍为未知,等待间隔时间后再发请求

function getTimeByTimes(times) {
    if (times < 6) {
        return 2000
    } else if (times < 11) {
        return 1000
    } else if (times < 21) {
        return 500
    } else {
        return null
    }
}

function TryAndGetResult() {
        let times = 0
        async function requestData(){
            console.log("第",times,"次请求")
            let res = await this.getRequest()
            if(!res){
             times=times+1
             let timeProid = this.getTimeByTimes(times)
             if(timeProid === null ){
                console.log("第",times-1,"次请求依旧失败")
                 //设置为失败
                 return 
             }else{
                 setTimeout(requestData,timeProid)
             }
            }
        }
        requestData()
}

function request(param) {
    return new Promise((resolve) => {
        let time = Math.random() * 1000    
        setTimeout(() => resolve({param, time}), time) 
    })
}

async function getRequest() {
    try {
        // console.log("请求---发送")
        let res = await request("ddddd")
        // console.log("请求--返回")
        return false
    } catch (error) {
        return false;
    }
}

TryAndGetResult()