当前位置:   article > 正文

微信小程序云开发_require('wx-server-sdk')

require('wx-server-sdk')

创建小程序云开发项目

在这里插入图片描述

创建数据库

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
// 定义数据库
const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  // event就是调用时传递的参数
  // 获取集合feedback的数据(feedback是云数据库的集合名称)
  // get获取数据
  // awiat等
  const data=await db.collection("feedback").get();
  return {
    data:data.data,
    event,
    sum:event.a+event.b, // 返回a与b的结果
    openid: wxContext.OPENID, //openid与appid
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

添加云函数

在这里插入图片描述

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
// 定义数据库
const db=cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  // 执行添加操作
  const data=await db.collection("feedback").add({
    data:{
      msg:event.msg, // 传入msg留言信息
      datetime:new Date(), // 当前时间
      userInfo:event.userInfo, // 用户信息
    }
  })

  return {
    data:data.data,
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}```



```javascript
// pages/home/home.js
const {formatDate}=require("../../utils/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    msg:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getfeedback()
  },
  inputHd(e){
    //textarea与msg数据实现双向绑定
    this.setData({msg:e.detail.value})
  },
  addFeed(){
    //获取本地用户信息
    const userInfo=wx.getStorageSync('userInfo')
    //如果有用户信息
    if(userInfo&&userInfo.nickName){
      //直接发表留言
      this.sendFeed()
    }else{
      //获取用户头像信息
      wx.getUserProfile({
        desc: '需要用户头像',
        success:(res)=>{
          //存储用户头像信息
          wx.setStorageSync('userInfo', res.userInfo)
          //发表留言
          this.sendFeed()
        }
      })
    }
  },
  sendFeed(){
    console.log("发表留言");
    //执行云函数
    wx.cloud.callFunction({
      name:"addFeed",
      data:{
        msg:this.data.msg,//留言信息
        userInfo:wx.getStorageSync('userInfo')//用户信息
      }
    })
    .then(res=>{
      console.log(res);
      //获取留言
      this.getfeedback()
      //更新msg
      this.setData({msg:""})
    })
    .catch(err=>{
      console.log(err,"err");
    })
  },
  getfeedback(){
    //微信云执行云函数
    wx.cloud.callFunction({
        //函数名
        name:"feedback",
        //传递参数
        data:{a:3,b:2}
      })
    .then(res=>{
      //云函数返回结果
      console.log(res.result);
      //更新本地list
      this.setData({list:res.result.data.map(item=>({...item,datetime:formatDate(item.datetime,"-")}))})
    })
    .catch(err=>{
      console.log(err,"err")
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
<!-- 添加 -->
<view>
<textarea value="{{msg}}" style="background-color: #f0f0f0;" bindinput="inputHd"></textarea>
<button type="primary" bindtap="addFeed">发表</button>
</view>


<view class="list">
<!-- wx:key语法比较特别,直接省略了item 省略{{}} -->
<view class="item" wx:for="{{list}}" wx:key="_id">
  <image src="{{item.userInfo.avatarUrl}}" style="width: 33px;height: 33px;"></image>
  <text>{{item.userInfo.nickName}}</text>
<view>{{item.name}}</view>
<view>{{item.datetime}}</view>
<view>{{item.msg}}</view>
</view>
</view>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/898117
推荐阅读
相关标签
  

闽ICP备14008679号