Dada.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------\
  11. namespace crmeb\services\delivery\storage;
  12. use crmeb\basic\BaseStorage;
  13. use crmeb\interfaces\DeliveryInterface;
  14. use think\exception\ValidateException;
  15. class Dada extends BaseStorage implements DeliveryInterface
  16. {
  17. const BASE_URL = 'https://newopen.imdada.cn';
  18. const ADD_MERCHANT = '/merchantApi/merchant/add';
  19. const ADD_SHOP = '/api/shop/add';
  20. const UPDATE_SHOP = '/api/shop/update';
  21. const GET_CITY_CODE = '/api/cityCode/list';
  22. const GET_SHOP_DETAIL = '/api/shop/detail';
  23. const GET_ORDER_PRICE = '/api/order/queryDeliverFee';
  24. const ADD_ORDER_AFTER_QUERY = '/api/order/addAfterQuery';
  25. const ADD_ORDER_STSATUS_QUERY = '/api/order/status/query';
  26. const GET_REASONS = '/api/order/cancel/reasons';
  27. const CANCEL_ORDER = '/api/order/formalCancel';
  28. const GET_BALANCE = '/api/balance/query';
  29. const GET_RECHARGE = '/api/recharge';
  30. public $config;
  31. public function initialize(array $config)
  32. {
  33. $this->config = $config;
  34. }
  35. //创建商户
  36. public function addMerchant($data)
  37. {
  38. return $this->sendRequest(self::ADD_MERCHANT, $data);
  39. }
  40. //创建门店
  41. public function addShop($data)
  42. {
  43. $parmas = [];
  44. foreach ($data as $key => $item) {
  45. if (!($item['lng']) || !($item['lat']))
  46. throw new ValidateException('经纬度不能为空');
  47. if (!($item['phone']) || !($item['contact_name']))
  48. throw new ValidateException('联系人信息不能为空');
  49. if (!($item['business']))
  50. throw new ValidateException('配送物品分类不能为空');
  51. if (!($item['station_name']) || !$item['station_address'])
  52. throw new ValidateException('门店信息不能为空');
  53. $value = [
  54. 'lng' => (float)$item['lng'],
  55. 'lat' => (float)$item['lat'],
  56. 'phone' => $item['phone'],
  57. 'business' => (int)$item['business'],
  58. 'contact_name' => $item['contact_name'],
  59. 'station_name' => $item['station_name'],
  60. 'station_address' => $item['station_address'],
  61. 'status' => 1,
  62. 'origin_shop_id' => $item['origin_shop_id'],
  63. ];
  64. if (isset($item['username']) && $item['username']) $value['username'] = $item['username'];
  65. if (isset($item['password']) && $item['password']) $value['password'] = $item['password'];
  66. $parmas[] = $value;
  67. }
  68. return $this->sendRequest(self::ADD_SHOP, $parmas);
  69. }
  70. //更新门店
  71. public function updateShop($data)
  72. {
  73. $params['origin_shop_id'] = $data['origin_shop_id'];
  74. if (isset($data['new_shop_id'])) $params['new_shop_id'] = $data['new_shop_id'];
  75. if (isset($data['station_name'])) $params['station_name'] = $data['station_name'];
  76. if (isset($data['business'])) $params['business'] = $data['business'];
  77. if (isset($data['station_address'])) $params['station_address'] = $data['station_address'];
  78. if (isset($data['lng'])) $params['lng'] = $data['lng'];
  79. if (isset($data['lat'])) $params['lat'] = $data['lat'];
  80. if (isset($data['contact_name'])) $params['contact_name'] = $data['contact_name'];
  81. if (isset($data['phone'])) $params['phone'] = $data['phone'];
  82. if (isset($data['status'])) $params['status'] = $data['status'];
  83. return $this->sendRequest(self::UPDATE_SHOP, $params);
  84. }
  85. //预发布订单
  86. public function addOrder($data)
  87. {
  88. $params = [
  89. 'deliveryNo' => $data['deliveryNo'],
  90. ];
  91. return $this->sendRequest(self::ADD_ORDER_AFTER_QUERY, $params);
  92. }
  93. //计算订单价格
  94. public function getOrderPrice($data)
  95. {
  96. $params = [
  97. 'shop_no' => $data['shop_no'],
  98. 'origin_id' => $data['origin_id'],
  99. 'city_code' => $data['city_code'],
  100. 'cargo_price' => $data['cargo_price'],
  101. 'is_prepay' => $data['is_prepay'],
  102. 'receiver_name' => $data['receiver_name'],
  103. 'receiver_address'=> $data['receiver_address'],
  104. 'callback' => $data['callback_url'],
  105. 'cargo_weight' => $data['cargo_weight'],
  106. 'receiver_phone' => $data['receiver_phone'],
  107. 'is_finish_code_needed'=> $data['is_finish_code_needed'],
  108. ];
  109. return $this->sendRequest(self::GET_ORDER_PRICE, $params);
  110. }
  111. //获取订单详情
  112. public function getOrderDetail($data)
  113. {
  114. $params['order_id'] = $data['origin_id'];
  115. return $this->sendRequest(self::ADD_ORDER_STSATUS_QUERY, $params);
  116. }
  117. //取消订单
  118. public function cancelOrder($data)
  119. {
  120. $params['order_id'] = $data['origin_id'];
  121. $params['cancel_reason'] = $data['cancel_reason'] ?? '无';
  122. $params['cancel_reason_id'] = $data['reason'];
  123. return $this->sendRequest(self::CANCEL_ORDER, $params);
  124. }
  125. //获取充值地址
  126. public function getRecharge($data =[])
  127. {
  128. $params = [
  129. 'amount' => $data['amount'] ?? 100,
  130. 'category'=> $data['category'] ?? 'PC',
  131. ];
  132. return $this->sendRequest(self::GET_RECHARGE, $params);
  133. }
  134. //获取余额
  135. public function getBalance($data)
  136. {
  137. $params['category'] = $data['category'] ?? 3;
  138. $res = $this->sendRequest(self::GET_BALANCE, $params);
  139. return [
  140. 'deliverBalance' => $res['deliverBalance']
  141. ];
  142. }
  143. //支付小费
  144. public function addTip($data)
  145. {
  146. }
  147. //取消原因
  148. public function reasons($data = '')
  149. {
  150. $options = $this->sendRequest(self::GET_REASONS, $data);
  151. foreach ($options as $option) {
  152. $value = $option['id'];
  153. $label = $option['reason'];
  154. $res[] = compact('value','label');
  155. }
  156. return $res;
  157. }
  158. //获取城市信息
  159. public function getCity($data = '')
  160. {
  161. $res = $this->sendRequest(self::GET_CITY_CODE, $data);
  162. foreach ($res as $item) {
  163. $data[] = [
  164. 'key' => $item['cityName'],
  165. 'label' => $item['cityName'],
  166. ];
  167. }
  168. return $data;
  169. }
  170. public function getShopDetail($id)
  171. {
  172. $data = ['origin_shop_id' => $id];
  173. return $this->sendRequest(self::GET_SHOP_DETAIL, $data);
  174. }
  175. public function getBusiness()
  176. {
  177. return [
  178. ['key' => 1 , 'label' => '食品小吃'],
  179. ['key' => 2 , 'label' => '饮料'],
  180. ['key' => 3 , 'label' => '鲜花绿植'],
  181. ['key' => 5 , 'label' => '其他'],
  182. ['key' => 8 , 'label' => '文印票务'],
  183. ['key' => 9 , 'label' => '便利店'],
  184. ['key' => 13 , 'label' => '水果生鲜'],
  185. ['key' => 19 , 'label' => '同城电商'],
  186. ['key' => 20 , 'label' => '医药'],
  187. ['key' => 21 , 'label' => '蛋糕'],
  188. ['key' => 24 , 'label' => '酒品'],
  189. ['key' => 25 , 'label' => '小商品市场'],
  190. ['key' => 26 , 'label' => '服装'],
  191. ['key' => 27 , 'label' => '汽修零配'],
  192. ['key' => 28 , 'label' => '数码家电'],
  193. ['key' => 29 , 'label' => '小龙虾/烧烤'],
  194. ['key' => 31 , 'label' => '超市'],
  195. ['key' => 51 , 'label' => '火锅'],
  196. ['key' => 53 , 'label' => '个护美妆'],
  197. ['key' => 55 , 'label' => '母婴'],
  198. ['key' => 57 , 'label' => '家居家纺'],
  199. ['key' => 59 , 'label' => '手机'],
  200. ['key' => 61 , 'label' => '家装'],
  201. ['key' => 63 , 'label' => '成人用品'],
  202. ];
  203. }
  204. public function sendRequest($api, $params)
  205. {
  206. $url = self::BASE_URL . $api;
  207. $params = $this->bulidRequestParams($params);
  208. $response = $this->httpRequestWithPost($url, $params);
  209. $data = $this->getMessage($response);
  210. return $data;
  211. }
  212. /**
  213. * 构造请求数据
  214. * data:业务参数,json字符串
  215. */
  216. public function bulidRequestParams($params)
  217. {
  218. $requestParams = array();
  219. $requestParams['app_key'] = $this->config['app_key'];
  220. $requestParams['body'] = json_encode($params);
  221. $requestParams['format'] = 'json';
  222. $requestParams['v'] = '1.0';
  223. $requestParams['source_id'] = $this->config['source_id'];
  224. $requestParams['timestamp'] = time();
  225. $requestParams['signature'] = $this->_sign($requestParams);
  226. return json_encode($requestParams);
  227. }
  228. /**
  229. * 签名生成signature
  230. */
  231. public function _sign($data)
  232. {
  233. //1.升序排序
  234. ksort($data);
  235. //2.字符串拼接
  236. $args = "";
  237. foreach ($data as $key => $value) {
  238. $args .= $key . $value;
  239. }
  240. $args = $this->config['app_secret'] . $args . $this->config['app_secret'];
  241. //3.MD5签名,转为大写
  242. $sign = strtoupper(md5($args));
  243. return $sign;
  244. }
  245. /**
  246. * 发送请求,POST
  247. * @param $url 指定URL完整路径地址
  248. * @param $data 请求的数据
  249. */
  250. public function httpRequestWithPost($url, $data, $headers = [])
  251. {
  252. $headers = array(
  253. 'Content-Type: application/json',
  254. );
  255. $curl = curl_init($url);
  256. curl_setopt($curl, CURLOPT_URL, $url);
  257. curl_setopt($curl, CURLOPT_HEADER, false);
  258. curl_setopt($curl, CURLOPT_POST, true);
  259. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  260. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  261. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  262. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  263. $resp = curl_exec($curl);
  264. $info = curl_getinfo($curl);
  265. curl_close($curl);
  266. if (isset($info['http_code']) && $info['http_code'] == 200) {
  267. return $resp;
  268. }
  269. return ;
  270. }
  271. protected function getMessage($json, $message = '未知错误!')
  272. {
  273. $data = json_decode($json, true);
  274. if ($data['code'] !== 0) {
  275. isset($data['msg']) && $message = $data['msg'];
  276. if ($data['errorCode'] == 7718) {
  277. foreach ($data['result']['failedList'] as $datum) {
  278. $message .= ':'.$datum['shopName'].'/'. $datum['msg'].';';
  279. }
  280. }
  281. throw new ValidateException('【达达错误提示】:'.$message);
  282. } else {
  283. if ($data['status'] == 'success') return $data['result'] ?? $data;
  284. return $data;
  285. }
  286. }
  287. }