- 有时候调试需要结合关闭防火墙
- 查看服务器端口是否开启;
- 接口walkman 开发命令行手册文档;
<?php
use WorkermanWorker;
require_once './Workerman/Autoloader.php';
$ws_worker = new Worker("websocket://0.0.0.0:39999");
$ws_worker->count = 36;
error_reporting(E_ERROR | E_WARNING);
date_default_timezone_set("Asia/Shanghai");
$ws_worker->onMessage = function($connection, $data)
{
$data = json_decode($data, true);
//setlog($data,[],'','do_walk.log');
/*链接数据库*/
$host = '127.0.0.1';
$username = '******';
$password = '*******';
$dataname = '******';
// 各类型订单的数量;
// 待接单,进行中,已完成,已取消;
$type_res = [
['type' => 1,'type_str' => '待接单','count' => 0],
['type' => 2,'type_str' => '进行中','count' => 0],
['type' => 3,'type_str' => '已完成','count' => 0],
['type' => 4,'type_str' => '已取消','count' => 0],
];
$return_arr = [
'type_res' => $type_res,
'order_1' => [] // 待接单订单;
];
if(!isset($data['uid']) || !isset($data['token']) || !isset($data['pnLat']) || !isset($data['pnLng'])){
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
//$connection->send($json);
$connection->send('缺少参数');
} else {
$uid = $data['uid'];
$token = $data['token'];
$pnLat = $data['pnLat'];
$pnLng = $data['pnLng'];
if(!$uid || !$pnLng){
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
$connection->send($json);
}
// 缺少经纬度
if(!$pnLat || !$token){
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
$connection->send($json);
}
$conn = new mysqli($host,$username,$password,$dataname);
mysqli_set_charset($conn, "utf8");
if (!$conn->connect_error){
} else {
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
$connection->send($json);
//die($conn->connect_error);
}
$u_sql = "select dispatch_id,dispatch_name,work_status,city_id,dispatch_level_id,city_id,place_up_time,todaytime,onlinetime from db_dispatch where `dispatch_id` = $uid and `token` = '" . $token . "'";
$uinfo_navtive = $conn->query($u_sql);
if($uinfo_navtive){
$dispatch_info = $uinfo_navtive->fetch_array();
// echo $dispatch_info['dispatch_id'];
// print_r($dispatch_info);
// exit;
$city_name = city_name_bypos($pnLat,$pnLng);
if($city_name){
$location_sql = "select id from `db_location` where `city` = '" . $city_name . "'";
$location_native = $conn->query($location_sql);
if($location_native->num_rows > 0){
$location = $location_native->fetch_array();
$city_id = $location['id'];
} else {
$city_id = 0;
}
} else {
$city_id = 0;
}
//判断是位置信息是否是今天的时间
if (substr($dispatch_info['place_up_time'], 0, 10) == date('Y-m-d')) {
// 如果是听单中
// if ($rets['work_status'] == 1) {
//计算早上9点到下午8点的算法, 9:00--20:00 才是有效工作时间
if (strtotime(date('Y-m-d H:i:s')) >= strtotime(date('Y-m-d 08:00:00')) && strtotime(date('Y-m-d H:i:s')) <= strtotime(date('Y-m-d 20:00:00'))) {
if ((time() - strtotime($dispatch_info['place_up_time'])) >= 57) {//-- todaytime,onlinetime单位都是分钟
$dispatch_info['todaytime'] += 1; //如果是则更新今日在线时长+1
$dispatch_info['onlinetime'] += 1; //总在线时长加一
$dispatch_info['timeis'] = 1;
}
}
// }
} else {
$dispatch_info['onlinetime'] = 1;
$dispatch_info['todaytime'] = 1; //如果不是则今日在线时长清空
$dispatch_info['timeis'] = 1;
}
$todaytime = $dispatch_info['todaytime']; //存储今日在线时长
$onlinetime = $dispatch_info['onlinetime']; //存储总在线时长
$timess = (int)$dispatch_info['timeis'];
$ret = 1;
//更新位置信息
if ($timess == 1) {
$place_up_time = date('Y-m-d H:i:s');
if($city_id){
$save_sql = "update `db_dispatch` set `city_id` = '{$city_id}',`place_up_time` = '{$place_up_time}',`todaytime` = '{$todaytime}',`onlinetime` = '{$onlinetime}',`latitude` = '{$pnLat}' , `longitude` = '{$pnLng}' where `dispatch_id` = '{$uid}' ";
} else {
$save_sql = "update `db_dispatch` set `place_up_time` = '{$place_up_time}',`todaytime` = '{$todaytime}',`onlinetime` = '{$onlinetime}',`latitude` = '{$pnLat}' , `longitude` = '{$pnLng}' where `dispatch_id` = '{$uid}' ";
}
$save_flag = $conn->query($save_sql);
} else {
$place_up_time = date('Y-m-d H:i:s');
if($city_id){
$save_sql = "update `db_dispatch` set `city_id` = '{$city_id}',`place_up_time` = '{$place_up_time}',`latitude` = '{$pnLat}' , `longitude` = '{$pnLng}' where `dispatch_id` = '{$uid}' ";
} else {
$save_sql = "update `db_dispatch` set `place_up_time` = '{$place_up_time}',`latitude` = '{$pnLat}' , `longitude` = '{$pnLng}' where `dispatch_id` = '{$uid}' ";
}
$save_flag = $conn->query($save_sql);
}
$rate_sql = "select serv_charge_rate from `db_dispatch_level` where `dispatch_level_id` = " . $dispatch_info['dispatch_level_id'];
$rate_native = $conn->query($rate_sql);
//setlog($rate_native,[$rate_sql],'','ssss.log');
if($rate_native->num_rows){
$rate = $rate_native->fetch_array();
setlog($rate,[],'','rate.log');
$arr = array();
foreach($type_res as &$v)
{
$count = 0;
switch($v['type']){
case 1:
$field_str = 'order_id,city_id,order_time,pay_time,order_type,order_amount,goods_amount,is_have_price,is_reservation,reservation_time,total_mileage,send_address,reci_address,send_latit,send_longit,reci_latit,reci_longit,res_type,remark';
$order_sql = "select " . $field_str . " from db_order where `order_time` >= '" . date('Y-m-d 00:00:00',time() - 24 * 3600 * 3) . "' and `city_id` = " . $dispatch_info['city_id'] . " and `status` = 1 and `is_del` = 0 and `dispatch_id` = 0 order by order_id desc limit 2000 ";
$ret_native = $conn->query($order_sql);
setlog([$order_sql],[$ret_native],'','ttt.log');
if($ret_native->num_rows > 0){
$ret_native_2 = $ret_native->fetch_all();
$field_arr = explode(',',$field_str);
$ret = [];
for($i = 0;$i < count($ret_native_2);$i++){
for($j = 0;$j < count($ret_native_2[$i]);$j++){
$ret[$i][$field_arr[$j]] = $ret_native_2[$i][$j];
}
}
// $ret = $ret_native->fetch_array($ret_native);
// dump($ret);
// exit;
$length = count($ret);
$start = $pnLat . "," . $pnLng;
if ($length > 0) {
for ($i = 0; $i < $length; $i++) {
// 如果是预约单,提前一个小时才可以放出来让接的... zb????
if($ret[$i]['reservation_time']){
if(time() + 3600 >= strtotime($ret[$i]['reservation_time'])){
$end = $ret[$i]['send_latit'] . "," . $ret[$i]['send_longit'];
$rets = line($start, $end);
// 计算,发货地址距离跑客的距离,判断是否在接单范围内.....zb
$distance = (int)$rets['result'][0]['distance']['text'];// 单位米
if ($distance <= 10000) { // origin data 3000 zb
$ret[$i]['pick_distance'] = (int)$distance; // 跑客去接单展示的取货距离;
$ret[$i]['expected_money'] = getOrderAccount($ret[$i],$dispatch_info,$rate['serv_charge_rate']); // 预计佣金 add zb
$ret[$i]['expected_money'] = round($ret[$i]['expected_money'],2);
// 加上预约订单开放时间,什么时间开放的;zb
$ret[$i]['reser_opentime'] = strtotime($ret[$i]['reservation_time']) - 3600;
$arr[] = $ret[$i];
}
}
} else {
$end = $ret[$i]['send_latit'] . "," . $ret[$i]['send_longit'];
$rets = line($start, $end);
// 计算,发货地址距离跑客的距离,判断是否在接单范围内.....zb
$distance = (int)$rets['result'][0]['distance']['text'];// 单位米
if ($distance <= 10000) { // origin data 3000 zb
$ret[$i]['pick_distance'] = (int)$distance; // 跑客去接单展示的取货距离;
$ret[$i]['expected_money'] = getOrderAccount($ret[$i],$dispatch_info,$rate['serv_charge_rate']); // 预计佣金 add zb
$ret[$i]['expected_money'] = round($ret[$i]['expected_money'],2);
$arr[] = $ret[$i];
}
}
}
}
$count = count($arr);
} else {
$count = 0;
}
break;
case 2:
$sql = "select order_id from db_order where `dispatch_id` = '{$uid}' and `city_id` = '" . $dispatch_info['city_id'] . "' and status in (2,3,4,5,6)";
$ret_native = $conn->query($sql);
if($ret_native->num_rows > 0){
$count = $ret_native->num_rows;
}
break;
case 3:
$sql = "select order_id from db_order where `dispatch_id` = '{$uid}' and `city_id` = '" . $dispatch_info['city_id'] . "' and status = 7";
$ret_native = $conn->query($sql);
if($ret_native->num_rows > 0){
$count = $ret_native->num_rows;
}
break;
case 4:
$sql = "select order_id from db_order where `dispatch_id` = '{$uid}' and `city_id` = '" . $dispatch_info['city_id'] . "' and status = 9";
$ret_native = $conn->query($sql);
if($ret_native->num_rows > 0){
$count = $ret_native->num_rows;
}
break;
default:
break;
}
$v['count'] = (int)$count;
}
unset($v);
$return_arr = [
'type_res' => $type_res,
'order_1' => $arr ?: [] // 待接单订单;
];
setlog($return_arr,$data,'','out.log');
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
$connection->send($json);
} else {
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
$connection->send($json);
}
} else {
$json = json_encode($return_arr,JSON_UNESCAPED_SLASHES);
$connection->send($json);
}
}
};
/***
*
* 根据经纬度,获取城市名称;
*/
function city_name_bypos($lon,$lat){
$str = $lat . ',' . $lon;
$url = "http://api.map.baidu.com/geocoder?location=".$str."&output=xml&key=*******************";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$data = FromXml($output);
if(isset($data['result']['addressComponent']['city']))
{
$city = $data['result']['addressComponent']['city'];
// 宿迁市
if($city){
return $city;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
function FromXml($xml)
{
if(!$xml){
//throw new WxPayException("xml数据异常!");
}
//将XML转为array
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
/***
*
*/
function dump($arr){
echo "<pre>";
print_r($arr);
echo "</pre>";
}
// 全局通用日志工具
function setlog($param = [],$result = [],$name='',$filename = 'shanpao.log',$path = '/tmp/log/'){
// 目录,权限,递归
$arr = array(
'param' => $param,
'result' => $result,
'name' => $name,
'time' => date('Y-m-d H:i:s')
);
file_put_contents($path.$filename,var_export($arr,true).PHP_EOL,FILE_APPEND);
}
/**
* 获取3种订单的跑腿费 = 订单金额*(1-佣金比值/100)
* @param $order_id 订单id
* @param $dispatch_id 跑客id
* @return float '金额'
*
* is_have_price [帮我买]是否知道价格0默认值 1 知道价格 2不知道价格
*
*/
function getOrderAccount($order_info,$dispatch_info,$serv_charge_rate)
{
if ($order_info) {
if ($dispatch_info) {
$real = $order_info['order_amount'];
if($order_info['order_type'] == 2 && $order_info['is_have_price'] == 1){
$real = $order_info['order_amount'] - $order_info['goods_amount'];
}
$balance_amount = round($real * (1 - $serv_charge_rate / 100),2);
$balance_amount = $balance_amount - 0.3;//---- 扣除0.03的信息费; add zb
return $balance_amount;
} else {
return 0;
}
} else {
return 0;
}
}
function line($start, $end)
{
$starts = explode(',', $start);
$ends = explode(',', $end);
$startlng = $starts[1];
$startlat = $starts[0];
$endlng = $ends[1];
$endlat = $ends[0];
//百度地图坐标转换为百度魔卡托坐标
$ak = '*****************';// 新申请的
$bdmc = 'http://api.map.baidu.com/geoconv/v1/?coords=' . $startlng . ',' . $startlat . ';' . $endlng . ',' . $endlat . '&ak=' . $ak . '&from=3&to=6';
//dump($bdmc);exit;
$ch = curl_init($bdmc);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cexeCute = curl_exec($ch);
curl_close($ch);
$result = json_decode($cexeCute, true);
//dump($result);exit;
$requestUrl = "http://api.map.baidu.com/routematrix/v2/riding?origins=" . $result['result'][0]['y'] . "," . $result['result'][0]['x'] . "&destinations=" . $result['result'][1]['y'] . "," . $result['result'][1]['x'] . "&ak=".$ak."&output=json&coord_type=bd09mc&tactics=12";
//dump($requestUrl);
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cexeCute = curl_exec($ch);
curl_close($ch);
$results = json_decode($cexeCute, true);
// 百度接口,小于1公里,返回米,大于1公里 返回 公里
if(isset($results['result'][0]['distance']['text'])){
// value 里面值单位是m
$results['result'][0]['distance']['text'] = (int)$results['result'][0]['distance']['value'];
// if(strpos($results['result'][0]['distance']['text'],'公里')){
// $results['result'][0]['distance']['text'] = (int)$results['result'][0]['distance']['text'] * 1000;
// }
}
//setlog($results,[],'','line.log');
return $results;
}
Worker::runAll();