Uupt.php 9.6 KB

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