From 709248d23d9889dcea83d02fd5dbe72b9b67bcbd Mon Sep 17 00:00:00 2001 From: weigang Date: Fri, 30 Oct 2020 10:46:12 +0800 Subject: [PATCH] get paid unionid --- app/Controller/v3/NotifyController.php | 19 ++++++++++++++++--- .../v3/Implementations/UserInfoService.php | 9 +++++++-- .../Interfaces/UserInfoServiceInterface.php | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/Controller/v3/NotifyController.php b/app/Controller/v3/NotifyController.php index 6754641..c51e122 100644 --- a/app/Controller/v3/NotifyController.php +++ b/app/Controller/v3/NotifyController.php @@ -197,6 +197,15 @@ class NotifyController extends BaseController // 公众号模板消息 $res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->global_order_id); + + co(function () use ($orderMain) { + $openid = User::query()->where(['id' => $orderMain->user_id])->value('openid'); + $unionid = $this->userInfoService->getPaidUnionId($openid, ['mch_id' => config('wxpay.mch_id'), 'out_trade_no' => $orderMain->global_order_id]); + if ($unionid) { + User::query()->where(['id' => $orderMain->user_id, 'openid' => $openid])->update(['unionid' => $unionid]); + } + }); + return true; } catch (Exception $e) { @@ -285,9 +294,13 @@ class NotifyController extends BaseController // 公众号模板消息 $res = $this->miniprogramService->sendTemMsgForOfflineOrder($orderMain->global_order_id); - $openid = User::query()->where(['id' => $orderMain->user_id])->value('openid'); - $res = $this->userInfoService->getPaidUnionId($openid); - $this->log->event('test_get_paid_unionid', ['res' => $res]); + co(function () use ($orderMain) { + $openid = User::query()->where(['id' => $orderMain->user_id])->value('openid'); + $unionid = $this->userInfoService->getPaidUnionId($openid, ['mch_id' => config('wxpay.mch_id'), 'out_trade_no' => $orderMain->global_order_id]); + if ($unionid) { + User::query()->where(['id' => $orderMain->user_id, 'openid' => $openid])->update(['unionid' => $unionid]); + } + }); return true; diff --git a/app/Service/v3/Implementations/UserInfoService.php b/app/Service/v3/Implementations/UserInfoService.php index 9232aa5..41f64b3 100644 --- a/app/Service/v3/Implementations/UserInfoService.php +++ b/app/Service/v3/Implementations/UserInfoService.php @@ -103,11 +103,16 @@ class UserInfoService implements UserInfoServiceInterface return $employees; } - public function getPaidUnionId($openid) + public function getPaidUnionId($openid, $options = []) { $config = config('applet'); $app = Factory::miniProgram($config); $app['guzzle_handler'] = CoroutineHandler::class; - return $app->base->getPaidUnionid($openid); + $res = $app->base->getPaidUnionid($openid, $options); + if (isset($res['unionid']) && $res['unionid']) { + return $res['unionid']; + } else { + return false; + } } } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/UserInfoServiceInterface.php b/app/Service/v3/Interfaces/UserInfoServiceInterface.php index f3378ee..3856830 100644 --- a/app/Service/v3/Interfaces/UserInfoServiceInterface.php +++ b/app/Service/v3/Interfaces/UserInfoServiceInterface.php @@ -10,5 +10,5 @@ interface UserInfoServiceInterface public function detail($userId); public function getStoreByUID($userId); public function getEmployeesByUID($userId); - public function getPaidUnionId($openid); + public function getPaidUnionId($openid, $options = []); } \ No newline at end of file