怎么实现wx.request的同步
发布于 5 年前 作者 tao25 12573 次浏览 来自 问答
  • 需求的场景描述(希望解决的问题)
let buf;
              if (bluetoothType == "jeez") {    //简易蓝牙的指令
                buf = that.string2buffer("open:05s");
              }
              if (bluetoothType == "mac") {     //门禁机的指令
                buf = that.string2buffer("-b&-3-12069-");
              }
              if (bluetoothType == "jz") {      //嵌入式门禁开门指令
                //获取用户的门禁权限列表
                let powerList_url = 'CustomerAPI/Index.aspx?method=getdoorsauthpool&clientcode=559574802685&doornumber=qrs001';
                http.get(powerList_url).then(res => {
                  // let hex = '0101fe 010201'
                  let hex = res.data;
                  //16进制字符串转为ArrayBuffer
                  let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
                    return parseInt(h, 16)
                  }))
                  buf = typedArray.buffer
                }).catch(res => {
 
                });
              }
              //发送开门指令
              wx.writeBLECharacteristicValue({
                deviceId: deviceId,
                serviceId: serviceId,
                characteristicId: write_id,
                value: buf,
                success: function (res) {
                  console.log("buf", buf);
                  console.log("write-suc", res);
 
                },
                fail(res) {
                  console.log(res);
                  //发送开门指令失败时提示
                  that.openDoorFail(deviceId);
                }
              })


因为只有一个if中需要请求接口,所以不希望将wx.writeBLECharacteristicValue加入到promise.then中,有能么办法能让这段代码同步执行吗?

1 回复

wx.writeBLECharacteristicValue 这一团内容写成一个方法

在最后一个if里加个else,调用方法,另外在网络请求的回调里也写上调用该方法

回到顶部