shuishen
2022-04-27 510ae9f8768cd16c39fde70e1ae40b164500d5d5
src/store/modules/mobilePositionLID1.js
@@ -1,251 +1,251 @@
function RemoGeoLocation() {
  this._remoteSvrUrl = "https://webapi.amap.com/html/geolocate.html";
    this._remoteSvrUrl = 'https://webapi.amap.com/html/geolocate.html'
  this._callbackList = [];
    this._callbackList = []
  this._seqBase = 1;
    this._seqBase = 1
  this._frameReady = 0;
    this._frameReady = 0
  this._watchIdMap = {};
    this._watchIdMap = {}
}
RemoGeoLocation.prototype = {
  _getSeq: function () {
    return this._seqBase++;
        return this._seqBase++
  },
  _onRrameReady: function (callback) {
    if (this._frameReady === 0) {
      if (!this._frameReadyList) {
        this._frameReadyList = [];
                this._frameReadyList = []
      }
      this._frameReadyList.push(callback);
            this._frameReadyList.push(callback)
      this._prepareIframe();
            this._prepareIframe()
      return;
            return
    }
    callback.call(this);
        callback.call(this)
  },
  _prepareIframe: function () {
    if (this._iframeWin) {
      return;
            return
    }
    var ifrm = document.createElement("iframe");
        var ifrm = document.createElement('iframe')
    ifrm.src =
      this._remoteSvrUrl + (this._remoteSvrUrl.indexOf("?") > 0 ? "&" : "?");
            this._remoteSvrUrl + (this._remoteSvrUrl.indexOf('?') > 0 ? '&' : '?')
    ifrm.width = "0px";
    ifrm.height = "0px";
    ifrm.style.position = "absolute";
    ifrm.style.display = "none";
        ifrm.width = '0px'
        ifrm.height = '0px'
        ifrm.style.position = 'absolute'
        ifrm.style.display = 'none'
    var self = this;
        var self = this
    var timeoutId = setTimeout(function () {
      self._frameReady = false;
            self._frameReady = false
      self._callbackFrameReadyList();
    }, 5000);
            self._callbackFrameReadyList()
        }, 5000)
    ifrm.onload = function () {
      clearTimeout(timeoutId);
            clearTimeout(timeoutId)
      self._frameReady = true;
            self._frameReady = true
      self._callbackFrameReadyList();
            self._callbackFrameReadyList()
      ifrm.onload = null;
    };
    document.body.appendChild(ifrm);
    this._iframeWin = ifrm.contentWindow;
    window.addEventListener(
      "message",
      function (e) {
        if (self._remoteSvrUrl.indexOf(e["origin"]) !== 0) {
          return;
            ifrm.onload = null
        }
        self._handleRemoteMsg(e["data"]);
        document.body.appendChild(ifrm)
        this._iframeWin = ifrm.contentWindow
        window.addEventListener(
            'message',
            function (e) {
                if (self._remoteSvrUrl.indexOf(e.origin) !== 0) {
                    return
                }
                self._handleRemoteMsg(e.data)
      },
      false
    );
        )
  },
  _callbackFrameReadyList: function () {
    if (this._frameReadyList) {
      var list = this._frameReadyList;
      this._frameReadyList = null;
            var list = this._frameReadyList
            this._frameReadyList = null
      for (var i = 0, len = list.length; i < len; i++) {
        list[i].call(this, this._frameReady);
                list[i].call(this, this._frameReady)
      }
    }
  },
  _pickCallback: function (seqNum, keepInList) {
    var callbackList = this._callbackList;
        var callbackList = this._callbackList
    for (var i = 0, len = callbackList.length; i < len; i++) {
      var cbkInfo = callbackList[i];
            var cbkInfo = callbackList[i]
      if (seqNum === cbkInfo.seq) {
        if (!keepInList) {
          callbackList.splice(i, 1);
                    callbackList.splice(i, 1)
        }
        return cbkInfo;
                return cbkInfo
      }
    }
  },
  _handleRemoteMsg: function (msg) {
    var seqNum = msg["seq"];
        var seqNum = msg.seq
    var cbkInfo = this._pickCallback(seqNum, !!msg["notify"]);
        var cbkInfo = this._pickCallback(seqNum, !!msg.notify)
    if (cbkInfo) {
      cbkInfo.cbk.call(null, msg["error"], msg["result"]);
            cbkInfo.cbk.call(null, msg.error, msg.result)
    } else {
      console.warn("Receive remote msg: ", msg);
            console.warn('Receive remote msg: ', msg)
    }
  },
  _postMessage: function (cmd, args, callback, seq) {
    this._prepareIframe();
        this._prepareIframe()
    var msg = {
      cmd: cmd,
      args: args,
      seq: seq || this._getSeq(),
    };
            seq: seq || this._getSeq()
        }
    this._callbackList.push({
      cbk: callback,
      seq: msg["seq"],
    });
            seq: msg.seq
        })
    this._onRrameReady(function () {
      if (this._frameReady === true) {
        try {
          this._iframeWin.postMessage(msg, "*");
                    this._iframeWin.postMessage(msg, '*')
        } catch (e) {
          this._pickCallback(msg["seq"]);
                    this._pickCallback(msg.seq)
          callback(e);
                    callback(e)
        }
      } else {
        this._pickCallback(msg["seq"]);
                this._pickCallback(msg.seq)
        callback({
          message: "iFrame load failed!",
        });
                    message: 'iFrame load failed!'
                })
      }
    });
        })
  },
  getCurrentPosition: function (succHandler, errHandler, options) {
    this._postMessage("getCurrentPosition", [options], function (err, result) {
        this._postMessage('getCurrentPosition', [options], function (err, result) {
      if (err) {
        if (errHandler) {
          errHandler(err);
                    errHandler(err)
        }
        return;
                return
      }
      if (succHandler) {
        succHandler(result);
                succHandler(result)
      }
    });
        })
  },
  watchPosition: function (succHandler, errHandler, options) {
    var watchKey = "wk" + this._getSeq(),
      cmdSeq = this._getSeq();
        var watchKey = 'wk' + this._getSeq()
        var cmdSeq = this._getSeq()
    this._watchIdMap[watchKey] = {
      stat: 0,
      seq: cmdSeq,
    };
    var self = this;
    this._postMessage(
      "watchPosition",
      [options],
      function (err, result) {
        var id = null;
        if (result) {
          id = result["id"];
            seq: cmdSeq
        }
        var watchInfo = self._watchIdMap[watchKey];
        var self = this
        watchInfo.id = id;
        watchInfo.stat = 1;
        this._postMessage(
            'watchPosition',
            [options],
            function (err, result) {
                var id = null
                if (result) {
                    id = result.id
                }
                var watchInfo = self._watchIdMap[watchKey]
                watchInfo.id = id
                watchInfo.stat = 1
        if (watchInfo.callbackList) {
          var list = watchInfo.callbackList;
          watchInfo.callbackList = null;
                    var list = watchInfo.callbackList
                    watchInfo.callbackList = null
          for (var i = 0, len = list.length; i < len; i++) {
            list[i].call(self, id);
                        list[i].call(self, id)
          }
        }
        if (err) {
          if (errHandler) {
            errHandler(err);
                        errHandler(err)
          }
          return;
                    return
        }
        if (succHandler) {
          succHandler(result["pos"]);
                    succHandler(result.pos)
        }
      },
      cmdSeq
    );
        )
    return watchKey;
        return watchKey
  },
  clearWatch: function (watchKey, callback) {
    if (!this._watchIdMap[watchKey]) {
      callback("Id not exists: " + watchKey);
      return;
            callback('Id not exists: ' + watchKey)
            return
    }
    var watchInfo = this._watchIdMap[watchKey];
        var watchInfo = this._watchIdMap[watchKey]
    var self = this;
        var self = this
    function clearId(id) {
      self._postMessage("clearWatch", [id], function (err, result) {
            self._postMessage('clearWatch', [id], function (err, result) {
        if (!err) {
          self._pickCallback(watchInfo.seq);
                    self._pickCallback(watchInfo.seq)
          delete self._watchIdMap[watchKey];
                    delete self._watchIdMap[watchKey]
        }
        if (callback) {
          callback(err, result);
                    callback(err, result)
        }
      });
            })
    }
    if (watchInfo.stat < 1) {
      if (!watchInfo.callbackList) {
        watchInfo.callbackList = [];
                watchInfo.callbackList = []
      }
      watchInfo.callbackList.push(function (id) {
        clearId(id);
      });
                clearId(id)
            })
    } else {
      clearId(watchInfo.id);
            clearId(watchInfo.id)
    }
  },
};
    }
}
export default RemoGeoLocation;
export default RemoGeoLocation