123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\services\delivery\storage;
- use crmeb\basic\BaseDelivery;
- use think\exception\ValidateException;
- class Uupt extends BaseDelivery
- {
- //uu跑腿
- public $config;
- //域名
- // const BASE_URL = 'http://openapi.test.uupt.com';
- const BASE_URL = 'https://openapi.uupt.com';
- //发布订单
- const ADD_ORDER = '/v2_0/addorder.ashx';
- //计算价格
- const GET_ORDER_PRICE = '/v2_0/getorderprice.ashx';
- //详情
- const GET_ORDER_DETAIL = '/v2_0/getorderdetail.ashx';
- //充值
- const GET_RECHARGE = '/v2_0/getrecharge.ashx';
- //取消
- const CANCEL_ORDER = '/v2_0/cancelorder.ashx';
- //查询门店
- const GET_SHOP = '/v2_0/getshoplist.ashx';
- //余额
- const GET_BALANCEDE = '/v2_0/getbalancedetail.ashx';
- //获取城市
- const GET_CITY = '/v2_0/getcitylist.ashx';
- public function initialize(array $config)
- {
- $this->config = $config;
- parent::initialize($config);
- }
- /**
- * 发布订单
- * @param $data
- * @return mixed
- */
- public function addOrder($data)
- {
- $params = [
- 'price_token' => $data['price_token'],
- 'order_price' => $data['total_money'],
- 'balance_paymoney' => $data['need_paymoney'],
- 'receiver' => $data['receiver'],
- 'receiver_phone' => $data['receiver_phone'],
- 'callback_url' => $this->callback_url,
- 'push_type' => '0', //推送方式(0 开放订单,2测试订单)默认传0即可
- 'special_type' => '0',
- 'callme_withtake' => $data['callme_withtake'] ?? '0',
- 'pay_type' => 0,
- ];
- if ($data['note']) $params['note'] = $data['note'];
- return $this->sendRequest(self::ADD_ORDER, $params);
- }
- /**
- * 计算订单价格
- * @param $data
- * @return mixed
- */
- public function getOrderPrice($data)
- {
- $params = [
- 'from_address' => $data['from_address'],
- 'to_address' => $data['to_address'],
- 'city_name' => $data['city_name'],
- 'goods_type' => $data['goods_type'],
- 'send_type' =>'0',
- 'to_lat' => (string)$data['to_lat'],
- 'to_lng' => (string)$data['to_lng'],
- 'from_lat' => (string)$data['from_lat'],
- 'from_lng' => (string)$data['from_lng'],
- ];
- return $this->sendRequest(self::GET_ORDER_PRICE, $params);
- }
- /**
- * 获取订单详情
- * @param $data
- * @return mixed
- */
- public function getOrderDetail($data)
- {
- if($data['order_sn']) $params['origin_id'] = $data['order_sn'];
- if($data['order_code']) $params['order_code'] = $data['order_code'];
- return $this->sendRequest(self::GET_ORDER_DETAIL, $params);
- }
- /**
- * 取消订单
- * @param $data
- * @return mixed
- */
- public function cancelOrder($data)
- {
- $params = [
- 'origin_id' => $data['origin_id'],
- 'order_code'=> $data['order_code'],
- 'reason' => $data['reason'],
- ];
- return $this->sendRequest(self::CANCEL_ORDER, $params);
- }
- /**
- * 获取充值地址
- * @param $data
- * @return mixed
- */
- public function getRecharge($data)
- {
- return $this->sendRequest(self::GET_RECHARGE, $data);
- }
- /**
- * 获取余额
- * @param $data
- * @return mixed
- */
- public function getBalance($data)
- {
- $res = $this->sendRequest(self::GET_BALANCEDE, $data);
- return [
- 'deliverBalance' => $res['AccountMoney']
- ];
- }
- /**
- * 支付小费
- * @param $data
- * @return mixed
- */
- public function addTip($data)
- {
- return true;
- }
- /**
- * 获取城市信息
- * @param $data
- * @return mixed
- */
- public function getCity($data = [])
- {
- $res = $this->sendRequest(self::GET_CITY, $data);
- foreach ($res['CityList'] as $item) {
- $data[] = [
- 'key' => $item['CityName'],
- 'label' => $item['CityName'],
- ];
- }
- return $data;
- }
- /**
- * 发送请求
- * @param $api
- * @param $params
- * @return mixed
- */
- public function sendRequest($api, $params = [])
- {
- $url = self::BASE_URL . $api;
- $params = $this->bulidRequestParams($params);
- $response = $this->httpRequestWithPost($url, $params);
- $data = $this->getMessage($response);
- return $data;
- }
- /**
- * 构造请求数据
- * @param array $params
- * @return mixed
- */
- public function bulidRequestParams(array $params = [])
- {
- $params['openid'] = $this->config['open_id'];
- $params['appid'] = $this->config['app_id'];
- $params['nonce_str'] = str_replace('-', '', $this->guid());
- $params['timestamp'] = time();
- $params['sign'] = $this->_sign($params);
- $arr = [];
- foreach ($params as $key => $value) {
- $arr[] = $key . '=' . $value;
- }
- $curlPost = implode('&', $arr);
- return $curlPost;
- }
- /**
- * 生成guid
- * @param $data
- * @return mixed
- */
- public function guid()
- {
- mt_srand((float)microtime() * 10000); //optional for php 4.2.0 and up.
- $charid = strtoupper(md5(uniqid(rand(), true)));
- $hyphen = chr(45); // "-"
- $uuid = substr($charid, 0, 8) . $hyphen
- . substr($charid, 8, 4) . $hyphen
- . substr($charid, 12, 4) . $hyphen
- . substr($charid, 16, 4) . $hyphen
- . substr($charid, 20, 12);
- return strtolower(str_replace('-', '', $uuid));
- }
- /**
- * 签名生成sign
- * @param $data
- * @return mixed
- */
- public function _sign($data)
- {
- ksort($data);
- $str = '';
- foreach ($data as $key => $value) {
- if (!is_null($value)) {
- $str .= $key . '=' . $value . '&';
- }
- }
- $str .= 'key=' . $this->config['app_key'];
- $str = mb_strtoupper($str, 'UTF-8');
- // halt($data,$str);
- return strtoupper(md5($str));
- }
- /**
- * 发送请求,POST
- * @param $url 指定URL完整路径地址
- * @param $data 请求的数据
- */
- public function httpRequestWithPost($url, $data)
- {
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_TIMEOUT, 3);
- $resp = curl_exec($curl);
- $info = curl_getinfo($curl);
- curl_close($curl);
- return $resp;
- }
- /**
- * 获取错误信息
- * @param $json
- * @param $message
- * @return mixed
- */
- protected function getMessage($json, $message = '未知错误!')
- {
- $data = json_decode($json, true);
- if (!in_array($data['return_code'], ['ok', 'fail'])) {
- isset($data['return_msg']) && $message = $data['return_msg'];
- $mes = $message == '未知错误!' ? $this->getCodeMap($data['return_code']) : $message;
- throw new ValidateException('【UU错误提示】:'.$mes);
- } else {
- return $data;
- }
- }
- /**
- * 获取错误代码
- * @param string $key 代码
- * @return String 错误代码与信息
- */
- protected function getCodeMap($key)
- {
- $codeMap = [
- '-101' => '参数格式校验错误',
- '-102' => 'timestamp错误',
- '-103' => 'appid无效',
- '-104' => '签名校验失败',
- '-105' => 'openid无效',
- '-199' => '参数格式校验错误',
- '-1001' => '无法解析起始地',
- '-1002' => '无法解析目的地',
- '-1003' => '无法获取订单城市相关信息',
- '-1004' => '订单小类出现错误',
- '-1005' => '没有用户信息',
- '-1006' => '优惠券ID错误',
- '-2001' => 'price_token无效',
- '-2002' => 'price_token无效',
- '-2003' => '收货人电话格式错误',
- '-2004' => 'special_type错误',
- '-2005' => 'callme_withtake错误',
- '-2006' => 'order_price错误',
- '-2007' => 'balance_paymoney错误',
- '-2008' => '订单总金额错误',
- '-2009' => '支付金额错误',
- '-2010' => '用户不一致',
- '-2011' => '手机号错误',
- '-2012' => '不存在绑定关系',
- '-4001' => '取消原因不能为空',
- '-4002' => '订单编号无效',
- '-5001' => '订单编号无效',
- '-5002' => '订单编号无效',
- '-5003' => '订单编号无效',
- '-10001' => '发送频率过快,请稍候重试',
- '-11001' => '请输入正确的验证码',
- ];
- $info = isset($codeMap[$key]) ? $codeMap[$key] : false;
- return $info;
- }
- /**
- * 创建商户
- * @param $data
- * @return mixed
- */
- public function addMerchant($data)
- {
- return true;
- }
- /**
- * 获取门店信息
- * @param $id
- * @return array
- */
- public function getShopDetail($id)
- {
- return [];
- }
- /**
- * 创建门店
- * @param $data
- * @return mixed
- */
- public function addShop($data)
- {
- return true;
- }
- /**
- * 更新门店
- * @param $data
- * @return mixed
- */
- public function updateShop($data)
- {
- return true;
- }
- /**
- *
- * @return mixed
- */
- public function getBusiness()
- {
- return [
- ['key' => 1, 'label' => '美食'],
- ['key' => 2, 'label' => '鲜花'],
- ['key' => 3, 'label' => '蛋糕'],
- ['key' => 4, 'label' => '手机'],
- ['key' => 5, 'label' => '钥匙'],
- ['key' => 6, 'label' => '文件'],
- ['key' => 0, 'label' => '其他'],
- ];
- }
- }
|