微信小程序 上传图片跳转页面-接收数据并调用接口-重新识别图片

展示:

首页:home

下一页:scan

home.wxml:

 <view class="camera-icon" bindtap="openCamera">
      <image src="/static/images/icon/camera.png" mode="aspectFill" style="width:18px;height:18px;margin:0 0 0 2px" />
    </view>  

home.js:


    // 打开相册选择图片
    openCamera() {
      console.log("11111")
      wx.chooseImage({
        count: 1, // 限制选择 1 张图片
        sizeType: ['original', 'compressed'], // 可以选择原图或压缩图
        sourceType: ['album', 'camera'], // 支持从相册或相机选择
        success: (res) => {
          const selectedImage = res.tempFilePaths[0]; // 获取选择的图片路径
          // 跳转到 scan 页面并传递图片路径
          wx.navigateTo({
            url: `/pages/scan/scan?imagePath=${encodeURIComponent(selectedImage)}`,
          });
        },
        fail: (err) => {
          console.log("选择图片失败", err);
        },
      });
    },

scan.wxml:

<view>


	<!-- 响应区域 -->
	<view class="response-container">
		<view class="image-container">
			<image src="{{imagePath}}" style="width: 100%;height:100%;" />
		</view>
		<view class="text-container">

		</view>
		<view bindtap="reupload" class="retry-button">
			<text>重新传图</text>
		</view>
	</view>

	<!-- 相关结果 -->
	<view wx:if="{{response}}" class="result-section">
		<view>相关结果:</view>
		<view wx:for="{{response.best_matches}}" wx:key="index">
			<image src="{{item.pic}}" />
			<text>{{item.image_name}}</text>
		</view>
	</view>

</view>

scan.js:

// 引用公共配置文件
const config = require('../../utils/config.js');

Page({
  data: {
    response: null, // 接收返回的数据
    imagePath: '',  // 默认图片路径
  },

  onLoad(options) {
    // 获取从上一个页面传递过来的参数
    if (options.imagePath) {
      this.setData({
        imagePath: decodeURIComponent(options.imagePath)  // 解码图片路径
      });

      // 如果图片路径存在,调用上传图片的方法
      this.uploadImage();
    } else {
      wx.showToast({
        title: '未选择图片',
        icon: 'none',
      });
    }
  },

  // 上传图片的方法
  uploadImage() {
    if (!this.data.imagePath) {
      wx.showToast({
        title: '图片路径不存在',
        icon: 'none',
      });
      return;
    }

    wx.showLoading({
      title: '加载中...',
    });

    wx.uploadFile({
      url: `${config.SCAN_URL}/find_match`, // 后端接口
      filePath: this.data.imagePath, // 需要上传的文件路径
      name: 'query_image', // 后端接收的字段名称
      formData: {
        additionalField: 'value', // 示例:传递额外的字段
      },
      success: (uploadRes) => {
        const data = JSON.parse(uploadRes.data); // 假设返回的数据是 JSON 格式

        // 为每个元素增加 pic 字段
        if (data && Array.isArray(data.best_matches)) {
          const newData = data.best_matches.map(item => ({
            ...item,
            pic: `${config.SCAN_URL}/api/${item.directory}/${item.file_name}`, // 拼接图片完整路径
          }));

          // 将修改后的数据更新到页面
          this.setData({
            response: {
              ...data,
              best_matches: newData, // 更新带有 pic 字段的数据
            },
          });
        } else {
          wx.showToast({
            title: '无匹配结果',
            icon: 'none',
          });
        }

        // 上传成功后隐藏加载中
        wx.hideLoading();
      },
      fail: (err) => {
        console.error('上传失败', err);

        // 上传失败后隐藏加载中
        wx.hideLoading();

        // 弹出提示信息
        wx.showToast({
          title: '上传失败,请重试',
          icon: 'none',
        });
      },
    });
  },

  // 点击重新上传图片
  reupload() {
    wx.chooseImage({
      count: 1, // 限制选择 1 张图片
      sizeType: ['original', 'compressed'], // 可以选择原图或压缩图
      sourceType: ['album', 'camera'], // 支持从相册或相机选择
      success: (res) => {
        const selectedImage = res.tempFilePaths[0]; // 获取选择的图片路径
        this.setData({
          imagePath: selectedImage, // 更新图片路径
          response: null, // 清空之前的响应数据
        });
        this.uploadImage(); // 重新上传图片
      },
      fail: (err) => {
        console.log("选择图片失败", err);
      },
    });
  },
});

scan.wxss:

/* 上传区域 */
.upload-container {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.upload-section {
  margin-top: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.upload-btn {
  background: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%);
  padding: 40px;
  border-radius: 10px;
  border: 2px dashed #007aff;
  display: inline-block;
  cursor: pointer;
  text-align: center;
  display: flex;
  align-items: center;
}

.upload-icon {
  width: 60px;
  height: 60px;
  margin-bottom: 10px;
}

.upload-text {
  font-size: 16px;
  color: #007aff;
}

.image-preview {

  
}

.response-container {
  width: 100%;
  height: 100px;
  background-color: #2a2a2a;
  color: white;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.image-container {
  margin-left: 10px;
  margin-top: 40px;
  width: 100px;
  height: 100px;
  background-color: white;
  border: 1px solid #999;
}

.text-container {
  flex: 1;
  margin-left: 10px;
  margin-right: 20px;
  margin-top: 0px;
  height: 60px;
  overflow: hidden;
  white-space: normal;
  word-wrap: break-word;
}

.retry-button {
  text-align: center;
  font-size: 12px;
  margin-right: 10px;
}

.retry-icon {
  width: 40px;
  height: 40px;
}

.result-section {
  margin-top: 20px;
  padding: 10px;
}

.match-item {
  margin-top: 20px;
}

.match-item text {
  display: block;
  margin-bottom: 5px;
}

### 实现微信小程序通过扫码跳转至微信公众号完成支付 #### 配置微信公众平台与小程序关联 为了使微信小程序能够顺利跳转到微信公众号完成支付操作,需确保两者已在微信公众平台上进行了绑定。这一步骤可通过微信公众平台后台设置来完成[^1]。 #### 创建带有参数的二维码用于跳转 创建特定格式的二维码,该二维码应包含指向目标微信公众号页面的信息以及必要的业务参数。当用户扫描此二维码时,会触发微信客户端识别其中路径(path),进而加载对应的网页或执行相应动作。对于希望传递给接收端的数据,则可以通过附加于path后的查询字符串形式加入[^3]。 ```json { "path": "/pages/index?source=miniProgram&orderNo=20230718123456" } ``` 上述JSON对象定义了一个简单的示例,`/pages/index`代表要访问的目标页面地址;而后面的问号后面的部分则是作为URL参数被发送出去供后续处理使用的订单编号(orderNo)等信息。 #### 处理来自小程序的请求 在接收到由小程序发起的请求之后,服务器端应当验证传入数据的有效性和安全性,依据实际需求调用微信支付API接口启动交易过程。通常情况下,这部分逻辑会在服务端实现,利用官方提供的SDK简化集成难度的同时也提高了系统的稳定性和可靠性[^2]。 #### 调用微信支付插件完成付款 最后,在确认所有前置条件均已满足的前提下,引导客户按照提示输入密码或其他认证方式以最终完成整个购物流程。值得注意的是,由于涉及到资金安全问题,因此在整个过程中务必遵循严格的权限控制机制,防止任何潜在风险的发生[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值