Wechat.php
7.3 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace app\index\controller;
use addons\wechat\model\WechatAutoreply;
use addons\wechat\model\WechatCaptcha;
use addons\wechat\model\WechatContext;
use addons\wechat\model\WechatResponse;
use addons\wechat\model\WechatConfig;
use EasyWeChat\Foundation\Application;
use addons\wechat\library\Wechat as WechatService;
use addons\wechat\library\Config as ConfigService;
use think\Log;
use think\Db;
use app\common\controller\Frontend;
use app\api\model\UserHouse;
use app\api\model\House;
use app\api\model\HousePhone;
/**
* 微信接口
*/
class Wechat extends Frontend
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $layout = '';
public $app = null;
public function _initialize()
{
parent::_initialize();
$this->app = new Application(ConfigService::load());
}
/**
* 微信API对接接口
*/
public function api()
{
$this->app->server->setMessageHandler(function ($message) {
$matches = null;
$openid = $message->FromUserName;
$to_openid = $message->ToUserName;
$event = $message->Event;
$eventkey = $message->EventKey ? $message->EventKey : $message->Event;
switch ($message->MsgType) {
case 'event': //事件消息
// //验证码消息
// if (in_array($event, ['subscribe', 'SCAN'])) {
// return $eventkey;
// }
switch ($event) {
case 'subscribe'://添加关注
$key = explode('_', $eventkey);
return $this->joinHouse($key[1],$openid);
case 'unsubscribe'://取消关注
return '';
case 'LOCATION'://获取地理位置
return '';
case 'VIEW': //跳转链接,eventkey为链接
return '';
case 'SCAN': //扫码
return $this->joinHouse($eventkey,$openid);
default:
break;
}
return "";
case 'text': //文字消息
case 'image': //图片消息
case 'voice': //语音消息
case 'video': //视频消息
case 'location': //坐标消息
case 'link': //链接消息
default: //其它消息
return "";
}
return ""; //SUCCESS
});
$response = $this->app->server->serve();
// 将响应输出
$response->send();
}
/**
* 加入小区
*/
private function joinHouse($house_id,$openid){
$user = WechatService::getUserByOpenid($openid);
$house = House::get($house_id);
// 发送内容
$content = "欢迎关注{$house['name']}!";
$phoneList = HousePhone::where('house_id',$house_id)->select();
if(!empty($phoneList)){
$content .= "\n";
foreach ($phoneList as $v) {
$content .= "\n"."{$v['name']}:{$v['phone']}";
}
}
$vue_url = config('option.vue_url');
$content .= "\n\n"."业主账号:%u"
."\n\n"."<a href='{$vue_url}/?house_id={$house_id}'>查看物业公告</a>"
."\n\n"."<a href='{$vue_url}/message?house_id={$house_id}'>给物业留言</a>";
//*------------------------已有账号,直接加入小区-----------------------*/
if($user){
$info = UserHouse::get(['user_id'=>$user['id'],'house_id'=>$house_id]);
if(!empty($info) && $info['status'] == 2){
// 添加当前社区
$user->house_id = $house_id;
$user->save();
return sprintf($content,$user['id_num']);
}
Db::startTrans();
try {
if(empty($info)){
// 直接绑定成功
$data = UserHouse::create([
'user_id' => $user['id'],
'house_id' => $house_id,
'name' => $user['nickname'],
'status' => 2,
'phone' => $user['mobile']
]);
}else{
// 申请中改为已绑定
$info->status = 2;
$info->save();
}
// 添加当前社区
$user->house_id = $house_id;
$user->save();
// 更新小区绑定数量
$bindnum = UserHouse::where(['house_id'=>$house_id,'status'=>2])->count();
House::where('id',$house_id)->setField('bindnum',$bindnum);
Db::commit();
} catch (\think\exception\PDOException $e) {
Db::rollback();
return $e->getMessage();
} catch (\think\Exception $e) {
Db::rollback();
return $e->getMessage();
}
return sprintf($content,$user['id_num']);
}
/*------------------------没有账号,先注册,后加入小区-----------------------*/
$access_token = WechatService::getAccessToken();
//获取用户信息
$queryarr = [
"access_token" => $access_token,
"openid" => $openid,
"lang" => 'zh_CN'
];
$ret = \fast\Http::get('https://api.weixin.qq.com/cgi-bin/user/info', $queryarr);
$userinfo = (array)json_decode($ret, true);
if(!$userinfo) return '获取用户信息失败';
if (isset($userinfo['errcode'])) return $userinfo['errcode'];
$userinfo['avatar'] = isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '';
// 注册
$loginret = \addons\third\library\Service::connect('wechat', [
'access_token' => $access_token,
'refresh_token' => '',
'expires_in' => 7200,
'openid' => $openid,
'unionid' => isset($userinfo['unionid']) ? $userinfo['unionid'] : '',
'userinfo' => $userinfo
]);
if (!$loginret) return '注册失败';
// 加入小区
Db::startTrans();
try {
$user = $this->auth->getUser();
// 直接绑定成功
$data = UserHouse::create([
'user_id' => $user['id'],
'house_id' => $house_id,
'name' => $user['nickname'],
'status' => 2,
'phone' => $user['mobile']
]);
// 添加当前社区
$user->house_id = $house_id;
$user->save();
// 更新小区绑定数量
$bindnum = UserHouse::where(['house_id'=>$house_id,'status'=>2])->count();
House::where('id',$house_id)->setField('bindnum',$bindnum);
Db::commit();
} catch (\think\exception\PDOException $e) {
Db::rollback();
return $e->getMessage();
} catch (\think\Exception $e) {
Db::rollback();
return $e->getMessage();
}
return sprintf($content,$user['id_num']);
}
}