Api.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\pospal;
  8. use app\models\Option;
  9. use app\constants\OptionSetting;
  10. /**
  11. * http://pospal.cn/openplatform/ticketapi.html#queryTicketPages
  12. */
  13. class Api
  14. {
  15. const TEST_DATA = 0;
  16. // const API_HOST = 'https://area11-win.pospal.cn:443';
  17. const API_PREFIX = '/pospal-api2/openapi/v1/';
  18. // const APP_ID = '8DA6E1645F34D18697DDC1ACCF6090E8';
  19. // const APP_KEY = '741629749281739495';
  20. public static function conf($store_id) {
  21. $conf = Option::get(OptionSetting::POSPAL, $store_id, 'store')['value'];
  22. if($conf){
  23. $conf = json_decode($conf, true);
  24. }
  25. return $conf;
  26. }
  27. public static function appid($store_id) {
  28. $conf = self::conf($store_id);
  29. return empty($conf['app_id']) ? '' : $conf['app_id'];
  30. }
  31. public static function appkey($store_id) {
  32. $conf = self::conf($store_id);
  33. return empty($conf['app_key']) ? '' : $conf['app_key'];
  34. }
  35. public static function apihost($store_id) {
  36. $conf = self::conf($store_id);
  37. return empty($conf['api_host']) ? '' : trim($conf['api_host'], '/');
  38. }
  39. public static function getUrl($store_id, $apiUrl) {
  40. return self::apihost($store_id) . self::API_PREFIX . $apiUrl;
  41. }
  42. public static function postUrl($store_id, $url, $arrayData)
  43. {
  44. $curl = curl_init();
  45. $json_data = json_encode($arrayData);
  46. $data_signature = self::signature($store_id, $json_data);
  47. $http_header = [
  48. 'User-Agent: openApi',
  49. 'Content-Type: application/json; charset=utf-8',
  50. 'accept-encoding: gzip,deflate',
  51. 'time-stamp:' . self::get_millisecond(),
  52. 'data-signature:' . $data_signature,
  53. 'Content-Type:' . 'application/json',
  54. ];
  55. debug_log('接口地址');
  56. debug_log(self::getUrl($store_id, $url));
  57. debug_log('请求参数');
  58. debug_log($arrayData);
  59. curl_setopt($curl, CURLOPT_URL, self::getUrl($store_id, $url));
  60. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($curl, CURLOPT_ENCODING, '');
  62. curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
  63. curl_setopt($curl, CURLOPT_TIMEOUT, 0);
  64. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  65. curl_setopt($curl, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  66. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  67. curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
  68. curl_setopt($curl, CURLOPT_HTTPHEADER, $http_header);
  69. $response = curl_exec($curl);
  70. curl_close($curl);
  71. $res = json_decode($response,true);
  72. debug_log('返回');
  73. debug_log($response);
  74. return $res;
  75. }
  76. public static function api($store_id, $url, $arrayData) {
  77. $arrayData['appId'] = self::appid($store_id);
  78. return self::postUrl($store_id, $url, $arrayData);
  79. }
  80. private static function signature($store_id, $json_data)
  81. {
  82. $data_signature = strtoupper(md5(self::appkey($store_id). $json_data));
  83. return $data_signature;
  84. }
  85. private static function get_millisecond()
  86. {
  87. $time = explode(" ", microtime());
  88. $time = $time [1] . ($time [0] * 1000);
  89. $time2 = explode(".", $time);
  90. $time = $time2 [0];
  91. return $time;
  92. }
  93. public static function queryTicketPages($store_id, $param = []) {
  94. $json = '{
  95. "status": "success",
  96. "messages": [],
  97. "data": {
  98. "postBackParameter": {
  99. "parameterType": "abcd",
  100. "parameterValue": "abcd"
  101. },
  102. "result": [
  103. {
  104. "cashierUid": 832101480292933201,
  105. "customerUid": 922312867022331106,
  106. "sn": "' . bcadd('201511121602084680006', random_int(1000000000000, 9000000000000)) . '",
  107. "datetime": "'. date('Y-m-d H:i:s', time() - 600) .'",
  108. "totalAmount": 72,
  109. "totalProfit": 42,
  110. "discount": 100,
  111. "externalOrderNo": "2017121521001004460219576817",
  112. "remark": "收银测试",
  113. "rounding": 0,
  114. "ticketType": "SELL",
  115. "invalid": 0,
  116. "payments": [
  117. {
  118. "code": "payCode_2",
  119. "amount": 9.42
  120. }
  121. ],
  122. "pointUsage":{
  123. "customerUid": 922312867022331106,
  124. "point": 80.00,
  125. "money": 8.00,
  126. "exchangeType": 1
  127. },
  128. "items": [
  129. {
  130. "name": "a2至初 婴儿配方奶粉6-12月2段",
  131. "buyPrice": 10,
  132. "sellPrice": 24,
  133. "customerPrice": 22,
  134. "quantity": 1,
  135. "discount": 100,
  136. "customerDiscount": 100,
  137. "totalAmount": 24,
  138. "totalProfit": 14,
  139. "isCustomerDiscount": 1,
  140. "productUid": 376007949281628195,
  141. "ticketitemattributes": [
  142. {
  143. "attributeName": "加粮",
  144. "attributeValue": "一匙"
  145. }
  146. ]
  147. },
  148. {
  149. "name": "亨氏 苹果蓝莓紫胡萝卜紫薯果汁泥 120g",
  150. "buyPrice": 4.5,
  151. "sellPrice": 7.80,
  152. "customerPrice": 7.80,
  153. "quantity": 2,
  154. "discount": 100,
  155. "customerDiscount": 100,
  156. "totalAmount": 15.6,
  157. "totalProfit": 10,
  158. "isCustomerDiscount": 1,
  159. "productUid": 498684986632650050,
  160. "ticketitemattributes": [
  161. {
  162. "attributeName": "加粮",
  163. "attributeValue": "一匙"
  164. }
  165. ]
  166. }
  167. ]
  168. }
  169. ],
  170. "pageSize": 100
  171. }
  172. }
  173. ';
  174. if(self::TEST_DATA){
  175. return json_decode($json, true);
  176. }
  177. $res = self::api($store_id, 'ticketOpenApi/queryTicketPages', $param);
  178. return $res;
  179. }
  180. public static function queryCustomerPages($store_id, $param = []) {
  181. $res = self::api($store_id, 'customerOpenApi/queryCustomerPages', $param);
  182. return $res;
  183. }
  184. public static function queryUserByUid($store_id, $uid = 0) {
  185. // $json = '{"status":"success","messages":[],"data":{"customrUid":922312867022331106,"customerUid":922312867022331106,"categoryName":"普通","number":"13932800866","name":"张","point":0.00,"discount":100,"balance":0,"phone":"13932800866","qq":"","email":"","address":"","remarks":"","createdDate":"2019-05-03 00:00:00","onAccount":0,"enable":1,"createStoreAppIdOrAccount":"9D5491A79556D828BC80BA6E990EBC84"}}';
  186. // if(self::TEST_DATA){
  187. // return json_decode($json, true);
  188. // }
  189. $res = self::api($store_id, 'customerOpenApi/queryByUid', ["customerUid" => $uid]);
  190. return $res;
  191. }
  192. public static function queryUserBytel($store_id, $tel = 0) {
  193. // $json = '{"status":"success","messages":[],"data":{"customrUid":922312867022331106,"customerUid":922312867022331106,"categoryName":"普通","number":"13932800866","name":"张","point":0.00,"discount":100,"balance":0,"phone":"13932800866","qq":"","email":"","address":"","remarks":"","createdDate":"2019-05-03 00:00:00","onAccount":0,"enable":1,"createStoreAppIdOrAccount":"9D5491A79556D828BC80BA6E990EBC84"}}';
  194. // if(self::TEST_DATA){
  195. // return json_decode($json, true);
  196. // }
  197. $res = self::api($store_id, 'customerOpenApi/queryBytel', ["customerTel" => $tel]);
  198. return $res;
  199. }
  200. public static function addUser($store_id, $param = []) {
  201. $res = self::api($store_id, 'customerOpenApi/add', $param);
  202. return $res;
  203. }
  204. public static function updateBalancePointByIncrement($store_id, $param = []) {
  205. $res = self::api($store_id, 'customerOpenApi/updateBalancePointByIncrement', $param);
  206. return $res;
  207. }
  208. public static function queryProductCategoryPages($store_id, $param = []) {
  209. $res = self::api($store_id, 'productOpenApi/queryProductCategoryPages', $param);
  210. return $res;
  211. }
  212. public static function queryProductPages($store_id, $param = []) {
  213. $res = self::api($store_id, 'productOpenApi/queryProductPages', $param);
  214. return $res;
  215. }
  216. public static function queryProductImgPages($store_id, $param = []) {
  217. $res = self::api($store_id, 'productOpenApi/queryProductImagePages', $param);
  218. // $res = '{"status":"success","messages":[],"data":{"postBackParameter":{"parameterType":"LAST_RESULT_MAX_ID","parameterValue":"332253"},"result":[{"productUid":307524486551868475,"productName":"美汁源爽粒花语红葡萄+玫瑰汁饮料420ml","productBarcode":"6956416205130","imageUrl":"http://pospalstoreimg.area63.pospal.cn:80//productImages/3208961/3e83648f-ebb4-4382-9244-5762dea1c9ca.jpg"},{"productUid":341670696225363202,"productName":"美汁源汁汁桃桃桃汁饮料420ml","productBarcode":"6956416206830","imageUrl":"http://pospalstoreimg.area63.pospal.cn:80//spd/23/638114656312203846.jpg"}],"pageSize":100}}';
  219. // $res = json_decode($res, true);
  220. return $res;
  221. }
  222. public static function queryProductImagesByBarcode($store_id, $uid = 0) {
  223. $res = self::api($store_id, 'productOpenApi/queryProductImagesByBarcode', ["productBarcode" => $uid]);
  224. // $res = '{"status":"success","messages":[],"data":[{"productUid":163971162588607457,"productName":"黑葡萄雨伞货号19303","productBarcode":"6928265693031","imageUrl":"http://pospalstoreimg.area63.pospal.cn:80/productImages/4953454/354b70f8-6eef-4a99-91ab-0bb60b59b59b.jpg"},{"productUid":163971162588607457,"productName":"黑葡萄雨伞货号19303","productBarcode":"6928265693031","imageUrl":"http://pospalstoreimg.area63.pospal.cn:80/productImages/4953454/585252ba-ae47-4eb6-bd65-44699f1145bf.jpg"},{"productUid":163971162588607457,"productName":"黑葡萄雨伞货号19303","productBarcode":"6928265693031","imageUrl":"http://pospalstoreimg.area63.pospal.cn:80/productImages/4953454/02b8eeff-e19b-4f9f-855c-e7a067383e93.jpg"},{"productUid":163971162588607457,"productName":"黑葡萄雨伞货号19303","productBarcode":"6928265693031","imageUrl":"http://pospalstoreimg.area63.pospal.cn:80/productImages/4953454/9c334ff4-1442-479a-b6a4-3669848637a4.jpg"}]}';
  225. // $res = json_decode($res, true);
  226. return $res;
  227. }
  228. public static function queryProductByUid($store_id, $uid = 0) {
  229. // $json = '{"status":"success","messages":[],"data":{"uid":376007949281628195,"categoryUid":1650434916469386247,"name":"a2至初 奶粉婴儿配方奶粉6-12月2段900g 1罐 新西兰进口","barcode":"9421902960017","buyPrice":321.000,"sellPrice":458.00,"sellPrice2":458.00,"stock":100.000,"maxStock":0,"minStock":5.00,"pinyin":"a2nf900g2d","customerPrice":412.00,"description":"","isCustomerDiscount":0,"supplierUid":0,"enable":1,"productionDate":"","createdDatetime":"2022-05-09 14:15:24","updatedDatetime":"2022-06-23 14:51:30","attribute1":"","attribute2":"","attribute3":"","attribute4":"9421902960017","attribute5":"","attribute6":"","attribute10":"100"}}';
  230. // if(self::TEST_DATA){
  231. // return json_decode($json, true);
  232. // }
  233. $res = self::api($store_id, 'productOpenApi/queryProductByUid', ["productUid" => $uid]);
  234. return $res;
  235. }
  236. public static function queryProductByBarcode($store_id, $Barcode = 0) {
  237. // $json = '{"status":"success","messages":[],"data":{"uid":376007949281628195,"categoryUid":1650434916469386247,"name":"a2至初 奶粉婴儿配方奶粉6-12月2段900g 1罐 新西兰进口","barcode":"9421902960017","buyPrice":321.000,"sellPrice":458.00,"sellPrice2":458.00,"stock":100.000,"maxStock":0,"minStock":5.00,"pinyin":"a2nf900g2d","customerPrice":412.00,"description":"","isCustomerDiscount":0,"supplierUid":0,"enable":1,"productionDate":"","createdDatetime":"2022-05-09 14:15:24","updatedDatetime":"2022-06-23 14:51:30","attribute1":"","attribute2":"","attribute3":"","attribute4":"9421902960017","attribute5":"","attribute6":"","attribute10":"100"}}';
  238. // if(self::TEST_DATA){
  239. // return json_decode($json, true);
  240. // }
  241. $res = self::api($store_id, 'productOpenApi/queryProductByBarcode', ["barcode" => $Barcode]);
  242. return $res;
  243. }
  244. public static function addProductInfo($store_id, $param = []) {
  245. $res = self::api($store_id, 'productOpenApi/addProductInfo', $param);
  246. return $res;
  247. }
  248. public static function updateProductInfo($store_id, $param = []) {
  249. $res = self::api($store_id, 'productOpenApi/updateProductInfo', $param);
  250. return $res;
  251. }
  252. public static function queryAccessTimes($store_id, $param = []) {
  253. $res = self::api($store_id, 'openApiLimitAccess/queryAccessTimes', $param);
  254. return $res;
  255. }
  256. public static function queryDailyAccessTimesLog($store_id, $param = []) {
  257. $res = self::api($store_id, 'openApiLimitAccess/queryDailyAccessTimesLog', $param);
  258. return $res;
  259. }
  260. }