ram-rest-weixin-controller.php 30.0 KB
Newer Older
ijianbo's avatar
ijianbo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class RAM_REST_Weixin_Controller  extends WP_REST_Controller{

    public function __construct() {
        $this->namespace     = 'watch-life-net/v1';
        $this->resource_name = 'weixin';
    }

    public function register_routes() {
        register_rest_route( $this->namespace, '/' . $this->resource_name.'/qrcodeimg', array(
            // Here we register the readable endpoint for collections.
            array(
                'methods'   => 'POST',
                'callback'  => array( $this, 'getWinxinQrcodeImg' ),
                'permission_callback' => array( $this, 'get_qrcodeimg_permissions_check' ),
                'args'               => array(              
                    'postid' => array(
                        'required' => true
                    ),                    
                    'path' => array(
                        'required' => true
                    )
                )
                 
            ),            
            'schema' => array( $this, 'get_public_item_schema' ),
        ) );


        register_rest_route( $this->namespace, '/' . $this->resource_name.'/sendmessage', array(
            // Here we register the readable endpoint for collections.
            array(
                'methods'   => 'POST',
                'callback'  => array( $this, 'sendmessage' ),
                'permission_callback' => array( $this, 'send_message_permissions_check' ),
                'args'               => array(              
                    'openid' => array(
                        'required' => true
                    ),                    
                    'template_id' => array(
                        'required' => true
                    ),
                    'postid' => array(
                        'required' => true
                    ),
                    'form_id' => array(
                        'required' => true
                    ),
                    'total_fee' => array(
                        'required' => true
                    ),
                    'flag' => array(
                        'required' => true
59 60 61
                    ),
                    'fromUser' => array(
                        'required' => true
ijianbo's avatar
ijianbo 已提交
62
                    )
63
                    
ijianbo's avatar
ijianbo 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                )
                 
            ),            
            'schema' => array( $this, 'get_public_item_schema' ),
        ) );

        register_rest_route( $this->namespace, '/' . $this->resource_name.'/getopenid', array(
            // Here we register the readable endpoint for collections.
            array(
                'methods'   => 'POST',
                'callback'  => array( $this, 'getOpenid' ),
                'permission_callback' => array( $this, 'get_openid_permissions_check' ),
                'args'               => array(              
                    'js_code' => array(
                        'required' => true
                    ),                    
                    'encryptedData' => array(
                        'required' => true
                    ),
                    'iv' => array(
                        'required' => true
                    ),
                    'avatarUrl' => array(
                        'required' => true
                    ),
                    'nickname' => array(
                        'required' => true
                    )
                )
                 
            ),            
            'schema' => array( $this, 'get_public_item_schema' ),
        ) );
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

        register_rest_route( $this->namespace, '/' . $this->resource_name.'/getuserinfo', array(
            // Here we register the readable endpoint for collections.
            array(
                'methods'   => 'GET',
                'callback'  => array( $this, 'getUserInfo' ),
                'permission_callback' => array( $this, 'get_userInfo_permissions_check' ),
                'args'               => array(              
                    'openid' => array(
                        'required' => true
                    )
                )
                 
            ),            
            'schema' => array( $this, 'get_public_item_schema' ),
        ) );

        register_rest_route( $this->namespace, '/' . $this->resource_name.'/updateuserinfo', array(
            // Here we register the readable endpoint for collections.
            array(
                'methods'   => 'POST',
                'callback'  => array( $this, 'updateUserInfo' ),
                'permission_callback' => array( $this, 'update_userInfo_permissions_check' ),
                'args'               => array(              
                    'openid' => array(
                        'required' => true
                    ),
                    'avatarUrl' => array(
                        'required' => true
                    ),
                    'nickname' => array(
                        'required' => true
                    )
                )
                 
            ),            
            'schema' => array( $this, 'get_public_item_schema' ),
        ) );

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        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' ),
            ) );

ijianbo's avatar
ijianbo 已提交
157 158
    }

159
    function updateUserInfo($request)
