Dgtdaysdata.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 Dgtdaysdata extends Backend
  11. {
  12. /**
  13. * Dgtdaysdata模型对象
  14. * @var \app\admin\model\Dgtdaysdata
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Dgtdaysdata;
  21. }
  22. /**
  23. * 营业日结数据上报
  24. */
  25. public function sendout()
  26. {
  27. $id = input('id',0);
  28. $info = Db::name('dgt_days_data')->where('id',$id)->find();
  29. $money = $info['money'];
  30. $addtime = $info['datatime'];
  31. // 设置一个公钥(key)和私钥(secret),公钥用于区分用户,私钥加密数据,不能公开
  32. $key = "44D1010BC0D8403D94AA35F014C0974D";
  33. $secret = "022BDF69C0A641008E5D70EB4E63BBCE";
  34. $mchId = 'BS202401243529022486';
  35. $store_id = 'b9f1b72b2cad493887cf';
  36. // 待发送的数据包
  37. $nowtime = $addtime;
  38. // $batchNo = $this->get_batch_no();
  39. $batchNo = date('YmdHis',$nowtime) . $mchId;
  40. $data = [
  41. 'batchNo' => $batchNo,
  42. 'mchId' => $mchId,
  43. 'batchDatetime' => date('Y-m-d H:i:s',$nowtime),
  44. 'totalCount' => '1',
  45. 'totalTurnover' => $money,
  46. 'totalActualAmount' => $money,
  47. 'dailyIncomeSummarys' => [
  48. [
  49. 'storeId' => $store_id,
  50. 'businessDate' => date('Y-m-d',$nowtime),
  51. 'storeStatus' => '1',
  52. 'turnover' => $money,
  53. 'actualAmount' => $money,
  54. ]/*,
  55. [
  56. 'storeId' => $store_id,
  57. 'businessDate' => '2021-10-30',
  58. 'storeStatus' => '1',
  59. 'turnover' => '-50.00',
  60. 'actualAmount' => '-50.00',
  61. ],
  62. [
  63. 'storeId' => $store_id,
  64. 'businessDate' => '2021-10-31',
  65. 'storeStatus' => '1',
  66. 'turnover' => '10855.50',
  67. 'actualAmount' => '9955.50',
  68. ],*/
  69. ],
  70. ];
  71. $data_json = json_encode($data,JSON_UNESCAPED_UNICODE);
  72. $time = $this->getMillisecond();
  73. $paramStr = $this->buildSignStr($data);
  74. $needSignStr = $paramStr . '&key=' . $secret . '&timestamp=' . $time;
  75. //dump($needSignStr);
  76. $signStr = base64_encode(hash_hmac('md5', $needSignStr, $secret,true));
  77. //dump($signStr);
  78. // echo 'needSignStr = [' . $needSignStr . ']' . ',sign=' .$signStr;
  79. $customHeader = array();
  80. $customHeader[] = 'Content-Type: application/json;charset=utf-8';
  81. $customHeader[] = 'appKey:' . $key ;
  82. $customHeader[] = 'timestamp:' . $time ;
  83. $customHeader[] = 'sign:' . $signStr ;
  84. $customHeader[] = 'verify:HmacMd5' ;
  85. $result = $this->send_post('https://sandbox.mcisaas.com/api/dock/nouser/api/oms/order/daily/financial-stat', $data_json,$customHeader);
  86. $update = [
  87. 'times' => $info['times'] + 1,
  88. 'sendtime' => time(),
  89. 'content' => $data_json,
  90. ];
  91. Db::name('dgt_days_data')->where('id',$id)->update($update);
  92. $this->success($result);
  93. }
  94. private function buildSignStr($data) {
  95. $paramStr = '';
  96. $array = [];
  97. foreach ($data as $key => $value) {
  98. $array[] = $key;
  99. }
  100. sort($array);
  101. foreach ($array as $key) {
  102. $value = $data[$key];
  103. if ($key === 'sign' || is_null($value) || $value === '') {
  104. continue;
  105. }
  106. if(is_array($value)){
  107. $paramStr .= $key;
  108. $paramStr .= '=';
  109. $paramStr .= '[';
  110. $paramStr .= $this->buildArray($value);
  111. $paramStr .= ']';
  112. }else {
  113. $paramStr .= implode('=', [$key, $value]);
  114. }
  115. $paramStr .='&';
  116. }
  117. if($paramStr == ''){
  118. return '';
  119. }
  120. return substr($paramStr,0,strripos($paramStr,'&'));
  121. }
  122. private function buildArray($data){
  123. $paramStr = '';
  124. foreach($data as $item){
  125. $paramStr .= '{';
  126. foreach($item as $field => $val){
  127. if(is_array($val)){
  128. // dump( __LINE__ );
  129. $paramStr .= $field;
  130. $paramStr .= '=';
  131. $paramStr .= '[';
  132. $paramStr .= $this->buildArray($val);
  133. $paramStr .= '], ';
  134. }else{
  135. // dump( __LINE__ );
  136. $paramStr .= $field.'='.$val.', ';
  137. }
  138. }
  139. $paramStr = substr($paramStr,0,-2);
  140. $paramStr .= '}, ';
  141. }
  142. if($paramStr == ''){
  143. return '';
  144. }
  145. return substr($paramStr,0,strripos($paramStr,','));
  146. }
  147. private function buildobject($data){
  148. $paramStr = '';
  149. foreach($data as $field => $val){
  150. $paramStr .= $field.'='.$val.', ';
  151. }
  152. //$paramStr = substr($paramStr,0,-2);
  153. return substr($paramStr,0,strripos($paramStr,','));
  154. }
  155. private function send_post($url, $postdata,$header) {
  156. $rs = curl_post($url,$postdata,$header,900);
  157. return $rs;
  158. /* $options = array(
  159. 'http' => array(
  160. 'method' => 'POST',
  161. 'header' => $header,
  162. 'content' => $postdata,
  163. 'timeout' => 15 * 60 // 超时时间(单位:s)
  164. )
  165. );
  166. $context = stream_context_create($options);
  167. return file_get_contents($url, false, $context);*/
  168. }
  169. /**
  170. * 获取时间戳到毫秒
  171. * @return bool|string
  172. */
  173. private function getMillisecond(){
  174. list($msec, $sec) = explode(' ', microtime());
  175. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  176. return $msectimes = substr($msectime,0,13);
  177. }
  178. private function get_batch_no(){
  179. $batch_id = Db::name('dgt_batchid_log')->order('id desc')->value('batch_id');
  180. $batch_id += 1;
  181. if($batch_id > 999999){
  182. $batch_id = 1;
  183. }
  184. Db::name('dgt_batchid_log')->insertGetId(['batch_id'=>$batch_id]);
  185. $zero_arr = [
  186. 1 => '00000',
  187. 2 => '0000',
  188. 3 => '000',
  189. 4 => '00',
  190. 5 => '0',
  191. 6 => '',
  192. ];
  193. $xuliehao = $zero_arr[strlen($batch_id)].$batch_id;
  194. $mchId = 'BS202401243529022486';
  195. $batchNo = date('Ymd',time()) . $xuliehao . $mchId;
  196. return $batchNo;
  197. }
  198. }