Gowhich

Durban's Blog

react-router 可以在 react中起到路由的作用,同时也有一个routerWillLeave,这个函数帮助我们再处理路由的时候,离开某个路由要做的某个判断起到了很好的作用,但是version 2 才有这个功能,这里记录下如何自定自己弹出框。

由于react-router自带的功能不是很好,需要我们自己处理一下,于是google参考了stackoverflow上的一篇文章,先建立一个函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function setAsyncRouteLeaveHook(router, route, hook) {
let withinHook = false
let finalResult = undefined
let finalResultSet = false
router.setRouteLeaveHook(route, nextLocation => {
withinHook = true
if (!finalResultSet) {
hook(nextLocation).then(result => {
finalResult = result
finalResultSet = true
if (!withinHook && nextLocation) {
router.replace(nextLocation)
}
})
}
let result = finalResultSet ? finalResult : false
withinHook = false
finalResult = undefined
finalResultSet = false
return result
})
}

原来的push,我这里改成了replace,为了适合我自己的逻辑。

然后添加一下routerWillLeave的逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
routerWillLeave(nextLocation) {
//获取红包相关的信息

return new Promise((resolve, reject) => {
Popbox.pop({
text: '你离使用红包只差一步之遥,确定放弃吗?',
confirmBtnText: '继续绑卡',
cancelBtnText: '放弃',
cancelFunc: () => {
resolve(true);
},
});
})

return false;
}

这里是需要返回一个Promise的,所以自己的代码记得处理一下。

最后我们跟根据自己的逻辑来设置一下这个Hook。

我这里是放在了componentDidMount中做的处理

1
2
3
4
5
6
7
componentDidMount() {
const { type } = this.props.params;

if(type){
setAsyncRouteLeaveHook(this.context.router, this.props.route, this.routerWillLeave);
}
}

好了,希望能帮到不了解英文的你,或者是找不到资料的你。

post的curl库,模拟post提交的时候,默认的方式 multipart/form-data ,这个算是post提交的几个基础的实现方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$postUrl = '';
$postData = array(
'user_name'=>$userName,
'identity_no'=>$idCardNo
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl);
curl_close($curl);

print_r($r);

php的curl库进行post提交还是蛮方便的。但是提交方式不同,contentType 不同导致你的api是否能接收到数据也是个变数,这里来个简单的实例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$postUrl = '';
$postData = array(
'user_name'=>$userName,
'identity_no'=>$idCardNo
);
$postData = http_build_query($postData);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl);
curl_close($curl);

print_r($r);

关键一段代码是

1
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

Docker 国内镜像配置收集

//DaoCloud

1
2
docker-machine ssh default
sudo sed -i "s|EXTRA_ARGS='|EXTRA_ARGS='--registry-mirror=http://723acd29.m.daocloud.io |g" /var/lib/boot2docker/profile

//灵雀云

1
2
docker-machine ssh default
sudo sh -c "echo EXTRA_ARGS='--registry-mirror=http://houchaohann.m.alauda.cn' >>/var/lib/boot2docker/profile

//是速云

1
docker-machine create -d virtualbox --engine-registry-mirror http://dapeng89.m.tenxcloud.net mydocker

事件统计防重复统计,不服来挑错

1
2
3
4
5
6
7
8
9
10
11
12
function track(eventName, params) {
var img = new Image();
if(typeof params == 'object') {
params = JSON.stringify(params);
} else {
params = '';
}
var t = new Date().valueOf();
var random = Math.random();
var referrer = document.referrer;
img.src = '/track?event='+encodeURIComponent(eventName)+'&params=' + encodeURIComponent(params) + '&t='+t+'&r='+random+'&referrer='+referrer;
}

后端将r值t值,外加一个ip的值,作为唯一的索引。为了做到数据准确到达,程序端少操作为主,这样会减少操作事件,将逻辑交给数据库操作了,增加数据到达率。

1
2
3
4
let insertEventSql = `REPLACE INTO ${config.mysql.prefix}xxxx.event_log 
(${keys.join(',')})
VALUES
(${values.join(',')})`;