ijianbo's avatar
ijianbo 已提交
160
    {
161 162 163
        $openId =$request['openid'];
        $nickname=empty($request['nickname'])?'':$request['nickname'];
        $nickname=filterEmoji($nickname);
J
jianbo 已提交
164 165
        $_nickname=base64_encode($nickname);          
		$_nickname=strlen($_nickname)>49?substr($_nickname,49):$_nickname;
166 167 168 169 170 171 172 173 174 175
        $avatarUrl=empty($request['avatarUrl'])?'':$request['avatarUrl']; 
        $user = get_user_by( 'login', $openId);
        if(empty($user))
        {
            return new WP_Error( 'error', '此用户不存在' , array( 'status' => 500 ) );
        }     
        $userdata =array(
            'ID'            => $user->ID,
            'first_name'	=> $nickname,
            'nickname'      => $nickname,
J
jianbo 已提交
176
            'user_nicename' => $_nickname,
177 178 179 180 181 182 183 184 185
            '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 ) );
        } 
                
        update_user_meta($userId,'avatar',$avatarUrl);
J
jianbo 已提交
186
        update_user_meta($userId,'usertype',"weixin","weixin");
187 188 189 190 191 192 193 194 195

        $userLevel= getUserLevel($userId);
        $result["code"]="success";
        $result["message"]= "更新成功";
        $result["status"]="200";
        $result["openid"]=$openId;
        $result["userLevel"]=$userLevel;            
        $response = rest_ensure_response($result);
        return $response;
ijianbo's avatar
ijianbo 已提交
196

197
    }
ijianbo's avatar
ijianbo 已提交
198

199 200 201 202 203 204
    function getUserInfo($request)
    {
      
        $openId =$request['openid'];
        $_user = get_user_by( 'login', $openId);  
        if(empty($_user ))
ijianbo's avatar
ijianbo 已提交
205
        {
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            return new WP_Error( 'error', '无此用户信息', array( 'status' => 500 ) );
       
        }
        else{

            $user['nickname']=$_user->display_name;
            $avatar= get_user_meta($_user->ID, 'avatar', true );
            if(empty($avatar))
            {
                $avatar = plugins_url()."/".REST_API_TO_MINIPROGRAM_PLUGIN_NAME."/includes/images/gravatar.png";
            }

            $userLevel=getUserLevel($_user->ID);
            $user['userLevel']=$userLevel;            
            $user['avatar']=$avatar;            
            $result["code"]="success";
            $result["message"]= "获取用户信息成功";
            $result["status"]="200";
            $result["user"]=$user;
            $response = rest_ensure_response($result);
            return $response;

        }
    }
    function getOpenid($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 ) );
ijianbo's avatar
ijianbo 已提交
241 242
        }
        else
243
        {        
ijianbo's avatar
ijianbo 已提交
244 245
            $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);
246 247 248 249
            if($access_result=='ERROR') {
                return new WP_Error( 'error', 'API错误:' . json_encode($access_result), array( 'status' => 501 ) );
            } 
            $api_result  = json_decode($access_result,true);            
J
jianbo 已提交
250 251
            if( empty( $api_result['openid'] ) || empty( $api_result['session_key'] )) {
                return new WP_Error('error', 'API错误:' . json_encode( $api_result ), array( 'status' => 502 ) );
252 253 254
            }            
            $openId = $api_result['openid']; 
            $sessionKey = $api_result['session_key'];                    
J
jianbo 已提交
255 256 257 258
            // $access_result =decrypt_data($appid, $sessionKey,$encryptedData, $iv, $data);                   
            // if($access_result !=0) {
            //     return new WP_Error( 'error', '解密错误:' . $access_result, array( 'status' => 503 ) );
            // }
259
            $userId=0;           
J
jianbo 已提交
260 261 262 263 264
            // $data = json_decode( $data, true );  
            $nickname=filterEmoji($nickname);         
            $_nickname=base64_encode($nickname);          
		    $_nickname=strlen($_nickname)>49?substr($_nickname,49):$_nickname;
            // $avatarUrl= $data['avatarUrl'];             
