>  기사  >  위챗 애플릿  >  대화메모 애플릿의 구현방법

대화메모 애플릿의 구현방법

小云云
小云云원래의
2018-01-27 10:29:425426검색

Zhihu를 모방한 WeChat 미니 프로그램을 포함하여 이전에도 WeChat 미니 프로그램에 대한 많은 기사를 공유한 적이 있습니다. 오늘은 계속해서 대화 메모를 얻을 수 있는 미니 프로그램 구현에 대해 공유하겠습니다.

참고: 데이터는 백엔드 없이 순수 프런트엔드, 로컬 캐시에서 작동하므로 정보 유출을 걱정할 필요가 없습니다.

먼저 구현 인터페이스 다이어그램을 살펴보겠습니다.

대화메모 애플릿의 구현방법

대화메모 애플릿의 구현방법

대화메모 애플릿의 구현방법

구현 단계(개인 버전):

1 WeChat 애플릿을 등록하고 appid를 받으세요

등록 URL: https: //mp.weixin.qq.com

2. 새 버전의 WeChat 개발자 도구를 다운로드하고, 새 메모 프로젝트를 생성하고, appid를 입력하고, 확인 후 자동으로 초기화 코드를 생성하세요

개발자 도구 다운로드: https:/ /mp.weixin.qq.com/debug/ wxadoc/dev/devtools/download.html

3. 디렉토리 구조

+-- assets          //静态文件夹
|   +-- font        //字体文件
|       +-- iconfont.eot
|          +-- iconfont.svg
|          +-- iconfont.ttf
|          +-- iconfont.woff  
|    +-- images
|        +-- share.jpg
+-- pages              //页面
|    +-- add              //添加备忘录
|   +-- add.js
|        +-- add.json 
|        +-- add.wxml
|        +-- add.wxss
|    +-- edit            //编辑备忘录
|   +-- edit.js
|        +-- edit.json 
|        +-- edit.wxml
|        +-- edit.wxss
|    +-- index          //首页
|   +-- index.js
|        +-- index.json 
|        +-- index.wxml
|        +-- index.wxss
|    +-- logs            //日志
|   +-- logs.js
|        +-- logs.json 
|        +-- logs.wxml
|        +-- logs.wxss
+-- utils        //公用js
|   +-- shareData.js    //分享短句
|   +-- util.js
+-- app.js
+-- app.json
+-- app.wxss
+-- project.config.json

4. 기능 모듈

메모 추가

//保存标题、内容和编辑时间到storage中
saveMemo: function(){
  var that = this;
  var stamp = +new Date();  //获取时间戳
  var time = util.format(stamp);  // 转换成标准时间格式
  var title = that.data.title;
  var memo_value = that.data.value;
  if (title == ''){
    wx.showToast({
      title: '请输入标题',
      icon: 'none',
      duration: 1000
    })
  }
  // else if (memo_value == '' ){
  //   wx.showToast({
  //     title: '请输入内容',
  //     icon: 'none',
  //     duration: 1000
  //   })
  // }
  else{
    //后编辑的放在前面
    that.data.memoLists.unshift({ "title": title, "text": memo_value, "time": time });
    //异步保存到storage中
    try {
      wx.setStorageSync('memoLists', that.data.memoLists)
    } catch (e) {
      wx.showToast({
        title: '保存失败',
        icon: 'error',
        duration: 2000
      })
    }
    wx.redirectTo({
      url: '/pages/index/index'
    })
  }
},

데이터 수집

var that = this;
//异步获取storage中保存的数组
try {
  var value = wx.getStorageSync('memoLists');
  if (value) {
    that.data.memoLists.push(value)
    that.setData({
      memoLists: that.data.memoLists,
      allLength: util.count(that.data.memoLists[0]),
      isNull: false
    })
  }
} catch (e) {
  wx.showToast({
    title: '获取数据失败',
    icon: 'none',
    duration: 1500
  })
};

데이터 편집

