2018年6月

有了 FormID,就相当于有了给用户发送模板消息的权限,一个Formid只能发送一条消息,且这个FormId 的有效期只有7天。

微信相关的文档:
https://developers.weixin.qq.com/miniprogram/dev/api/notice.html#发送模板消息

之前也在网上看过到类型的代码和思路参考,只是他们的实现方法都不太符合我的要求,所以自己写了。

目前普遍的做发就是先把用户在小程序里的操作产生的formid收集到后台系统保存起来,需要发的时候直接从后台数据中取出,发送一条消耗一个fromid,过期就自动删除掉。要产生formid前台还必需使用form标签,再写上对应的属性,然后用户要手动去点击了表单的提交按钮才能得到一个fromID。

思路
就是把你页面中(尽量可能多的把所有页面都加上),用户经常会去点的按钮,图片等都用form标签给包起来,再form里面放上一个提交的按钮,改下按钮的样式,透明、大小和原本的按钮、图片的大小一样,这样用户去点击的时候,际实上是点击的表单的提交按钮,所以就拿到了formid ,然后提交到后台保存,后台推荐用像redis这样的缓存服务来保存

实现
这里我把自己写的收集代码片段分享给大家(使用wepy框架,仅供大家参考提供思路):

这是mixins组件,怎么使用这里就不说明了
PS:$post是我自己封装的一个请求方法, 注意:the formId is a mock one 这是开发者工具的固定值,这里不作提交,开发可以去掉看效果

form.js

import wepy from 'wepy'

import {$post, config} from './base'

/**
 * @author Joyber
 * 收集用户的FormID的MIXINS
 * 用法:
 *
 <form class="_f [原有的classname]" report-submit @submit="FormSubmit"
    data-event="[原有的方法]">
 <button class="_f" form-type="submit"></button>
    [原有的标签内容]
 </form>

 */

export default class formMixin extends wepy.mixin {

    methods = {
        FormSubmit(e) {
            let _this = this;
            const {formId, value} = e.detail;
            const {dataset} = e.currentTarget;
            const {event} = dataset;

            config.debug && console.log('formId', formId);
            //the formId is a mock one  这是开发者工具的固定值,这里不作提交
            formId && formId!='the formId is a mock one' && $post("member/form/id", {formId});

            if (!event) return;
            if (typeof _this.methods[event] == 'function') {
                _this.methods[event].call(_this, e);
            } else if (typeof _this[event] == 'function') {
                _this[event].call(_this, e);
            } else {

                let flag = false;
                const {$mixins} = _this;
                config.debug && console.log(typeof $mixins, $mixins);

                $mixins.forEach((mixin, index)=>{
                    config.debug && console.log('find mixin function', index);
                    if (mixin.methods && typeof mixin.methods[event] == 'function') {
                        flag = true;
                        mixin.methods[event].call(_this, e);
                    }else if (typeof mixin[event] == 'function') {
                        flag = true;
                        mixin[event].call(_this, e);
                    }
                });
                !flag && console.error('page function undefined: ' + event);
            }
        }
    }
}

使用

全局CSS(less语法):

form{
    &._f{
        position: relative;
        display: block;
        &._b{display: block;}
        button {
            &._f {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                background: transparent;
                z-index:1;
                &::after {display: none;}
            }
        }
    }
}

代码改造:
原来的页面代码比如:

<view class="XXXX1" @tap="xxxx2" data-id="xxxx3"><image src="xxx4"></image></view>

改造后的页面代码:

 <form class="_f XXXX1" report-submit @submit="FormSubmit"
    data-event="xxxx2" data-id="xxxx3">
 <button class="_f" form-type="submit"></button>
    <image src="xxx4"></image>
 </form>

而JS中只需要把 form.js import进来

wx.page 的 mixins 中 增加 formMixin 即可。
mixins = [formMixin]

本人文采不好,见谅!

感谢老铁无私的分享
插件出处:http://www.yzmb.me/archives/net/audio-player-for-typecho