265 266 267 268
            if(!username_exists($openId) ) {                
                $new_user_data = apply_filters( 'new_user_data', array(
                    'user_login'    => $openId,
                    'first_name'	=> $nickname ,
J
jianbo 已提交
269 270
                    'nickname'      => $nickname,                    
                    'user_nicename' => $_nickname,
271 272 273 274 275 276 277
                    'display_name'  => $nickname,
                    'user_pass'     => $openId,
                    '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 ) );				
ijianbo's avatar
ijianbo 已提交
278
                }
279 280

                update_user_meta( $userId,'avatar',$avatarUrl);
J
jianbo 已提交
281
                update_user_meta($userId,'usertype',"weixin");
282 283 284 285 286 287 288 289

            }            
            else{
                $user = get_user_by( 'login', $openId);     
                $userdata =array(
                    'ID'            => $user->ID,
                    'first_name'	=> $nickname,
                    'nickname'      => $nickname,
J
jianbo 已提交
290
                    'user_nicename' => $_nickname,
291 292 293 294 295 296
                    '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 ) );
J
jianbo 已提交
297 298 299
                }             
                update_user_meta($userId,'avatar',$avatarUrl);
                update_user_meta($userId,'usertype',"weixin","weixin");
ijianbo's avatar
ijianbo 已提交
300
                
301
                  
ijianbo's avatar
ijianbo 已提交
302
            }
303 304
            $userLevel= getUserLevel($userId);
            $result["code"]="success";
J
jianbo 已提交
305
            
306 307 308 309 310 311 312
            $result["message"]= "获取用户信息成功";
            $result["status"]="200";
            $result["openid"]=$openId;
            $result["userLevel"]=$userLevel;            
            $response = rest_ensure_response($result);
            return $response; 
        }  
ijianbo's avatar
ijianbo 已提交
313 314
    }

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    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; 
        }  
    }

