Authored by hf

do add union codes

  1 +<?php
  2 +
  3 +/**
  4 + * 联盟数据转换
  5 + */
  6 +
  7 +namespace Plugin;
  8 +
  9 +use Hood\Core\Security\AuthCode;
  10 +use Api\Yohobuy;
  11 +use LibModels\Wap\Home\OrderData;
  12 +
  13 +class UnionTrans
  14 +{
  15 +
  16 + /**
  17 + * 第三方联盟名称
  18 + *
  19 + * @var array
  20 + */
  21 + private static $unionNames = array(
  22 + 1001 => '亿起发',
  23 + 1010 => '领科特',
  24 + 1009 => '成果网',
  25 + 2995 => '亿起发wap',
  26 + 2997 => '领科特wap',
  27 + 3001 => '51fanli',
  28 + 3017 => '多麦web',
  29 + 3019 => '多麦wap',
  30 + 3057 => '多麦优质',
  31 + );
  32 +
  33 + /**
  34 + * 过滤人民币
  35 + *
  36 + * @param string $price
  37 + * @return array
  38 + */
  39 + private static function filterPrice($price)
  40 + {
  41 + return strtr($price, array('-' => '', '+' => '', '¥' => ''));
  42 + }
  43 +
  44 + /**
  45 + * 添加成交记录
  46 + *
  47 + * @param String $order_code
  48 + * @param Integer $pay_time
  49 + * @param Integer $ip
  50 + * @return bool
  51 + */
  52 + public static function set($uid, $order_code, $order_amount)
  53 + {
  54 + do {
  55 + /* 判断是否是友盟过来的用户 */
  56 + if (empty($_COOKIE['_QYH_UNION'])) {
  57 + break;
  58 + }
  59 +
  60 + /* 判断参数是否有效 */
  61 + if (empty($uid) || empty($order_code) || empty($order_amount)) {
  62 + break;
  63 + }
  64 +
  65 + /* 解密客户端联盟信息 */
  66 + $unionKey = AuthCode::decode($_COOKIE['_QYH_UNION'], 'q_union_yohobuy');
  67 + if (empty($unionKey)) {
  68 + break;
  69 + }
  70 +
  71 + /* 检查联盟参数是否有效 */
  72 + $unionInfo = json_decode($unionKey, true);
  73 + if (empty($unionInfo['client_id'])) {
  74 + break;
  75 + }
  76 +
  77 + /* 通过用户下单量判断新老客户 (有下单1, 没有0) */
  78 + $is_old_user = 0;
  79 + try {
  80 + $orderList = OrderData::getOrderData(1, 1, 10, '1,2,3', 1, $uid);
  81 + if (!empty($orderList['data']['total']) && intval($orderList['data']['total']) > 1) {
  82 + $is_old_user = 1;
  83 + }
  84 + } catch (Exception $e) {
  85 + // do nothing
  86 + }
  87 +
  88 + $clientId = intval($unionInfo['client_id']);
  89 + $append = isset($unionInfo['append']) ? $unionInfo['append'] : '';
  90 + $channel_code = isset($unionInfo['channel_code']) ? $unionInfo['channel_code'] : '';
  91 + $mbr_name = isset($unionInfo['mbr_name']) ? $unionInfo['mbr_name'] : '';
  92 + $u_id = isset($unionInfo['u_id']) ? $unionInfo['u_id'] : '';
  93 + $create_times = time();
  94 +
  95 + /* 加入联盟订单库 */
  96 + $keyStr = 'client_id=' . $unionInfo['client_id'] . '&order_code=' . $order_code . '&orders_price=' . $order_amount . '&create_time=' . $create_times . '&channel_code=' . $channel_code . '&append=' . $append . '&mbr_name=' . $mbr_name . '&u_id=' . $u_id;
  97 + Yohobuy::get('http://union.yohobuy.com/order', array(
  98 + 'client_id' => $unionInfo['client_id'],
  99 + 'order_code' => $order_code,
  100 + 'orders_price' => $order_amount,
  101 + 'buy_time' => $create_times,
  102 + 'channel_code' => $channel_code,
  103 + 'append' => $append,
  104 + 'mbr_name' => $mbr_name,
  105 + 'u_id' => $u_id,
  106 + 'is_old_user' => $is_old_user,
  107 + 'verify_code' => md5($keyStr)
  108 + ));
  109 +
  110 + /* 通知新ERP (老的ERP http://192.168.200.102/CreateOrderUnion.aspx) */
  111 + Yohobuy::post('http://portal.admin.yohobuy.com/api/orderunion/updateunion', array(
  112 + 'data' => json_encode(array(
  113 + 'order_code' => $order_code,
  114 + 'unionid' => $unionInfo['client_id'],
  115 + 'union_name' => isset(self::$unionNames[$clientId]) ? self::$unionNames[$clientId] : '',
  116 + ))
  117 + ));
  118 +
  119 + /* 对接第三方联盟 */
  120 +
  121 + // 亿起发
  122 + if ($unionInfo['client_id'] == 1001) {
  123 + $keyStr = 'client_id=' . $unionInfo['client_id'] . '&orders_id=' . $order_code . '&orders_price=' . $order_amount . '&create_time=' . $create_times . '&channel_code=' . $channel_code . '&append=' . $append . '&is_old_user=' . $is_old_user;
  124 + Yohobuy::get('http://o.yiqifa.com/adv/yoho.jsp', array(
  125 + 'client_id' => $unionInfo['client_id'],
  126 + 'orders_id' => $order_code,
  127 + 'orders_price' => $order_amount,
  128 + 'create_time' => $create_times,
  129 + 'channel_code' => $channel_code,
  130 + 'append' => $append,
  131 + 'is_old_user' => $is_old_user,
  132 + 'verify_code' => md5($keyStr)
  133 + ));
  134 + break;
  135 + }
  136 +
  137 + // 亿起发wap
  138 + if ($unionInfo['client_id'] == 2995) {
  139 + $keyStr = 'client_id=' . $unionInfo['client_id'] . '&orders_id=' . $order_code . '&orders_price=' . $order_amount . '&create_time=' . $create_times . '&channel_code=' . $channel_code . '&append=' . $append . '&is_old_user=' . $is_old_user;
  140 + Yohobuy::get('http://o.yiqifa.com/adv/yoho.jsp', array(
  141 + 'client_id' => $unionInfo['client_id'],
  142 + 'orders_id' => $order_code,
  143 + 'orders_price' => $order_amount,
  144 + 'create_time' => $create_times,
  145 + 'channel_code' => $channel_code,
  146 + 'append' => $append,
  147 + 'is_old_user' => $is_old_user,
  148 + 'dt' => 'm',
  149 + 'verify_code' => md5($keyStr)
  150 + ));
  151 + break;
  152 + }
  153 +
  154 + // 多麦
  155 + $clientUrls = array(
  156 + 3017 => 'http://www.duomai.com/api/push/yohobuy.php',
  157 + 3019 => 'http://www.duomai.com/api/push/myohobuy.php',
  158 + 3057 => 'http://www.duomai.com/api/push/yohobuyroi.php',
  159 + );
  160 + if (array_key_exists($clientId, $clientUrls)) {
  161 + $urldata = self::getMaiDuoSplitData($order_code, $unionInfo['client_id'], $mbr_name);
  162 + Yohobuy::post($clientUrls[$clientId], array(
  163 + 'content' => $urldata,
  164 + ));
  165 + break;
  166 + }
  167 +
  168 + // 51fanli
  169 + if ($unionInfo['client_id'] == 3001) {
  170 + $xmldata = self::getOrderXmlData($order_code, $uid, $unionInfo['client_id'], $u_id, $mbr_name, $channel_code);
  171 + $keyStr = 'content=' . $xmldata . '&shopid=690';
  172 + Yohobuy::post('http://union.fanli.com/dingdan/push?shopid=690', array(
  173 + 'content' => $xmldata,
  174 + 'shopid' => 690,
  175 + 'verify_code' => md5($keyStr)
  176 + ));
  177 + break;
  178 + }
  179 +
  180 + // 领科特wap 成果 51fanli
  181 + $clientUrls = array(
  182 + 1010 => 'http://service.linktech.cn/purchase_cps.php',
  183 + 1009 => 'http://count.chanet.com.cn/add_action_ec.cgi',
  184 + 2997 => 'http://service.linktech.cn/purchase_cps.php',
  185 + 3001 => 'http://union.fanli.com/dingdan/push?shopid=690',
  186 + );
  187 + if (array_key_exists($clientId, $clientUrls)) {
  188 + Yohobuy::post($clientUrls[$clientId], array(
  189 + 'client_id' => $unionInfo['client_id'],
  190 + 'order_id' => $order_code,
  191 + 'create_time' => $create_times,
  192 + 'channel_code' => $channel_code,
  193 + 'append' => $append,
  194 + 'mbr_name' => $mbr_name,
  195 + 'UID' => $u_id,
  196 + 'is_old_user' => $is_old_user,
  197 + ));
  198 + break;
  199 + }
  200 + } while (false);
  201 + }
  202 +
  203 + /**
  204 + * 根据用户id和订单号, 返回xml数据
  205 + */
  206 + public static function getOrderXmlData($order_id, $uid, $client_id, $u_id, $mbr_name, $channel_code, $client_type = 'web')
  207 + {
  208 + $xmlData = '';
  209 +
  210 + do {
  211 + $orderDetail = OrderData::viewOrderData($order_id, $uid, '');
  212 + if (empty($orderDetail['data'])) {
  213 + break;
  214 + }
  215 +
  216 + $orderDetail = $orderDetail['data'];
  217 + $amount = self::filterPrice($orderDetail['amount']) - self::filterPrice($orderDetail['shipping_cost']);
  218 + if (!empty($orderDetail['order_goods'])) {
  219 + break;
  220 + }
  221 +
  222 + $goodsAmount = $orderDetail['goods_total_amount'];
  223 + $goodsData = array();
  224 +
  225 + foreach ($orderDetail['order_goods'] as $key => $val) {
  226 + $goodsData['goods' . $key] = array(
  227 + 'pid' => $val['product_skn'],
  228 + 'title' => $val['product_name'],
  229 + 'category' => $val['color_name'],
  230 + 'category_title' => '',
  231 + 'url' => 'http://item.m.yohobuy.com/product/show_' . $val['product_skn'] . '.html',
  232 + 'num' => $val['buy_number'],
  233 + 'price' => $val['goods_price'],
  234 + 'real_pay_fee' => round($amount * ($val['goods_amount'] / $goodsAmount), 2),
  235 + 'refund_num' => '',
  236 + 'commission' => round($amount * ($val['goods_amount'] / $goodsAmount) * 0.05, 2),
  237 + 'comm_type' => 'A',
  238 + );
  239 + }
  240 +
  241 + $orderData[] = array(
  242 + 's_id' => 690,
  243 + 'order_id_parent' => $orderDetail['order_code'],
  244 + 'order_id' => $orderDetail['order_code'],
  245 + 'order_time' => date('Y-m-d H:i:s', $orderDetail['create_time']),
  246 + 'uid' => $u_id,
  247 + 'uname' => $mbr_name,
  248 + 'tc' => $channel_code,
  249 + 'pay_time' => '',
  250 + 'status' => $orderDetail['status'],
  251 + 'locked' => '',
  252 + 'lastmod' => '',
  253 + 'is_newbuyer' => '',
  254 + 'platform' => $client_type == 'h5' ? 2 : 1,
  255 + 'code' => '',
  256 + 'remark' => '',
  257 + 'products' => $goodsData,
  258 + );
  259 +
  260 + $xmlData = strtr(self::toXml($orderData), array('<![CDATA[", "]]>' => ''));
  261 +
  262 + } while (false);
  263 +
  264 + return $xmlData;
  265 + }
  266 +
  267 + /**
  268 + * 麦多联盟
  269 + */
  270 + public static function getMaiDuoSplitData($order_code, $uid, $client_id, $mbr_name, $is_old_user)
  271 + {
  272 + $result = '';
  273 +
  274 + do {
  275 + $orderDetail = OrderData::viewOrderData($order_code, $uid, '');
  276 + if (empty($orderDetail['data']['order_goods'])) {
  277 + break;
  278 + }
  279 + $orderDetail = $orderDetail['data'];
  280 +
  281 + if ($client_id == '3017') {
  282 + $hash = '96613bf38393aa3d16451218f22344a8';
  283 + $channel = 0;
  284 + } elseif ($client_id == '3019') {
  285 + $hash = 'd54be2dbc75753eb863ba6139950656b';
  286 + $channel = 1;
  287 + } elseif ($client_id == '3057') {
  288 + $hash = 'bbf70bcaf5c52947ad26853f7cc1176d';
  289 + $channel = 0;
  290 + } else {
  291 + break;
  292 + }
  293 +
  294 + $goodsName = '';
  295 + $goodsIds = '';
  296 + $goodsPrice = '';
  297 + $goodsTotalPrice = '';
  298 + $goodsNum = '';
  299 +
  300 + foreach ($orderDetail['order_goods'] as $val) {
  301 + $goodsName .= str_replace(array("&", "<", ">", "'", '"', " ", "|"), array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "%20", "%20"), $val['product_name']) . '|';
  302 + $goodsIds = $val['product_sku'] . '|';
  303 + $goodsPrice = $val['goods_price'] . '|';
  304 + $goodsTotalPrice = $val['goods_amount'] . '|';
  305 + $goodsNum = $val['buy_number'] . '|';
  306 + }
  307 +
  308 + $orderStatus = $orderDetail['status_str'];
  309 +
  310 + if ($orderDetail['is_cancel'] == 'Y') {
  311 + $orderStatus = -1;
  312 + } elseif ($orderDetail['payment_status'] == 'N' && $orderDetail['status'] == 0) {
  313 + $orderStatus = 0;
  314 + }
  315 +
  316 + $orderData = array(
  317 + 'hash' => $hash,
  318 + 'euid' => $mbr_name,
  319 + 'order_sn' => $order_code,
  320 + 'order_time' => date('Y-m-d H:i:s', $orderDetail['create_time']),
  321 + 'orders_price' => sprintf('%.2f', $orderDetail['goods_total_amount']),
  322 + 'promotion_code' => 0,
  323 + 'is_new_custom' => $is_old_user ? 0 : 1,
  324 + 'channel' => $channel,
  325 + 'status' => $orderStatus,
  326 + 'goods_id' => rtrim($goodsIds, '|'), //商品id,
  327 + 'goods_name' => rtrim($goodsName, '|'), //商品名称,
  328 + 'goods_price' => rtrim($goodsPrice, '|'), //商品单价,
  329 + 'goods_ta' => rtrim($goodsNum, '|'), //商品数量,
  330 + 'goods_cate' => '', //商品分类
  331 + 'goods_cate_name' => 0,
  332 + 'totalPrice' => rtrim($goodsTotalPrice, '|'), //商品总净金额,
  333 + 'rate' => 0,
  334 + 'commission' => '', //佣金,
  335 + 'commission_type' => 0,
  336 + 'coupon' => sprintf('%.2f', self::filterPrice($orderDetail['coupons_amount']))
  337 + );
  338 + $result = http_build_query($orderData);
  339 +
  340 + } while (false);
  341 +
  342 + return $result;
  343 + }
  344 +
  345 + /**
  346 + * @param array $data
  347 + * @param string $rootNodeName - what you want the root node to be - defaultsto data.
  348 + * @param SimpleXMLElement $xml - should only be used recursively
  349 + * @return string XML
  350 + */
  351 + public static function toXml($data, $rootNodeName = 'data', $xml = null)
  352 + {
  353 + // turn off compatibility mode as simple xml throws a wobbly if you don't.
  354 + if (ini_get('zend.ze1_compatibility_mode') == 1) {
  355 + ini_set('zend.ze1_compatibility_mode', 0);
  356 + }
  357 +
  358 + if ($xml == null) {
  359 + $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
  360 + }
  361 +
  362 + // loop through the data passed in.
  363 + foreach ($data as $key => $value) {
  364 + // no numeric keys in our xml please!
  365 + if (is_numeric($key)) {
  366 + // make string key...
  367 + $key = "unknownNode_" . (string) $key;
  368 + }
  369 +
  370 + // if there is another array found recrusively call this function
  371 + if (is_array($value)) {
  372 + $node = $xml->addChild($key);
  373 + // recrusive call.
  374 + self::toXml($value, $rootNodeName, $node);
  375 + } else {
  376 + // add single node.
  377 + $value = htmlentities($value);
  378 + $xml->addChild($key, $value);
  379 + }
  380 + }
  381 + // pass back as string. or simple xml object if you want!
  382 + return $xml->asXML();
  383 + }
  384 +
  385 +}
@@ -4,6 +4,7 @@ use Action\AbstractAction; @@ -4,6 +4,7 @@ use Action\AbstractAction;
4 use Index\CartModel; 4 use Index\CartModel;
5 use Index\UserModel; 5 use Index\UserModel;
6 use Plugin\Helpers; 6 use Plugin\Helpers;
  7 +use Plugin\UnionTrans;
7 8
8 /** 9 /**
9 * 购物车相关的控制器 10 * 购物车相关的控制器
@@ -429,6 +430,10 @@ class IndexController extends AbstractAction @@ -429,6 +430,10 @@ class IndexController extends AbstractAction
429 // 提交成功清除Cookie 430 // 提交成功清除Cookie
430 $this->setCookie('order-info', null); 431 $this->setCookie('order-info', null);
431 432
  433 + if ($uid) {
  434 + UnionTrans::set($uid, $order_code, $order_amount);
  435 + }
  436 +
432 $this->echoJson($result); 437 $this->echoJson($result);
433 } 438 }
434 } 439 }