我们有时候会把日志数据写入数据库,如果该表不能自动备份,表中的数据会越来越多,影响速度。可以定期将表中数据备份到另外一个表中来解决。

解决方案1
使用MySQL中的存储过程+事件解决。

假设你的日志表名:operationlog

存储过程逻辑为:
  1)创建一个新表operationlog_temp,各字段同operationlog相同;
  2)将表operationlog更名为operationlog_yyyy-mm-dd;
  3)将表operationlog_temp更名为operationlog

事件逻辑为:
  1)每个3个月定时调用一次存储过程bakOpLog

注意:开始事件功能(MySQL必须先开启事件功能,才能使用事件)。

查看当前是否已开启事件计划(调度器)有3种方法:
1) SHOW VARIABLES LIKE ‘event_scheduler’;
2) SELECT @@event_scheduler;
3) SHOW PROCESSLIST;

开启事件计划(调度器)开关有4种方法:重启就会销毁
1) SET GLOBAL event_scheduler = 1;
2) SET @@global.event_scheduler = 1;
3) SET GLOBAL event_scheduler = ON;
4) SET @@global.event_scheduler = ON;

1、定义存储过程bakOpLog:

CREATE DEFINER=`sa`@`%` PROCEDURE `bakOpLog`()
BEGIN
create table operationlog_temp like operationlog;
set @i=current_date();
--执行rename table operationlog to operationlog_yyyy-mm-dd
set @sqlstr=CONCAT('rename table operationlog to `operationlog_',cast(@i as char),'`');
select @sqlstr;
PREPARE renameOpLog FROM @sqlstr;
EXECUTE renameOpLog;
rename table operationlog_temp to operationlog;
END;

2、定义事件callProcedureBakOpLog

CREATE DEFINER=`sa`@`%` EVENT `callProcedureBakOpLog` ON SCHEDULE EVERY 1 DAY STARTS '2014-12-30 00:00:00' ENDS '2015-01-06 00:00:00' ON COMPLETION PRESERVE ENABLE DO call bakOpLog();

3、存储过程用到的一些语法

set @i=current_date();  //将全局变量i赋值为当前日期
set @sqlstr=CONCAT('rename table operationlog to `operationlog_',cast(@i as char),'`'); //sqlstr=rename table operationlog to operationlog_yyyy-mm-dd
PREPARE renameOpLog FROM @sqlstr;   //定义预处理语句
EXECUTE renameOpLog;    //执行预处理语句

查看创建的事件

SHOW EVENTS;

也可以在mysql库中产看event表

1) 临时关闭事件

ALTER EVENT e_test DISABLE;

2) 开启事件

ALTER EVENT e_test ENABLE;

3) 将每天清空test表改为5天清空一次:

ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;

4) 删除事件(DROP EVENT)

DROP EVENT [IF EXISTS] event_name

例如删除前面创建的e_test事件

DROP EVENT e_test;

当然前提是这个事件存在,否则会产生ERROR 1513 (HY000): Unknown event错误,因此最好加上IF EXISTS

DROP EVENT IF EXISTS e_test;

一款老牌的文件双向、单向同步软件,可用于自动发布,来现实代码同步到多台服务器

虽然很多年前的软件了,但由于我使用起来还比较顺手,所以记录一下

Unison
当前版本:v2.51.2
项目地址:https://github.com/bcpierce00/unison
官网地址:https://www.cis.upenn.edu/~bcpierce/unison/

ocaml安装

cd /tmp
wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
tar xzf ocaml-3.12.1.tar.gz
cd ocaml-3.12.1
./configure
make world opt
make install

unison安装

cd /tmp
wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz
tar xzf unison-2.40.63.tar.gz
cd unison-2.40.63
make UISTYLE=text
mkdir /root/bin
make install
cp /root/bin/unison /usr/bin/

unison的使用

英文文档:http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html
中文文章:https://www.centos.bz/2012/02/use-unison-implement-file-two-way-sync/#unison%E7%9A%84%E4%BD%BF%E7%94%A8