微信小程序客服配置
anlondon Lv6

前言

# 建议各位先按底部的参考资料顺序查看,以便充分了解步骤以及设置要点

配置:

  • php7.4
  • tp5
  • easyWechat

部署

一,验证token

根据消息推送|token 验证的说明,在配置消息推送时,需要在对应的请求里添加操作
image

1
2
3
4
5
6
7
8
9
10
11
12
13
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$tmpArr = array($this->Token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if($tmpStr == $signature)
return true;
else
return false;

后端完整代码

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
<?php

namespace addons\shopro\controller;

use addons\shopro\library\Wechat as WechatLibrary;
use think\Request;

/**
* 微信小程序客服接口
*/
class WechatKefu extends Base
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $app = null;

protected $Token = null;
protected $EncodingAESKey = null;
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx****&secret='***'; //替换成自己的小程序id和secret
protected $get_accessToken_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential'; // 获取accessToken
protected $send_message_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='; // 发送消息给用户,用于用户联系客服时自动回复,不让微信服务器自动转发

public function __construct(Request $request = null)
{
parent::__construct($request);

$this->Token = config('site.wechatKefuToken');
$this->EncodingAESKey = config('site.wechatKefuEncodingAESKey');

$wechat = new WechatLibrary('wxMiniProgram');
$this->app = $wechat->getApp();
}

/**
* 微信小程序客服对接、处理消息回复
*/
public function index(){
if (isset($_GET['echostr']))
$this->valid();
else
$this->responseMsg();
}

// 验证token时使用 配置小程序后台->开发->开发管理->开发设置->消息推送
private function valid(){
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
header('content-type:text');
echo $echoStr; // 成功
exit;
}else{
exit('fail! '.$echoStr.'+++'.$this->Token); // 失败
}
}

// 配置小程序->消息推送->token验证时启用
private function checkSignature(){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$tmpArr = array($this->Token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if($tmpStr == $signature)
return true;
else
return false;
}

// 发送消息
private function responseMsg(){
$postStr = file_get_contents('php://input');

if(empty($postStr) && !is_string($postStr)){
echo "";
exit;
}

//禁止引用外部xml实体
//libxml_disable_entity_loader(true);
//$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$postArr =json_decode($postStr,true);

$fromUsername =$postArr['FromUserName']; //发送者openid
$toUserName =$postArr['ToUserName']; //小程序id
if(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='text'){//文本消息
$textTpl =array(
"ToUserName"=>$fromUsername,
"FromUserName"=>$toUserName,
"CreateTime"=>$_SERVER['REQUEST_TIME'],
"MsgType"=>"transfer_customer_service",
);

exit(json_encode($textTpl));
}elseif(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='image'){//图文消息
$textTpl =array(
"ToUserName"=>$fromUsername,
"FromUserName"=>$toUserName,
"CreateTime"=>$_SERVER['REQUEST_TIME'],
"MsgType"=>"transfer_customer_service",
);
exit(json_encode($textTpl));
}elseif($postArr['MsgType'] =='event' &&$postArr['Event']=='user_enter_tempsession'){//进入客服动作
$content ='您好,有什么能帮助你?';
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"text",
"text"=>array("content"=>$content)
);
$json =json_encode($data,JSON_UNESCAPED_UNICODE);

$this->requestAPI($json);
}else
exit('Error,未知类型会话');

}

// 发送消息到指定
private function requestAPI($json){
$access_token = $this->get_accessToken();
$this->send_message_url = $this->send_message_url.$access_token; // 发送消息 POST发送https请求客服接口api

//以'json'格式发送post的https请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->send_message_url);
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($json))
curl_setopt($curl, CURLOPT_POSTFIELDS,$json);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
$output = curl_exec($curl);
if (curl_errno($curl))
echo 'Errno'.curl_error($curl);//捕抓异常

curl_close($curl);

if($output == 0) {
echo 'success';
exit;
}
}

// 调用微信api,获取access_token,有效期7200s
private function get_accessToken(){
$url = $this->get_accessToken_url.'&appid='.$this->app->config['app_id'].'&secret='.$this->app->config['secret']; //替换成自己的小程序id和secret
@$weixin = file_get_contents($url);
@$jsondecode = json_decode($weixin);
@$array = get_object_vars($jsondecode);
$token = $array['access_token'];
return $token;
}

}


参考资料:

 Comments