| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- <?php
- //这个类专门用来处理各种模板消息
- class LewaimaiWeixinTemplate {
- public static function SendOrderConfirmedTemplate($orderid, $orderModel = null)
- {
- if (!$orderModel)
- {
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if (!$orderModel)
- {
- LewaimaiDebug::Log("no this order");
- return false;
- }
- }
-
- $admin_id = $orderModel->admin_id;
-
- $templateModel = Template::model()->find("admin_id='$admin_id'");
- if (!$templateModel)
- {
- LewaimaiDebug::Log("no template");
- return false;
- }
-
- if (!$templateModel->is_used)
- {
- return false;
- }
-
- if (!$templateModel->confirm_template_id)
- {
- return false;
- }
-
- $adminModel = Admin::model()->findByPk($admin_id);
- if (!$adminModel)
- {
- LewaimaiDebug::Log("adminModel is null");
- return false;
- }
-
- if ($adminModel->is_weixinauth)
- {
- $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
- }
- else
- {
- $access_token = LewaimaiUtility::GetAccessToken($admin_id);
- }
- if (!$access_token)
- {
- LewaimaiDebug::Log("get access_token failed!");
- return false;
- }
- $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
- if (!$openid)
- {
- return false;
- }
-
- $shopModel = Config::model()->findByPk($orderModel->shop_id);
- if (!$shopModel)
- {
- LewaimaiDebug::Log("shopModel is null");
- return false;
- }
- $shopName = $shopModel->shopname;
-
- if ($adminModel->order_num_type == 0)
- {
- $orderNo = $orderModel->admin_num;
- }
- else
- {
- $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
- }
- $money = $orderModel->total_price;
- $detail = '';
- $fooditemlist = LewaimaiOrderItem::model()->findAll("lewaimai_order_id = " . $orderid);
- foreach($fooditemlist as $fooditem){
- $detail .= $fooditem->food_name . ' ' . $fooditem->quantity . '份 ';
- }
- $array = array(
- 'touser' => $openid,
- 'template_id' => $templateModel->confirm_template_id,
- 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>"您的订单商家已确认"),
- 'keyword1' => array('value'=>$orderNo),
- 'keyword2' => array('value'=>$orderModel->init_date),
- 'keyword3' => array('value'=>$detail),
- 'keyword4' => array('value'=>'¥'.$money,"color"=>"#ff0000"),
- 'remark' => array('value'=>$templateModel->confirm_remark),
- ),
- );
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- $ret = LewaimaiHttp::POSTJSON($url, $json);
- }
-
- public static function SendOrderCourierTemplate($orderid, $orderModel = null)
- {
- if (!$orderModel)
- {
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if (!$orderModel)
- {
- LewaimaiDebug::Log("no this order");
- return false;
- }
- }
-
- $admin_id = $orderModel->admin_id;
-
- $templateModel = Template::model()->find("admin_id='$admin_id'");
- if (!$templateModel)
- {
- LewaimaiDebug::Log("no template");
- return false;
- }
-
- if (!$templateModel->is_used)
- {
- return false;
- }
-
- if (!$templateModel->courier_template_id)
- {
- return false;
- }
-
- $adminModel = Admin::model()->findByPk($admin_id);
- if (!$adminModel)
- {
- LewaimaiDebug::Log("adminModel is null");
- return false;
- }
-
- if ($adminModel->is_weixinauth)
- {
- $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
- }
- else
- {
- $access_token = LewaimaiUtility::GetAccessToken($admin_id);
- }
- if (!$access_token)
- {
- LewaimaiDebug::Log("get access_token failed!");
- return false;
- }
-
- $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
- if (!$openid)
- {
- return false;
- }
-
- $shopModel = Config::model()->findByPk($orderModel->shop_id);
- if (!$shopModel)
- {
- LewaimaiDebug::Log("shopModel is null");
- return false;
- }
- $shopName = $shopModel->shopname;
-
- if ($adminModel->order_num_type == 0)
- {
- $orderNo = $orderModel->admin_num;
- }
- else
- {
- $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
- }
-
- $money = $orderModel->total_price;
-
- $detail = '';
- $fooditemlist = LewaimaiOrderItem::model()->findAll("lewaimai_order_id = " . $orderid);
- foreach($fooditemlist as $fooditem){
- $detail .= $fooditem->food_name . ' ' . $fooditem->quantity . '份 ';
- }
-
- if($orderModel->charge_type == 1)
- {
- $charge_type = '货到付款';
- }
- elseif($orderModel->charge_type == 2)
- {
- $charge_type = '余额支付';
- }
- else
- {
- $charge_type = '在线支付';
- }
-
- $courier_id = $orderModel->courier_id;
- $courier = Courier::model()->findByPk($courier_id);
- if (!$courier)
- {
- return false;
- }
-
- $array = array(
- 'touser' => $openid,
- 'template_id' => $templateModel->courier_template_id,
- 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>"您的订单商家已开始配送"),
- 'keyword1' => array('value'=>$shopName),
- 'keyword2' => array('value'=>$orderNo),
- 'keyword3' => array('value'=>$detail),
- 'keyword4' => array('value'=>$charge_type),
- 'keyword5' => array('value'=>$courier->name.",".$courier->phone),
- 'remark' => array('value'=>$templateModel->courier_remark),
- ),
- );
-
- LewaimaiDebug::LogArray($array);
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- $ret = LewaimaiHttp::POSTJSON($url, $json);
- }
-
- public static function SendOrderSuccessTemplate($orderid, $orderModel = null)
- {
- if (!$orderModel)
- {
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if (!$orderModel)
- {
- LewaimaiDebug::Log("no this order");
- return false;
- }
- }
-
- $admin_id = $orderModel->admin_id;
-
- $templateModel = Template::model()->find("admin_id='$admin_id'");
- if (!$templateModel)
- {
- LewaimaiDebug::Log("no template");
- return false;
- }
-
- if (!$templateModel->is_used)
- {
- return false;
- }
-
- if (!$templateModel->success_template_id)
- {
- return false;
- }
-
- $adminModel = Admin::model()->findByPk($admin_id);
- if (!$adminModel)
- {
- LewaimaiDebug::Log("adminModel is null");
- return false;
- }
-
- if ($adminModel->is_weixinauth)
- {
- $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
- }
- else
- {
- $access_token = LewaimaiUtility::GetAccessToken($admin_id);
- }
- if (!$access_token)
- {
- LewaimaiDebug::Log("get access_token failed!");
- return false;
- }
-
- $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
- if (!$openid)
- {
- return false;
- }
- $shopModel = Config::model()->findByPk($orderModel->shop_id);
- if (!$shopModel)
- {
- LewaimaiDebug::Log("shopModel is null");
- return false;
- }
- $shopName = $shopModel->shopname;
-
- if ($adminModel->order_num_type == 0)
- {
- $orderNo = $orderModel->admin_num;
- }
- else
- {
- $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
- }
-
- $array = array(
- 'touser' => $openid,
- 'template_id' => $templateModel->success_template_id,
- 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>"您的订单已完成。"),
- 'keyword1' => array('value'=>$orderNo),
- 'keyword2' => array('value'=>$orderModel->complete_date),
- 'remark' => array('value'=>$templateModel->success_remark),
- ),
- );
-
- LewaimaiDebug::LogArray($array);
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- $ret = LewaimaiHttp::POSTJSON($url, $json);
- }
-
- public static function SendOrderCancelTemplate($orderid, $orderModel = null)
- {
- if (!$orderModel)
- {
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if (!$orderModel)
- {
- LewaimaiDebug::Log("no this order");
- return false;
- }
- }
-
- $admin_id = $orderModel->admin_id;
-
- $templateModel = Template::model()->find("admin_id='$admin_id'");
- if (!$templateModel)
- {
- LewaimaiDebug::Log("no template");
- return false;
- }
-
- if (!$templateModel->is_used)
- {
- return false;
- }
-
- if (!$templateModel->cancel_template_id)
- {
- return false;
- }
-
- $adminModel = Admin::model()->findByPk($admin_id);
- if (!$adminModel)
- {
- LewaimaiDebug::Log("adminModel is null");
- return false;
- }
-
- if ($adminModel->is_weixinauth)
- {
- $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
- }
- else
- {
- $access_token = LewaimaiUtility::GetAccessToken($admin_id);
- }
- if (!$access_token)
- {
- LewaimaiDebug::Log("get access_token failed!");
- return false;
- }
-
- $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
- if (!$openid)
- {
- return false;
- }
-
- $shopModel = Config::model()->findByPk($orderModel->shop_id);
- if (!$shopModel)
- {
- LewaimaiDebug::Log("shopModel is null");
- return false;
- }
- $shopName = $shopModel->shopname;
-
- if ($adminModel->order_num_type == 0)
- {
- $orderNo = $orderModel->admin_num;
- }
- else
- {
- $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
- }
- $array = array(
- 'touser' => $openid,
- 'template_id' => $templateModel->cancel_template_id,
- 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>"您的订单已取消。"),
- 'keyword1' => array('value'=>$orderNo),
- 'keyword2' => array('value'=>$orderModel->complete_date),
- 'remark' => array('value'=>$templateModel->cancel_remark),
- ),
- );
-
- LewaimaiDebug::LogArray($array);
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- $ret = LewaimaiHttp::POSTJSON($url, $json);
- }
-
- //在商家有新订单的时候,通过乐外卖的微信公众号给商家发模板消息提醒
- public static function SendNewOrderTemplate($orderid, $orderModel = NULL, $shopModel = NULL)
- {
- if (!$orderModel)
- {
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if (!$orderModel)
- {
- LewaimaiDebug::Log("no this order");
- return false;
- }
- }
-
- if (!$shopModel)
- {
- $shopModel = Config::model()->findByPk($orderModel->shop_id);
- if (!$shopModel)
- {
- LewaimaiDebug::Log("shopModel is null");
- return false;
- }
- }
-
- //接受微信订单提醒的商家的顾客ID
- $lewaimai_customer_id = $shopModel->lewaimai_customerid;
-
- $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
- if (!$lewaimaicustomerModel)
- {
- LewaimaiDebug::Log("no this lewaimaicustomer");
- return false;
- }
-
- $openid = $lewaimaicustomerModel->openid;
-
- $order_id = $orderModel->id;
- $admin_id = $orderModel->admin_id;
- $shop_id = $orderModel->shop_id;
- $init_date = $orderModel->init_date;
- $charge_type = $orderModel->charge_type;
- $lewaimai_customer_id = $orderModel->lewaimai_customer_id;
- $is_discount = $orderModel->is_discount;
-
- //为了防止猜到别人的订单,这里加个密
- $strsign = $init_date.$admin_id.$shop_id.$charge_type.$order_id.$lewaimai_customer_id.$is_discount;
- $md5 = md5($strsign);
-
- $shopname = $shopModel->shopname;
-
- $detail = '';
- $fooditemlist = LewaimaiOrderItem::model()->findAll("lewaimai_order_id = " . $orderid);
- foreach($fooditemlist as $fooditem){
- $detail .= $fooditem->food_name . ' ' . $fooditem->quantity . '份 ';
- }
-
- $arr_charge_type = array('1'=>'货到付款', '2'=>'余额支付(已付款)','3'=>'在线支付');
- $order_status = isset($arr_charge_type[$orderModel->charge_type]) ? $arr_charge_type[$orderModel->charge_type] : '未知';
-
- $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
- if($access_token)
- {
- $array = array(
- 'touser' => $openid,
- 'template_id' => LEWAIMAI_NEWORDER_TEMPLATE_ID,
- 'url'=>LewaimaiUrl::createWeixinUrl("site/orderinfo", array("m"=>$order_id, "a_id"=>$md5)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>'您有一笔新的乐外卖订单'),
- 'keyword1' => array('value'=>$shopname),
- 'keyword2' => array('value'=>$detail),
- 'keyword3' => array('value'=>$orderModel->init_date),
- 'keyword4' => array('value'=>$orderModel->total_price),
- 'keyword5' => array('value'=>$order_status),
- 'remark' => array('value'=>"点击查看订单详情 ↓↓↓")
- ),
- );
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- LewaimaiHttp::POSTJSON($url, $json);
- }
- }
-
- //发送取消订单模板消息通知商家
- public static function SendCancelOrderTemplate($orderid, $orderModel = NULL, $shopModel = NULL)
- {
- if (!$orderModel)
- {
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if (!$orderModel)
- {
- LewaimaiDebug::Log("no this order");
- return false;
- }
- }
-
- if (!$shopModel)
- {
- $shopModel = Config::model()->findByPk($orderModel->shop_id);
- if (!$shopModel)
- {
- LewaimaiDebug::Log("shopModel is null");
- return false;
- }
- }
-
- //接受微信订单提醒的商家的顾客ID
- $lewaimai_customer_id = $shopModel->lewaimai_customerid;
-
- $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
- if (!$lewaimaicustomerModel)
- {
- LewaimaiDebug::Log("no this lewaimaicustomer");
- return false;
- }
-
- $openid = $lewaimaicustomerModel->openid;
-
- $order_id = $orderModel->id;
- $admin_id = $orderModel->admin_id;
- $shop_id = $orderModel->shop_id;
- $init_date = $orderModel->init_date;
- $charge_type = $orderModel->charge_type;
- $lewaimai_customer_id = $orderModel->lewaimai_customer_id;
- $is_discount = $orderModel->is_discount;
-
- //为了防止猜到别人的订单,这里加个密
- $strsign = $init_date.$admin_id.$shop_id.$charge_type.$order_id.$lewaimai_customer_id.$is_discount;
- $md5 = md5($strsign);
-
- $shopname = $shopModel->shopname;
-
- $arr_charge_type = array('1'=>'货到付款', '2'=>'余额支付(已付款)','3'=>'在线支付');
- $order_status = isset($arr_charge_type[$orderModel->charge_type]) ? $arr_charge_type[$orderModel->charge_type] : '未知';
-
- $adminModel = Admin::model()->findByPk($admin_id);
- if ($adminModel->order_num_type == 0)
- {
- $orderNo = $orderModel->admin_num;
- }
- else
- {
- $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
- }
-
- $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
- if($access_token)
- {
- $array = array(
- 'touser' => $openid,
- 'template_id' => LEWAIMAI_CANCELORDER_TEMPLATE_ID,
- 'url'=>LewaimaiUrl::createWeixinUrl("site/orderinfo", array("m"=>$order_id, "a_id"=>$md5)),
- 'topcolor' => '#FF0000',
- 'data' => array(
- 'first' => array('value'=>'您有一笔乐外卖订单被用户取消。'),
- 'OrderSn' => array('value'=>$orderNo),
- 'OrderStatus' => array('value'=>'已取消',"color"=>"#FF0000"),
- 'remark' => array('value'=>"点击查看订单详情 ↓↓↓")
- ),
- );
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- LewaimaiHttp::POSTJSON($url, $json);
- }
- }
-
- //这个是用来发送顾客进行积分兑换的时候,给商家发送微信消息提醒
- public static function SendPointPrizeOrderTemplate($orderid, $orderModel, $pointsetModel, $remark)
- {
- $lewaimai_customer_id = $pointsetModel->lewaimai_customerid;
- $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
- if (!$lewaimaicustomerModel)
- {
- LewaimaiDebug::Log("no this lewaimaicustomer");
- return false;
- }
- $openid = $lewaimaicustomerModel->openid;
-
- $lewaimaiordercustomerModel = LewaimaiOrderCustomer::model()->find("admin_id = " . $orderModel->admin_id . " AND lewaimai_customer_id = " . $orderModel->lewaimai_customer_id);
- if ($lewaimaiordercustomerModel)
- {
- $new_point = $lewaimaiordercustomerModel->point;
- }
- else
- {
- $new_point = '未知';
- }
-
- $admin_id = $orderModel->admin_id;
- $init_date = strtotime($orderModel->init_time);
-
- $strsign = $admin_id.$orderid.$init_date.$lewaimai_customer_id;
- $md5 = md5($strsign);
-
- $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
- if($access_token)
- {
- $array = array(
- 'touser' => $openid,
- 'template_id' => LEWAIMAI_POINTPRIZEORDER_TEMPLATE_ID,
- 'url'=>LewaimaiUrl::createWeixinUrl("site/pointorderinfo", array("m"=>$orderid, "a_id"=>$md5)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>'您有顾客进行积分兑换奖品啦!'),
- 'FieldName' => array('value'=>'兑换日期'),
- 'Account' => array('value'=>$orderModel->init_time),
- 'change' => array('value'=>'消费积分'),
- 'change' => array('value'=>$orderModel->total_point),
- 'CreditTotal' => array('value'=>$new_point),
- 'Remark' => array('value'=>$remark),
- ),
- );
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- LewaimaiHttp::POSTJSON($url, $json);
- }
- }
- //跑腿订单的模板消息提醒
- public static function SendErrandOrderTemplate($orderid, $type="ybq")
- {
- $statusName = array(
- 'ybq' => '已被抢',
- 'ycs' => '已超时',
- 'yqh' => '已取货',
- 'ysb' => '已失败',
- 'ycg' => '已成功',
- );
-
- if(!isset($statusName[$type])){
- return ;
- }
-
- $errandsModel = ErrandsOrder::model()->findByPk($orderid);
- if(!$errandsModel){
- Yii::log("no this errandorder");
- return ;
- }
-
- $adminId = $errandsModel->admin_id;
- $adminModel = Admin::model()->findByPk($adminId);
- if (!$adminModel)
- {
- Yii::log("adminModel is null");
- return ;
- }
-
- $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($errandsModel->lewaimai_customer_id);
- if (!$lewaimaicustomerModel)
- {
- Yii::log("no this lewaimaicustomermodel");
- return ;
- }
-
- if ($adminModel->wx_type == 3)
- {
- //这种情况使用商家自己的微信公众号来推送模板消息
- $openid = LewaimaiUtility::getWeixinOpenid($adminId, $errandsModel->lewaimai_customer_id);
- if (!$openid)
- {
- Yii::log("not this openid");
- return false;
- }
-
- $tmp_admin_id = $adminId;
-
- $templateModel = Template::model()->find("admin_id = " . $adminId);
- if (!$templateModel)
- {
- Yii::log("no templateModel!");
- return ;
- }
-
- $template_id = $templateModel->paotui_template_id;
-
- if ($adminModel->is_weixinauth)
- {
- $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($adminId);
- }
- else
- {
- $access_token = LewaimaiUtility::GetAccessToken($adminId);
- }
- }
- else
- {
- $openid = $lewaimaicustomerModel->openid;
- $tmp_admin_id = 19;
-
- $template_id = LEWAIMAI_ERRANDORDER_TEMPLATE_ID;
-
- $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
- }
-
- if($access_token)
- {
- $init_time = $errandsModel->init_time;
- $lewaimai_customer_id = $errandsModel->lewaimai_customer_id;
- $strsign = $orderid.$init_time.$lewaimai_customer_id;
- $md5 = md5($strsign);
-
- //计算订单编号
- $orderNo = $errandsModel->trade_no;
- $remark = '';
- if($type == 'ybq' || $type == 'yqh')
- {
- //这两种状态要去先获取当前的配送员信息
- $url = constant("LEKUAISONG_ORDERINFO");
- $post = LewaimaiAboutLekuaisong::CreateErrandRequest();
- $post['trade_no'] = $errandsModel->trade_no;
-
- $post_json = json_encode($post);
- $post_array['param'] = $post_json;
- $result = LewaimaiHttp::POST($url, $post_array);
- $res = json_decode($result, true);
-
- $courier_name = isset($res['data']['d_name']) ? $res['data']['d_name'] : '';
- $courier_phone = isset($res['data']['d_phone']) ? $res['data']['d_phone'] : '';
- $courier_lat = isset($res['data']['d_lat']) ? $res['data']['d_lat'] : '';
- $courier_lng = isset($res['data']['d_lng']) ? $res['data']['d_lng'] : '';
- $customer_lat = $lewaimaicustomerModel -> coordinate_x;
- $customer_lng = $lewaimaicustomerModel -> coordinate_y;
-
- if($courier_name)
- {
- $remark .= '快送骑士姓名'.$courier_name.',';
- }
-
- if($courier_phone)
- {
- $remark .= '电话'.$courier_phone.',';
- }
-
- if($courier_lat > 0 and $courier_lng > 0 and $customer_lat > 0 and $customer_lng > 0)
- {
- $range = LewaimaiUtility::GetDistance($customer_lat, $customer_lng, $courier_lat, $courier_lng);
- if ($range < 1)
- {
- $remark .= '距离您'. ($range * 1000) . " 米
-
- 点击可查看订单详情";
- }
- elseif ($range >= 100000)
- {
-
- }
- else
- {
- $range = sprintf("%.1f", $range);
- $remark .= '距离您'. $range*1 . "千米
-
- 点击可查看订单详情";
- }
- }
- }
-
- if($remark == '')
- {
- $remark = '
- 点击可查看订单详情';
- }
-
- $array = array(
- 'touser' => $openid,
- 'template_id' => $template_id,
- 'url'=>LewaimaiUrl::createWeixinUrl("site/errandsorderinfo", array("m"=>$orderid, "a_id"=>$md5)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>'您有一笔乐外卖跑腿订单'.$statusName[$type]),
- 'OrderSn' => array('value'=>$orderNo),
- 'OrderStatus' => array('value'=>$statusName[$type]),
- 'remark' => array('value'=>$remark)
- ),
- );
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- $res = LewaimaiHttp::POSTJSON($url, $json);
- }else{
- Yii::log('errandserroraccess_token:'.$access_token);
- }
- }
-
- //发送乐快送模式接单(代购模式)的模板消息
- public static function SendLekuaisongTemplate($orderid, $type="ybq", $trade_no="")
- {
- $statusName = array(
- 'ybq' => '已被抢',
- 'ycs' => '已超时',
- 'yqh' => '已取货',
- 'ysb' => '已失败',
- 'ycg' => '已成功',
- );
-
- if(!isset($statusName[$type])){
- return;
- }
-
- $orderModel = LewaimaiOrder::model()->findByPk($orderid);
- if(!$orderModel){
- return;
- }
-
- $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($orderModel->lewaimai_customer_id);
- if(!$lewaimaicustomerModel){
- return;
- }
-
- $tmp_admin_id = 19;
- $openid = $lewaimaicustomerModel->openid;
-
- $init_time = $orderModel->init_date;
- $lewaimai_customer_id = $orderModel->lewaimai_customer_id;
-
- $strsign = $orderid.$init_time.$lewaimai_customer_id;
- $md5 = md5($strsign);
-
- $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($tmp_admin_id);
- if($access_token)
- {
- //计算订单编号
- $orderNo = $trade_no;
- $remark = '';
- if($type == 'ybq' || $type == 'yqh'){
- $url = constant("LEKUAISONG_ORDERINFO");
- $post = LewaimaiAboutLekuaisong::CreateRequest();
- $post['trade_no'] = $trade_no;
-
- $post_json = json_encode($post);
- $post_array['param'] = $post_json;
- $result = LewaimaiHttp::POST($url, $post_array);
- $res = json_decode($result, true);
-
- $courier_name = isset($res['data']['d_name']) ? $res['data']['d_name'] : '';
- $courier_phone = isset($res['data']['d_phone']) ? $res['data']['d_phone'] : '';
- $courier_lat = isset($res['data']['d_lat']) ? $res['data']['d_lat'] : '';
- $courier_lng = isset($res['data']['d_lng']) ? $res['data']['d_lng'] : '';
- $customer_lat = $lewaimaicustomerModel -> coordinate_x;
- $customer_lng = $lewaimaicustomerModel -> coordinate_y;
- if($courier_name)
- {
- $remark .= '快送骑士姓名'.$courier_name.',';
- }
- if($courier_phone)
- {
- $remark .= '电话'.$courier_phone.',';
- }
- if($courier_lat > 0 and $courier_lng > 0 and $customer_lat > 0 and $customer_lng > 0)
- {
- $range = LewaimaiUtility::GetDistance($customer_lat, $customer_lng, $courier_lat, $courier_lng);
- if ($range < 1)
- {
- $remark .= '距离您'. ($range * 1000) . " 米
-
- 点击可查看订单详情";
- }
- elseif ($range >= 100000)
- {
-
- }
- else
- {
- $range = sprintf("%.1f", $range);
- $remark .= '距离您'. $range*1 . "千米
-
- 点击可查看订单详情";
- }
- }
- }
-
- if($remark == '')
- {
- $remark = '
- 点击可查看订单详情';
- }
-
- $array = array(
- 'touser' => $openid,
- 'template_id' => LEWAIMAI_ERRANDORDER_TEMPLATE_ID,
- 'url'=>LewaimaiUrl::createWeixinUrl("site/lekuaisongorderinfo", array("m"=>$orderid, "a_id"=>$md5)),
- 'topcolor' => '#00FF00',
- 'data' => array(
- 'first' => array('value'=>'您有一笔乐外卖订单'.$statusName[$type]),
- 'OrderSn' => array('value'=>$orderNo),
- 'OrderStatus' => array('value'=>$statusName[$type]),
- 'remark' => array('value'=>$remark)
- ),
- );
-
- $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
- $json = json_encode($array);
-
- $res = LewaimaiHttp::POSTJSON($url, $json);
- }else{
- Yii::log('lekuaisongapierroraccess_token:'.$access_token);
- }
- }
- }
|