-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
87 lines (69 loc) · 2.12 KB
/
server.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require 'vendor/autoload.php';
include "wechat.class.php";
include "config.php";
include "db.php";
$log = new Katzgrau\KLogger\Logger(__DIR__ . '/log');
$weObj = new Wechat($config['weixin']);
$returnText = $config['default_reply_msg'];
$msgType = $weObj->getRev()->getRevType();
$OpenID = $weObj->getRevFrom();
$log->info($OpenID);
$db = new Db($OpenID);
switch($msgType) {
case Wechat::MSGTYPE_TEXT: //文本消息
$content = $weObj->getRevContent();
$command = substr($content, 0 , 1);
if ($command == '#') { //更新手机
$phone = substr($content, 1 , 11);
if ( ! ctype_digit($phone) ) {
$log->info('无法识别的手机号码');
$returnText = '无法识别的手机号码';
}
if ( $db->is_have_phone($phone) ) {
$log->info('手机号码已经存在');
$returnText = '手机号码已经存在';
}
if ($db->phone($phone)) {
$returnText = '更新手机成功';
} else {
$returnText = '更新手机失败';
}
}
if ($command == '*') { //获取推荐二维码
$ticket = $weObj->getQRCode($OpenID, 2);
$qrcode = $weObj->getQRUrl($ticket['ticket']);
$returnText = $qrcode;
}
break;
case Wechat::MSGTYPE_EVENT: //事件消息
$eventType = $weObj->getRevEvent();
switch ($eventType['event']) {
case Wechat::EVENT_SUBSCRIBE: //订阅
$key = $eventType['key'];
if ( ! $db->is_have() ) { //如果系统中不存在则新增用户
$userinfo = $weObj->getUserInfo($OpenID);
$rfcode = '';
if ($key != '') { //推荐人
$rfcode = substr($key, 8);
}
$user_id = $db->add($userinfo['nickname'], $userinfo['headimgurl'], $rfcode);
if ($user_id && $rfcode != '') {
$returnText .= "你是由 " . $rfcode . " 推荐的";
}
} else {
$returnText .= "欢迎您重新回来。";
}
break;
case Wechat::EVENT_SCAN: //扫描带参二维码
$key = $eventType['key'];
$returnText .= "欢迎回来,推荐功能只对新用户有效哦。";
break;
default:
break;
}
break;
default:
}
$weObj->text($returnText);
$weObj->reply();