垃圾分类小程序,云开发 (附源码)

image 速览

  

部分主要源码

<!--index.wxml-->
<view class="container">

  <!-- 用户 openid -->
  <view class="userinfo">
    <button 
      open-type="getUserInfo" 
      bindgetuserinfo="onGetUserInfo"
      class="userinfo-avatar"
      style="background-image: url({{avatarUrl}})"
    ></button>
    <view>
      <button class="userinfo-nickname" bindtap="onGetOpenid">点击获取 openid</button>
    </view>
  </view>


  <!-- 上传图片 -->
  <view class="uploader">
    <view class="uploader-text" bindtap="doUpload">
      <text>上传图片</text>
    </view>
    <view class="uploader-container" wx:if="{{imgUrl}}">
      <image class="uploader-image" src="{{imgUrl}}" mode="aspectFit" bindtap="previewImg"></image>
    </view>
  </view>


  <!-- 操作数据库 -->
  <view class="uploader">
    <navigator url="../databaseGuide/databaseGuide" open-type="navigate" class="uploader-text">
      <text>前端操作数据库</text>
    </navigator>
  </view>

  <!-- 新建云函数 -->
  <view class="uploader">
    <navigator url="../addFunction/addFunction" open-type="navigate" class="uploader-text">
      <text>快速新建云函数</text>
    </navigator>
  </view>

  <!-- 云调用 -->
  <view class="uploader">
    <navigator url="../openapi/openapi" open-type="navigate" class="uploader-text">
      <text>云调用</text>
    </navigator>
  </view>


</view>


/**index.wxss**/

page {
  background: #f6f6f6;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.userinfo, .uploader, .tunnel {
  margin-top: 40rpx;
  height: 140rpx;
  width: 100%;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-left: none;
  border-right: none;
  display: flex;
  flex-direction: row;
  align-items: center;
  transition: all 300ms ease;
}

.userinfo-avatar {
  width: 100rpx;
  height: 100rpx;
  margin: 20rpx;
  border-radius: 50%;
  background-size: cover;
  background-color: white;
}

.userinfo-avatar:after {
  border: none;
}

.userinfo-nickname {
  font-size: 32rpx;
  color: #007aff;
  background-color: white;
  background-size: cover;
}

.userinfo-nickname::after {
  border: none;
}

.uploader, .tunnel {
  height: auto;
  padding: 0 0 0 40rpx;
  flex-direction: column;
  align-items: flex-start;
  box-sizing: border-box;
}

.uploader-text, .tunnel-text {
  width: 100%;
  line-height: 52px;
  font-size: 34rpx;
  color: #007aff;
}

.uploader-container {
  width: 100%;
  height: 400rpx;
  padding: 20rpx 20rpx 20rpx 0;
  display: flex;
  align-content: center;
  justify-content: center;
  box-sizing: border-box;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.uploader-image {
  width: 100%;
  height: 360rpx;
}

.tunnel {
  padding: 0 0 0 40rpx;
}

.tunnel-text {
  position: relative;
  color: #222;
  display: flex;
  flex-direction: row;
  align-content: center;
  justify-content: space-between;
  box-sizing: border-box;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.tunnel-text:first-child {
  border-top: none;
}

.tunnel-switch {
  position: absolute;
  right: 20rpx;
  top: -2rpx;
}

.disable {
  color: #888;
}

.service {
  position: fixed;
  right: 40rpx;
  bottom: 40rpx;
  width: 140rpx;
  height: 140rpx;
  border-radius: 50%;
  background: linear-gradient(#007aff, #0063ce);
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
  display: flex;
  align-content: center;
  justify-content: center;
  transition: all 300ms ease;
}

.service-button {
  position: absolute;
  top: 40rpx;
}

.service:active {
  box-shadow: none;
}

.request-text {
  padding: 20rpx 0;
  font-size: 24rpx;
  line-height: 36rpx;
  word-break: break-all;
}


//index.js
const app = getApp()

Page({
  data: {
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    takeSession: false,
    requestResult: ''
  },

  onLoad: function() {
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

  onGetUserInfo: function(e) {
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
    }
  },

  onGetOpenid: function() {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', JSON.stringify(res))
        app.globalData.openid = res.result.openid
        wx.navigateTo({
          url: '../userConsole/userConsole',
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        wx.navigateTo({
          url: '../deployFunctions/deployFunctions',
        })
      }
    })
  },

  // 上传图片
  doUpload: function () {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        
        // 上传图片
        const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功:', res)

            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
            
            wx.navigateTo({
              url: '../storageConsole/storageConsole'
            })
          },
          fail: e => {
            console.error('[上传文件] 失败:', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      }
    })
  },

})

  

   

部分主要源码:

<view class='top-view'>
  <text>保护环境,人人有责</text>
</view>
<view class="cu-bar bg-darkGray search"bindtap="goSearch">
  <view class=" search-form  round ">
    <text class="cuIcon-search text-green"></text>
    <input type="text" placeholder="输入垃圾名称" confirm-type="search" bindinput="searchIcon"></input>
  </view>
</view>
<view class="grid col-2 padding-sm">
  <view class="padding-sm " wx:for="{{ColorList}}" bindtap="onClick"  data-index='{{index}}' wx:key>
    <image class="icon" src='{{item}}'></image>
  </view>
</view>
<ad unit-id="adunit-0f31496a9162efb6"></ad>


.icon{
  width: 220rpx;
  height: 220rpx;
  margin: 20rpx 40rpx;
  border-radius: 20rpx;
}
.padding-sm{
  width: 100%;
}
.top-view{
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin-top: 60rpx;
  margin-bottom: 30rpx;
}

