
应用场景
现在网站都有客服系统,客服与user沟通时,掌握user的信息越多越方便沟通。这样就需要客服系统能够与网站的user系统对接,这样能够不用user告诉客服,就能够知道用户的order,account等相关信息。
此时系统需要api接口与web端后台对接。
测试了live800客服系统,由于没有官方Api接口,自己手写一个,特记录一下,方便后人。
基本逻辑
- 在调用live800的js后面加入info信息,将userid和username等信息传递live800
- 在live800客服端设置信任url和key值
- live800客户端请求信任url(/live-user),在客户端中加载此页面显示。
- live-user页面对应控制器api/live/getUserInfo,在控制器中,需要验证用户key,防止信息泄露
代码如下
| 12
 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
 
 | 
 
 
 
 class ControllerLive800 extends Controller
 {
 private $liveHostUrl = 'http://v2.live800.com/live800/chatClient/floatButton.js?';
 
 public function __construct($registry)
 {
 parent::__construct($registry);
 $this->load->model('live/client');
 }
 
 
 
 
 
 
 
 public function getLive800Script()
 {
 $url = $this->liveHostUrl;
 $str = $this->getInfoValueStr();
 if(!empty($str)){
 $url .= '&info=' . $str;
 }
 return $url;
 }
 
 
 
 
 
 
 
 public function getUserInfo()
 {
 $input = [
 'userId'    => isset($this->request->get['userId']) ? $this->request->get['userId'] : '',
 'timestamp' => isset($this->request->get['timestamp']) ? $this->request->get['timestamp'] : '',
 'hashCode'  => isset($this->request->get['hashCode']) ? $this->request->get['hashCode'] : '',
 ];
 $obj = $this->model_live_client;
 foreach($input as $key=>$val){
 $obj->setParameter($key, $val);
 }
 
 if(empty($obj->checkSign())){
 return false;
 }
 
 
 }
 
 
 
 
 public function getInfoValueStr()
 {
 $isLogged = $this->customer->isLogged();
 if(!$isLogged){
 return '';
 }
 $data = [
 'userId'    => 'userid',
 'name'    => 'username',
 'memo'    => 'test',
 'timestamp' => time(),
 ];
 
 $obj = $this->model_api_live_client;
 foreach($data as $key=>$val){
 $obj->setParameter($key, $val);
 }
 
 return $obj ->getInfoValueStr();
 }
 }
 
 
 | 
| 12
 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
 
 | class ModelLiveClient extends Model
 {
 private $key = 'you key';
 private $parameters = [];
 
 
 
 
 function getKey()
 {
 return $this->key;
 }
 
 
 
 
 
 
 function checkSign() {
 $signPars = "";
 foreach($this->parameters as $k => $v) {
 if("hashCode" != $k && "" != $v) {
 $signPars .= $v;
 }
 }
 $signPars .= $this->getKey();
 
 $sign = md5(($signPars));
 $tenpaySign = $this->getParameter("hashCode");
 return $sign == $tenpaySign;
 }
 
 
 
 
 function formatStr($paraMap, $urlencode)
 {
 $buff = "";
 foreach ($paraMap as $k => $v)
 {
 if($urlencode)
 {
 $v = urlencode($v);
 }
 $buff .= $v;
 }
 return $buff;
 }
 
 
 
 
 function formatBizQueryParaMap($paraMap)
 {
 $buff = "";
 foreach ($paraMap as $k => $v)
 {
 $buff .= $k . "=" . $v. '&';
 }
 if (strlen($buff) > 0)
 {
 $buff = substr($buff, 0, strlen($buff)-1);
 }
 return $buff;
 }
 
 public function getInfoValueStr()
 {
 $str = $this->formatBizQueryParaMap($this->parameters);
 $str .= '&hashCode=' . $this->getSign($this->parameters);
 return urlencode($str);
 }
 
 
 
 
 public function getSign($Obj)
 {
 $Parameters = [];
 foreach ($Obj as $k => $v)
 {
 if("hashCode" == $k){
 continue;
 }
 $Parameters[$k] = $v;
 }
 
 $string = $this->formatStr($Parameters, false);
 
 
 $string = $string . $this->getKey();
 
 $string = md5(urlencode($string));
 
 return $string;
 }
 
 
 
 
 function setParameter($parameter, $parameterValue)
 {
 $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
 }
 
 
 
 
 function getParameter($parameter) {
 return isset($this->parameters[$parameter])?$this->parameters[$parameter] : '';
 }
 
 function trimString($value)
 {
 $ret = null;
 if (null != $value)
 {
 $ret = $value;
 if (strlen($ret) == 0)
 {
 $ret = null;
 }
 }
 return $ret;
 }
 }
 
 |