From fb1bf6e8b397d775edde4d1c1e0cdba14fe84d69 Mon Sep 17 00:00:00 2001 From: jianbo Date: Sat, 20 Mar 2021 14:32:38 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=88=86=E7=B1=BB=E9=A1=B5=E6=97=A0=E6=B3=95=E6=89=93=E5=BC=80?= =?UTF-8?q?=E5=AA=92=E4=BD=93=E5=BA=93=E7=9A=84bug=202=E3=80=81=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=99=BB=E5=BD=95=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/api/ram-rest-weixin-controller.php | 109 ++++++++++++++++++++ includes/filter/ram-custom-category.php | 8 +- 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/includes/api/ram-rest-weixin-controller.php b/includes/api/ram-rest-weixin-controller.php index 08e126b..d732f2e 100644 --- a/includes/api/ram-rest-weixin-controller.php +++ b/includes/api/ram-rest-weixin-controller.php @@ -133,6 +133,27 @@ class RAM_REST_Weixin_Controller extends WP_REST_Controller{ 'schema' => array( $this, 'get_public_item_schema' ), ) ); + register_rest_route( $this->namespace, '/' . $this->resource_name . '/userlogin', array( + array( + 'methods' => 'POST', + 'callback' => array( $this, 'userlogin' ), + 'permission_callback' => array( $this, 'get_openid_permissions_check' ), + 'args' => array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + 'avatarUrl' => array( + 'required' => true + ), + 'nickname' => array( + 'required' => true + ), + 'js_code' => array( + 'required' => true + ) + ) + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) ); + } function updateUserInfo($request) @@ -291,6 +312,94 @@ class RAM_REST_Weixin_Controller extends WP_REST_Controller{ } } + function userlogin($request) + { + $js_code= $request['js_code']; + // $encryptedData=$request['encryptedData']; + // $iv=$request['iv']; + $avatarUrl=$request['avatarUrl']; + $nickname=empty($request['nickname'])?'':$request['nickname']; + + + $appid = get_option('wf_appid'); + $appsecret = get_option('wf_secret'); + if(empty($appid) || empty($appsecret) ){ + return new WP_Error( 'error', 'appid或appsecret为空', array( 'status' => 500 ) ); + } + else + { + $access_url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$js_code."&grant_type=authorization_code"; + $access_result = https_request($access_url); + if($access_result=='ERROR') { + return new WP_Error( 'error', 'API错误:' . json_encode($access_result), array( 'status' => 501 ) ); + } + $api_result = json_decode($access_result,true); + if( empty( $api_result['openid'] ) || empty( $api_result['session_key'] )) { + return new WP_Error('error', 'API错误:' . json_encode( $api_result ), array( 'status' => 502 ) ); + } + $openId = $api_result['openid']; + $sessionKey = $api_result['session_key']; + $userId=0; + $nickname=filterEmoji($nickname); + $_nickname=base64_encode($nickname); + $_nickname=strlen($_nickname)>49?substr($_nickname,49):$_nickname; + // $avatarUrl= $data['avatarUrl']; + if(!username_exists($openId) ) { + $new_user_data = apply_filters( 'new_user_data', array( + 'user_login' => $openId, + 'first_name' => $nickname , + 'nickname' => $nickname, + 'user_nicename' => $_nickname, + 'display_name' => $nickname, + 'user_pass' => null, + 'user_email' => $openId.'@weixin.com' + ) ); + $userId = wp_insert_user( $new_user_data ); + if ( is_wp_error( $userId ) || empty($userId) || $userId==0 ) { + return new WP_Error( 'error', '插入wordpress用户错误:', array( 'status' => 500 ) ); + } + + update_user_meta( $userId,'avatar',$avatarUrl); + update_user_meta($userId,'usertype',"weixin"); + + } + else{ + $user = get_user_by( 'login', $openId); + $userId= $user->ID; + $userdata =array( + 'ID' => $user->ID, + 'first_name' => $nickname, + 'nickname' => $nickname, + 'user_nicename' => $_nickname, + 'display_name' => $nickname, + 'user_email' => $openId.'@weixin.com' + ); + $userId =wp_update_user($userdata); + if(is_wp_error($userId)){ + return new WP_Error( 'error', '更新wp用户错误:' , array( 'status' => 500 ) ); + } + if (delete_user_meta($userId, 'avatar') ) { + update_user_meta($userId,'avatar',$avatarUrl); + } + + if (delete_user_meta($userId, 'usertype') ) { + $flag=update_user_meta($userId,'usertype',"weixin"); + } + + + } + $userLevel= getUserLevel($userId); + $result["code"]="success"; + $result["message"]= "获取用户信息成功"; + $result["status"]="200"; + $result["openid"]=$openId; + $result["userLevel"]=$userLevel; + $result["userId"]=$userId; + $response = rest_ensure_response($result); + return $response; + } + } + function sendmessage($request) { $openid= $request['openid']; diff --git a/includes/filter/ram-custom-category.php b/includes/filter/ram-custom-category.php index d11188a..1f6866f 100644 --- a/includes/filter/ram-custom-category.php +++ b/includes/filter/ram-custom-category.php @@ -1,6 +1,9 @@ term_id, 'catcover', true ); + if ( function_exists( 'wp_enqueue_media' ) ) { + wp_enqueue_media(); + } wp_enqueue_script('rawscript', plugins_url().'/'.REST_API_TO_MINIPROGRAM_PLUGIN_NAME.'/includes/js/script.js', false, '1.0'); if ( ! $catcover ) $catcover = $default; ?> -- GitLab