.bg-darkGray {
	background-color:  #f6f6f6;
	color:#ffffff;
}
.cu-bar .search-form {
  background-color:  #ffffff;
}
.cu-bar{
  margin-top: 20rpx;
}


const app = getApp();
Page({
  data: {
    ColorList: [
      "../../images/RecycleableWaste.jpg",
      "../../images/HazardouAwaste.jpg",
      "../../images/HouseholdfoodWaste.jpg",
      "../../images/ResidualWaste.png",
    ]
  },
  goSearch: function () {
    wx.navigateTo({
      url: '/pages/ai/search',
    })
  },
  onClick:function(e){
    console.log(JSON.stringify(e))
    var index = e.currentTarget.dataset.index
    var indexClick=0;
    switch (index){
      case 0:
      indexClick=1
      break;
       case 1:
        indexClick = 2
      break;
      case 2:
        indexClick = 3
        break;
      case 3:
        indexClick = 4
        break;
    }
    wx.navigateTo({
      url: '/pages/ai/filter/filter?type=' + indexClick,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "智能分类垃圾",
      imageUrl: "https://6465-debug-5c669b-1259717830.tcb.qcloud.la/share.png?sign=2ec95a7fa286225f6df67ad718c214aa&t=1565925671",
      path: "pages/sort/sort"
    }
  },
})

   

部分主要源码:

<view class="page-wrapper">
  <view class="title">干垃圾</view>
  <view class="desc">干垃圾即其它垃圾,指除可回收物、有害垃圾、厨余垃圾(湿垃圾)以外的其它生活废弃物。干垃圾是对垃圾按照可回收垃圾、厨余垃圾、有害垃圾分类后剩余下来的一种垃圾。生活垃圾的具体分类标准可根据经济社会发展水平、生活垃圾特性和处置利用需要予以调整。</view>
  <view class='imgList-li' wx:for='{{imgArr}}'>
    <image class='paycode' src='{{item}}' data-index='{{index}}' bindtap='previewImg'></image>
  </view>
  <view class="adContainer">
  <ad unit-id="adunit-2f4b14df029312bf"></ad>
  </view>
</view>



.page-wrapper{
    width: 100%;
    height: -webkit-fill-available;
    background-color: #f8f8f8;
    text-align: center;
}
.page-wrapper .title{
    text-align: left;
    height: 50px;
    line-height: 50px;
    vertical-align: middle;
    border-bottom: 1px solid #CCEEFF;
    width: 90%;
    margin: 0 auto;
}
.page-wrapper .desc{
    text-align: left;
    border-bottom: 1px solid #CCEEFF;
    width: 90%;
    margin: 0 auto;    
    padding: 10px 1px; 
}
.page-wrapper .imgList-li{
    padding-top: 10px;
}
/* 外层组件的宽度可设置成100%或具体数值 */
.page-wrapper .adContainer {
    margin-top: 5px;
    width: 100%;
  }



Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgArr: [
      "https://6465-debug-5c669b-1259717830.tcb.qcloud.la/icon1.png?sign=668b8a87b48989c203b5aa9a5e35234b&t=1565939748"
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  previewImg:function(e){
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.imgArr;
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function(res) {
        console.log(res)
      },
      fail: function(res) {
        console.log(res)
      },
      complete: function(res) {
        console.log(res)
      },
    })
  }  
})

准备工作

  1. 数据库:存储四种垃圾分类的相关垃圾数据, 创建表commit,sort,product。把sort.json 和product.csv 导入云数据库即可
  2. 云函数:获取百度识别库的accessToken 百度AI识别库地址 QQ AI地址

注意:

  1. 小程序key 在文件project.config.json->appid 记住创建小程序的时候选择云开发
  2. 百度key 主要做拍照识别的cloudfunctions->baiduAccessToken->index->apiKey和secretKey 此处替换为:API Key 和 Secret Key

常见错误

  1. 没有自己部署云函数
  2. 数据库没有给与相应的权限,最好给最大权限
  3. 需要的key 配置错误。
  4. 没有创建数据库名称

祝:学习愉快、工作顺利!

关注公众号「码农园区」,获取项目源码
在这里插入图片描述

  • 8
    点赞
  • 138
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
CSDN微信小程序云开发项目源码是一个提供给开发者学习、参考和使用的项目示例代码。CSDN是一个IT技术社区,其中包含了丰富的技术文章和资源,有很多程序员在这里分享自己的经验和代码。 微信小程序云开发是微信官方提供的一种快速开发的方式,它相比传统的开发方式更加简单和高效。通过云开发,我们可以快速地搭建起一个小程序后台服务器,并且无需关心服务器的运维维护等问题。小程序云开发项目源码就是一个基于微信小程序云开发技术实现的示例项目,其中包含了一些常见的功能和操作,供开发者学习和参考。 通过学习和使用CSDN微信小程序云开发项目源码,开发者可以了解到如何搭建一个小程序云开发项目的整体架构,掌握小程序云开发的基本操作和原理。其中可能会包含一些常见的功能模块,比如用户登录、数据的增删改查、图片上传和下载等。通过阅读源码,开发者可以学习到各种技术细节和实现方法,有助于提升自己的开发能力和项目实践经验。 需要注意的是,虽然CSDN提供了微信小程序云开发项目源码,但作为开发者,我们应该尊重和遵守他人的知识产权,合理使用源码来进行学习和开发。同时,也要注重自己的创新能力,根据项目需求进行适当的修改和优化,以使得项目更加符合自己的实际情况,并且能够满足用户的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦之归途

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值