Official.php
4.6 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
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* Trait类 微信公众号
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Factories\Wechat
* @package App\Factories\Wechat
* @author Richer <yangzi1028@163.com>
* @date 2021年5月6日10:44:56
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.xxxxxx.com License
* @link http://www.xxxxxx.com
*/
namespace App\Factories\Wechat;
/**
* Trait Official
*
* @category App\Factories\Wechat
* @package App\Factories\Wechat
* @author Richer <yangzi1028@163.com>
* @date 2021年5月6日10:44:56
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.xxxxxx.com License
* @link http://www.xxxxxx.com
*/
trait Official
{
/**
* 消息发送
*
* 根据消息类型分别处理
*
* @return mixed
*/
public function push()
{
//
$this->official->server->push(function ($message) {
$method = Str::camel('handle_'. $message['MsgType']);
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], [$message]);
}
});
return $this->official->server->serve();
}
/**
* 事件类消息处理
*
* @param $message
* @return mixed
*/
protected function handleEvent($message)
{
Log::info('---------------------微信返回的消息---------------------');
Log::info($message);
//按事件类型处理
$event = \Str::camel('event_'. $message['Event']);
if (method_exists($this, $event)) {
return call_user_func_array([$this, $event], [$message]);
}
}
/**
* 事件类消息处理
*
* 关注公众号
*
* @param $message
* @return string
*/
protected function eventSubscribe($message)
{
// 设置了场景
if (\Arr::get($message, 'EventKey')) {
$message['EventKey'] = str_replace('qrscene_', '', \Arr::get($message, 'EventKey'));
$user_id = str_replace('INVITE', '', $message['EventKey']);
$user = User::findOrFail($user_id);
if ($user) {
$register_url = url('register') . '?inviter_id=' . $user_id;
return '欢迎关注"游米游"公众号,定制师【' . $user->nickname . '】邀请您成为游米游平台定制师,忙里偷闲赚取佣金,请前往注册:' . $register_url;
}
}
return '欢迎关注"游米游"公众号';
}
/**
* 事件类消息处理
*
* 扫描带参数二维码
*
* @param $message
* @return string
*/
protected function eventSCAN($message)
{
if (\Arr::get($message, 'EventKey')) {
$message['EventKey'] = str_replace('qrscene_', '', \Arr::get($message, 'EventKey'));
$user_id = str_replace('INVITE', '', $message['EventKey']);
$user = User::findOrFail($user_id);
if ($user) {
$register_url = url('register') . '?inviter_id=' . $user_id;
return '欢迎来到"游米游"公众号,定制师【' . $user->nickname . '】邀请您成为游米游平台定制师,忙里偷闲赚取佣金,请前往注册:' . $register_url;
}
}
return '欢迎来到"游米游"公众号';
}
/**
* 获取菜单
*
* @return void
*/
public function menu()
{
$list = $this->official->menu->list();
return $list;
}
public function pushTempMessage($templateId, $data)
{
// $userInfo = $request->userInfo;
// $first = '恭喜,您有新的粉丝挷定成功';
// $remark = '请您及时查看!';
//
// $result = $this->official->template_message->send([
// 'touser' => $openid,
// 'template_id' => '5OfQTYHbeXsqT2f8nkGeagyocjtBysNrGDaiZ_lStdw',
// 'url' => config('app.url'),
// 'miniprogram' => [
// 'appid' => config('wechat.mini_program.default.app_id'),
// 'path' => 'pages/details/standardDetail/standardDetail?id='.$id,
// ],
// 'data' => [
// 'first' => $first,
// 'keyword1' => $userInfo['nickName'],
// 'keyword2' => '无',
// 'remark' => $remark,
// ],
// ]);
}
}