Applet developed pit

Recently made a small program, the successful on-line. (Purely entertain themselves, look at the Internet, said the audit uncomfortable to think of myself luck, it is not to mention more than twice. Who knows quickly reviewed, and luck was okay, but later submitted as entry when must the authorization was required rectification) are interested can experience the scan code (see end of text). I summarize here to share my experience of the pit, and the problems have not been solved (the proposed method is not necessarily the best), we welcome the discussion message

Database permissions

I used the built-in cloud development database to store data (of course you can use your own, I did not do it, so I do not participate in the discussion), then this is a problem, the default permissions on the database is: create only those who can read and write . Then the page is likely to get js inside, can not modify data, that is, apart from you, other people can not access the page can access the data.
ps: I once forgot this, and then find where the problem of finding data, check for a long time

Solution:

  1. Change the database permissions, so that it can read or write
  2. For the operation of the whole operation of the database function into the cloud to

Dynamic monitor database

I have a list of friends, hoping to see friends dynamic state, so the state has been changed listening friends, get a buddy list on the re

//创建监听器
const watcher = db.collection('T_STATUS').where({
          openId: db.command.in(friendsId)
        }).field({//只监听这个字段
          updateDate: true
        }).watch({
          onChange: function (snapshot) {
            xxx
          },
          onError: function (err) {
            console.error('the watch closed because of error', err)
          }
        })
      }
//关闭监听器
watcher.close()

In the monitor should ensure that the principle of least get the data, I only listen all friends updateDate this field. Then when the user changes any state are updating this field, to effectively monitor

Page introduction of the definition of utility functions

Function defined wxs file (/utils/app.wxs) in

var formatTime = function (time) {
  return (time/60 > 9 ? parseInt(time/60) : "0"+parseInt(time/60)) + ":" + (time%60 > 9 ? time%60 : "0"+time%60);
}

//  要引用这个文件的函数或者变量,除了在要引用的的js文件中模块化之外(var utils=require('js地址')),
// 在被引用的的js中要通过 module.exports={a:a}作为面向对象的变量输出函数如下:
module.exports = {
  formatTime: formatTime
}

The introduction of utility functions in the file, you can use wxml

<wxs src="../../utils/app.wxs" module="utils" />
{{utils.formatTime()}}

Several pages loading method used

onLoad:监听页面加载
onShow:监听页面显示
onHide:监听页面隐藏
onUnload:监听页面卸载
onShareAppMessage:用户点击右上角转发,或者控件有open-type="share"

Sharing application

Mainly custom image, lacks this skill

    //分享应用给好友
    onShareAppMessage: function (options) {
      var that = this;
      var shareObj = {
       title: "这是一个小程序",
       path: '/pages/index/index',        // 默认是当前页面,必须是以‘/’开头的完整路径
       imageUrl: '/images/sharePhoto.png',    
      }
      return shareObj;
    }

Mainly talk about is imageURL, not imgURL, see the Internet some people say behind this (I was misled by this), I hope we will not be misled, that is in front

Two timers

  1. I am doing is a Type A timer tool, directly used the timer
//启动计时器
var interval = setInterval(
       function () {
         xxx//每一秒执行
       }, 1000);
//清除计时器
clearInterval(interval)

There is a problem with this timer, small program to cut back, it stops working, it will work into the foreground.

Solution:
. A method in which onHide stop the timer, and note the time stamp when the next stop
b open timer onShow method, the plus stops.

  1. Use the countdown tool, because the page has a button to punch after punch word becomes; so set this countdown, set up after the early morning of the next day between now and the time interval, and status buttons change out
timer = setTimeout(function () {
       xxx/下一天的凌晨执行
     }, twelveClock);

The problem encountered is how also received less than eight East region and the next day early morning time

Solution:
A eight districts in Eastern Time: later mentioned, yet to be resolved
next time one morning b: var = new new nextDay a Date (new new a Date (date.getTime () + 1000 60 60 * 24-) .setUTCHours (0, 0,0,0));

List of input and send event

Each line in my buddy list, and put an input box and the Send button, because the buddy list is dynamic, so these events are dynamic, this page achieve this function is a little challenging.
First, there is a dynamic list traversal List, it may be a default subscript index to take. Control exists data-key attribute, key corresponding to the variable name, you can get the last pass in the value of the triggering event; input controls there is a bindInput property, can be equipped with a way to monitor the input event

//wxml
<view class="friends" wx:for="{{friendList}}"
      <view class="message">
        <input class="weui-input" maxlength="200" placeholder="留言..." adjust-position="true" data-index="{{index}}" bindinput="messageInput" value="{{item.message}}"/>
      </view>

      <view class="send" bindtap="send" data-index="{{index}}">
        <image src="/images/send.png"></image>
      </view>
</view>

//js
messageInput: function(event) {
  //console.log(event.currentTarget.dataset)
  var index = event.currentTarget.dataset.index
  this.data.friendList[index].message = event.detail.value
  this.setData({
    friendList: this.data.friendList
  })
},
//发送消息
send: function(event) {
  //console.log(event.currentTarget.dataset)
  var index = event.currentTarget.dataset.index
  console.log(this.data.friendList[index].message)
  ...
 }

Unresolved Issues

Time zone issue date function in the cloud:
when used date function in the cloud, I will new Date () turn into eight districts in Eastern Time (As for how to turn, we can Baidu), this time there three different times : 1 time cloud printing log function is 8 hours before GMT; 2 I turn time is GMT; 3 I transfer time to the database, the display is GMT plus 8 hours. You ask me what to ask East eight districts time to turn, because I saw the time inconsistency, styled.

Tom and Jerry time

Guess you like

Origin www.cnblogs.com/so-easy/p/11839280.html