ijianbo's avatar
ijianbo 已提交
403 404 405 406 407 408 409 410
    function sendmessage($request)
    {
      $openid= $request['openid'];
      $template_id=$request['template_id'];
      $postid=$request['postid'];
      $form_id=$request['form_id'];
      $total_fee=$request['total_fee'];
      $flag=$request['flag'];
411
      $fromUser =$request['fromUser'];
ijianbo's avatar
ijianbo 已提交
412
      $parent=0;
413 414 415
      if (isset($request['parent'])) {
          $parent =(int)$request['parent'];
      }
ijianbo's avatar
ijianbo 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

        $appid = get_option('wf_appid');
        $appsecret = get_option('wf_secret');
        $page='';
        if($flag =='1'  || $flag=='2' )
        {
            $total_fee= $total_fee.'元';
        }

        
        if($flag=='1' || $flag=='3' )
        {
            $page='pages/detail/detail?id='.$postid;

        }
        elseif($flag=='2')
        {
            $page='pages/about/about';
        }

        if(empty($appid) || empty($appsecret) )
        {
                $result["code"]="success";
                $result["message"]= "appid  or  appsecret is  empty";
                $result["status"]="500";                   
                return $result;
        }
        else
        {
        
            $access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
            $access_result = https_request($access_url);
            if($access_result !="ERROR")
            {
                $access_array = json_decode($access_result,true);
                if(empty($access_array['errcode']))
                {
                    $access_token = $access_array['access_token']; 
                    $expires_in = $access_array['expires_in'];
                    $data = array();
                    $data1 = array(
                            "keyword1"=>array(
                            "value"=>$total_fee,                     
                             "color" =>"#173177"
                            ),
                            "keyword2"=>array(
                                "value"=>'谢谢你的赞赏,你的支持,是我前进的动力.',
                                "color"=> "#173177"
                            )
                        );  

                     date_default_timezone_set('PRC');
                     $datetime =date('Y-m-d H:i:s');
                     $data2 = array(
                            "keyword1"=>array(
                            "value"=>$fromUser,                     
                             "color" =>"#173177"
                            ),
                            "keyword2"=>array(
                                "value"=>$total_fee,
                                "color"=> "#173177"
                            ),
                            "keyword3"=>array(
                                "value"=>$datetime,
                                "color"=> "#173177"
                            )
                        );  


                    if($flag=='1' || $flag=='2' )
                    {
                        
                       $postdata['data']=$data1;

                    }
                    elseif ($flag=='3') {
                       
                        $postdata['data']=$data2;
                        
                    }

                    $postdata['touser']=$openid;
                    $postdata['template_id']=$template_id;
                    $postdata['page']=$page;
                    $postdata['form_id']=$form_id;
                    $postdata['template_id']=$template_id;
                    

                    $url ="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token;

                    $access_result = $this->https_curl_post($url,$postdata,'json');

                    if($access_result !="ERROR"){
                        $access_array = json_decode($access_result,true);
                        if($access_array['errcode'] =='0')
                        {

                            
                            if($parent  !=0)
                            {
                                $delFlag=delete_comment_meta($parent,"formId",$form_id);
                                if($delFlag)
                                {
                                  $result["message"]= "发送消息成功(formId删除)";  
                                }
                                else
                                {
                                   $result["message"]= "发送消息成功(formId删除失败)"; 
                                }
                                
                            }
                            else
                            {
                                $result["message"]= "模板消息发送成功";
                            }
                            $result["code"]="success";
                            $result["status"]="200";                   
                            

                        }
                        else

                        {
                            $result["code"]=$access_array['errcode'];
                            $result["message"]= $access_array['errmsg'];
                            $result["status"]="500";
                            return $result;
                        }

                        
                    }
                    else{
                        $result["code"]="success";
                        $result["message"]= "https请求失败";
                        $result["status"]="500";                   
                        return $result;
                    }
                }               
                else
                {
                
                    $result["code"]=$access_array['errcode'];
                    $result["message"]= $access_array['errmsg'];
                    $result["status"]="500";
                    return $result;
                
                }
                
            }
            else
            {
                    $result["code"]="success";
                    $result["message"]= "https请求失败";
                    $result["status"]="500";
                    return $result;
            }
            
            
        }

        $response = rest_ensure_response( $result);
        return $response;


    }

    function https_curl_post($url,$data,$type){
        if($type=='json'){
            //$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
            $data=json_encode($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
        $data = curl_exec($curl);
        if (curl_errno($curl)){
            return 'ERROR';
        }
        curl_close($curl);
        return $data;
    }
 

    function getWinxinQrcodeImg($request)
    {
        $postid= $request['postid'];      
        $path=$request['path'];
        $openid =$request['openid']; 

        $qrcodeName = 'qrcode-'.$postid.'.png';//文章小程序二维码文件名     
        $qrcodeurl = REST_API_TO_MINIPROGRAM_PLUGIN_DIR.'qrcode/'.$qrcodeName;//文章小程序二维码路径
J
jianbo 已提交
615
        $qrcodeimgUrl = plugins_url().'/'.REST_API_TO_MINIPROGRAM_PLUGIN_NAME.'/qrcode/'.$qrcodeName;
ijianbo's avatar
ijianbo 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
        //自定义参数区域,可自行设置      
        $appid = get_option('wf_appid');
        $appsecret = get_option('wf_secret');
       
        //判断文章小程序二维码是否存在,如不存在,在此生成并保存
        if(!is_file($qrcodeurl)) {
            //$ACCESS_TOKEN = getAccessToken($appid,$appsecret,$access_token);
            $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
             $access_token_result = https_request($access_token_url);
             if($access_token_result !="ERROR")
              {
                $access_token_array= json_decode($access_token_result,true);
                if(empty($access_token_array['errcode']))
                {
                  $access_token =$access_token_array['access_token'];
                  if(!empty($access_token))
                  {

                    //接口A小程序码,总数10万个(永久有效,扫码进入path对应的动态页面)
                    $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
                    //接口B小程序码,不限制数量(永久有效,将统一打开首页,可根据scene跟踪推广人员或场景)
                    //$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN;
                    //接口C小程序二维码,总数10万个(永久有效,扫码进入path对应的动态页面)
                    //$url = 'http://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='.$ACCESS_TOKEN;

                    //header('content-type:image/png');
                    $color = array(
                        "r" => "0",  //这个颜色码自己到Photoshop里设
                        "g" => "0",  //这个颜色码自己到Photoshop里设
                        "b" => "0",  //这个颜色码自己到Photoshop里设
                    );
                    $data = array(
                        //$data['scene'] = "scene";//自定义信息,可以填写诸如识别用户身份的字段,注意用中文时的情况
                        //$data['page'] = "pages/index/index";//扫码后对应的path,只能是固定页面
                        'path' => $path, //前端传过来的页面path
                        'width' => intval(100), //设置二维码尺寸
                        'auto_color' => false,
                        'line_color' => $color,
                    );
                    $data = json_encode($data);
                    //可在此处添加或者减少来自前端的字段
                    $QRCode = get_content_post($url,$data);//小程序二维码
                    if($QRCode !='error')
                    {
                      //输出二维码
                      file_put_contents($qrcodeurl,$QRCode);
                      //imagedestroy($QRCode);
                      $flag=true;
                    }
                    
                  }
                  else
                  {
                    $flag=false;
                  }

                }
                else
                {
                  $flag=false;
                }

              }
              else
              {
                $flag=false;
              }
            
        }
        else
        {

          $flag=true;
        }

        if($flag)
        {
          $result["code"]="success";
J
jianbo 已提交
694 695
            $result["message"]= "小程序码创建成功";
            $result["qrcodeimgUrl"]=$qrcodeimgUrl; 
ijianbo's avatar
ijianbo 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
            $result["status"]="200"; 
            

        }
        else {
            $result["code"]="success";
            $result["message"]= "小程序码创建失败"; 
            $result["status"]="500"; 
            
        } 

        $response = rest_ensure_response( $result);
        return $response;
      
    }

    function send_message_permissions_check($request)
    {
      $openid= $request['openid'];
      $template_id=$request['template_id'];
      $postid=$request['postid'];
      $form_id=$request['form_id'];
      $total_fee=$request['total_fee'];
      $flag=$request['flag'];
720 721
      $fromUser =$request['fromUser'];
      //$parent=(int)$request['parent'];
ijianbo's avatar
ijianbo 已提交
722

723
      
ijianbo's avatar
ijianbo 已提交
724

725
      if(empty($openid)  || empty($template_id) || empty($postid) || empty($form_id) || empty($total_fee) || empty($flag) || empty($fromUser))
ijianbo's avatar
ijianbo 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739
      {
          return new WP_Error( 'error', '参数错误', array( 'status' => 500 ) );
      }
      else if(!function_exists('curl_init')) {
          return new WP_Error( 'error', 'php curl 
            扩展没有启用', array( 'status' => 500 ) );
      }
      return true;      
      
    }

    function get_qrcodeimg_permissions_check($request)
    {
        $postid= $request['postid'];      
740
        $path=$request['path'];        
ijianbo's avatar
ijianbo 已提交
741 742 743 744 745 746 747 748 749

        if(empty($postid)  || empty($path))
        {
            return new WP_Error( 'error', '参数错误', array( 'status' => 500 ) );
        }
        else if(get_post($postid)==null)
        {
             return new WP_Error( 'error', 'postId参数错误', array( 'status' => 500 ) );
        }
750 751 752 753 754 755 756 757
        return true;
    }
    function  get_userInfo_permissions_check($request)
    {
        return true;
    }
    function  update_userInfo_permissions_check($request)
    {
ijianbo's avatar
ijianbo 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
        return true;
    }

    function get_openid_permissions_check($request)
    {
      $js_code= $request['js_code'];
      $encryptedData=$request['encryptedData'];
      $iv=$request['iv'];
      $avatarUrl=$request['avatarUrl'];
      $nickname=empty($request['nickname'])?'':$request['nickname'];
      if(empty($js_code))
      {
          return new WP_Error( 'error', 'js_code是空值', array( 'status' => 500 ) );
      }
      else if(!function_exists('curl_init')) {
          return new WP_Error( 'error', 'php  curl扩展没有启用', array( 'status' => 500 ) );
      }

      return true;
    }


}