diff --git a/includes/api/ram-rest-weixin-controller.php b/includes/api/ram-rest-weixin-controller.php index 08e126b0be480dfa47128ce5c7ed784e51475b94..d732f2e4e01522b7a25135661f95249ebb52588c 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 d11188adb3b7d5e93370ad9e23bf3b5cab0312f1..1f6866fcdc3d1459bfaa655cdea3f3c7a0a97f56 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; ?>