iotAR小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

490 lines
13 KiB

import {
getLocation,
getProjectBasicInfo,
stopBeaconScan,
} from "ais-h5-location";
import { STATES, promptStateMap } from "./index-helper";
import { getMapData, post } from "../../getMapData";
let configData;
let map;
const icons = {
left: "/pages/index/left.png",
straight: "/pages/index/straight.png",
right: "/pages/index/right.png",
end: "/pages/index/end.png",
escalatorUp: "/pages/index/escalator-up.png",
escalatorDown: "/pages/index/escalator-down.png",
elevatorUp: "/pages/index/elevator-up.png",
elevatorDown: "/pages/index/elevator-down.png",
};
let locationOption;
let initFloorOrder;
Page({
/**
* 页面的初始数据
*/
data: {
inited: false,
floors: [],
floorOrder: null,
floorId: null,
floorName: null,
windowHeight: 812,
states: STATES,
state: STATES.selectFloor,
promptStateMap,
map: null,
e: null,
loc: null,
tab: 0,
showTabs: false,
shop: null,
nextFloor: null,
nextNextFloor: null,
navIcon: icons.straight,
distanceToNextPoint: null,
totalDistance: 0,
distance: 0,
arriveEnd: false,
safeTop: 54,
searchType: 0,
point: null,
warningCount: 0,
bluetoothOpened: false,
mode: "bluetooth",
naviData: null,
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad({ e, searchType = 0, floorOrder }) {
initFloorOrder = floorOrder;
const { config } = await getMapData();
configData = config;
this.getSystemInfo();
wx.setKeepScreenOn({
keepScreenOn: true,
});
const {
globalData: { floors },
} = getApp();
const { windowHeight } = wx.getSystemInfoSync();
this.setData({
floors,
windowHeight,
e: decodeURIComponent(e),
searchType: Number(searchType),
});
await this.openBluetooth();
this.startBlueTooth();
},
openBluetooth() {
return new Promise((resolve, reject) => {
wx.openBluetoothAdapter({
success: () => {
this.setData({ bluetoothOpened: true });
console.log("bluetoothOpened");
resolve();
},
fail: ({ errno, errCode }) => {
this.setData({ bluetoothOpened: false });
console.warn("蓝牙未开启", errCode || errno);
reject(errCode || errno);
},
});
});
},
async startBlueTooth() {
const ak = configData.map.find(({ ak }) => ak).ak;
console.log("ak:", ak);
if (!ak) return console.warn("no ak");
let projectInfo;
try {
projectInfo = await getProjectBasicInfo(ak);
} catch (error) {
console.log("getProjectBasicInfo", error);
}
try {
if (this.data.bluetoothOpened !== true) await this.openBluetooth();
} catch (err) {
return this.setState(
err === 103 ? STATES.bluetoothRequired : STATES.systemBluetoothRequired
);
}
getLocation({
projectInfo,
intervalTime: 1000, // 定时器(请求频率时长)
successCb: (result) => {
// console.log("getLocation -> successCb", result);
const { location, floor = "" } = result.body && result.body[0];
const floorId = floor && floor.toUpperCase();
const matchedfloor = this.data.floors.find((f) => f.floorId == floorId);
const notSameFloor = matchedfloor && this.data.floorId !== floorId;
if (!matchedfloor) {
this.finishNavigation();
return this.setState(STATES.fail8s);
}
this.setData({
floorId,
floorOrder: matchedfloor.floorOrder,
floorName: matchedfloor.name,
});
// pointX、pointY格式处理
const splitArray = location && location.split(",");
const pointX = parseFloat(splitArray[0]);
const pointY = parseFloat(splitArray[1]);
// 过滤蓝牙location 无效值
let isValidLocation;
if (splitArray && splitArray.length) {
isValidLocation = pointX > 1 && pointY > 1;
}
if (!isValidLocation) return;
/**
* vpas updateBluetoothLocation 触发条件:
* 选择楼层 等于 蓝牙返回楼层时
* indoor = 1(室内)
*/
console.log(pointX, pointY);
const x = (pointX - 118.785893) * 3000000;
const y = -(pointY - 32.088021) * 6000000;
if (!this.data.loc) {
this.firstSuccess(x, y);
}
console.log(x, y);
this.setData({
loc: [x, y],
});
if (notSameFloor) {
this.reroute();
}
},
failCb: (e) => {
console.log("failCb", e);
try {
const { errCode, errno } = e;
if (errCode === 11000) {
console.log("系统蓝牙未开启");
return this.setState(STATES.systemBluetoothRequired);
} else if (errno && errno === 1) {
console.log("未扫描到附近蓝牙信息");
this.finishNavigation();
return this.setState(STATES.fail8s);
}
} catch (error) {
console.log("failCb error", error);
}
},
});
},
firstSuccess(x, y) {
this.setData({
totalDistance: 0,
loc: [x, y],
state: STATES.successPrompt,
});
console.log("firstSuccess---", this.data.e);
if (!map) return;
let shop = map.shopMap[this.data.e]
? map.shopMap[this.data.e]
: map.facilityMap[this.data.e]
? map.facilityMap[this.data.e]
: map.pMap[this.data.e];
if (!shop) {
const endParamList = this.data.e.split("_");
if (endParamList.length === 3) {
let [efloororder, epoint, ename] = endParamList;
efloororder = Number(efloororder);
epoint = Number(epoint);
shop = {
isDevice: true,
name: ename,
navPoint: epoint,
yaxis: epoint,
floorOrder: efloororder,
floorName: map.mall.floors[efloororder][1],
};
}
}
console.log("end", shop);
if (!shop)
return wx.showToast({
icon: "none",
title: `未匹配到该终点:${this.data.e}`,
});
this.setData({
shop: {
...shop,
logoPath: shop.logoPath ? shop.logoPath.replaceAll("\\", "/") : null,
},
});
this.reroute();
setTimeout(() => {
this.setState(STATES.inNav);
this.setData({
showTabs: true,
tab: 1,
});
}, 1000);
},
reroute() {
const {
loc: [x, y],
shop,
} = this.data;
const { naviData, nextFloorId, nextNextFloorId } = map.requestRoute(
[x, y, this.data.floorId],
{ floorOrder: shop.floorOrder, navPoint: shop.yaxis }
);
if (!naviData) return;
let nextFloor = this.data.floors.find(
({ floorId }) => nextFloorId == floorId
);
let nextNextFloor = this.data.floors.find(
({ floorId }) => nextNextFloorId == floorId
);
this.setData({ naviData, nextFloor, nextNextFloor });
},
exit() {
wx.redirectTo({
url: "/pages/h5map/index",
});
},
handleToMap() {
this.setState(STATES.tomap);
},
handlePromptBtn({ detail: { btn } }) {
const { state } = this.data;
if (state === STATES.bluetoothRequired) {
if (btn === "1")
return wx.openSetting({
withSubscriptions: true,
success: ({ authSetting }) => {
if (authSetting["scope.bluetooth"]) {
this.setState(STATES.startScan);
this.startBlueTooth();
}
},
});
if (btn === "2") return this.exit();
}
if (state === STATES.systemBluetoothRequired) {
if (btn === "1") return this.exit();
}
if (state === STATES.fail8s) {
if (btn === "1") return this.handleRelocate();
if (btn === "2") return this.setState(STATES.selectFloor);
if (btn === "3") return this.exit();
}
if (state === STATES.wrongLocation) return this.exit();
if (state === STATES.badConnection) return this.handleRelocate();
if (
state === STATES.arriveEscalator ||
state === STATES.arriveElevator ||
state === STATES.arriveMall ||
state === STATES.arrivePark
) {
const nextFloor = this.data.nextNextFloor
? this.data.nextNextFloor
: this.data.nextFloor;
if (nextFloor)
this.setData({
floorOrder: nextFloor.floorOrder,
point: null,
floorId: nextFloor.floorId,
floorName: nextFloor.name,
});
return this.handleRelocate();
}
if (state === STATES.requestEnd || state === STATES.arriveEnd) {
if (btn === "1") return this.exit();
if (btn === "2") return this.setState(STATES.inNav);
}
if (state === STATES.noCamera) {
if (btn === "1")
return wx.openSetting({
withSubscriptions: true,
success: ({ authSetting }) => {
if (authSetting["scope.camera"]) {
wx.redirectTo({
url: `/pages/index/index?e=${this.data.e}`,
});
}
},
});
if (btn === "2") return this.exit();
}
if (state === STATES.tomap) {
if (btn === "1")
return wx.redirectTo({
url: `/pages/h5map/index?e=${this.data.e}`,
});
if (btn === "2") return this.setState(STATES.inNav);
}
},
setState(state) {
if (
(this.data.state === STATES.arriveEscalator ||
this.data.state === STATES.arriveElevator ||
this.data.state === STATES.arriveMall ||
this.data.state === STATES.arrivePark) &&
state === STATES.inNav
)
return console.log("到达弹窗出现忽略导航中状态变更触发");
if (
state === STATES.systemBluetoothRequired &&
this.data.state !== STATES.systemBluetoothRequired
) {
systemBluetoothInterval = setInterval(() => {
this.openBluetooth()
.then(() => {
if (systemBluetoothInterval) {
clearInterval(systemBluetoothInterval);
systemBluetoothInterval = null;
}
this.setState(STATES.startScan);
this.startBlueTooth();
})
.catch((err) => {
this.setState(
err === 103
? STATES.bluetoothRequired
: STATES.systemBluetoothRequired
);
});
}, 1000);
}
if (
this.data.state === STATES.systemBluetoothRequired &&
state === STATES.bluetoothRequired
)
return;
this.setData({
state,
});
},
handleMap({ detail }) {
map = detail;
this.setData({
inited: true,
});
const { floors } = this.data;
const floor = floors[initFloorOrder];
initFloorOrder = undefined;
if (floor) {
this.setData({
floorOrder: floor.floorOrder,
point: null,
floorId: floor.floorId,
floorName: floor.name,
});
}
},
handleTabChange({ detail }) {
this.setData({
tab: detail,
});
},
handleRelocate() {
stopBeaconScan();
this.setData({ loc: null });
this.startBlueTooth();
},
handleDistance({ detail: { minDistance: distance, distance2End } }) {
console.log(
"偏移距离:",
distance / 27,
"距离区间终点距离:",
distance2End / 27
);
if (distance / 27 > 10 && this.data.state === STATES.inNav) {
this.reroute();
}
},
handleNavExit() {
this.setState(STATES.requestEnd);
},
handleNoCamera() {
this.setState(STATES.noCamera);
},
getSystemInfo() {
const res = wx.getSystemInfoSync();
console.log("设备信息:", res);
this.setData({
safeTop: res.safeArea.top,
windowHeight: res.windowHeight,
});
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.getSystemInfo();
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
this.finishNavigation();
},
finishNavigation() {
try {
if (systemBluetoothInterval) {
clearInterval(systemBluetoothInterval);
systemBluetoothInterval = null;
}
stopBeaconScan();
} catch (error) {}
this.setData({ totalDistance: 0 });
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {},
/**
* 用户点击右上角分享
*/
onShareAppMessage({ from }) {
const e =
from === "button" && this.data.point !== null
? `${this.data.floorOrder}_${this.data.point}_来自分享的位置`
: null;
return e === null
? {
title: "",
path: "/pages/h5map/index",
imageUrl: "/pages/h5map/share.png",
}
: {
title: "来自分享的位置",
path: `/pages/h5map/index?e=${e}`,
imageUrl: "/pages/h5map/share.png",
};
},
handlePoint({ detail }) {
this.setData({ point: detail });
},
});