| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\controllers;
- //include_once '../../../utils/Wechat/crypt/wxBizMsgCrypt.php';
- use app\models\DeliveryInfo;
- use app\models\Order;
- use app\models\Store;
- use app\models\WechatConfig;
- use app\models\WechatMenuConfig;
- use app\utils\Delivery\Delivery;
- use EasyWeChat\Factory;
- use yii\web\Controller;
- use EasyWeChat\Kernel\Messages\News;
- use EasyWeChat\Kernel\Messages\NewsItem;
- /**
- * 配送单回调消息通知
- * Class DeliveryController
- * @package app\modules\common\controllers
- */
- class WechatMsgController extends Controller
- {
- /**
- * @var Store $store
- */
- public $store;
- /**
- * 配送消息推送event
- */
- const WAYBILL_EVENT = 'update_waybill_status';
- private static $validEvent = [
- self::WAYBILL_EVENT
- ];
- /**
- * [
- * 'signature' => '49d0de53b76cd91e1ce606a4b0ca26cb57144a07',
- * 'timestamp' => '1635318076',
- * 'nonce' => '1091106830',
- * 'ToUserName' => 'gh_c32588e3eaaf',
- * 'FromUserName' => 'or1pO5cEyXqa3al4iVCaXQy-oC-E',
- * 'CreateTime' => 1635318076,
- * 'MsgType' => 'event',
- * 'Event' => 'update_waybill_status',
- * 'shopid' => 'test_shop_id',
- * 'shop_order_id' => 'tianxin100',
- * 'waybill_id' => 'test_waybill_id',
- * 'action_time' => 1635318076,
- * 'order_status' => 103,
- * 'action_msg' => '',
- * 'agent' => [
- * 'name' => '',
- * 'phone' => '',
- * ],
- * 'shop_no' => '',
- * ]
- */
- public function actionIndex() {
- $params = post_params();
- \Yii::error($params);
- if($_GET["echostr"]){
- if ($this->checkSignature()) {
- return $_GET["echostr"];
- }
- }
- $order = Order::findOne($params['shop_order_id']);
- $this->store = Store::findOne($order->store_id);
- if (!$this->store) {
- \Yii::error(['即时配送状态更改回调===============>', '获取商户信息失败']);
- return;
- }
- if (!$this->checkSignature()) {
- \Yii::error(['即时配送状态更改回调===============>', '验签失败']);
- return;
- }
- // 即时配送状态更新
- if (in_array($params['Event'], self::$validEvent)) {
- try {
- return $this->handleDeliveryOrderStatus($params);
- } catch (\Exception $e) {
- \Yii::error('---------===========');
- \Yii::error(json_encode($e));
- }
- }
- }
- /**
- * 测试接入
- * @return bool
- */
- private function checkSignature()
- {
- $signature = $_GET["signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
- $token = $this->store->wechat_msg_token ?: 'weixin';
- $tmpArr = array($token, $timestamp, $nonce);
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode( $tmpArr );
- $tmpStr = sha1( $tmpStr );
- if ($tmpStr == $signature ) {
- return true;
- } else {
- return false;
- }
- }
- private function handleDeliveryOrderStatus($data) {
- $delivery_info = DeliveryInfo::findOne(['store_id' => $this->store->id, 'order_no' => $data['shop_order_id'], 'waybill_id' => $data['waybill_id']]);
- if (!$delivery_info) {
- \Yii::error(['即时配送状态更改回调===============>', '获取商户信息失败']);
- return;
- }
- $delivery_info->status = $data['order_status'];
- $delivery_info->updated_at = $data['action_time'];
- if (!empty($data['agent']['name']) && !empty($data['agent']['phone'])) {
- $delivery_info->rider_name = $data['agent']['name'];
- $delivery_info->rider_mobile = $data['agent']['phone'];
- }
- if (!$delivery_info->save()) {
- \Yii::error(['即时配送状态更改回调===============>', '更新数据状态失败']);
- return;
- }
- $order = Order::findOne(['order_no' => $data['shop_order_id']]);
- if (in_array($data['order_status'], Delivery::$validRiderGetGoods)) {
- $order->trade_status = Order::ORDER_FLOW_SEND;
- }
- if (in_array($data['order_status'], Delivery::$validComplete)) {
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- }
- if (in_array($data['order_status'], Delivery::$validError)) {
- $order->trade_status = Order::ORDER_FLOW_CANCEL;
- }
- $order->save();
- return 'success';
- }
- // private function decryptMsg() {
- //
- // $pc = new \WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->app_id);
- //
- //
- // $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
- // $from_xml = sprintf($format, $encrypt);
- //
- // // 第三方收到公众号平台发送的消息
- // $msg = '';
- // $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
- // if ($errCode == 0) {
- // \Yii::error("解密后: " . $msg . "\n");
- // } else {
- // \Yii::error($errCode . "\n");
- // }
- // }
- public function actionApi() {
- try {
- $store_id = get_store_id();
- $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => 2, 'is_delete' => 0]);
- $config = [];
- if ($wechat_config && $wechat_config->app_secret && $wechat_config->app_id && $wechat_config->ext) {
- $ext = json_decode($wechat_config->ext, true);
- $token = $ext['token'] ?: '';
- $config = [
- 'app_id' => $wechat_config->app_id,
- 'secret' => $wechat_config->app_secret,
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- 'token' => $token
- ];
- $app = Factory::officialAccount($config);
- $app->server->push(function ($message) {
- debug_log($message,'wechat_msg.log');
- switch ($message['Event']) {
- case "CLICK":
- if (strpos($message['EventKey'], 'news_') !== false) {
- $id = str_replace('news_', '', $message['EventKey']);
- debug_log($id,'wechat_msg.log');
- $menu_config = WechatMenuConfig::findOne($id);
- debug_log($menu_config->value,'wechat_msg.log');
- if ($menu_config->value) {
- $value = json_decode($menu_config->value, true);
- $title = $value['title'];
- $pic = $value['pic'];
- $link = $value['link'];
- $desc = $value['desc'];
- $items = [
- new NewsItem([
- 'title' => $title,
- 'description' => $desc,
- 'url' => $link,
- 'image' => $pic,
- // ...
- ]),
- ];
- return new News($items);
- }
- }
- }
- return '您好,欢迎使用';
- });
- $response = $app->server->serve();
- $response->send();
- return $response;
- }
- } catch (\Exception $e) {
- debug_log($e->getMessage(),'wechat_msg.log');
- }
- }
- }
|