uberEate数据重组, 接上篇:优步外卖(Ubereat)接口请求封装-全球uberapi
public function dataChange($data, $store_id = 0){
$new_data = [
'in' => [],
'out' => []
];
switch ($this->uri){
case 'getStoreStatus':
$status = 0;
if($data['data']['status'] == 'ONLINE'){
$status = 1;
}elseif ($data['data']['status'] == 'OFFLINE' || $data['data']['status'] == 'PAUSED'){
$status = 0;
}
$new_data['in'][$store_id] = [
'id' => $store_id,
'is_in_business' => $status
];
break;
case 'setStoreStatus':
$status = 'PAUSED';
if($data['status'] == 1){
$status = 'ONLINE';
}
$new_data['out'][$store_id] = [
'status' => $status
];
$return_status = 0;
if($data['data']['status'] == 'ONLINE'){
$return_status = 1;
}
$new_data['in'][$store_id] = [
'id' => $store_id,
'is_in_business' => $return_status
];
break;
case 'getHolidayHours':
foreach ($data['data']['holiday_hours'] as $key=>$val){
foreach ($val['open_time_periods'] as $key1=>$hour){
$val['open_time_periods'][$key1] = [
's' => $hour['start_time'],
'e' => $hour['end_time'],
];
}
$new_data['in'][$store_id] = [
'week' => $this->getTimeWeek($key),
'business_hours' => $val['open_time_periods']
];
}
break;
case 'setHolidayHours':
foreach ($data['business_hours'] as $hour){
$new_data['out']['holiday_hours'][$data['date']]['open_time_periods'][] = [
'start_time' => $hour['s'],
'end_time' => $hour['e'],
];
}
break;
case 'setItem':
if(isset($data['price'])){
$new_data['out']['price_info']['price'] = (int)$data['price'] * 100;
}
if(isset($data['time'])){
$new_data['out']['suspension_info'] = [
'suspension' => [
'suspend_until' => $data['time'],
'reason' => isset($data['reason']) ? $data['reason'] : '暂停售卖'
]
];
}
if(isset($data['menu_type'])){
if($data['menu_type'] == 1){
$new_data['out']['menu_type'] = 'MENU_TYPE_FULFILLMENT_DELIVERY';
}elseif ($data['menu_type'] == 2){
$new_data['out']['menu_type'] = 'MENU_TYPE_FULFILLMENT_PICK_UP';
}else{
$new_data['out']['menu_type'] = '';
}
}
break;
case 'putMenus':
$new_data['out'] = $this->dealData($store_id);
break;
case 'getMenus':
$new_data['in'] = $this->dataReBuild($data);
break;
default:break;
}
return $new_data;
}
public function getTimeWeek($date) {
$time = strtotime($date);
$week_array_name = ['日','一','二','三','四','五','六'];
$week = date('w', $time);
if($week == 0){
$week = 7;
}
return ['name' => '星期'.$week_array_name[$week], 'num' => $week];
}
public function dealData($store_id = 0){
$init_data = [
'menus' => [
[
'id' => 'All-day',
'title' => [
'translations' => [
'en_us' => 'All day',
]
],
'service_availability' => [
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'monday'
],
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'tuesday'
],
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'wednesday'
],
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'thursday'
],
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'friday'
],
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'saturday'
],
[
'time_periods' => [
[
'start_time' => '00:00',
'end_time' => '23:59',
]
],
'day_of_week' => 'sunday'
],
]
],
],
'categories' => [],
'items' => [],
'modifier_groups' => [],
'display_options' => [
'disable_item_instructions' => true
],
];
if(!$store_id){
return false;
}
$store = pdo_get('ims_tiny_wmall_store', array('id' => $store_id));
if(!$store){
return false;
}
$categorys = pdo_getall('tiny_wmall_goods_category', array('sid' => $store_id, 'isdeleted' => 0));
if(count($categorys) < 1){
return false;
}
$category_id_name = 'Category-';
$prefix_group = 'GROUP-';
foreach ($categorys as $key=>$category){
$init_data['menus'][0]['category_ids'][] = $category_id_name.$category['id'];
}
$goods = pdo_getall('ims_tiny_wmall_goods', array('sid' => $store_id));
$new_goods = [];
$entities = [];
$group_name = 'group-item-';
foreach ($goods as $key=>$good){
$new_goods[$good['id']] = $good;
foreach ($categorys as $item){
if($item['id'] == $good['cid']){
$entities[$category_id_name.$item['id']][] = [
'id' => 'Item-'.$good['id'],
'type' => 'ITEM',
];
}
}
$overrides = [];
$group_ids = [];
$num = 0;
$attrs = iunserializer($good['attrs']);
foreach ($attrs as $key1=>$attr){
$group_name .= $key.'-';
$overrides[$good['id']][] = [
'context_type' => 'ITEM',
'context_value' => $prefix_group.($key1 + 1 + $key)
];
$group_ids[$good['id']][] = $prefix_group.($key1 + 1 + $key);
foreach ($attr['label'] as $item){
++$num;
$init_data['items'][] = [
'id' => $group_name.$num,
'title' => [
'translations' => [
"zh_tw" => $item
]
],
'price_info' => [
'price' => 0,
]
];
$modifier_options[$key1 + 1 + $key][] = [
'id' => $group_name.$num,
'type' => 'ITEM'
];
}
if(!empty($modifier_options)){
$group = [
'id' => $prefix_group.($key1 + 1 + $key),
'title' => [
'translations' => [
"zh_tw" => $attrs[$key1]['name']
]
],
'modifier_options' => $modifier_options[$key1 + 1 + $key]
];
array_push($init_data['modifier_groups'], $group);
}
if($good['type'] == 1){
$init_data['menu_type'] = 'MENU_TYPE_FULFILLMENT_DELIVERY';
}elseif ($good['type'] == 2){
$init_data['menu_type'] = 'MENU_TYPE_FULFILLMENT_PICK_UP';
}
}
$init_data['items'][] = [
'id' => 'Item-'.$good['id'],
'external_data' => $good['data'],
'title' => [
'translations' => [
"zh_tw" => $good['title']
]
],
'description' => [
'translations' => [
"zh_tw" => $good['content']
]
],
'image_url' => tomedia($good['thumb']),
'price_info' => [
'price' => (int)$good['price'] * 100,
],
'quantity_info' => [
'quantity' => [
'max_permitted' => (int)$good['unitnum'],
'default_quantity' => 1
],
'overrides' => $good['id'] ? $overrides[$good['id']] : [],
],
'modifier_group_ids' => [
'ids' => $good['id'] ? $group_ids[$good['id']] : []
],
];
}
foreach ($init_data['menus'][0]['category_ids'] as $key=>$category_id){
if(!empty($entities[$category_id])){
$init_data['categories'][] = [
'id' => $category_id,
'title' => [
'translations' => [
"zh_tw" => $categorys[$key]['title']
]
],
'entities' => $entities[$category_id]
];
}else{
$init_data['categories'][] = [
'id' => $category_id,
'title' => [
'translations' => [
"zh_tw" => $categorys[$key]['title']
]
]
];
}
}
return $init_data;
}
public function setOrderData($data){
}
public function dataReBuild($res){
$categories = $res['data']['categories'] ?? [];
$items = $res['data']['items'] ?? [];
$modifier_groups = $res['data']['modifier_groups'] ?? [];
$product_group = [];
$attrs = [];
$new_categories = [];
$new_items = [];
foreach ($categories as $category){
$categories_id_arr = explode('-', $category['id']);
$new_categories[$categories_id_arr[1]] = [
'id' => $categories_id_arr[1],
'title' => $category['title']['translations']['zh_tw']
];
foreach ($category['entities'] as $entity){
foreach ($items as $key=> $item){
if($item['id'] == $entity['id']){
if($res['data']['menu_type'] == 'MENU_TYPE_FULFILLMENT_DELIVERY'){
$type = 1;
}elseif ($res['data']['menu_type'] == 'MENU_TYPE_FULFILLMENT_PICK_UP'){
$type = 2;
}else{
$type = 3;
}
$entity_id_arr = explode('-', $entity['id']);
$new_items[$entity_id_arr[1]] = [
'id' => $entity_id_arr[1],
'data' => $item['external_data'],
'title' => $item['title']['translations']['zh_tw'],
'thumb' => tomedia($item['image_url']),
'content' => $item['description']['translations']['zh_tw'],
'unitnum' => $item['quantity_info']['quantity']['max_permitted'],
'price' => $item['price_info']['price'] / 100,
'type' => $type
];
if(!empty($item['modifier_group_ids']['ids'])){
$product_group[$entity_id_arr[1]] = $item['modifier_group_ids']['ids'];
}
}
}
}
}
foreach ($product_group as $key1=>$groups){
foreach ($modifier_groups as $key2=>$modifier_group){
foreach ($groups as $group){
if($modifier_group['id'] == $group){
$attrs[$key1][$key2] = [
'name' => $modifier_group['title']['translations']['zh_tw'],
'label' => []
];
foreach ($modifier_group['modifier_options'] as $modifier_option){
foreach ($items as $item){
if($modifier_option['id'] == $item['id']){
array_push($attrs[$key1][$key2]['label'], $item['title']['translations']['zh_tw']);
}
}
}
}
}
}
}
foreach ($new_items as &$item){
$item['attrs'] = $attrs[$item['id']] ? serialize($attrs[$item['id']]) : serialize([]);
}
return ['items' => $new_items, 'categories' => $new_categories];
}
uber接口文档:https://developer.uber.com/docs/eats/api/v1/post-eats-store-storeid-status
菜鸟代码请绕到此地:https://download.csdn.net/download/weixin_31501115/20005098?spm=1001.2014.3001.5501
|