A little self-summary of the WeChat public account development, I am afraid that I will forget it later, so I summarize it here.

Q: Hero, may I ask, if the custom menu of the official account has been authorized to a third party, how to deal with the authorization to obtain the code?

 

A: The same official account can only have one control background, the official account background: development -> basic configuration -> server configuration -> server address, only one address can be filled here!

There are two ways to customize the menu of the official account. One is directly on the left side of the background. According to the interface provided by WeChat, you can edit it yourself, which is suitable for some static connection and display. It is taken over by the background program in the previous server configuration. If there is already a third-party address here, unless you modify their code, let the menu in their code jump to the address and go to our background program.

 

How to carry the public account visitor's information (openid) from the official account custom menu into your own background (this is OAuth authentication), and exchange access_token through oauth_token, this is the second difficulty in WeChat development, the first difficulty is how to To connect the official account with our own background, the third difficulty is the 9 major interfaces of WeChat, which need to be developed with jssdk. Generally, the background development of the official account only requires PHP, but jssdk also needs to learn js. usage.

 

Let’s go too far, let’s continue to talk about the problem of getting code for OAuth authentication, assuming that the jump link on the custom menu is https://www.abc.com/wx/index.php,

 

The content of this index.php is as follows:

<?php

    include_once("wx_appidpwd.php");
    
    // Write the configuration path into the wx_appidpwd.php file, which is referenced here to facilitate unified modification of the deployment 
    header ('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='. $ appid .'&redirect_uri='. $myappurl .'oauth.php&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect' );
    
?>

 

$myappurl is the path to jump again, for example $myappurl ='https://yoooko.s1.natapp.cc/wx/'; spliced ​​together is redirect_uri=https://yoooko.si1.natapp.cc/wx /oauth.php

 

The content of oauth.php is as follows:

<?php
    $code = $_GET['code'];
    $state = $_GET['state'];

    // $appid is defined in $appsecret in wx_appidpwd.php file 
    include_once ("wx_appidpwd.php" );
    
    if ( empty ( $code )) $this ->error('authorization failed' );

    //获取oauth_token
    $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
    $token = json_decode(file_get_contents($token_url));
    if (isset($token->errcode)) {
        echo '<h1>错误:</h1>'.$token->errcode;
        echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
        exit;
    }

    // Get access_token through oauth_token. Note: This access_token is different from the basic access_token, so it must be obtained again this time, instead of being obtained from the cache 
    $access_token_url = 'https://api.weixin.qq.com/sns/ oauth2/refresh_token?appid='. $appid .'&grant_type=refresh_token&refresh_token='. $token -> refresh_token;
     // Convert to object 
    $access_token = json_decode( file_get_contents ( $access_token_url ));
     if ( isset ( $access_token -> errcode )) {
         echo '<h1>Error:</h1>'. $access_token -> errcode;
         echo '<br/><h2>Error message: </h2>'.$access_token->errmsg;
        exit;
    }

    // Get userinfo through access_token 
    $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='. $access_token ->access_token.'&openid='. $access_token ->openid.'&lang =zh_CN' ;
     // Convert to object 
    $user_info = json_decode( file_get_contents ( $user_info_url ));
     if ( isset ( $user_info -> errcode)) {
         echo '<h1>Error:</h1>'. $user_info -> errcode;
         echo '<br/><h2>Error information:</h2>'. $user_info ->errmsg;
        exit;
    }

    $user_openid = $user_info->openid;
    //ok echo $openid;
    
    //print user info 
    /*
    echo '<pre>';
    print_r($user_info);
    echo '</pre>';
    */ 

  //Other business logic code, such as login, etc. . .
?>


In this way, we have obtained the information of the official account visitor, including all kinds of information such as openid, and we can do the business logic such as login later.

 

Two years ago, I developed WeChat public account, but I felt that I was too restricted by WeChat. Then I learned about the cross-platform H5, so I went to develop the app of H5+. After a year and a half, js became my own. The main working language has completed two small products, a mobile version of the invoicing software, and an ordering platform for the invoicing software. The advantage of cross-platform is that as long as you write the code once, both Android and Apple can use it, and the mobile phone Basically, all kinds of functions can be called without the restriction of WeChat. For example, if a notification message is required, the subscription account and service account of WeChat public account cannot be implemented, and an enterprise account must be used. App development requires a third-party account. The platform sends transparent messages to achieve this. If some more in-depth applications, such as using a mobile phone for Bluetooth printing, also need to know some knowledge of Android and iOS native development, and may even develop their own plug-ins.

 

After I finish the two apps in my hand, I will summarize some experiences of H5+ development, just to remember.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325397790&siteId=291194637