Open.Class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/9/7
  6. * Time: 11:33 AM
  7. */
  8. namespace Util\KeRuYun;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Factory;
  12. class Open
  13. {
  14. // 基础域名
  15. //private $baseUrl = 'https://gldopenapi.keruyun.com';
  16. private $baseUrl = 'https://openapi.keruyun.com';
  17. // 客如云门店id
  18. private $shopIdenty = '';
  19. // 授权KEY
  20. private $appKey = '';
  21. // 授权密钥
  22. private $secretKey = '';
  23. // 版本
  24. private $version = '1.0';
  25. //公共header头
  26. private $commonHeader = [
  27. CURLOPT_HTTPHEADER => [
  28. 'Content-Type: application/json'
  29. ],
  30. ];
  31. public function __construct($shopIdenty='', $appKey = '', $secretKey = '' )
  32. {
  33. $this->shopIdenty = $shopIdenty;
  34. $this->appKey = $appKey;
  35. $this->secretKey = $secretKey;
  36. }
  37. /**
  38. * 公共请求参数
  39. * 官方文档地址: https://open.keruyun.com/apidoc/cn/common/common-1.html
  40. */
  41. public function commonRequestData()
  42. {
  43. return $commonRequestData = [
  44. 'appKey' => $this->appKey,
  45. 'shopIdenty' => $this->shopIdenty,
  46. 'version' => $this->version,
  47. 'timestamp' => time(),
  48. 'sign' => ''
  49. ];
  50. }
  51. /**
  52. * 生成签名方法
  53. * 官方文档地址: https://open.keruyun.com/apidoc/cn/common/common-3.html
  54. */
  55. public function getSign($token)
  56. {
  57. $commonRequestData = self::commonRequestData();
  58. unset($commonRequestData['sign']);
  59. ksort($commonRequestData, SORT_STRING);
  60. $parameters = '';
  61. foreach ($commonRequestData as $key => $value){
  62. $parameters .= $key.$value;
  63. }
  64. return hash('sha256', $parameters.$token);
  65. }
  66. /**
  67. * 生成请求地址
  68. */
  69. public function getUrl($apiUrl, $token)
  70. {
  71. $commonRequestData = self::commonRequestData();
  72. $sign = self::getSign($token);
  73. $commonRequestData['sign'] = $sign;
  74. $url = '';
  75. foreach ($commonRequestData as $key => $value){
  76. $url .= $key.'='.$value;
  77. if($key != 'sign'){
  78. $url .= '&';
  79. }
  80. }
  81. return $this->baseUrl.$apiUrl.'?'.$url;
  82. }
  83. /**
  84. * 获取门店token
  85. * 官方文档地址: https://open.keruyun.com/apidoc/cn/common/common-2.html
  86. */
  87. public function getToken()
  88. {
  89. $url = self::getUrl('/open/v1/token/get', $this->secretKey);
  90. $response = request($url, null, 30, true);
  91. return self::commonReponseResult($response);
  92. }
  93. /**
  94. * 菜品分页查询
  95. * 官方文档地址: https://open.keruyun.com/apidoc/cn/dish/dish-3.html
  96. */
  97. public function dishMenu($token, $startId = 1, $pageNum = 1000)
  98. {
  99. $url = self::getUrl('/open/v1/cater/dish/dishMenu', $token);
  100. $postData = [
  101. 'shopIdenty' => $this->shopIdenty,
  102. 'startId' => $startId,
  103. 'pageNum' => $pageNum
  104. ];
  105. $response = request($url, json_encode($postData), 30, true, $this->commonHeader);
  106. return self::commonReponseResult($response);
  107. }
  108. /**
  109. * 菜品精确查询
  110. * 官方文档地址: https://open.keruyun.com/apidoc/cn/dish/dish-4.html
  111. */
  112. public function dishMenuByIds($token, $ids)
  113. {
  114. $url = self::getUrl('/open/v1/cater/dish/dishMenuByIds', $token);
  115. $postData = [
  116. 'shopIdenty' => $this->shopIdenty,
  117. 'ids' => $ids,
  118. ];
  119. $response = request($url, json_encode($postData), 30, true, $this->commonHeader);
  120. return self::commonReponseResult($response);
  121. }
  122. /**
  123. * 创建顾客
  124. * 官方文档地址: https://open.keruyun.com/apidoc/cn/crm/crm-1.html
  125. */
  126. public function createCustomer($token, $customerData)
  127. {
  128. $url = self::getUrl('/open/v1/crm/createCustomer', $token);
  129. $postData = [
  130. 'birthday' => $customerData['birthday'], // 生日时间戳
  131. 'loginId' => $customerData['loginId'], // 手机号或者微信id
  132. 'loginType' => $customerData['loginType'], //登陆类型(0:手机注册客户;1:微信注册用户;2:座机号;101:微信会员卡卡号;)
  133. 'name' => $customerData['name'],
  134. 'sex' => $customerData['sex'],
  135. ];
  136. $response = request($url, json_encode($postData), 30, true, $this->commonHeader);
  137. return self::commonReponseResult($response);
  138. }
  139. /**
  140. * 创建/升级会员
  141. * 官方文档地址: https://open.keruyun.com/apidoc/cn/crm/crm-2.html
  142. */
  143. public function createOrUpgradeMember($token, $customerData)
  144. {
  145. $url = self::getUrl('/open/v1/crm/createOrUpgradeMember', $token);
  146. $postData = [
  147. 'attentionWxTime' => time(), // 关注微信时间
  148. 'birthday' => time(), // 生日时间戳
  149. 'consumePwd' => '123456',
  150. 'customerId' => 0,
  151. 'customerMainId' => 0,
  152. 'loginId' => $customerData['loginId'], // 手机号或者微信id
  153. 'loginType' => $customerData['loginType'], //登陆类型(0:手机注册客户;1:微信注册用户;2:座机号;101:微信会员卡卡号;)
  154. 'name' => $customerData['name'],
  155. 'sex' => $customerData['sex'],
  156. ];
  157. $response = request($url, json_encode($postData), 30, true, $this->commonHeader);
  158. return self::commonReponseResult($response);
  159. }
  160. /**
  161. * 顾客/会员登录
  162. * 官方文档地址: https://open.keruyun.com/apidoc/cn/crm/login-1.html
  163. */
  164. public function login($token, $mobile)
  165. {
  166. $url = self::getUrl('/open/v1/crm/login', $token);
  167. $postData = [
  168. 'loginId' => $mobile, // 手机号或者微信id
  169. 'loginType' => 0, //登陆类型(0:手机注册客户;1:微信注册用户;2:座机号;101:微信会员卡卡号;)
  170. ];
  171. $response = request($url, json_encode($postData), 30, true, $this->commonHeader);
  172. return self::commonReponseResult($response);
  173. }
  174. /**
  175. * 查询会员信息
  176. * 官方文档地址; https://open.keruyun.com/apidoc/cn/crm/crm-3.html
  177. */
  178. public function getCustomerDetailById($token, $customerId)
  179. {
  180. $url = self::getUrl('/open/v1/crm/getCustomerDetailById', $token);
  181. $postData = [
  182. 'customerId' => $customerId,
  183. 'isNeedCredit' => 1
  184. ];
  185. $response = request($url, json_encode($postData), 30, true, $this->commonHeader);
  186. return self::commonReponseResult($response);
  187. }
  188. /**
  189. * 会员充值
  190. * 官方文档地址:https://open.keruyun.com/apidoc/cn/crm/charge-2.html
  191. */
  192. public function recharge($token, $customerRechargeData)
  193. {
  194. $url = self::getUrl('/open/v1/crm/member/recharge', $token);
  195. $postData = [
  196. 'customerId' => $customerRechargeData['customerId'],
  197. 'cardType' => 2,
  198. 'businessType' => 1,
  199. 'amount' => $customerRechargeData['amount'],
  200. 'tpOrderId' => $customerRechargeData['tpOrderId'],
  201. ];
  202. $response = request($url, json_encode($postData, JSON_UNESCAPED_UNICODE), 30, true, $this->commonHeader); V($response);
  203. return self::commonReponseResult($response);
  204. }
  205. /**
  206. * 外卖下单
  207. * 官网文档地址:https://open.keruyun.com/apidoc/cn/takeout/takeout-1.html
  208. */
  209. public function createTakeoutOrder($token, $orderData)
  210. {
  211. $url = self::getUrl('/open/v1/takeout/order/create', $token);
  212. $postData = [
  213. 'tpOrderId' => $orderData['tpOrderId'], // 第三方订单号
  214. 'createTime' => $orderData['createTime'], // 订单创建时间
  215. 'peopleCount' => 1, // 就餐人数
  216. 'shop' =>[
  217. 'shopIdenty' => $this->shopIdenty, // 客如云门店ID
  218. 'tpShopId' => $orderData['tpShopId'], // 合作方商户id
  219. 'shopName' => $orderData['shopName'] // 商户名称
  220. ],
  221. 'products' => $orderData['products'], // 商品信息
  222. 'delivery' => $orderData['delivery'], // 配送信息
  223. 'payment' => $orderData['payment'], // 支付信息
  224. 'customers' => $orderData['customers'], // 客户信息
  225. 'remark' => $orderData['remark'],
  226. ];
  227. $response = request($url, json_encode($postData, JSON_UNESCAPED_UNICODE), 30, true, $this->commonHeader);
  228. return self::commonReponseResult($response);
  229. }
  230. /**
  231. * 堂食快餐下单
  232. * 官网地址:
  233. */
  234. public function createSnackOrder($token, $orderData)
  235. {
  236. $postData = [
  237. 'tpOrderId' => $orderData['tpOrderId'],
  238. 'createTime' => $orderData['createTime'],
  239. 'updateTime' => $orderData['updateTime'],
  240. 'payment' => $orderData['payment'],
  241. 'peopleCount' => 1, // 就餐人数
  242. 'print' => 1, // 打印
  243. 'remark' => '小程序订单', // 备注
  244. 'status' => 2, // 已经确认
  245. 'shopIdenty' => $this->shopIdenty,
  246. 'shopName' => $orderData['shopName'],
  247. 'products' => $orderData['products'],
  248. 'customers' => [$orderData['customers']],
  249. 'totalPrice' => $orderData['totalPrice'],
  250. ];
  251. $url = self::getUrl('/open/v1/snack/order/create', $token);
  252. $response = request($url, json_encode($postData), 30, true, $this->commonHeader); V($response);
  253. return self::commonReponseResult($response);
  254. }
  255. /**
  256. * 取消订单
  257. * 官网文档地址 https://open.keruyun.com/apidoc/cn/takeout/takeout-6.html
  258. */
  259. public function cancel($token, $orderId)
  260. {
  261. $url = self::getUrl('/open/v1/takeout/order/cancel', $token);
  262. $postData = [
  263. 'orderId' => $orderId, // 创建订单时系统返回给开发者的开放平台订单id
  264. 'reason' => '不想买了'
  265. ];
  266. $response = request($url, json_encode($postData, JSON_UNESCAPED_UNICODE), 30, true, $this->commonHeader); V($response);
  267. return self::commonReponseResult($response);
  268. }
  269. /**
  270. * 堂食快餐订单查询
  271. * 官网地址: https://open.keruyun.com/apidoc/cn/zhengcan/zhengcan-19.html
  272. */
  273. public function dinnerOrderDetail($token, $orderId)
  274. {
  275. $url = self::getUrl('/open/v1/dinner/order/detail', $token);
  276. $postData = [
  277. 'id' => $orderId,
  278. ];
  279. $response = request($url, json_encode($postData, JSON_UNESCAPED_UNICODE), 30, true, $this->commonHeader);
  280. return self::commonReponseResult($response);
  281. }
  282. /**
  283. * 订单详情
  284. * 官方文档地址
  285. */
  286. public function orderExportDetail($token, $orderIds = [])
  287. {
  288. $url = self::getUrl('/open/v1/data/order/exportDetail', $token);
  289. $postData = [
  290. 'shopIdenty' => $this->shopIdenty,
  291. 'ids' => $orderIds,
  292. ];
  293. $response = request($url, json_encode($postData, JSON_UNESCAPED_UNICODE), 30, true, $this->commonHeader); V($response);
  294. return self::commonReponseResult($response);
  295. }
  296. /**
  297. * 公共的请去响应处理函数
  298. */
  299. public function commonReponseResult($response)
  300. {
  301. if ($response['httpcode'] != 200) {
  302. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  303. }
  304. $responseData = json_decode($response['content'], true);
  305. if($responseData['code'] != 0){
  306. return ResultWrapper::fail($responseData['message'], $responseData['code']);
  307. }
  308. return ResultWrapper::success(isset($responseData['result'])?$responseData['result']:$responseData['message']);
  309. }
  310. }