LewaimaiWeixinTemplate.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <?php
  2. //这个类专门用来处理各种模板消息
  3. class LewaimaiWeixinTemplate {
  4. public static function SendOrderConfirmedTemplate($orderid, $orderModel = null)
  5. {
  6. if (!$orderModel)
  7. {
  8. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  9. if (!$orderModel)
  10. {
  11. LewaimaiDebug::Log("no this order");
  12. return false;
  13. }
  14. }
  15. $admin_id = $orderModel->admin_id;
  16. $templateModel = Template::model()->find("admin_id='$admin_id'");
  17. if (!$templateModel)
  18. {
  19. LewaimaiDebug::Log("no template");
  20. return false;
  21. }
  22. if (!$templateModel->is_used)
  23. {
  24. return false;
  25. }
  26. if (!$templateModel->confirm_template_id)
  27. {
  28. return false;
  29. }
  30. $adminModel = Admin::model()->findByPk($admin_id);
  31. if (!$adminModel)
  32. {
  33. LewaimaiDebug::Log("adminModel is null");
  34. return false;
  35. }
  36. if ($adminModel->is_weixinauth)
  37. {
  38. $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
  39. }
  40. else
  41. {
  42. $access_token = LewaimaiUtility::GetAccessToken($admin_id);
  43. }
  44. if (!$access_token)
  45. {
  46. LewaimaiDebug::Log("get access_token failed!");
  47. return false;
  48. }
  49. $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
  50. if (!$openid)
  51. {
  52. return false;
  53. }
  54. $shopModel = Config::model()->findByPk($orderModel->shop_id);
  55. if (!$shopModel)
  56. {
  57. LewaimaiDebug::Log("shopModel is null");
  58. return false;
  59. }
  60. $shopName = $shopModel->shopname;
  61. if ($adminModel->order_num_type == 0)
  62. {
  63. $orderNo = $orderModel->admin_num;
  64. }
  65. else
  66. {
  67. $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
  68. }
  69. $money = $orderModel->total_price;
  70. $detail = '';
  71. $fooditemlist = LewaimaiOrderItem::model()->findAll("lewaimai_order_id = " . $orderid);
  72. foreach($fooditemlist as $fooditem){
  73. $detail .= $fooditem->food_name . ' ' . $fooditem->quantity . '份 ';
  74. }
  75. $array = array(
  76. 'touser' => $openid,
  77. 'template_id' => $templateModel->confirm_template_id,
  78. 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
  79. 'topcolor' => '#00FF00',
  80. 'data' => array(
  81. 'first' => array('value'=>"您的订单商家已确认"),
  82. 'keyword1' => array('value'=>$orderNo),
  83. 'keyword2' => array('value'=>$orderModel->init_date),
  84. 'keyword3' => array('value'=>$detail),
  85. 'keyword4' => array('value'=>'¥'.$money,"color"=>"#ff0000"),
  86. 'remark' => array('value'=>$templateModel->confirm_remark),
  87. ),
  88. );
  89. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  90. $json = json_encode($array);
  91. $ret = LewaimaiHttp::POSTJSON($url, $json);
  92. }
  93. public static function SendOrderCourierTemplate($orderid, $orderModel = null)
  94. {
  95. if (!$orderModel)
  96. {
  97. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  98. if (!$orderModel)
  99. {
  100. LewaimaiDebug::Log("no this order");
  101. return false;
  102. }
  103. }
  104. $admin_id = $orderModel->admin_id;
  105. $templateModel = Template::model()->find("admin_id='$admin_id'");
  106. if (!$templateModel)
  107. {
  108. LewaimaiDebug::Log("no template");
  109. return false;
  110. }
  111. if (!$templateModel->is_used)
  112. {
  113. return false;
  114. }
  115. if (!$templateModel->courier_template_id)
  116. {
  117. return false;
  118. }
  119. $adminModel = Admin::model()->findByPk($admin_id);
  120. if (!$adminModel)
  121. {
  122. LewaimaiDebug::Log("adminModel is null");
  123. return false;
  124. }
  125. if ($adminModel->is_weixinauth)
  126. {
  127. $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
  128. }
  129. else
  130. {
  131. $access_token = LewaimaiUtility::GetAccessToken($admin_id);
  132. }
  133. if (!$access_token)
  134. {
  135. LewaimaiDebug::Log("get access_token failed!");
  136. return false;
  137. }
  138. $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
  139. if (!$openid)
  140. {
  141. return false;
  142. }
  143. $shopModel = Config::model()->findByPk($orderModel->shop_id);
  144. if (!$shopModel)
  145. {
  146. LewaimaiDebug::Log("shopModel is null");
  147. return false;
  148. }
  149. $shopName = $shopModel->shopname;
  150. if ($adminModel->order_num_type == 0)
  151. {
  152. $orderNo = $orderModel->admin_num;
  153. }
  154. else
  155. {
  156. $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
  157. }
  158. $money = $orderModel->total_price;
  159. $detail = '';
  160. $fooditemlist = LewaimaiOrderItem::model()->findAll("lewaimai_order_id = " . $orderid);
  161. foreach($fooditemlist as $fooditem){
  162. $detail .= $fooditem->food_name . ' ' . $fooditem->quantity . '份 ';
  163. }
  164. if($orderModel->charge_type == 1)
  165. {
  166. $charge_type = '货到付款';
  167. }
  168. elseif($orderModel->charge_type == 2)
  169. {
  170. $charge_type = '余额支付';
  171. }
  172. else
  173. {
  174. $charge_type = '在线支付';
  175. }
  176. $courier_id = $orderModel->courier_id;
  177. $courier = Courier::model()->findByPk($courier_id);
  178. if (!$courier)
  179. {
  180. return false;
  181. }
  182. $array = array(
  183. 'touser' => $openid,
  184. 'template_id' => $templateModel->courier_template_id,
  185. 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
  186. 'topcolor' => '#00FF00',
  187. 'data' => array(
  188. 'first' => array('value'=>"您的订单商家已开始配送"),
  189. 'keyword1' => array('value'=>$shopName),
  190. 'keyword2' => array('value'=>$orderNo),
  191. 'keyword3' => array('value'=>$detail),
  192. 'keyword4' => array('value'=>$charge_type),
  193. 'keyword5' => array('value'=>$courier->name.",".$courier->phone),
  194. 'remark' => array('value'=>$templateModel->courier_remark),
  195. ),
  196. );
  197. LewaimaiDebug::LogArray($array);
  198. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  199. $json = json_encode($array);
  200. $ret = LewaimaiHttp::POSTJSON($url, $json);
  201. }
  202. public static function SendOrderSuccessTemplate($orderid, $orderModel = null)
  203. {
  204. if (!$orderModel)
  205. {
  206. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  207. if (!$orderModel)
  208. {
  209. LewaimaiDebug::Log("no this order");
  210. return false;
  211. }
  212. }
  213. $admin_id = $orderModel->admin_id;
  214. $templateModel = Template::model()->find("admin_id='$admin_id'");
  215. if (!$templateModel)
  216. {
  217. LewaimaiDebug::Log("no template");
  218. return false;
  219. }
  220. if (!$templateModel->is_used)
  221. {
  222. return false;
  223. }
  224. if (!$templateModel->success_template_id)
  225. {
  226. return false;
  227. }
  228. $adminModel = Admin::model()->findByPk($admin_id);
  229. if (!$adminModel)
  230. {
  231. LewaimaiDebug::Log("adminModel is null");
  232. return false;
  233. }
  234. if ($adminModel->is_weixinauth)
  235. {
  236. $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
  237. }
  238. else
  239. {
  240. $access_token = LewaimaiUtility::GetAccessToken($admin_id);
  241. }
  242. if (!$access_token)
  243. {
  244. LewaimaiDebug::Log("get access_token failed!");
  245. return false;
  246. }
  247. $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
  248. if (!$openid)
  249. {
  250. return false;
  251. }
  252. $shopModel = Config::model()->findByPk($orderModel->shop_id);
  253. if (!$shopModel)
  254. {
  255. LewaimaiDebug::Log("shopModel is null");
  256. return false;
  257. }
  258. $shopName = $shopModel->shopname;
  259. if ($adminModel->order_num_type == 0)
  260. {
  261. $orderNo = $orderModel->admin_num;
  262. }
  263. else
  264. {
  265. $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
  266. }
  267. $array = array(
  268. 'touser' => $openid,
  269. 'template_id' => $templateModel->success_template_id,
  270. 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
  271. 'topcolor' => '#00FF00',
  272. 'data' => array(
  273. 'first' => array('value'=>"您的订单已完成。"),
  274. 'keyword1' => array('value'=>$orderNo),
  275. 'keyword2' => array('value'=>$orderModel->complete_date),
  276. 'remark' => array('value'=>$templateModel->success_remark),
  277. ),
  278. );
  279. LewaimaiDebug::LogArray($array);
  280. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  281. $json = json_encode($array);
  282. $ret = LewaimaiHttp::POSTJSON($url, $json);
  283. }
  284. public static function SendOrderCancelTemplate($orderid, $orderModel = null)
  285. {
  286. if (!$orderModel)
  287. {
  288. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  289. if (!$orderModel)
  290. {
  291. LewaimaiDebug::Log("no this order");
  292. return false;
  293. }
  294. }
  295. $admin_id = $orderModel->admin_id;
  296. $templateModel = Template::model()->find("admin_id='$admin_id'");
  297. if (!$templateModel)
  298. {
  299. LewaimaiDebug::Log("no template");
  300. return false;
  301. }
  302. if (!$templateModel->is_used)
  303. {
  304. return false;
  305. }
  306. if (!$templateModel->cancel_template_id)
  307. {
  308. return false;
  309. }
  310. $adminModel = Admin::model()->findByPk($admin_id);
  311. if (!$adminModel)
  312. {
  313. LewaimaiDebug::Log("adminModel is null");
  314. return false;
  315. }
  316. if ($adminModel->is_weixinauth)
  317. {
  318. $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($admin_id);
  319. }
  320. else
  321. {
  322. $access_token = LewaimaiUtility::GetAccessToken($admin_id);
  323. }
  324. if (!$access_token)
  325. {
  326. LewaimaiDebug::Log("get access_token failed!");
  327. return false;
  328. }
  329. $openid = LewaimaiUtility::getWeixinOpenid($orderModel->admin_id, $orderModel->lewaimai_customer_id);
  330. if (!$openid)
  331. {
  332. return false;
  333. }
  334. $shopModel = Config::model()->findByPk($orderModel->shop_id);
  335. if (!$shopModel)
  336. {
  337. LewaimaiDebug::Log("shopModel is null");
  338. return false;
  339. }
  340. $shopName = $shopModel->shopname;
  341. if ($adminModel->order_num_type == 0)
  342. {
  343. $orderNo = $orderModel->admin_num;
  344. }
  345. else
  346. {
  347. $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
  348. }
  349. $array = array(
  350. 'touser' => $openid,
  351. 'template_id' => $templateModel->cancel_template_id,
  352. 'url' => LewaimaiUrl::createWeixinUrl("lewaimaiuser/orderinfo", array("order_id"=>$orderid, "adminId"=>$orderModel->admin_id)),
  353. 'topcolor' => '#00FF00',
  354. 'data' => array(
  355. 'first' => array('value'=>"您的订单已取消。"),
  356. 'keyword1' => array('value'=>$orderNo),
  357. 'keyword2' => array('value'=>$orderModel->complete_date),
  358. 'remark' => array('value'=>$templateModel->cancel_remark),
  359. ),
  360. );
  361. LewaimaiDebug::LogArray($array);
  362. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  363. $json = json_encode($array);
  364. $ret = LewaimaiHttp::POSTJSON($url, $json);
  365. }
  366. //在商家有新订单的时候,通过乐外卖的微信公众号给商家发模板消息提醒
  367. public static function SendNewOrderTemplate($orderid, $orderModel = NULL, $shopModel = NULL)
  368. {
  369. if (!$orderModel)
  370. {
  371. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  372. if (!$orderModel)
  373. {
  374. LewaimaiDebug::Log("no this order");
  375. return false;
  376. }
  377. }
  378. if (!$shopModel)
  379. {
  380. $shopModel = Config::model()->findByPk($orderModel->shop_id);
  381. if (!$shopModel)
  382. {
  383. LewaimaiDebug::Log("shopModel is null");
  384. return false;
  385. }
  386. }
  387. //接受微信订单提醒的商家的顾客ID
  388. $lewaimai_customer_id = $shopModel->lewaimai_customerid;
  389. $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
  390. if (!$lewaimaicustomerModel)
  391. {
  392. LewaimaiDebug::Log("no this lewaimaicustomer");
  393. return false;
  394. }
  395. $openid = $lewaimaicustomerModel->openid;
  396. $order_id = $orderModel->id;
  397. $admin_id = $orderModel->admin_id;
  398. $shop_id = $orderModel->shop_id;
  399. $init_date = $orderModel->init_date;
  400. $charge_type = $orderModel->charge_type;
  401. $lewaimai_customer_id = $orderModel->lewaimai_customer_id;
  402. $is_discount = $orderModel->is_discount;
  403. //为了防止猜到别人的订单,这里加个密
  404. $strsign = $init_date.$admin_id.$shop_id.$charge_type.$order_id.$lewaimai_customer_id.$is_discount;
  405. $md5 = md5($strsign);
  406. $shopname = $shopModel->shopname;
  407. $detail = '';
  408. $fooditemlist = LewaimaiOrderItem::model()->findAll("lewaimai_order_id = " . $orderid);
  409. foreach($fooditemlist as $fooditem){
  410. $detail .= $fooditem->food_name . ' ' . $fooditem->quantity . '份 ';
  411. }
  412. $arr_charge_type = array('1'=>'货到付款', '2'=>'余额支付(已付款)','3'=>'在线支付');
  413. $order_status = isset($arr_charge_type[$orderModel->charge_type]) ? $arr_charge_type[$orderModel->charge_type] : '未知';
  414. $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
  415. if($access_token)
  416. {
  417. $array = array(
  418. 'touser' => $openid,
  419. 'template_id' => LEWAIMAI_NEWORDER_TEMPLATE_ID,
  420. 'url'=>LewaimaiUrl::createWeixinUrl("site/orderinfo", array("m"=>$order_id, "a_id"=>$md5)),
  421. 'topcolor' => '#00FF00',
  422. 'data' => array(
  423. 'first' => array('value'=>'您有一笔新的乐外卖订单'),
  424. 'keyword1' => array('value'=>$shopname),
  425. 'keyword2' => array('value'=>$detail),
  426. 'keyword3' => array('value'=>$orderModel->init_date),
  427. 'keyword4' => array('value'=>$orderModel->total_price),
  428. 'keyword5' => array('value'=>$order_status),
  429. 'remark' => array('value'=>"点击查看订单详情 ↓↓↓")
  430. ),
  431. );
  432. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  433. $json = json_encode($array);
  434. LewaimaiHttp::POSTJSON($url, $json);
  435. }
  436. }
  437. //发送取消订单模板消息通知商家
  438. public static function SendCancelOrderTemplate($orderid, $orderModel = NULL, $shopModel = NULL)
  439. {
  440. if (!$orderModel)
  441. {
  442. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  443. if (!$orderModel)
  444. {
  445. LewaimaiDebug::Log("no this order");
  446. return false;
  447. }
  448. }
  449. if (!$shopModel)
  450. {
  451. $shopModel = Config::model()->findByPk($orderModel->shop_id);
  452. if (!$shopModel)
  453. {
  454. LewaimaiDebug::Log("shopModel is null");
  455. return false;
  456. }
  457. }
  458. //接受微信订单提醒的商家的顾客ID
  459. $lewaimai_customer_id = $shopModel->lewaimai_customerid;
  460. $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
  461. if (!$lewaimaicustomerModel)
  462. {
  463. LewaimaiDebug::Log("no this lewaimaicustomer");
  464. return false;
  465. }
  466. $openid = $lewaimaicustomerModel->openid;
  467. $order_id = $orderModel->id;
  468. $admin_id = $orderModel->admin_id;
  469. $shop_id = $orderModel->shop_id;
  470. $init_date = $orderModel->init_date;
  471. $charge_type = $orderModel->charge_type;
  472. $lewaimai_customer_id = $orderModel->lewaimai_customer_id;
  473. $is_discount = $orderModel->is_discount;
  474. //为了防止猜到别人的订单,这里加个密
  475. $strsign = $init_date.$admin_id.$shop_id.$charge_type.$order_id.$lewaimai_customer_id.$is_discount;
  476. $md5 = md5($strsign);
  477. $shopname = $shopModel->shopname;
  478. $arr_charge_type = array('1'=>'货到付款', '2'=>'余额支付(已付款)','3'=>'在线支付');
  479. $order_status = isset($arr_charge_type[$orderModel->charge_type]) ? $arr_charge_type[$orderModel->charge_type] : '未知';
  480. $adminModel = Admin::model()->findByPk($admin_id);
  481. if ($adminModel->order_num_type == 0)
  482. {
  483. $orderNo = $orderModel->admin_num;
  484. }
  485. else
  486. {
  487. $orderNo = $shopModel->order_prefix . $orderModel->shop_num;
  488. }
  489. $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
  490. if($access_token)
  491. {
  492. $array = array(
  493. 'touser' => $openid,
  494. 'template_id' => LEWAIMAI_CANCELORDER_TEMPLATE_ID,
  495. 'url'=>LewaimaiUrl::createWeixinUrl("site/orderinfo", array("m"=>$order_id, "a_id"=>$md5)),
  496. 'topcolor' => '#FF0000',
  497. 'data' => array(
  498. 'first' => array('value'=>'您有一笔乐外卖订单被用户取消。'),
  499. 'OrderSn' => array('value'=>$orderNo),
  500. 'OrderStatus' => array('value'=>'已取消',"color"=>"#FF0000"),
  501. 'remark' => array('value'=>"点击查看订单详情 ↓↓↓")
  502. ),
  503. );
  504. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  505. $json = json_encode($array);
  506. LewaimaiHttp::POSTJSON($url, $json);
  507. }
  508. }
  509. //这个是用来发送顾客进行积分兑换的时候,给商家发送微信消息提醒
  510. public static function SendPointPrizeOrderTemplate($orderid, $orderModel, $pointsetModel, $remark)
  511. {
  512. $lewaimai_customer_id = $pointsetModel->lewaimai_customerid;
  513. $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
  514. if (!$lewaimaicustomerModel)
  515. {
  516. LewaimaiDebug::Log("no this lewaimaicustomer");
  517. return false;
  518. }
  519. $openid = $lewaimaicustomerModel->openid;
  520. $lewaimaiordercustomerModel = LewaimaiOrderCustomer::model()->find("admin_id = " . $orderModel->admin_id . " AND lewaimai_customer_id = " . $orderModel->lewaimai_customer_id);
  521. if ($lewaimaiordercustomerModel)
  522. {
  523. $new_point = $lewaimaiordercustomerModel->point;
  524. }
  525. else
  526. {
  527. $new_point = '未知';
  528. }
  529. $admin_id = $orderModel->admin_id;
  530. $init_date = strtotime($orderModel->init_time);
  531. $strsign = $admin_id.$orderid.$init_date.$lewaimai_customer_id;
  532. $md5 = md5($strsign);
  533. $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
  534. if($access_token)
  535. {
  536. $array = array(
  537. 'touser' => $openid,
  538. 'template_id' => LEWAIMAI_POINTPRIZEORDER_TEMPLATE_ID,
  539. 'url'=>LewaimaiUrl::createWeixinUrl("site/pointorderinfo", array("m"=>$orderid, "a_id"=>$md5)),
  540. 'topcolor' => '#00FF00',
  541. 'data' => array(
  542. 'first' => array('value'=>'您有顾客进行积分兑换奖品啦!'),
  543. 'FieldName' => array('value'=>'兑换日期'),
  544. 'Account' => array('value'=>$orderModel->init_time),
  545. 'change' => array('value'=>'消费积分'),
  546. 'change' => array('value'=>$orderModel->total_point),
  547. 'CreditTotal' => array('value'=>$new_point),
  548. 'Remark' => array('value'=>$remark),
  549. ),
  550. );
  551. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  552. $json = json_encode($array);
  553. LewaimaiHttp::POSTJSON($url, $json);
  554. }
  555. }
  556. //跑腿订单的模板消息提醒
  557. public static function SendErrandOrderTemplate($orderid, $type="ybq")
  558. {
  559. $statusName = array(
  560. 'ybq' => '已被抢',
  561. 'ycs' => '已超时',
  562. 'yqh' => '已取货',
  563. 'ysb' => '已失败',
  564. 'ycg' => '已成功',
  565. );
  566. if(!isset($statusName[$type])){
  567. return ;
  568. }
  569. $errandsModel = ErrandsOrder::model()->findByPk($orderid);
  570. if(!$errandsModel){
  571. Yii::log("no this errandorder");
  572. return ;
  573. }
  574. $adminId = $errandsModel->admin_id;
  575. $adminModel = Admin::model()->findByPk($adminId);
  576. if (!$adminModel)
  577. {
  578. Yii::log("adminModel is null");
  579. return ;
  580. }
  581. $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($errandsModel->lewaimai_customer_id);
  582. if (!$lewaimaicustomerModel)
  583. {
  584. Yii::log("no this lewaimaicustomermodel");
  585. return ;
  586. }
  587. if ($adminModel->wx_type == 3)
  588. {
  589. //这种情况使用商家自己的微信公众号来推送模板消息
  590. $openid = LewaimaiUtility::getWeixinOpenid($adminId, $errandsModel->lewaimai_customer_id);
  591. if (!$openid)
  592. {
  593. Yii::log("not this openid");
  594. return false;
  595. }
  596. $tmp_admin_id = $adminId;
  597. $templateModel = Template::model()->find("admin_id = " . $adminId);
  598. if (!$templateModel)
  599. {
  600. Yii::log("no templateModel!");
  601. return ;
  602. }
  603. $template_id = $templateModel->paotui_template_id;
  604. if ($adminModel->is_weixinauth)
  605. {
  606. $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($adminId);
  607. }
  608. else
  609. {
  610. $access_token = LewaimaiUtility::GetAccessToken($adminId);
  611. }
  612. }
  613. else
  614. {
  615. $openid = $lewaimaicustomerModel->openid;
  616. $tmp_admin_id = 19;
  617. $template_id = LEWAIMAI_ERRANDORDER_TEMPLATE_ID;
  618. $access_token = LewaimaiUtility::GetLewaimaiAccessToken();
  619. }
  620. if($access_token)
  621. {
  622. $init_time = $errandsModel->init_time;
  623. $lewaimai_customer_id = $errandsModel->lewaimai_customer_id;
  624. $strsign = $orderid.$init_time.$lewaimai_customer_id;
  625. $md5 = md5($strsign);
  626. //计算订单编号
  627. $orderNo = $errandsModel->trade_no;
  628. $remark = '';
  629. if($type == 'ybq' || $type == 'yqh')
  630. {
  631. //这两种状态要去先获取当前的配送员信息
  632. $url = constant("LEKUAISONG_ORDERINFO");
  633. $post = LewaimaiAboutLekuaisong::CreateErrandRequest();
  634. $post['trade_no'] = $errandsModel->trade_no;
  635. $post_json = json_encode($post);
  636. $post_array['param'] = $post_json;
  637. $result = LewaimaiHttp::POST($url, $post_array);
  638. $res = json_decode($result, true);
  639. $courier_name = isset($res['data']['d_name']) ? $res['data']['d_name'] : '';
  640. $courier_phone = isset($res['data']['d_phone']) ? $res['data']['d_phone'] : '';
  641. $courier_lat = isset($res['data']['d_lat']) ? $res['data']['d_lat'] : '';
  642. $courier_lng = isset($res['data']['d_lng']) ? $res['data']['d_lng'] : '';
  643. $customer_lat = $lewaimaicustomerModel -> coordinate_x;
  644. $customer_lng = $lewaimaicustomerModel -> coordinate_y;
  645. if($courier_name)
  646. {
  647. $remark .= '快送骑士姓名'.$courier_name.',';
  648. }
  649. if($courier_phone)
  650. {
  651. $remark .= '电话'.$courier_phone.',';
  652. }
  653. if($courier_lat > 0 and $courier_lng > 0 and $customer_lat > 0 and $customer_lng > 0)
  654. {
  655. $range = LewaimaiUtility::GetDistance($customer_lat, $customer_lng, $courier_lat, $courier_lng);
  656. if ($range < 1)
  657. {
  658. $remark .= '距离您'. ($range * 1000) . " 米
  659. 点击可查看订单详情";
  660. }
  661. elseif ($range >= 100000)
  662. {
  663. }
  664. else
  665. {
  666. $range = sprintf("%.1f", $range);
  667. $remark .= '距离您'. $range*1 . "千米
  668. 点击可查看订单详情";
  669. }
  670. }
  671. }
  672. if($remark == '')
  673. {
  674. $remark = '
  675. 点击可查看订单详情';
  676. }
  677. $array = array(
  678. 'touser' => $openid,
  679. 'template_id' => $template_id,
  680. 'url'=>LewaimaiUrl::createWeixinUrl("site/errandsorderinfo", array("m"=>$orderid, "a_id"=>$md5)),
  681. 'topcolor' => '#00FF00',
  682. 'data' => array(
  683. 'first' => array('value'=>'您有一笔乐外卖跑腿订单'.$statusName[$type]),
  684. 'OrderSn' => array('value'=>$orderNo),
  685. 'OrderStatus' => array('value'=>$statusName[$type]),
  686. 'remark' => array('value'=>$remark)
  687. ),
  688. );
  689. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  690. $json = json_encode($array);
  691. $res = LewaimaiHttp::POSTJSON($url, $json);
  692. }else{
  693. Yii::log('errandserroraccess_token:'.$access_token);
  694. }
  695. }
  696. //发送乐快送模式接单(代购模式)的模板消息
  697. public static function SendLekuaisongTemplate($orderid, $type="ybq", $trade_no="")
  698. {
  699. $statusName = array(
  700. 'ybq' => '已被抢',
  701. 'ycs' => '已超时',
  702. 'yqh' => '已取货',
  703. 'ysb' => '已失败',
  704. 'ycg' => '已成功',
  705. );
  706. if(!isset($statusName[$type])){
  707. return;
  708. }
  709. $orderModel = LewaimaiOrder::model()->findByPk($orderid);
  710. if(!$orderModel){
  711. return;
  712. }
  713. $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($orderModel->lewaimai_customer_id);
  714. if(!$lewaimaicustomerModel){
  715. return;
  716. }
  717. $tmp_admin_id = 19;
  718. $openid = $lewaimaicustomerModel->openid;
  719. $init_time = $orderModel->init_date;
  720. $lewaimai_customer_id = $orderModel->lewaimai_customer_id;
  721. $strsign = $orderid.$init_time.$lewaimai_customer_id;
  722. $md5 = md5($strsign);
  723. $access_token = LewaimaiWeixinauth::getAuthorizerAccessToken($tmp_admin_id);
  724. if($access_token)
  725. {
  726. //计算订单编号
  727. $orderNo = $trade_no;
  728. $remark = '';
  729. if($type == 'ybq' || $type == 'yqh'){
  730. $url = constant("LEKUAISONG_ORDERINFO");
  731. $post = LewaimaiAboutLekuaisong::CreateRequest();
  732. $post['trade_no'] = $trade_no;
  733. $post_json = json_encode($post);
  734. $post_array['param'] = $post_json;
  735. $result = LewaimaiHttp::POST($url, $post_array);
  736. $res = json_decode($result, true);
  737. $courier_name = isset($res['data']['d_name']) ? $res['data']['d_name'] : '';
  738. $courier_phone = isset($res['data']['d_phone']) ? $res['data']['d_phone'] : '';
  739. $courier_lat = isset($res['data']['d_lat']) ? $res['data']['d_lat'] : '';
  740. $courier_lng = isset($res['data']['d_lng']) ? $res['data']['d_lng'] : '';
  741. $customer_lat = $lewaimaicustomerModel -> coordinate_x;
  742. $customer_lng = $lewaimaicustomerModel -> coordinate_y;
  743. if($courier_name)
  744. {
  745. $remark .= '快送骑士姓名'.$courier_name.',';
  746. }
  747. if($courier_phone)
  748. {
  749. $remark .= '电话'.$courier_phone.',';
  750. }
  751. if($courier_lat > 0 and $courier_lng > 0 and $customer_lat > 0 and $customer_lng > 0)
  752. {
  753. $range = LewaimaiUtility::GetDistance($customer_lat, $customer_lng, $courier_lat, $courier_lng);
  754. if ($range < 1)
  755. {
  756. $remark .= '距离您'. ($range * 1000) . " 米
  757. 点击可查看订单详情";
  758. }
  759. elseif ($range >= 100000)
  760. {
  761. }
  762. else
  763. {
  764. $range = sprintf("%.1f", $range);
  765. $remark .= '距离您'. $range*1 . "千米
  766. 点击可查看订单详情";
  767. }
  768. }
  769. }
  770. if($remark == '')
  771. {
  772. $remark = '
  773. 点击可查看订单详情';
  774. }
  775. $array = array(
  776. 'touser' => $openid,
  777. 'template_id' => LEWAIMAI_ERRANDORDER_TEMPLATE_ID,
  778. 'url'=>LewaimaiUrl::createWeixinUrl("site/lekuaisongorderinfo", array("m"=>$orderid, "a_id"=>$md5)),
  779. 'topcolor' => '#00FF00',
  780. 'data' => array(
  781. 'first' => array('value'=>'您有一笔乐外卖订单'.$statusName[$type]),
  782. 'OrderSn' => array('value'=>$orderNo),
  783. 'OrderStatus' => array('value'=>$statusName[$type]),
  784. 'remark' => array('value'=>$remark)
  785. ),
  786. );
  787. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
  788. $json = json_encode($array);
  789. $res = LewaimaiHttp::POSTJSON($url, $json);
  790. }else{
  791. Yii::log('lekuaisongapierroraccess_token:'.$access_token);
  792. }
  793. }
  794. }