Uupt.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\BaseDelivery;
  13. use think\exception\ValidateException;
  14. class Uupt extends BaseDelivery
  15. {
  16. //uu跑腿
  17. public $config;
  18. //域名
  19. // const BASE_URL = 'http://openapi.test.uupt.com';
  20. const BASE_URL = 'https://openapi.uupt.com';
  21. //发布订单
  22. const ADD_ORDER = '/v2_0/addorder.ashx';
  23. //计算价格
  24. const GET_ORDER_PRICE = '/v2_0/getorderprice.ashx';
  25. //详情
  26. const GET_ORDER_DETAIL = '/v2_0/getorderdetail.ashx';
  27. //充值
  28. const GET_RECHARGE = '/v2_0/getrecharge.ashx';
  29. //取消
  30. const CANCEL_ORDER = '/v2_0/cancelorder.ashx';
  31. //查询门店
  32. const GET_SHOP = '/v2_0/getshoplist.ashx';
  33. //余额
  34. const GET_BALANCEDE = '/v2_0/getbalancedetail.ashx';
  35. //获取城市
  36. const GET_CITY = '/v2_0/getcitylist.ashx';
  37. public function initialize(array $config)
  38. {
  39. $this->config = $config;
  40. parent::initialize($config);
  41. }
  42. /**
  43. * 发布订单
  44. * @param $data
  45. * @return mixed
  46. */
  47. public function addOrder($data)
  48. {
  49. $params = [
  50. 'price_token' => $data['price_token'],
  51. 'order_price' => $data['total_money'],
  52. 'balance_paymoney' => $data['need_paymoney'],
  53. 'receiver' => $data['receiver'],
  54. 'receiver_phone' => $data['receiver_phone'],
  55. 'callback_url' => $this->callback_url,
  56. 'push_type' => '0', //推送方式(0 开放订单,2测试订单)默认传0即可
  57. 'special_type' => '0',
  58. 'callme_withtake' => $data['callme_withtake'] ?? '0',
  59. 'pay_type' => 0,
  60. ];
  61. if ($data['note']) $params['note'] = $data['note'];
  62. return $this->sendRequest(self::ADD_ORDER, $params);
  63. }
  64. /**
  65. * 计算订单价格
  66. * @param $data
  67. * @return mixed
  68. */
  69. public function getOrderPrice($data)
  70. {
  71. $params = [
  72. 'from_address' => $data['from_address'],
  73. 'to_address' => $data['to_address'],
  74. 'city_name' => $data['city_name'],
  75. 'goods_type' => $data['goods_type'],
  76. 'send_type' =>'0',
  77. 'to_lat' => (string)$data['to_lat'],
  78. 'to_lng' => (string)$data['to_lng'],
  79. 'from_lat' => (string)$data['from_lat'],
  80. 'from_lng' => (string)$data['from_lng'],
  81. ];
  82. return $this->sendRequest(self::GET_ORDER_PRICE, $params);
  83. }
  84. /**
  85. * 获取订单详情
  86. * @param $data
  87. * @return mixed
  88. */
  89. public function getOrderDetail($data)
  90. {
  91. if($data['order_sn']) $params['origin_id'] = $data['order_sn'];
  92. if($data['order_code']) $params['order_code'] = $data['order_code'];
  93. return $this->sendRequest(self::GET_ORDER_DETAIL, $params);
  94. }
  95. /**
  96. * 取消订单
  97. * @param $data
  98. * @return mixed
  99. */
  100. public function cancelOrder($data)
  101. {
  102. $params = [
  103. 'origin_id' => $data['origin_id'],
  104. 'order_code'=> $data['order_code'],
  105. 'reason' => $data['reason'],
  106. ];
  107. return $this->sendRequest(self::CANCEL_ORDER, $params);
  108. }
  109. /**
  110. * 获取充值地址
  111. * @param $data
  112. * @return mixed
  113. */
  114. public function getRecharge($data)
  115. {
  116. return $this->sendRequest(self::GET_RECHARGE, $data);
  117. }
  118. /**
  119. * 获取余额
  120. * @param $data
  121. * @return mixed
  122. */
  123. public function getBalance($data)
  124. {
  125. $res = $this->sendRequest(self::GET_BALANCEDE, $data);
  126. return [
  127. 'deliverBalance' => $res['AccountMoney']
  128. ];
  129. }
  130. /**
  131. * 支付小费
  132. * @param $data
  133. * @return mixed
  134. */
  135. public function addTip($data)
  136. {
  137. return true;
  138. }
  139. /**
  140. * 获取城市信息
  141. * @param $data
  142. * @return mixed
  143. */
  144. public function getCity($data = [])
  145. {
  146. $res = $this->sendRequest(self::GET_CITY, $data);
  147. foreach ($res['CityList'] as $item) {
  148. $data[] = [
  149. 'key' => $item['CityName'],
  150. 'label' => $item['CityName'],
  151. ];
  152. }
  153. return $data;
  154. }
  155. /**
  156. * 发送请求
  157. * @param $api
  158. * @param $params
  159. * @return mixed
  160. */
  161. public function sendRequest($api, $params = [])
  162. {
  163. $url = self::BASE_URL . $api;
  164. $params = $this->bulidRequestParams($params);
  165. $response = $this->httpRequestWithPost($url, $params);
  166. $data = $this->getMessage($response);
  167. return $data;
  168. }
  169. /**
  170. * 构造请求数据
  171. * @param array $params
  172. * @return mixed
  173. */
  174. public function bulidRequestParams(array $params = [])
  175. {
  176. $params['openid'] = $this->config['open_id'];
  177. $params['appid'] = $this->config['app_id'];
  178. $params['nonce_str'] = str_replace('-', '', $this->guid());
  179. $params['timestamp'] = time();
  180. $params['sign'] = $this->_sign($params);
  181. $arr = [];
  182. foreach ($params as $key => $value) {
  183. $arr[] = $key . '=' . $value;
  184. }
  185. $curlPost = implode('&', $arr);
  186. return $curlPost;
  187. }
  188. /**
  189. * 生成guid
  190. * @param $data
  191. * @return mixed
  192. */
  193. public function guid()
  194. {
  195. mt_srand((float)microtime() * 10000); //optional for php 4.2.0 and up.
  196. $charid = strtoupper(md5(uniqid(rand(), true)));
  197. $hyphen = chr(45); // "-"
  198. $uuid = substr($charid, 0, 8) . $hyphen
  199. . substr($charid, 8, 4) . $hyphen
  200. . substr($charid, 12, 4) . $hyphen
  201. . substr($charid, 16, 4) . $hyphen
  202. . substr($charid, 20, 12);
  203. return strtolower(str_replace('-', '', $uuid));
  204. }
  205. /**
  206. * 签名生成sign
  207. * @param $data
  208. * @return mixed
  209. */
  210. public function _sign($data)
  211. {
  212. ksort($data);
  213. $str = '';
  214. foreach ($data as $key => $value) {
  215. if (!is_null($value)) {
  216. $str .= $key . '=' . $value . '&';
  217. }
  218. }
  219. $str .= 'key=' . $this->config['app_key'];
  220. $str = mb_strtoupper($str, 'UTF-8');
  221. // halt($data,$str);
  222. return strtoupper(md5($str));
  223. }
  224. /**
  225. * 发送请求,POST
  226. * @param $url 指定URL完整路径地址
  227. * @param $data 请求的数据
  228. */
  229. public function httpRequestWithPost($url, $data)
  230. {
  231. $curl = curl_init($url);
  232. curl_setopt($curl, CURLOPT_URL, $url);
  233. curl_setopt($curl, CURLOPT_HEADER, false);
  234. curl_setopt($curl, CURLOPT_POST, true);
  235. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  236. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  237. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  238. $resp = curl_exec($curl);
  239. $info = curl_getinfo($curl);
  240. curl_close($curl);
  241. return $resp;
  242. }
  243. /**
  244. * 获取错误信息
  245. * @param $json
  246. * @param $message
  247. * @return mixed
  248. */
  249. protected function getMessage($json, $message = '未知错误!')
  250. {
  251. $data = json_decode($json, true);
  252. if (!in_array($data['return_code'], ['ok', 'fail'])) {
  253. isset($data['return_msg']) && $message = $data['return_msg'];
  254. $mes = $message == '未知错误!' ? $this->getCodeMap($data['return_code']) : $message;
  255. throw new ValidateException('【UU错误提示】:'.$mes);
  256. } else {
  257. return $data;
  258. }
  259. }
  260. /**
  261. * 获取错误代码
  262. * @param string $key 代码
  263. * @return String 错误代码与信息
  264. */
  265. protected function getCodeMap($key)
  266. {
  267. $codeMap = [
  268. '-101' => '参数格式校验错误',
  269. '-102' => 'timestamp错误',
  270. '-103' => 'appid无效',
  271. '-104' => '签名校验失败',
  272. '-105' => 'openid无效',
  273. '-199' => '参数格式校验错误',
  274. '-1001' => '无法解析起始地',
  275. '-1002' => '无法解析目的地',
  276. '-1003' => '无法获取订单城市相关信息',
  277. '-1004' => '订单小类出现错误',
  278. '-1005' => '没有用户信息',
  279. '-1006' => '优惠券ID错误',
  280. '-2001' => 'price_token无效',
  281. '-2002' => 'price_token无效',
  282. '-2003' => '收货人电话格式错误',
  283. '-2004' => 'special_type错误',
  284. '-2005' => 'callme_withtake错误',
  285. '-2006' => 'order_price错误',
  286. '-2007' => 'balance_paymoney错误',
  287. '-2008' => '订单总金额错误',
  288. '-2009' => '支付金额错误',
  289. '-2010' => '用户不一致',
  290. '-2011' => '手机号错误',
  291. '-2012' => '不存在绑定关系',
  292. '-4001' => '取消原因不能为空',
  293. '-4002' => '订单编号无效',
  294. '-5001' => '订单编号无效',
  295. '-5002' => '订单编号无效',
  296. '-5003' => '订单编号无效',
  297. '-10001' => '发送频率过快,请稍候重试',
  298. '-11001' => '请输入正确的验证码',
  299. ];
  300. $info = isset($codeMap[$key]) ? $codeMap[$key] : false;
  301. return $info;
  302. }
  303. /**
  304. * 创建商户
  305. * @param $data
  306. * @return mixed
  307. */
  308. public function addMerchant($data)
  309. {
  310. return true;
  311. }
  312. /**
  313. * 获取门店信息
  314. * @param $id
  315. * @return array
  316. */
  317. public function getShopDetail($id)
  318. {
  319. return [];
  320. }
  321. /**
  322. * 创建门店
  323. * @param $data
  324. * @return mixed
  325. */
  326. public function addShop($data)
  327. {
  328. return true;
  329. }
  330. /**
  331. * 更新门店
  332. * @param $data
  333. * @return mixed
  334. */
  335. public function updateShop($data)
  336. {
  337. return true;
  338. }
  339. /**
  340. *
  341. * @return mixed
  342. */
  343. public function getBusiness()
  344. {
  345. return [
  346. ['key' => 1, 'label' => '美食'],
  347. ['key' => 2, 'label' => '鲜花'],
  348. ['key' => 3, 'label' => '蛋糕'],
  349. ['key' => 4, 'label' => '手机'],
  350. ['key' => 5, 'label' => '钥匙'],
  351. ['key' => 6, 'label' => '文件'],
  352. ['key' => 0, 'label' => '其他'],
  353. ];
  354. }
  355. }