Kuaidi.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\enum\Setting as SettingEnum;
  4. use app\common\library\express\Kuaidi100;
  5. use app\api\model\Kuaidi as KuaidiModel;
  6. use app\common\model\store\Setting as SettingModel;
  7. use think\facade\Log;
  8. /**
  9. * 快递100接口
  10. * Class Index
  11. * @package app\api\controller
  12. */
  13. class Kuaidi extends Controller
  14. {
  15. //订阅请求接口
  16. public function subscribe(){
  17. $arg = $this->request->param();
  18. // 获取快递100配置项
  19. // 实例化快递100类
  20. $config = SettingModel::getItem(SettingEnum::DELIVERY);
  21. if (empty($config['kuaidi100']['customer']) || empty($config['kuaidi100']['key'])) {
  22. throwError('请先到后台-设置-配送方式 补充物流查询API配置');
  23. }
  24. // 实例化快递100类
  25. $Kuaidi100 = new Kuaidi100($config['kuaidi100']);
  26. // 请求订阅接口
  27. $list = $Kuaidi100->subscribe($arg['number'],$arg['type'] ?? 1,$arg['company'],$arg['phone']??'');
  28. return $this->renderSuccess(compact('list'));
  29. }
  30. public function dynamic(){
  31. $arg = $this->request->param();
  32. if (empty($arg['name']) || empty($arg['code']) || empty($arg['no'])){
  33. return $this->renderError('参数错误');
  34. }
  35. // 获取快递100配置项
  36. // 实例化快递100类
  37. $config = SettingModel::getItem(SettingEnum::DELIVERY);
  38. if (empty($config['kuaidi100']['customer']) || empty($config['kuaidi100']['key'])) {
  39. throwError('请先到后台-设置-配送方式 补充物流查询API配置');
  40. }
  41. // 实例化快递100类
  42. $Kuaidi100 = new \app\common\model\Express($config['kuaidi100']);
  43. // 请求订阅接口
  44. $list = $Kuaidi100->dynamic($arg['name'],$arg['code'],$arg['no'],$arg['phone'] ?? '');
  45. if (empty($list)){
  46. return $this->renderError('查询出错');
  47. }
  48. /* //手动保存物流路径
  49. $kuaidiModle = new \app\api\model\Kuaidi();
  50. $res = $kuaidiModle->get(['number'=>$arg['no']]);
  51. if (!empty($res['id'])){
  52. $json= [
  53. "status" => "",
  54. "billstatus" => "",
  55. "message" => "",
  56. "lastResult"=>[
  57. "message"=>"ok",
  58. "nu"=>$arg['no'],
  59. "isCheck"=>"1",
  60. "com"=>$arg['code'],
  61. "status"=>"200",
  62. "data"=>$list['list']]
  63. ];
  64. $kuaidiModle->updateBase(['json' => serialize($json),'update_time'=>time()], ['id'=>$res['id']]);
  65. }*/
  66. return $this->renderSuccess(compact('list'));
  67. }
  68. /**
  69. * 快递推送回调接口
  70. * @return array
  71. * @author: zjwhust
  72. * @Time: 2021/9/29 14:06
  73. */
  74. public function callback(){
  75. $model = new KuaidiModel;
  76. Log::info('callback:'.':'.json_encode($this->request->param()));
  77. if ($model->upd($this->request->param())) {
  78. return json([
  79. "result"=>true,
  80. "returnCode"=>"200",
  81. "message"=>"成功"
  82. ]);
  83. }
  84. return json([
  85. "result"=>true,
  86. "returnCode"=>"500",
  87. "message"=>"没有接收到订单信息"
  88. ]);
  89. }
  90. }