小程序模板網(wǎng)

微信小程序評(píng)論/留言功能,附:前端+后端代碼+視頻講解!

發(fā)布時(shí)間:2018-09-19 08:44 所屬欄目:小程序開(kāi)發(fā)教程

前端界面:

 

演示:



 

<!-- 表單 -->
<form bindsubmit="formSubmit">
<input type="text" name="liuyantext" placeholder='輸入留言內(nèi)容' class="input-style"/>
<button formType="submit" class="btn" wx:if="{{nickName == empty}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" bindtap='login'>授權(quán)登錄</button>
<button formType="submit" class="btn" wx:else>留言</button>
<input type="text" name="nickname" value='{{nickName}}' style="display:none;"/>
<input type="text" name="headimg" value='{{avatarUrl}}' style="display:none;"/>
</form>

<view wx:for="{{re}}" wx:key="re">
  <view class="result">{{item.result}}</view>
</view>
<view style="text-align:center;font-size:14px;color:#ccc;margin-top:20px;">以下是留言內(nèi)容</view>
<view wx:for="{{liuyanlist}}" wx:key="liuyanlist" class="liuyanview">
  <view class="headimg"><image src="{{item.headimg}}"></image></view>
  <view class="nickname_liuyantext">
    <view class="nickname">{{item.nickname}} <view class="time">{{item.lytime}}</view></view>
    <view class="text">{{item.liuyantext}}</view>
  </view>
  <!-- 占位符 -->
  <view style="width:100%;height:10px;"></view>
</view>
//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()
Page({
  /**
   * 頁(yè)面的初始數(shù)據(jù)
   */
  data: {
    
  },

  //授權(quán)登錄
  login: function () {
    var that = this;
    // 查看是否授權(quán)
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已經(jīng)授權(quán),可以直接調(diào)用 getUserInfo 獲取頭像昵稱
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo),
                that.setData({
                  nickName: res.userInfo.nickName,
                  avatarUrl: res.userInfo.avatarUrl,
                })
            }
          })
        }
      }
    })
  },
  bindGetUserInfo(e) {
    console.log(e.detail.userInfo)
  },


  formSubmit: function (e) {
    wx.showToast({
      title: '已留言',
      icon: 'success'
    })
    var that = this;
    var liuyantext = e.detail.value.liuyantext; //獲取表單所有name=liuyantext的值 
    var nickName = e.detail.value.nickname; //獲取表單所有name=nickName的值 
    var headimg = e.detail.value.headimg; //獲取表單所有name=headimg的值 
    wx.request({
      url: 'http://localhost/liuyanserver/liuyan.php?liuyantext=' + liuyantext + '&nickname=' + nickName + '&headimg=' + headimg,
      data: {
        liuyantext,
        nickName,
        headimg
      },
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data)
        that.setData({
          re: res.data,
        })
        wx.hideToast();
      }
    })
  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that = this
    wx.request({
      url: 'http://localhost/liuyanserver/loadliuyan.php',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        //將獲取到的json數(shù)據(jù),存在名字叫l(wèi)ist的這個(gè)數(shù)組中
        that.setData({
          liuyanlist: res.data,
          //res代表success函數(shù)的事件對(duì),data是固定的,liuyanlist是數(shù)組
        })
        // 隱藏導(dǎo)航欄加載框
        wx.hideNavigationBarLoading();
        // 停止下拉動(dòng)作
        wx.stopPullDownRefresh();
      }
    })
  },

  //加載最新數(shù)據(jù)
  onLoad: function () {
    var that = this
    wx.request({
      url: 'http://localhost/liuyanserver/loadliuyan.php',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        //將獲取到的json數(shù)據(jù),存在名字叫l(wèi)ist的這個(gè)數(shù)組中
        that.setData({
          liuyanlist: res.data,
          //res代表success函數(shù)的事件對(duì),data是固定的,liuyanlist是數(shù)組
        })
      }
    })
  }
})
/**index.wxss**/
.input-style{
  width: 90%;
  height: 50px;
  border:1px solid #ccc;
  margin:10px auto;
}

.btn{
  width: 88%;
  margin:5px auto;
}

.liuyanview{
  width: 90%;
  margin: 10px auto;
}

.result{
  text-align: center;
  font-size: 14px;
  color: #f00;
  margin-top: 20px;
}

.headimg{
  width: 45px;
  height: 45px;
  border-radius: 100%;
}

.headimg image{
  width: 45px;
  height: 45px;
  border-radius: 100%;
}

.nickname_liuyantext{
  width: calc(100% - 55px);
  float: right;
  margin-top:-45px;
}

.nickname_liuyantext .nickname{
  font-size: 15px;
  color: #999;
}

.nickname_liuyantext .nickname .time{
  font-size: 11px;
  color: #999;
  float: right;
}

.nickname_liuyantext .text{
  font-size: 16px;
  color: #666;
}

以上是前端部分
后端有兩個(gè)文件。

 

數(shù)據(jù)庫(kù)格式:

 



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://u-renovate.com/wxmini/doc/course/24818.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