最近使用docker部署gitlab,启动后会有一个问题,不知道哪里会一直在执行mail_room 这个命令,查看了下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Reply by email
# Allow users to comment on issues and merge requests by replying to notification emails.
# For documentation on how to set this up, see http://doc.gitlab.com/ce/incoming_email/README.html
gitlab_rails['incoming_email_enabled'] = true
#
# # The email address including the `%{key}` placeholder that will be replaced to reference the item being replied to.
# # The `+%{key}` placeholder is added after the user part, after a `+` character, before the `@`.
gitlab_rails['incoming_email_address'] = "gitlab-incoming+%{key}@xxxx.com"
#
# # Email account username
# # With third party providers, this is usually the full email address.
# # With self-hosted email servers, this is usually the user part of the email address.
gitlab_rails['incoming_email_email'] = "xx@xx"
# # Email account password
gitlab_rails['incoming_email_password'] = "xxxxx"
#
# # IMAP server host
gitlab_rails['incoming_email_host'] = "imap.xxxx.com"
# # IMAP server port
gitlab_rails['incoming_email_port'] = 993
# # Whether the IMAP server uses SSL
gitlab_rails['incoming_email_ssl'] = true
# # Whether the IMAP server uses StartTLS
gitlab_rails['incoming_email_start_tls'] = true
#
# # The mailbox where incoming mail will end up. Usually "inbox".
gitlab_rails['incoming_email_mailbox_name'] = "inbox"
#

这里是启动的。

错误日志提示

