'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 = ""; // $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'); } } }