// pages/takePhoto/takePhoto.js import { uploadFile } from "../../utils/upload"; import { postApi, getApi } from "../../apis/api"; Page({ /** * 页面的初始数据 */ data: { checkinStatus: 0, // 签到状态 checkinDatetime: "", checkinDesc: "", shipOwnerId: 0, locked: false, // 锁定状态 terminalServices: [], // 码头服务列表 terminalSearchWords: "", // 搜索关键词 loading: false, // 加载状态 waterLevels: [], // 水位数据 demolitionPolicy: [], // 拆解政策 shipbuildingPolicy: [], // 造船政策 shipAnnualInspection: [], shipCertificate: [], shipCrewCertificate: [], certOperationContact: [], palletList: [ { id: 5, cargoName: "222", loadPortName: "2", dischargePortName: "3", cargoTons: 4, contactName: "5", contactPhone: "13666666666", status: 1, releaseTime: "2025/05/08 19:52:33", createTime: "2025/05/07 23:36:26", }, { id: 6, cargoName: "测试1", loadPortName: "上海", dischargePortName: "张家港", cargoTons: 111, contactName: "11", contactPhone: "11111111111", status: 1, releaseTime: "2025/05/08 19:52:14", createTime: "2025/05/08 11:06:03", }, ], palletListSearchWords: "", // 货盘搜索关键词 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getTerminalServices(); this.getWaterLevel(); this.getNewEnergyPolicy(); this.getIntelligentService(); this.getPalletList(); this.getCheckinStatus(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { if (typeof this.getTabBar === "function" && this.getTabBar()) { this.getTabBar().setData({ selected: 0, }); } }, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, /** * 获取码头服务数据 */ getTerminalServices(terminalSearchWords = "") { this.setData({ loading: true, }); postApi("/pallet/terminal/services", { searchWords: terminalSearchWords, }) .then((res) => { if (res.data && res.data.status === 0) { this.setData({ terminalServices: res.data.result || [], loading: false, }); } else { this.setData({ loading: false, }); } }) .catch((res) => { this.setData({ loading: false, }); }); }, /** * 搜索码头服务 */ searchTerminalServices() { this.getTerminalServices(this.data.terminalSearchWords); }, /** * 输入框内容变化事件 */ onSearchInput(e) { this.setData({ terminalSearchWords: e.detail.value, }); }, /** * 拨打电话 */ makePhoneCall(e) { const phone = e.currentTarget.dataset.phone; if (phone) { wx.makePhoneCall({ phoneNumber: phone, }); } else { wx.showToast({ title: "电话号码不存在", icon: "none", }); } }, async getWaterLevel() { let { data } = await getApi("/pallet/water/level"); if (data.status === 0) { // 判断data.result是数组内对象waterLevelFluctuation字段的第一个字符如果是负号,新增change字段的值为down,如果是正号,新增change字段的值为up,如果是0,新增change字段的值为zero data.result.forEach((item) => { if (item.waterLevelFluctuation[0] === "-") { item.change = "falling"; item.changeIcon = "↓"; } if (item.waterLevelFluctuation[0] === "+") { item.change = "rising"; item.changeIcon = "↑"; } if (item.waterLevelFluctuation[0] === "0") { item.change = "stable"; } }); this.setData({ waterLevels: data.result, }); } else { this.setData({ waterLevels: [], }); } }, async getNewEnergyPolicy() { let { data } = await getApi("/pallet/new/energy/subsidy/policy"); if (data.status === 0) { this.setData({ demolitionPolicy: data.result.demolitionPolicy, shipbuildingPolicy: data.result.shipbuildingPolicy, }); } else { this.setData({ demolitionPolicy: [], shipbuildingPolicy: [], }); } }, // intelligent/service获取智能服务 async getIntelligentService() { let { data } = await postApi("/pallet/intelligent/service", {}); if (data.status === 0) { this.setData(data.result); } else { this.setData({ shipAnnualInspection: [], shipCertificate: [], shipCrewCertificate: [], certOperationContact: [], }); } }, /** * 获取货盘列表数据 */ async getPalletList(palletListSearchWords = "") { this.setData({ loading: true, }); try { let { data } = await postApi("/pallet/list", { searchWords: palletListSearchWords, }); if (data && data.status === 0) { this.setData({ palletList: data.result || [], loading: false, }); } else { this.setData({ loading: false, palletList: [], }); } } catch (error) { console.log(error); this.setData({ loading: false, }); } }, /** * 搜索货盘列表 */ searchPalletList() { this.getPalletList(this.data.palletListSearchWords); }, /** * 货盘搜索输入框内容变化事件 */ onPalletSearchInput(e) { this.setData({ palletListSearchWords: e.detail.value, }); }, /** * 获取签到状态 */ async getCheckinStatus() { try { let { data } = await getApi("/checkin/wx/checkin/status"); if (data && data.status === 0) { this.setData(data.result); } } catch (error) {} }, checkToken() { if (!wx.getStorageSync("accessToken")) { wx.showToast({ title: "尚未登录,正在体验", icon: "none", }); return false; } return true; }, async onSignIn() { if (!this.checkToken()) return; if (this.data.checkinStatus == 1) return; if (this.data.locked) { wx.showLoading({ title: "高精度定位中...", }); return; } this.data.locked = true; wx.getLocation({ type: "gcj02", isHighAccuracy: true, success: async (e) => { let { latitude, longitude } = e; let { data } = await postApi("/checkin", { latitude, longitude, }); this.setData(data.result); if (data.status == 0) { wx.showToast({ title: data.msg, icon: "success", duration: 2000, mask: true, }); // 更新签到状态 this.getCheckinStatus(); } }, complete: (e) => { wx.hideLoading({}); this.data.locked = false; }, }); }, async takePhoto(e) { if (this.data.locked) { wx.showLoading({ title: "高精度定位中...", }); return; } this.data.locked = true; let { mediatype } = e.currentTarget.dataset; wx.getLocation({ type: "gcj02", isHighAccuracy: true, success: (e) => { let { latitude, longitude } = e; console.log("获取定位成功!", e); this.data.latitude = latitude; this.data.longitude = longitude; wx.setStorageSync("latitude", latitude); wx.setStorageSync("longitude", longitude); wx.chooseMedia({ mediaType: ["image"], sourceType: ["camera"], success: (e) => { console.log("获取媒体成功!", e); let src = e.tempFiles[0].tempFilePath; if (e.type == "video") { wx.showLoading({ title: "正在压缩...", }); wx.compressVideo({ src, quality: "high", bitrate: "", fps: "", resolution: "", success: async (e) => { if (wx.getStorageSync("userName")) { wx.showLoading({ title: "正在上传...", }); let res = await uploadFile(e.tempFilePath, { type: 4, userId: wx.getStorageSync("userId"), location: `${this.data.longitude},${this.data.latitude}`, }); if (res.status == 0) { console.log(res); wx.showToast({ title: res.msg, }); wx.redirectTo({ url: "/pages/takePhoto/success/success", }); } else { wx.showToast({ title: res.msg, }); } } else { // 新用户视频 wx.hideLoading({ success: (res) => {}, }); console.log("新用户视频", e); wx.setStorageSync("type", 2); wx.setStorageSync("file", e.tempFilePath); wx.redirectTo({ url: `/pages/newCachePage/newCachePage`, }); } }, fail: (e) => { console.log(e); }, }); } else { wx.compressImage({ src, quality: 80, // 压缩质量 success: async (e) => { console.log("图片压缩成功!", e); wx.hideLoading({ success: (res) => {}, }); if (wx.getStorageSync("userName")) { wx.showLoading({ title: "正在上传...", }); let postData = { type: mediatype, userId: wx.getStorageSync("userId"), }; if (mediatype == 3) { postData.location = `${this.data.longitude},${this.data.latitude}`; } else { postData.location = ""; } let res = await uploadFile(e.tempFilePath, postData); console.log("上传结束", res); if (res.status == 0) { wx.showToast({ title: res.msg, }); wx.setStorageSync("shareImageUrl", res.result.viewUrl); console.log(wx.getStorageSync("shareImageUrl")); wx.downloadFile({ url: res.result.viewUrl, success: (e) => { console.log("下载调用!", e); wx.setStorageSync("cacheImage", e.tempFilePath); if (mediatype == 3) { wx.saveImageToPhotosAlbum({ filePath: e.tempFilePath, success: (e) => { if (e.errMsg == "saveImageToPhotosAlbum:ok") { wx.showToast({ title: "保存成功!", }); wx.removeStorageSync("cacheImage"); } }, fail: (e) => { console.log("失败4", e); this.setData({ authModal: true, modalText: "文件存储", }); }, }); } wx.redirectTo({ url: "/pages/takePhoto/success/success", }); }, fail: (e) => { console.log("失败3", e); }, }); } else { wx.showToast({ title: res.msg, }); } } else { // 新用户图片 console.log("新用户图片", e); wx.setStorageSync("type", 1); wx.setStorageSync("file", e.tempFilePath); wx.redirectTo({ url: `/pages/newCachePage/newCachePage`, }); } }, fail: (e) => { console.log("失败2", e); }, }); } }, fail: (e) => { console.log("失败1", e); }, }); }, fail: (e) => { this.setData({ authModal: true, modalText: "位置信息", }); }, complete: (e) => { wx.hideLoading({ success: (res) => {}, }); this.data.locked = false; }, }); }, takeBill() { if (this.data.locked) return; wx.redirectTo({ url: "/pages/takeBill/takeBill", }); }, });