欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序实现留言板

程序员文章站 2023-11-25 17:06:28
本文实例为大家分享了微信小程序实现留言板的具体代码,供大家参考,具体内容如下 css: /* pages/order/message2/message2...

本文实例为大家分享了微信小程序实现留言板的具体代码,供大家参考,具体内容如下

微信小程序实现留言板

css:

/* pages/order/message2/message2.wxss */
 .msg-box{
 padding: 20px;
 }
 .send-box{
 display: flex;
 }
 .input{
 border: 1px solid #b0c4de;
 padding: 5px;
 }
.msg-info{
 display: block;
 margin: 10px 0 0 0 ;
 color: #339900;
 
 }
 .place-input{
 color: salmon;
 }
 .list-view{
 margin: 20px 0 0 0;
 }
 .item{
 overflow: hidden;
 border-bottom: 1px dashed #87ceff;
 height: 30px;
 line-height: 30px;
 }
 .text1{
 float: left;
 }
 .close-btn{
 float: right;
 margin: 5px 5px 0 0;
 }

js:

const app = getapp();
page({
 
 /**
 * 页面的初始数据
 */
 data: {
 
 msgdata:
 [
  {
  child_id: 1,
  msg:
  "我想做个眉毛,到店后求推荐。",
  
  checked:''
  },
  {
  child_id: 2,
  msg:
  "我只有2小时,您看着安排吧。",
  checked: ''
  },
  {
  child_id: 3,
  msg:
  "我和朋友一起过来。",
  checked: ''
  },
  {
  child_id: 4,
  msg:
  "美甲样式到店挑选。",
  checked: ''
  },
  {
  child_id: 5,
  msg:
  
  "给眉毛再补个颜色。",
  checked: ''
  },
  {
  child_id: 6,
  msg:
  "我要补睫毛哦。",
   checked:''
  }
 ],
 message: '',
 message_id:[],
 },
 
 bindtextareachange: function(e){
 var that = this
 that.setdata({
  message:e.detail.value
 })
 },
 
 click:function(e){
 var that = this;
 let id = e.currenttarget.dataset.id;
 let index = e.currenttarget.dataset.index;
 var value = [];
 value = this.data.message_id;
 var array_i = this.in_array(id, value);
 var chekeds = that.data.msgdata;
 var msg = chekeds[index].msg;
 var message = that.data.message;
 if (!e.currenttarget.dataset.checked){ 
  chekeds[index].checked = true
  that.setdata({
  message: message + msg
  })
 }else{
  chekeds[index].checked = false
  that.setdata({
  message: message.replace(msg, '')
  })
 }
 that.setdata({
  msgdata: chekeds
 })
 if (array_i) {
  value.splice(array_i, 1);
 } else {
  value.push(id);
 }
 this.setdata({
  message_id: value,
 })
 },
 in_array: function (search, array) {
 for (var i in array) {
  if (array[i] == search) {
  return i;
  }
 }
 return false;
 },
 submit:function(){
 var value = [];
 var message = this.data.message;
 var msgdata = this.data.msgdata;
 if (message == '' && !value.length) {
  wx.showtoast({
  title: '暂无选择项目',
  icon:'none'
  })
  return;
 }
 app.globaldata.message = message;
 for (var i = 0; i < msgdata.length; i++) {
  if(message.indexof(msgdata[i].msg) > -1){
  value[i] = msgdata[i].child_id;
  }
 }
 wx.request({
  url: 'https://www.omeiclub.com/app/public/index.php/index/index/server',
  method: 'post',
  data: { message_id: value, openid: app.globaldata.openid, message: message, token: app.globaldata.token},
  header: {
  'accept': 'application/json'
  },
  success: function (res) {
  if(res){
   // wx.showtoast({
   // title: '捎话成功',
   // success:function(){
    
   // }
   // })
   wx.switchtab({
   url: '/pages/order/order',
   success: function (e) {
    var page = getcurrentpages().pop();
    if (page == undefined || page == null) return;
    page.onload();
   }
   })
   app.globaldata.message = message;
  }
  
  console.log(res)
  }
 })
 },
 
 /**
 * 生命周期函数--监听页面加载
 */
 onload: function (options) {
 var that = this;
 wx.request({
  url: 'https://www.omeiclub.com/app/public/index.php/index/index/serversle',
  method: 'post',
  data: { openid: app.globaldata.openid, token: app.globaldata.token},
  header: {
  'accept': 'application/json'
  },
  success: function (res) {
  if (res.data){
   var message_id = res.data.message_id;
   var value = that.data.msgdata;
   var message = res.data.message;
   that.setdata({
   message: message
   });
   for (var i = 0; i < value.length;i++) {
   if (that.in_array(value[i].child_id, message_id)) {
    value[i].checked = true;
    that.setdata({
    msgdata: value,
    });
   }
   }
  }
  }
 })
 
 },
 
 /**
 * 生命周期函数--监听页面初次渲染完成
 */
 onready: function () {
 
 },
 
 /**
 * 生命周期函数--监听页面显示
 */
 onshow: function () {
 
 },
 
 /**
 * 生命周期函数--监听页面隐藏
 */
 onhide: function () {
 
 },
 
 /**
 * 生命周期函数--监听页面卸载
 */
 onunload: function () {
 
 
 },
 
 /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
 onpulldownrefresh: function () {
 
 },
 
 /**
 * 页面上拉触底事件的处理函数
 */
 onreachbottom: function () {
 
 },
 
 /**
 * 用户点击右上角分享
 */
 onshareappmessage: function () {
 
 }
})

html

<!--pages/order/message/message.wxml--> 
<view class='message_nav'> 
 <form bindsubmit='formsubmit'> 
 <view class='section'> 
 <textarea class='message1' type='text' placeholder="请选择或者输入捎话(60字以内)" maxlength='80' style="word- wrap:break-word" bindinput='bindtextareachange' value="{{message}}"></textarea> 
</view> 
 <view class='fast'>快速捎话:</view> 
<checkbox-group class="checkboxchange"> 
 <view wx:for="{{msgdata}}" wx:key="{{index}}" data-index="{{index}}" data-id="{{item.child_id}}" data-checked='{{item.checked}}' bindtap='click' > 
 <checkbox value='{{item.msg}}' class='item' checked='{{item.checked}}' >{{item.msg}}</checkbox> 
 </view> 
</checkbox-group> 
<button class='submit' bindtap='submit'>捎话</button> 
</form> 
</view>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。