电商小程序(3)之电商小程序雏形

3.1Functional module analysis

功能展示图

3.2 Cloud database to add product information

  1. database add emall collection
  2. pages/index/wxml add (Adding goods)button
<button type="primary" bindtap="addMall">添加商品</button>
  1. pages/index/js implements the emall function
 addMall() {
    db.collection("emall").add({
      data: {
        description: "learn cloud database",
        due: new Date("2018-09-01"),
        tags: [
          "cloud", "database"        
        ],

      },
      success: res => {
        console.log(res)
        wx.showToast({
          title: '添加成功',
        })
      }
    })
  },
  1. Problems encountered
    It shows “cloud is not define”,finally I found that I forgot to write “wx.” in “const db = wx.cloud.database()”

  2. Unresolved question

pages/index/wxml

    <button wx:if="{{!userInfo.openid}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">登陆</button>
    <block wx:else>
      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
      <button type="primary" bindtap="addMall">添加商品</button>
    </block>

It didn’t realize the function of not needing to Click the login button when I log in again.Mybe it doesn’t work on compiler?
Solved(√):I write these codes in pages/index/js/onload

  onLoad: function() {
    var value = wx.getStorageSync('userInfo')
    if (value) {
      console.log(value)
      app.globalData.userInfo = value
    }

3.3Cloud database to add product picture

//添加图片
  addMall(){
    wx.chooseImage({
      count:1,
      success: function (res) {
        console.log(res)
        const filePath = res.tempFilePaths[0]
        const tempFile = filePath.split('.')
        const cloudPath = 'panda-img-'+tempFile[tempFile.length-2]+'.png'
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success:res=>{
            console.log(res)
          }
        })
        console.log(res)
       },
    })
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值