305 lines
12 KiB
JavaScript
305 lines
12 KiB
JavaScript
// 封一些公共方法
|
|
import api from './api'
|
|
// 判断小程序端中 是否已经有了位置权限
|
|
export function handleHavePointInMin() {
|
|
return new Promise((resolve, reject) => {
|
|
uni.getSetting({
|
|
success(res) {
|
|
// 判断现在有没有拿到微信信息的权限 没有的话 就去跳转设置
|
|
if (!res.authSetting["scope.userFuzzyLocation"]) {
|
|
uni.showModal({
|
|
title: "注意",
|
|
content: "拒绝授权位置服务会导致小程序功能无法正常使用,建议授权位置信息",
|
|
confirmText: "去设置",
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
uni.openSetting({
|
|
success(settingRes) {
|
|
console.log("settingRes", settingRes);
|
|
// 如果点击了同意就去拿位置 存起来
|
|
if (settingRes.authSetting["scope.userFuzzyLocation"]) {
|
|
uni.getFuzzyLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
success: async (res) => {
|
|
let seatInfo = {
|
|
latitude: res.latitude,
|
|
longitude: res.longitude,
|
|
};
|
|
uni.setStorageSync(
|
|
"seatInfo",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
uni.setStorageSync(
|
|
"actualLocation",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
resolve(seatInfo)
|
|
// _this.seatInfo = seatInfo;
|
|
// _this.handleGetOnLoad();
|
|
},
|
|
});
|
|
}
|
|
},
|
|
});
|
|
} else if (res.cancel) {
|
|
console.log("用户点击取消");
|
|
}
|
|
},
|
|
});
|
|
} else {
|
|
let seatInfo = uni.getStorageSync("seatInfo")
|
|
if (seatInfo) {
|
|
return JSON.parse(seatInfo)
|
|
} else {
|
|
uni.getFuzzyLocation({
|
|
type: "gcj02",
|
|
altitude: true,
|
|
success: async (res) => {
|
|
let seatInfo = {
|
|
latitude: res.latitude,
|
|
longitude: res.longitude,
|
|
};
|
|
uni.setStorageSync(
|
|
"seatInfo",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
uni.setStorageSync(
|
|
"actualLocation",
|
|
JSON.stringify(seatInfo)
|
|
);
|
|
resolve(seatInfo)
|
|
},
|
|
});
|
|
}
|
|
}
|
|
},
|
|
});
|
|
})
|
|
}
|
|
|
|
// 判断在 安卓 app端中 是否有了位置权限
|
|
export function handleHavePointInApp() {
|
|
return new Promise((resolve, reject) => {
|
|
const context = plus.android.runtimeMainActivity();
|
|
const PackageManager = plus.android.importClass("android.content.pm.PackageManager");
|
|
const hasPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION") === PackageManager.PERMISSION_GRANTED;
|
|
|
|
// true 的时候 就是有了位置权限
|
|
if (hasPermission) {
|
|
let seatInfo = uni.getStorageSync("seatInfo")
|
|
if (seatInfo) {
|
|
return JSON.parse(seatInfo)
|
|
} else {
|
|
plus.geolocation.getCurrentPosition(
|
|
async function (position) {
|
|
console.log("当前位置:", position);
|
|
let res = position.coords;
|
|
let seatInfo = {
|
|
latitude: res.latitude,
|
|
longitude: res.longitude,
|
|
};
|
|
uni.setStorageSync("seatInfo", JSON.stringify(seatInfo));
|
|
uni.setStorageSync("actualLocation", JSON.stringify(seatInfo));
|
|
_this.seat = seatInfo;
|
|
let latitude = res.latitude;
|
|
let longitude = res.longitude;
|
|
return {
|
|
latitude: latitude,
|
|
longitude: longitude
|
|
}
|
|
}
|
|
);
|
|
}
|
|
} else {
|
|
uni.showModal({
|
|
title: "注意",
|
|
content: "拒绝授权位置服务会导致小程序功能无法正常使用,建议授权位置信息",
|
|
confirmText: "去设置",
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
const main = plus.android.runtimeMainActivity();
|
|
const Intent = plus.android.importClass(
|
|
"android.content.Intent"
|
|
);
|
|
const Settings = plus.android.importClass(
|
|
"android.provider.Settings"
|
|
);
|
|
const intent = new Intent(
|
|
Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
|
);
|
|
const uri = plus.android.invoke(
|
|
"android.net.Uri",
|
|
"parse",
|
|
"package:" + main.getPackageName()
|
|
);
|
|
intent.setData(uri);
|
|
main.startActivity(intent);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
// 拿到最近的服务区数据
|
|
export async function handleGetNearService(_this, longitude, latitude) {
|
|
|
|
let seatInfo = JSON.parse(uni.getStorageSync("seatInfo"));
|
|
let req = {
|
|
Province_Code: "530000",
|
|
longitude: longitude ? longitude : seatInfo.longitude ? seatInfo.longitude : '',
|
|
latitude: latitude ? latitude : seatInfo.latitude ? seatInfo.latitude : '',
|
|
};
|
|
uni.showLoading({
|
|
title: "查找最近的服务区...",
|
|
});
|
|
// const data = await _this.$api.$get(
|
|
// "/CommercialApi/BaseInfo/GetServerpartList",
|
|
// req
|
|
// );
|
|
const data = await this.$api.$javaGet('/third-party/getServerPartList', req)
|
|
let _data = data.Result_Data.List;
|
|
let obj = handleChangeServiceInfo(_data[0]);
|
|
console.log('publicMethods', obj);
|
|
uni.setStorageSync("currentService", obj); // 当前选中服务区信息
|
|
uni.hideLoading();
|
|
return obj
|
|
}
|
|
|
|
// 兼容一下老接口要的服务区对象的内容
|
|
function handleChangeServiceInfo(obj) {
|
|
let newObj = {
|
|
...obj,
|
|
Distance: obj.SERVERPART_DISTANCE,
|
|
OwnerUnitId: obj.OWNERUNIT_ID,
|
|
OwnerUnitName: obj.OWNERUNIT_NAME,
|
|
ProvinceCode: "530000",
|
|
ServerPart_Id: obj.SERVERPART_ID,
|
|
ServerPart_Name: obj.SERVERPART_NAME,
|
|
ServerPart_Tel: "",
|
|
ServerPart_X: obj.SERVERPART_X,
|
|
ServerPart_Y: obj.SERVERPART_Y,
|
|
showName: `${obj.SERVERPART_NAME}(${obj.SERVERPART_DISTANCE}km)`,
|
|
};
|
|
return newObj;
|
|
}
|
|
|
|
// 计算经纬度之间的距离
|
|
export function getDistanceBetweenCoordinates(newCoord, oldCoord) {
|
|
// 解构赋值,提取经纬度
|
|
const { latitude: lat1, longitude: lon1 } = newCoord;
|
|
const { latitude: lat2, longitude: lon2 } = oldCoord;
|
|
|
|
// 地球半径(单位:公里)
|
|
const R = 6371;
|
|
// 将经纬度从度数转为弧度
|
|
const rad = Math.PI / 180;
|
|
const dLat = (lat2 - lat1) * rad;
|
|
const dLon = (lon2 - lon1) * rad;
|
|
|
|
// Haversine 公式计算
|
|
const a =
|
|
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
Math.cos(lat1 * rad) * Math.cos(lat2 * rad) *
|
|
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
|
|
const distance = R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
|
|
// 返回保留两位小数的结果
|
|
return Math.round(distance * 100) / 100;
|
|
}
|
|
|
|
// 商城和 点餐的订单存缓存的方法
|
|
export async function handleSaveOrderStore() {
|
|
// 商城订单数据
|
|
const req = {
|
|
action_type: "GetMallOrderList",
|
|
salebillType: '3000,3001,3002,3999',
|
|
salebillState: "",
|
|
PageIndex: 1,
|
|
pageSize: 99999,
|
|
ownerUnitId: 911,
|
|
requestType: "application/x-www-form-urlencoded",
|
|
noLoading: true
|
|
};
|
|
const data = await api.postCoop(req);
|
|
console.log('商城订单', data);
|
|
let mallOrderCountList = []
|
|
if (data.ResultCode === "100") {
|
|
let list = data.Data.List
|
|
// 待付款
|
|
let obligation = []
|
|
// 待发货
|
|
let pendingShipment = []
|
|
// 待收货
|
|
let pendingReceiptOfGoods = []
|
|
|
|
if (list && list.length > 0) {
|
|
list.forEach((item) => {
|
|
if (item.SALEBILL_STATE === 1005) {
|
|
obligation.push(item)
|
|
} else if (item.SALEBILL_STATE === 1010) {
|
|
pendingShipment.push(item)
|
|
} else if (item.SALEBILL_STATE === 2010) {
|
|
pendingReceiptOfGoods.push(item)
|
|
}
|
|
})
|
|
}
|
|
|
|
mallOrderCountList = [obligation && obligation.length > 0 ? obligation.length : 0,
|
|
pendingShipment && pendingShipment.length > 0 ? pendingShipment.length : 0,
|
|
pendingReceiptOfGoods && pendingReceiptOfGoods.length > 0 ? pendingReceiptOfGoods.length : 0,]
|
|
}
|
|
|
|
uni.setStorageSync("shopOrderStatus", mallOrderCountList)
|
|
|
|
const reqFood = {
|
|
action_type: "GetOrderList",
|
|
salebillType: 6000,
|
|
salebillState: "",
|
|
PageIndex: 1,
|
|
pageSize: 99999,
|
|
ownerUnitId: 911,
|
|
requestType: "application/x-www-form-urlencoded",
|
|
noLoading: true
|
|
};
|
|
const dataFood = await api.postCoop(reqFood);
|
|
let foodOrderCountList = []
|
|
if (dataFood.ResultCode === "100") {
|
|
let list = dataFood.Data.List
|
|
// 待付款
|
|
let obligation = []
|
|
// 待接单
|
|
let pendingShipment = []
|
|
// 制作中
|
|
let pendingReceiptOfGoods = []
|
|
|
|
if (list && list.length > 0) {
|
|
list.forEach((item) => {
|
|
if (item.SALEBILL_STATE === 1005) {
|
|
obligation.push(item)
|
|
} else if (item.SALEBILL_STATE === 1010) {
|
|
pendingShipment.push(item)
|
|
} else if (item.SALEBILL_STATE === 2000) {
|
|
pendingReceiptOfGoods.push(item)
|
|
}
|
|
})
|
|
}
|
|
|
|
foodOrderCountList = [obligation && obligation.length > 0 ? obligation.length : 0,
|
|
pendingShipment && pendingShipment.length > 0 ? pendingShipment.length : 0,
|
|
pendingReceiptOfGoods && pendingReceiptOfGoods.length > 0 ? pendingReceiptOfGoods.length : 0,]
|
|
}
|
|
|
|
uni.setStorageSync("foodOrderStatus", foodOrderCountList)
|
|
}
|
|
|
|
export function handleFormatDate(dateTimeStr) {
|
|
if (!dateTimeStr) return '';
|
|
const d = new Date(dateTimeStr.replace(/-/g, '/')); // 兼容 iOS
|
|
const y = d.getFullYear();
|
|
const m = String(d.getMonth() + 1).padStart(2, '0');
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
return `${y}-${m}-${day}`;
|
|
} |