Qiyu.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\common\service;
  3. /*
  4. * 223c300175fbea7b5f6ca4465ac6071f
  5. * 4D4EED8859E646DFA3BFB79B73075914
  6. *
  7. * */
  8. use app\admin\model\Admin;
  9. use GuzzleHttp\Client;
  10. use traits\think\Instance;
  11. class Qiyu{
  12. use Instance;
  13. protected $appKey='223c300175fbea7b5f6ca4465ac6071f';
  14. protected $appSecret='4D4EED8859E646DFA3BFB79B73075914';
  15. public function login(Admin $admin){
  16. $data=[
  17. 'staffName'=>$admin['username'],
  18. ];
  19. $json=$this->post('http://qiyukf.com/openapi/staff/login',$data);
  20. return $json['result']['sdk_url']??null;
  21. }
  22. /**
  23. * 普通客服 12762328
  24. */
  25. public function create(Admin $admin,$password){
  26. $data=[
  27. 'username'=>"service{$admin['id']}",
  28. 'password'=>md5($password),
  29. 'role'=>0,
  30. 'subRoleId'=>$admin['is_seller']?12764513:12767377,
  31. 'realname'=>$admin['nickname'],
  32. 'nickname'=>$admin['nickname'],
  33. 'mobile'=>$admin['mobile'],
  34. 'email'=>$admin['email']??'',
  35. ];
  36. $json=$this->post('https://qiyukf.com/openapi/kefu/add',$data);
  37. if($json['code']!==200){
  38. throw_user('['.$json['code'].']'.$json['message']);
  39. }
  40. return $json['id'];
  41. }
  42. /**
  43. * 销售 12764513
  44. * 管理 12767377
  45. */
  46. public function update(Admin $admin,$password){
  47. $data=[
  48. 'id'=>$admin['kf_id'],
  49. 'password'=>md5($password),
  50. 'role'=>0,
  51. 'subRoleId'=>$admin['is_seller']?12764513:12767377,
  52. 'realname'=>$admin['nickname'],
  53. 'nickname'=>$admin['nickname'],
  54. 'mobile'=>$admin['mobile'],
  55. 'email'=>$admin['email'],
  56. ];
  57. if(!$password){
  58. unset($data['password']);
  59. }
  60. $json=$this->post('https://qiyukf.com/openapi/kefu/update',$data);
  61. if($json['code']!==200){
  62. throw_user('修改失败'.$json['message']);
  63. }
  64. }
  65. public function delete(Admin $admin){
  66. if($admin['kf_id']){
  67. $data=[
  68. 'id'=>$admin['kf_id'],
  69. ];
  70. $this->post('https://qiyukf.com/openapi/kefu/delete',$data);
  71. }
  72. }
  73. protected function sum($data){
  74. $res=sha1(sprintf('%s%s%d',$this->appSecret,md5(json_encode($data)),$time=time()));
  75. return [$res,$time];
  76. }
  77. protected function post($url,$data){
  78. list($sum,$time)=$this->sum($data);
  79. $res=(new Client)
  80. ->post("$url?appKey={$this->appKey}&checksum={$sum}&time={$time}",[
  81. 'json'=>$data,
  82. ]);
  83. $json=json_decode($res->getBody()->getContents(),true);
  84. return $json;
  85. }
  86. }