Przeglądaj źródła

feat(takePhoto): 新增签到功能和拍摄货物、单据功能

- 新增签到状态获取和签到功能
- 实现拍摄货物和单据的照片功能
- 优化页面布局和按钮显示逻辑
- 修复了一些代码问题,提高了代码可读性
wzg 8 miesięcy temu
rodzic
commit
e6c39c31fa

+ 2 - 3
miniprogram/pages/index/index.js

@@ -13,14 +13,13 @@ Page({
     let { data } = await postApi("/login/openid", {
       openId: wx.getStorageSync("openId"),
     });
-
     if (data.status == 0) {
       let obj = {
         ...data.result.userInfo,
         ...data.result.shipInfo,
       };
-      Object.keys(data).forEach(function (key) {
-        wx.setStorageSync(key, data[key]);
+      Object.keys(obj).forEach(function (key) {
+        wx.setStorageSync(key, obj[key]);
       });
       wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
     }

+ 254 - 0
miniprogram/pages/takePhoto/takePhoto.js

@@ -1,10 +1,17 @@
 // 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, // 加载状态
@@ -53,6 +60,7 @@ Page({
     this.getNewEnergyPolicy();
     this.getIntelligentService();
     this.getPalletList();
+    this.getCheckinStatus();
   },
 
   /**
@@ -285,4 +293,250 @@ Page({
       palletListSearchWords: e.detail.value,
     });
   },
+  /**
+   * 获取签到状态
+   */
+  async getCheckinStatus() {
+    try {
+      let { data } = await getApi("/checkin/wx/checkin/status");
+      if (data && data.status === 0) {
+        this.setData(data.result);
+      } else {
+        wx.showToast({
+          title: data.msg || "获取签到状态失败",
+          icon: "none",
+        });
+      }
+    } catch (error) {
+      console.log(error);
+      wx.showToast({
+        title: "网络请求失败",
+        icon: "none",
+      });
+    }
+  },
+
+  async onSignIn() {
+    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",
+    });
+  },
 });

+ 8 - 6
miniprogram/pages/takePhoto/takePhoto.wxml

@@ -5,9 +5,11 @@
   <view class="content">
     <!-- 签到、拍货物、拍单据按钮 -->
     <view class="btn-group">
-      <view class="btn btn-green" bindtap="onSignIn">签到</view>
-      <view class="btn btn-blue" bindtap="onTakeCargoPhoto">拍货物</view>
-      <view class="btn btn-red" bindtap="onTakeDocPhoto">拍单据</view>
+      <view class="btn btn-green" bindtap="onSignIn">
+        {{checkinStatus==0?'签到':'已签到: '+checkinDatetime}}
+      </view>
+      <view class="btn btn-blue" bindtap="takePhoto" data-mediatype="{{3}}">拍货物</view>
+      <view class="btn btn-red" bindtap="takeBill" data-mediatype="{{0}}">拍单据</view>
     </view>
     <!-- 码头服务查询 -->
     <view class="card">
@@ -146,14 +148,14 @@
           <input type="text" class="search-input" placeholder="输入关键词搜索" bindinput="onPalletSearchInput" value="{{palletListSearchWords}}" />
           <view class="search-btn" bindtap="searchPalletList">搜索</view>
         </view>
-        <view class="map-container">
+        <!-- <view class="map-container">
           <view>地图区域 - 显示船舶与货盘</view>
-          <!-- 这里可以使用微信小程序的map组件替换 -->
+          这里可以使用微信小程序的map组件替换
           <view class="map-marker ship"></view>
           <view class="map-marker cargo" style="top: 30%; left: 60%"></view>
           <view class="map-marker cargo" style="top: 50%; left: 30%"></view>
           <view class="map-marker cargo" style="top: 60%; left: 55%"></view>
-        </view>
+        </view> -->
         <!-- 货盘列表 -->
         <view class="cargo-list">
           <view class="cargo-item" wx:for="{{palletList}}">

+ 13 - 12
miniprogram/utils/upload.js

@@ -1,22 +1,23 @@
-import {
-  apiUrl
-} from "../apis/apiConfig"
+import { apiUrl } from "../apis/apiConfig";
 
 function uploadFile(filePath, formData) {
   return new Promise((resolve, reject) => {
     wx.uploadFile({
-      url: `${apiUrl}/cos/upload`,
+      url: `${apiUrl}/file/upload`,
       filePath,
-      name: 'file',
+      name: "file",
+      header: {
+        accessToken: wx.getStorageSync("accessToken"),
+      },
       formData,
-      success: e => {
-        resolve(JSON.parse(e.data))
+      success: (e) => {
+        resolve(JSON.parse(e.data));
       },
-      fail: reject
-    })
-  })
+      fail: reject,
+    });
+  });
 }
 
 module.exports = {
-  uploadFile
-}
+  uploadFile,
+};