Dgtdetaildata.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 滴灌通订单详情数据
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Dgtdetaildata extends Backend
  11. {
  12. /**
  13. * Dgtdetaildata模型对象
  14. * @var \app\admin\model\Dgtdetaildata
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Dgtdetaildata;
  21. }
  22. /**
  23. * 查看
  24. */
  25. public function index()
  26. {
  27. //当前是否为关联查询
  28. $this->relationSearch = true;
  29. //设置过滤方法
  30. $this->request->filter(['strip_tags', 'trim']);
  31. if ($this->request->isAjax()) {
  32. //如果发送的来源是Selectpage,则转发到Selectpage
  33. if ($this->request->request('keyField')) {
  34. return $this->selectpage();
  35. }
  36. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  37. $list = $this->model
  38. ->with(['car'])
  39. ->where($where)
  40. ->order($sort, $order)
  41. ->paginate($limit);
  42. foreach ($list as $row) {
  43. $row->getRelation('car')->visible(['chepaihao']);
  44. }
  45. $result = array("total" => $list->total(), "rows" => $list->items());
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 订单详情数据上报
  52. */
  53. public function sendout(){
  54. $id = input('id',0);
  55. $info = Db::name('dgt_detail_data')->where('id',$id)->find();
  56. $money = $info['money'];
  57. $addtime = $info['datatime'];
  58. //车辆信息
  59. $car_info = Db::name('car')->where('id',$info['car_id'])->find();
  60. $chepaihao = $car_info['chepaihao'];
  61. //gps信息
  62. $gps_json = '';
  63. $gps_info = $this->getgpsinfo($car_info['chepaihao'],$car_info['chepaicolor']);
  64. if(is_array($gps_info) && isset($gps_info['Longitude']) && isset($gps_info['Latitude'])){
  65. $gps_json = json_encode([
  66. 'Longitude' => $gps_info['Longitude'],
  67. 'Latitude' => $gps_info['Latitude'],
  68. ]);
  69. }
  70. // 设置一个公钥(key)和私钥(secret),公钥用于区分用户,私钥加密数据,不能公开
  71. $key = "44D1010BC0D8403D94AA35F014C0974D";
  72. $secret = "022BDF69C0A641008E5D70EB4E63BBCE";
  73. $mchId = 'BS202401243529022486';
  74. $store_id = 'b9f1b72b2cad493887cf';
  75. // 待发送的数据包
  76. $nowtime = $addtime;
  77. $batchNo = date('YmdHis',$nowtime) . $mchId;
  78. $data = [
  79. 'batchNo' => $batchNo,
  80. 'mchId' => $mchId,
  81. 'batchDatetime' => date('Y-m-d H:i:s',$nowtime),
  82. 'totalCount' => 1,
  83. 'totalTurnover' => $money,
  84. 'totalActualAmount' => $money,
  85. 'businessDate' => date('Y-m-d',$nowtime),
  86. 'orderDetails' => [
  87. [
  88. 'storeId' => $store_id,
  89. 'orderNo' => createUniqueNo('D'),
  90. // 'originalOrderNo' => '',
  91. 'completedAt' => date('Y-m-d H:i:s',$nowtime),
  92. 'orderType' => '00',
  93. 'turnover' => $money,
  94. 'actualAmount' => $money,
  95. 'tradeDetails' => [
  96. [
  97. 'tradeName' => $chepaihao,
  98. 'tradeCount' => $money,
  99. ]
  100. ],
  101. 'extContentJson' => $gps_json,
  102. ]/*,
  103. [
  104. 'storeId' => '12506389',
  105. 'orderNo' => '20220981223312',
  106. // 'originalOrderNo' => '20220981223312',
  107. 'completedAt' => '2022-08-22 14:22:34',
  108. 'orderType' => '00',
  109. 'turnover' => 30,
  110. 'actualAmount' => 30,
  111. 'tradeDetails' => [
  112. [
  113. 'tradeName' => '鲁Q123456',
  114. 'tradeCount' => 100,
  115. ],
  116. ],
  117. ],
  118. [
  119. 'storeId' => '12506389',
  120. 'orderNo' => '20220981223312',
  121. // 'originalOrderNo' => '20220981223312',
  122. 'completedAt' => '2022-08-22 14:22:34',
  123. 'orderType' => '00',
  124. 'turnover' => 40,
  125. 'actualAmount' => 40,
  126. 'tradeDetails' => [
  127. [
  128. 'tradeName' => '鲁Q123456',
  129. 'tradeCount' => 100,
  130. ],
  131. ],
  132. ],*/
  133. ]
  134. ];
  135. $data_json = json_encode($data,JSON_UNESCAPED_UNICODE);
  136. $time = $this->getMillisecond();
  137. $paramStr = $this->buildSignStr($data);
  138. $needSignStr = $paramStr . '&key=' . $secret . '&timestamp=' . $time;
  139. $signStr = base64_encode(hash_hmac('md5', $needSignStr, $secret,true));
  140. //dump($signStr);
  141. // echo 'needSignStr = [' . $needSignStr . ']' . ',sign=' .$signStr;
  142. $customHeader = array();
  143. $customHeader[] = 'Content-Type: application/json;charset=utf-8';
  144. $customHeader[] = 'appKey:' . $key ;
  145. $customHeader[] = 'timestamp:' . $time ;
  146. $customHeader[] = 'sign:' . $signStr ;
  147. $customHeader[] = 'verify:HmacMd5' ;
  148. $result = $this->send_post('https://sandbox.mcisaas.com/api/dock/nouser/api/oms/order/daily/order-details', $data_json,$customHeader);
  149. $update = [
  150. 'times' => $info['times'] + 1,
  151. 'sendtime' => time(),
  152. 'content' => $data_json,
  153. ];
  154. Db::name('dgt_detail_data')->where('id',$id)->update($update);
  155. $this->success($result);
  156. }
  157. private function getgpsinfo($chepaihao = '',$chepaicolor = '')
  158. {
  159. $url = 'http://140.210.193.143:8088/topgps/services/TopDataService.ashx';
  160. $url = 'http://ads.cetgps.com/topgps/services/TopDataService.ashx';
  161. $request = [
  162. 'Action' => 'GetVehicleGpsInfo',
  163. 'UserId' => 'dxqc',
  164. 'Pwd' => strtoupper(md5('134679aA#')),
  165. 'Vehicles' => [
  166. [
  167. 'PlateNum' => $chepaihao,
  168. 'ColorCode' => $chepaicolor,
  169. ],
  170. ],
  171. 'NeedAddress' => '1',
  172. ];
  173. $curl_data = ['request'=>json_encode($request)];
  174. $result = curl_post($url,$curl_data);
  175. //$result = '{"Ret":0,"Msg":"","Data":[{"PlateNum":"川AA80498","ColorCode":"5","GpsTime":"2024/1/17 15:04:13","Mileage":"50805.90","Speed":"5.5","Longitude":"104.045393","Latitude":"30.646638","Address":"四川省成都市武侯区武侯祠大街231-2号 成都武侯祠博物馆南272米","Direction":"94","Altitude":"463","IsGpsValid":"1","AlarmInfo":"","StateInfo":"ACC开;定位;运营;","DriverLicense":"","DriverName":"","OrgName":"成都登欣汽车服务有限公司"}],"Datal":[]}';
  176. $data = [];
  177. $result = json_decode($result,true);
  178. if(is_array($result) && isset($result['Ret']) && $result['Ret'] == 0){
  179. if(isset($result['Data'][0])){
  180. $data0 = $result['Data'][0];
  181. unset($data0['DriverLicense']);
  182. unset($data0['DriverName']);
  183. unset($data0['OrgName']);
  184. }
  185. }
  186. //dump($data);
  187. return $data0;
  188. }
  189. private function buildSignStr($data) {
  190. $paramStr = '';
  191. $array = [];
  192. foreach ($data as $key => $value) {
  193. $array[] = $key;
  194. }
  195. sort($array);
  196. foreach ($array as $key) {
  197. $value = $data[$key];
  198. if ($key === 'sign' || is_null($value) || $value === '') {
  199. continue;
  200. }
  201. if(is_array($value)){
  202. $paramStr .= $key;
  203. $paramStr .= '=';
  204. $paramStr .= '[';
  205. $paramStr .= $this->buildArray($value);
  206. $paramStr .= ']';
  207. }else {
  208. $paramStr .= implode('=', [$key, $value]);
  209. }
  210. $paramStr .='&';
  211. }
  212. if($paramStr == ''){
  213. return '';
  214. }
  215. return substr($paramStr,0,strripos($paramStr,'&'));
  216. }
  217. private function buildArray($data){
  218. $paramStr = '';
  219. foreach($data as $item){
  220. $paramStr .= '{';
  221. foreach($item as $field => $val){
  222. if(is_array($val)){
  223. // dump( __LINE__ );
  224. $paramStr .= $field;
  225. $paramStr .= '=';
  226. $paramStr .= '[';
  227. $paramStr .= $this->buildArray($val);
  228. $paramStr .= '], ';
  229. }else{
  230. // dump( __LINE__ );
  231. $paramStr .= $field.'='.$val.', ';
  232. }
  233. }
  234. $paramStr = substr($paramStr,0,-2);
  235. $paramStr .= '}, ';
  236. }
  237. if($paramStr == ''){
  238. return '';
  239. }
  240. return substr($paramStr,0,strripos($paramStr,','));
  241. }
  242. private function buildobject($data){
  243. $paramStr = '';
  244. foreach($data as $field => $val){
  245. $paramStr .= $field.'='.$val.', ';
  246. }
  247. //$paramStr = substr($paramStr,0,-2);
  248. return substr($paramStr,0,strripos($paramStr,','));
  249. }
  250. private function send_post($url, $postdata,$header) {
  251. $rs = curl_post($url,$postdata,$header,900);
  252. return $rs;
  253. /* $options = array(
  254. 'http' => array(
  255. 'method' => 'POST',
  256. 'header' => $header,
  257. 'content' => $postdata,
  258. 'timeout' => 15 * 60 // 超时时间(单位:s)
  259. )
  260. );
  261. $context = stream_context_create($options);
  262. return file_get_contents($url, false, $context);*/
  263. }
  264. /**
  265. * 获取时间戳到毫秒
  266. * @return bool|string
  267. */
  268. private function getMillisecond(){
  269. list($msec, $sec) = explode(' ', microtime());
  270. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  271. return $msectimes = substr($msectime,0,13);
  272. }
  273. private function get_batch_no(){
  274. $batch_id = Db::name('dgt_batchid_log')->order('id desc')->value('batch_id');
  275. $batch_id += 1;
  276. if($batch_id > 999999){
  277. $batch_id = 1;
  278. }
  279. Db::name('dgt_batchid_log')->insertGetId(['batch_id'=>$batch_id]);
  280. $zero_arr = [
  281. 1 => '00000',
  282. 2 => '0000',
  283. 3 => '000',
  284. 4 => '00',
  285. 5 => '0',
  286. 6 => '',
  287. ];
  288. $xuliehao = $zero_arr[strlen($batch_id)].$batch_id;
  289. $mchId = 'BS202401243529022486';
  290. $batchNo = date('Ymd',time()) . $xuliehao . $mchId;
  291. return $batchNo;
  292. }
  293. }