1
2016-08-23_02:07:08.56923 /opt/gitlab/embedded/lib/ruby/2.1.0/openssl/ssl.rb:240:in `post_connection_check': hostname "imap.xxxx.com" does not match the server certificate (OpenSSL::SSL::SSLError)

这里的是imap.xxx.com是一个腾讯企业邮箱的imap的配置,后来查看了下具体的配置,改为了imap.exmail.qq.com,还是报错

1
2
3
4
2016-08-23_02:21:11.00526 /opt/gitlab/embedded/lib/ruby/2.1.0/net/imap.rb:1158:in `get_tagged_response': ������Ч���߲�֧�� (Net::IMAP::BadResponseError)
2016-08-23_02:21:11.00553 from /opt/gitlab/embedded/lib/ruby/2.1.0/net/imap.rb:1210:in `block in send_command'
2016-08-23_02:21:11.00574 from /opt/gitlab/embedded/lib/ruby/2.1.0/net/imap.rb:1192:in `send_command'
2016-08-23_02:21:11.00578 from /opt/gitlab/embedded/lib/ruby/2.1.0/net/imap.rb:373:in `starttls'

具体不知道啥原因,果断将true改为false。

1
2
3
4
5
## Reply by email
# Allow users to comment on issues and merge requests by replying to notification emails.
# For documentation on how to set this up, see http://doc.gitlab.com/ce/incoming_email/README.html
gitlab_rails['incoming_email_enabled'] = false
#

看下面这个crontab

1
* * * * * /usr/bin/python /home/zhangdapeng/del.py > /dev/null 2>&1

一般的比较安全的,无困扰的情况下是这样的

但是调试很不方便,报错了,不知道为啥报错了,找不到原因,改一下

1
* * * * * /usr/bin/python /home/zhangdapeng/del.py > /path/result.log 2>&1

这样的话就能在result.log知道原因了。

python脚本执行shell,通过crontab执行python脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
#-*-coding:utf-8-*-

import os
import time
import datetime
import subprocess

today =datetime.date.today()
deltadays = datetime.timedelta(days=1) #确定日期差额,如前天 days=2
yesterday = today - deltadays

month = yesterday.strftime('%b')
date = yesterday.strftime('%d')

command1 = "ls -hl /log1 | grep '%s %s' | awk '{print i$9}' i='/log1/' | xargs rm " % (month, date)
command11 = "ls -hl /log1 | grep '%s %s' | awk '{print i$9}' i='/log1/' | xargs rm " % (month, date)
command2 = "ls -hl /log2 | grep '%s %s' | awk '{print i$9}' i='/log2/' | xargs rm " % (month, date)
command22 = "ls -hl /log2 | grep '%s %s' | awk '{print i$9}' i='/log2/' | xargs rm " % (month, date)

os.system(command1)
os.system(command11)
os.system(command2)
os.system(command22)

注意

1
ls -hl /log2 | grep '%s  %s' | awk '{print i$9}' i='/log2/' | xargs rm

这段shell命令最好是通过python执行shell答应看下具体的文件列出来的格式,防止无效

1
print os.system("ls -hl /log2")

这样运行后就能得出结果。然后根据具体情况修改就好了。

配置主题前提条件

安装插件包

  1. Material-Theme
  2. Material Theme Appbar

然后将配置按照如下配置下,效果界面很爽眼。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"always_show_minimap_viewport": true,
"bold_folder_labels": true,
"color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",
"font_face": "Source Code Pro for Powerline",
"font_size": 13,
"highlight_line": true,
"ignored_packages":
[
"Vintage"
],
"indent_guide_options":
[
"draw_normal",
"draw_active"
],
"material_theme_accent_red": true,
"material_theme_bold_tab": true,
"material_theme_compact_panel": true,
"material_theme_compact_sidebar": true,
"material_theme_contrast_mode": true,
"material_theme_panel_separator": true,
"material_theme_small_statusbar": true,
"material_theme_small_tab": true,
"material_theme_tabs_autowidth": true,
"material_theme_tabs_separator": true,
"material_theme_tree_headings": true,
"overlay_scroll_bars": "enabled",
"rulers":
[
80,
110
],
"show_encoding": true,
"theme": "Material-Theme.sublime-theme",
"word_wrap": false
}

第一种,直接使用Intent去传递数据

1
2
3
4
5
6
7
//传递数据
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("key","value");
startActivity(intent);
//接收数据
Intent intent = getIntent();
String name = intent.getStringExtra("key");

第二种,使用Application/全局变量传递传递数据

创建一个Application的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MyApp extends Application {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void onCreate() {
super.onCreate();
setName("maomao");
}
}
//使用并定义myApp;
private MyApp myApp;
//实例化Application,并传递数据
myApp = (MyApp)getApplication();
myApp.setName("maomaomao");
Intent intent = new Intent(MainActivity.this, ShowNameActivity.class);
startActivity(intent);
//接收数据
private MyApp myApp;
myApp = (MyApp) getApplication();
String name = myApp.getName();
  • 这种方式千万别忘记要配置一下AndroidManifest.xml这个文件

在application 添加

1
android:name=".MyApp"

这里的MyApp 就是上面定义的

第三种,使用剪切板传递数据

1
2
3
4
5
//传递数据
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(name);
Intent intent = new Intent(MainActivity.this, ShowNameActivity.class);
startActivity(intent);

//接收数据

1
2
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String name = clipboardManager.getText();

也可以使用剪切板的方式传递对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//创建对象MyData
public class MyData implements Serializable {
private int age;
private String name;
public MyData(String name, int age){
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyData{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
//对象方式传数据
MyData myData = new MyData("mao", 23);
//将对象转为字符串
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String base64String = "";
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(myData);
base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
objectOutputStream.close();
} catch (Exception e) {
}
clipboardManager.setText(base64String);
Intent intent = new Intent(MainActivity.this, ShowNameActivity.class);
startActivity(intent);
//接收对象数据
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String msg = clipboardManager.getText().toString();
byte[] base64Bype = Base64.decode(msg, Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64Bype);
try{
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
MyData myData = (MyData) objectInputStream.readObject();
String str = myData.toString();
}catch (Exception e){
}

第四种,直接使用静态变量传递数据

这种方式需要在接收数据的Activity中定义自己的属性变量

1
2
3
4
5
6
7
8
9
10
11
//定义变量
public static int age;
public static String name;
//传递数据
Intent intent = new Intent();
intent.setClass(MainActivity.this, ShowNameActivity.class);
ShowNameActivity.name = "maomaomaomao";
ShowNameActivity.age = 90;
startActivity(intent);
//接收数据-直接使用变量的数据就好了
Log.in("name >>>" + name + " age>>>" + age);
0%