//编辑备忘录后重新保存 
  saveMemo: function () {
    var that = this;
    var stamp = +new Date();  //获取时间戳
    var time = util.format(stamp);  // 转换成标准时间格式
    var title = that.data.title;
    var memo_value = that.data.value;
    var editMemo = that.data.memoLists[that.data.id];
    //标题不能为空
    if (title == '') {
      wx.showToast({
        title: '请输入标题',
        icon: 'none',
        duration: 800
      })
    }
    // else if (memo_value == '') {
    //   wx.showToast({
    //     title: '请输入内容',
    //     icon: 'none',
    //     duration: 800
    //   })
    // }
    else {
      //如果标题和内容都没改,编辑时间不变,否则时间更改
      if(editMemo.title != title || editMemo.text != memo_value){
        editMemo.time = time;
      }else{
        editMemo.time = that.data.time;
      }
      //更新标题和内容
      editMemo.title = title;
      editMemo.text = memo_value;
      //异步更新数组
      try {
        wx.setStorageSync('memoLists', that.data.memoLists);
        wx.redirectTo({
          url: '/pages/index/index'
        })
      } catch (e) {
        wx.showToast({
          title: '保存失败',
          icon: 'error',
          duration: 2000
        })
      }
    }
  },

데이터 삭제

// 删除单条备忘记录
 delMemoLists: function(e) {
   var that = this;
     try {
       wx.showModal({
         title: '',
         content: '确认删除这' + that.data.checkboxLength+'条吗?',
         success: function (res) {
           if (res.confirm) {
               try {
                 var delValue = wx.getStorageSync('delLists');
                 // 数组从大到小排序
                 delValue.sort(function (a, b) {
                   return a < b;
                 })
                 if (delValue) {
                   if (that.data.allLength == that.data.checkboxLength) {
                     //直接清空缓存
                     wx.removeStorage({
                           key: &#39;memoLists&#39;
                      });  
                   }else{
                   for(var i=0; i<delValue.length; i++){
                       try {
                         that.data.memoLists[0].splice(delValue[i] - 1, 1);   //删除指定下标的值
                         wx.setStorageSync(&#39;memoLists&#39;, that.data.memoLists[0]);   //异步更新列表缓存
                         wx.showToast({
                           title: &#39;删除成功&#39;,
                           icon: &#39;success&#39;,
                           duration: 500
                         });
                       } catch (e) { }
                     }
                   }
                   // 删除后刷新页面
                   setTimeout(function () {
                     wx.redirectTo({
                       url: &#39;/pages/index/index&#39;
                     });
                   }, 500);
                 } else {
                   wx.showToast({
                     title: &#39;获取数据失败&#39;,
                     icon: &#39;none&#39;,
                     duration: 1000
                   });
                 }
               } catch (e) {
                 wx.showToast({
                   title: &#39;删除失败&#39;,
                   icon: &#39;none&#39;,
                   duration: 1500
                 })
               }
             }
           } 
       })
     } catch (e) {
       wx.showToast({
         title: &#39;删除失败&#39;,
         icon: &#39;none&#39;,
         duration: 1500
       })
     }
 }

공유 기능

const shareData = require(&#39;../../utils/shareData.js&#39;)   //引入自定义分享标题
// 分享
  onShareAppMessage: function (res) {
    return {
      title: shareData[Math.round(Math.random() * (shareData.length - 1))],  //从数据中随机备选一条
      path: &#39;/pages/index/index&#39;,
      imageUrl: &#39;../../assets/images/share.jpg&#39;,
      success: function (res) {
        console.log(&#39;已转发&#39;)
      },
      fail: function (res) {
        console.log(&#39;用户取消转发&#39;)
      }
    }
  }

관련 추천:

문자로 놀 수 있는 WeChat 미니 프로그램(연발)

Zhihu 예제 공유의 WeChat 미니 프로그램 버전

WeChat에서 발생한 문제 요약 미니 프로그램 개발

위 내용은 대화메모 애플릿의 구현방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.