From 7d1fc27023db6afd37d89070ef35736a2ff12af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=A4=A9?= <442384644@qq.com> Date: Fri, 24 Feb 2023 14:34:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/wechat/AuthController.php | 7 ++++- .../controller/v2/wechat/AuthController.php | 17 +++++++++++ crmeb/app/api/route/v2.php | 4 ++- crmeb/app/services/wechat/RoutineServices.php | 28 +++++++++++++++++++ .../services/wechat/WechatUserServices.php | 12 ++++++-- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/crmeb/app/api/controller/v1/wechat/AuthController.php b/crmeb/app/api/controller/v1/wechat/AuthController.php index 68e5a84fb..ac5016300 100644 --- a/crmeb/app/api/controller/v1/wechat/AuthController.php +++ b/crmeb/app/api/controller/v1/wechat/AuthController.php @@ -37,7 +37,12 @@ class AuthController /** * 小程序授权登录 * @param Request $request - * @return mixed + * @return \think\Response + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @author 吴汐 + * @email 442384644@qq.com + * @date 2023/02/24 */ public function mp_auth(Request $request) { diff --git a/crmeb/app/api/controller/v2/wechat/AuthController.php b/crmeb/app/api/controller/v2/wechat/AuthController.php index 8a5913a8f..822abbb63 100644 --- a/crmeb/app/api/controller/v2/wechat/AuthController.php +++ b/crmeb/app/api/controller/v2/wechat/AuthController.php @@ -79,6 +79,23 @@ class AuthController return app('json')->fail(410019); } + /** + * 小程序绑定手机号 + * @param string $code + * @param string $iv + * @param string $encryptedData + * @return \think\Response + * @author 吴汐 + * @email 442384644@qq.com + * @date 2023/02/24 + */ + public function bindingPhone($code = '', $iv = '', $encryptedData = '') + { + if (!$code || !$iv || !$encryptedData) return app('json')->fail(100100); + $this->services->bindingPhone($code, $iv, $encryptedData); + return app('json')->success(410016); + } + /** 以下方法该版本暂未使用 */ /** * 小程序授权登录 diff --git a/crmeb/app/api/route/v2.php b/crmeb/app/api/route/v2.php index 070ad4002..a40c1ef07 100644 --- a/crmeb/app/api/route/v2.php +++ b/crmeb/app/api/route/v2.php @@ -35,7 +35,9 @@ Route::group('v2', function () { //是否强制绑定手机号 Route::get('bind_status', 'v2.PublicController/bindPhoneStatus'); //小程序授权绑定手机号 - Route::post('auth_bindind_phone', 'v2.wechat.AuthController/authBindingPhone'); + Route::post('auth_bindind_phone', 'v2.wechat.AuthController/bindingPhone'); + //小程序授权后绑定手机号 + Route::post('routine/binding_phone', 'v2.wechat.AuthController/authBindingPhone'); //小程序手机号登录直接绑定 Route::post('phone_silence_auth', 'v2.wechat.AuthController/silenceAuthBindingPhone'); //微信手机号登录直接绑定 diff --git a/crmeb/app/services/wechat/RoutineServices.php b/crmeb/app/services/wechat/RoutineServices.php index c52a8cebd..2c9a8260d 100644 --- a/crmeb/app/services/wechat/RoutineServices.php +++ b/crmeb/app/services/wechat/RoutineServices.php @@ -461,6 +461,34 @@ class RoutineServices extends BaseServices throw new ApiException(410019); } + /** + * @param $code + * @param $iv + * @param $encryptedData + * @return bool + * @author 吴汐 + * @email 442384644@qq.com + * @date 2023/02/24 + */ + public function bindingPhone($code, $iv, $encryptedData) + { + [$userInfoCong, $userInfo] = app()->make(OAuth::class, ['mini_program'])->oauth($code, [ + 'iv' => $iv, + 'encryptedData' => $encryptedData + ]); + if (!$userInfo || !isset($userInfo['purePhoneNumber'])) { + throw new ApiException(410079); + } + $uid = app()->make(WechatUserServices::class)->openidTouid($userInfoCong['openid']); + $userServices = app()->make(UserServices::class); + if ($userServices->count(['phone|account' => $userInfo['purePhoneNumber']])) { + throw new ApiException(410028); + } + $res = app()->make(UserServices::class)->update(['id' => $uid], ['phone' => $userInfo['purePhoneNumber']]); + if ($res) return true; + throw new ApiException(410017); + } + /** * 更新用户信息 diff --git a/crmeb/app/services/wechat/WechatUserServices.php b/crmeb/app/services/wechat/WechatUserServices.php index 50f76e0b6..59be45e76 100644 --- a/crmeb/app/services/wechat/WechatUserServices.php +++ b/crmeb/app/services/wechat/WechatUserServices.php @@ -198,6 +198,10 @@ class WechatUserServices extends BaseServices * 授权后获取用户信息 * @param $openid * @param $user_type + * @return array|\think\Model|null + * @author 吴汐 + * @email 442384644@qq.com + * @date 2023/02/24 */ public function getAuthUserInfo($openid, $user_type) { @@ -256,10 +260,14 @@ class WechatUserServices extends BaseServices /** * 微信授权成功后 - * @param $event + * @param array $data + * @return array|mixed|\think\Model|null * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException - * @throws \think\exception\DbException + * @author 吴汐 + * @email 442384644@qq.com + * @date 2023/02/24 */ public function wechatOauthAfter(array $data) {