Home >Backend Development >PHP Tutorial >(2) Web page authorization to obtain user basic information, obtain basic information_PHP tutorial

(2) Web page authorization to obtain user basic information, obtain basic information_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:311261browse

(2) Web page authorization to obtain basic user information, obtain basic information

During the configuration process of public accounts, many developers will add HTML5 pages to the menu. Sometimes it is necessary to access the user information of the page within the page. In this case, the web page authorization is required to obtain the user’s basic information

PS: The WeChat development described in this blog post is based on the Yii2.0 framework

1. Set authorization callback domain name: Development ---> Interface permissions

Find "Web page authorization to obtain basic user information", click the corresponding "Modify" at the end, and fill in the authorization callback domain name in the response position of the pop-up box. The domain name here does not need to add http:// (For details about the web page authorization callback domain name, please refer to the public platform developer documentation)

2. Obtain authorization

About OAuth2.0 bloggers refer to the blog post of Fangbei Studio http://www.cnblogs.com/txw1958/p/weixin71-oauth20.html (PS: Fangbei is a WeChat development master , the WeChat development content is relatively detailed, recommended reference), which analyzes the relevant content of WeChat official documents in detail, and also provides more detailed ideas and solutions for obtaining authorization.

In fact, the key to obtaining user information is to obtain the user's openid. The blogger wants to realize that the user can automatically authorize the page by clicking on the official account menu, so as to perform database operations on the user, so there are the following two methods:  

(1) Use the custom menu to request authorization page

I will write a separate blog post after the custom menu. Here I will briefly describe authorization through the custom menu. This method requires advanced interface permissions and is limited to users who follow the official account and enter the page directly from the menu. .

<span> 1</span> <span>$menu</span> = '<span>{
</span><span> 2</span> <span>  "button":[
</span><span> 3</span> <span>    {
</span><span> 4</span> <span>      "type": "view",
</span><span> 5</span> <span>      "name": "商城",
</span><span> 6</span> <span>      "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=http://tx.heivr.com/index.php&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
</span><span> 7</span> <span>    },
</span><span> 8</span> 
<span> 9</span> <span>    {
</span><span>10</span> <span>      "name":"快递服务",
</span><span>11</span> <span>      "sub_button":[
</span><span>12</span> <span>        {
</span><span>13</span> <span>           "type":"click",
</span><span>14</span> <span>           "name":"发快递",
</span><span>15</span> <span>           "key":"express"
</span><span>16</span> <span>        },
</span><span>17</span> <span>        {
</span><span>18</span> <span>           "type":"click",
</span><span>19</span> <span>           "name":"快递查询",
</span><span>20</span> <span>           "key":"ww"
</span><span>21</span> <span>        }
</span><span>22</span> <span>      ]
</span><span>23</span> <span>    },
</span><span>24</span> <span>  ]
</span><span>25</span> }';

 For views that require authorization, directly fill in the authorization request address provided by WeChat at the url, where:

  • appid: Fill in the AppID in the basic configuration of the WeChat public platform;
  • redirect_uri: fill in the address of the page that will jump after the authorization is completed, that is, your own html5 page;
  • state: parameters for jumping to the callback page;
  • response_type: two scopes for web page authorization. The official WeChat documentation explains as follows:
    1<span>、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)
    </span>2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。

    Click "Mall" according to this method to receive the returned openid, and then proceed to the next step of obtaining user information.  

  (2) Use JS to automatically request the authorization page

This method is relatively clumsy and the steps are slightly complicated. However, there is currently no simplified method that can solve the needs. In addition, due to page jumps, the time to access the page will increase in most cases, but compared to the previous A method that can obtain basic information about non-following users. Some programs may involve page sharing. The program does not force following, but other users who enter the page directly through sharing also need to record user information. In this case, you can consider this method. (The code bloggers related to WeChat development are encapsulated into tool calls. The used parts are posted here first. After the arrangement is completed, all will be posted with download links)

The idea of ​​this method is: js request link to get code ---> Use code in exchange for openid ---> Get basic user information

a. Edit configuration

In order to facilitate the writing of some WeChat parameters used in a separate class, it is convenient to modify, add and call

<span> 1</span> <?<span>php
</span><span> 2</span> <span>namespace common\tools\wechat;
</span><span> 3</span> 
<span> 4</span> <span>/*</span><span>*
</span><span> 5</span> <span> * 微信请求相关配置类库
</span><span> 6</span>  <span>*/</span>
<span> 7</span> <span>class</span><span> ConfigTool {
</span><span> 8</span> 
<span> 9</span>     <span>/*</span><span>*
</span><span>10</span> <span>     * 微信配置参数
</span><span>11</span> <span>     * @return array 配置参数
</span><span>12</span>      <span>*/</span>
<span>13</span>     <span>public</span> <span>function</span><span> setConfig() {
</span><span>14</span> 
<span>15</span>         <span>//</span><span> 用于验证微信接口配置信息的Token,可以任意填写</span>
<span>16</span>         <span>$config</span>['token'] = '自己的token'<span>;
</span><span>17</span>          
<span>18</span>         <span>//</span><span> appID</span>
<span>19</span>         <span>$config</span>['appid'] = '自己的appid'<span>;
</span><span>20</span>          
<span>21</span>         <span>//</span><span> appSecret</span>
<span>22</span>         <span>$config</span>['secret'] = '自己的secret'<span>;
</span><span>23</span>          
<span>24</span>         <span>//</span><span> 回调链接地址</span>
<span>25</span>         <span>$config</span>['redirect_uri'] = 'http://tx.heivr.com/index.php?'<span>;
</span><span>26</span>          
<span>27</span>         <span>//</span><span> 是否以 HTTPS 安全协议访问接口</span>
<span>28</span>         <span>$config</span>['https_request'] = <span>false</span><span>;
</span><span>29</span>          
<span>30</span>         <span>//</span><span> 授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
</span><span>31</span> <span>        // snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,
</span><span>32</span> <span>        // 即使在未关注的情况下,只要用户授权,也能获取其信息)</span>
<span>33</span>         <span>$config</span>['scope'] = 'snsapi_userinfo'<span>;
</span><span>34</span>          
<span>35</span>         <span>//</span><span> 语言</span>
<span>36</span>         <span>$config</span>['lang'] = 'zh_CN'; <span>//</span><span> zh_CN 简体,zh_TW 繁体,en 英语
</span><span>37</span>          
<span>38</span> <span>        // 微信公众账户授权地址</span>
<span>39</span>         <span>$config</span>['mp_authorize_url'] = 'https://api.weixin.qq.com/cgi-bin/token'<span>;
</span><span>40</span>         <span>//</span><span> 微信公众账户js临时票据地址</span>
<span>41</span>         <span>$config</span>['jsapi_ticket_url'] = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'<span>;
</span><span>42</span>         <span>//</span><span> 授权地址</span>
<span>43</span>         <span>$config</span>['authorize_url'] = 'https://open.weixin.qq.com/connect/oauth2/authorize'<span>;
</span><span>44</span>         <span>//</span><span> 获取access token 的地址</span>
<span>45</span>         <span>$config</span>['access_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/access_token'<span>;
</span><span>46</span>         <span>//</span><span> 刷新 token 的地址</span>
<span>47</span>         <span>$config</span>['refresh_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/refresh_token'<span>;
</span><span>48</span>         <span>//</span><span> 获取用户信息地址</span>
<span>49</span>         <span>$config</span>['userinfo_url'] = 'https://api.weixin.qq.com/sns/userinfo'<span>;
</span><span>50</span>         <span>//</span><span> 验证access token</span>
<span>51</span>         <span>$config</span>['valid_token_url'] = 'https://api.weixin.qq.com/sns/auth'<span>;
</span><span>52</span>         <span>//</span><span> 上传临时素材地址</span>
<span>53</span>         <span>$config</span>['media_temp_upload_url'] = 'https://api.weixin.qq.com/cgi-bin/media/upload?'<span>;
</span><span>54</span>         <span>//</span><span> 上传永久素材地址</span>
<span>55</span>         <span>$config</span>['media_forever_upload_url'] = 'https://api.weixin.qq.com/cgi-bin/material/add_material?'<span>;
</span><span>56</span> 
<span>57</span>         <span>return</span> <span>$config</span><span>;
</span><span>58</span>         
<span>59</span> <span>    }
</span><span>60</span> }

b. https request tool

<span> 1</span> <?<span>php
</span><span> 2</span> <span>namespace common\tools;
</span><span> 3</span> 
<span> 4</span> <span>/*</span><span>*
</span><span> 5</span> <span> * https请求相关类库
</span><span> 6</span>  <span>*/</span>
<span> 7</span> <span>class</span><span> HttpsTool {
</span><span> 8</span> 
<span> 9</span>     <span>const</span> TIMEOUT = 5;                        <span>//</span><span> 设置超时时间</span>
<span>10</span> 
<span>11</span>     <span>private</span> <span>$ch</span>;                                <span>//</span><span> curl对象</span>
<span>12</span> 
<span>13</span>     <span>/*</span><span>*
</span><span>14</span> <span>     * 发送curl请求,并获取请求结果
</span><span>15</span> <span>     * @param string 请求地址
</span><span>16</span> <span>     * @param array 如果是post请求则需要传入请求参数
</span><span>17</span> <span>     * @param string 请求方法,get 或者 post, 默认为get
</span><span>18</span> <span>     * @param bool 是否以https协议请求
</span><span>19</span>      <span>*/</span>
<span>20</span>     <span>public</span> <span>function</span> send_request(<span>$requests</span>, <span>$params</span> = <span>null</span>, <span>$method</span> = 'get', <span>$https</span> = <span>true</span><span>) {
</span><span>21</span>         <span>//</span><span> 以get方式提交</span>
<span>22</span>         <span>if</span> (<span>$method</span> == 'get'<span>) {
</span><span>23</span>             <span>if</span>(<span>$params</span><span>){
</span><span>24</span>                 <span>$request</span> = <span>$requests</span> . <span>$this</span>->create_url(<span>$params</span><span>);
</span><span>25</span>             }<span>else</span><span>{
</span><span>26</span>                 <span>$request</span> = <span>$requests</span><span>;
</span><span>27</span> <span>            }
</span><span>28</span>         }<span>else</span><span>{
</span><span>29</span>             <span>$request</span> = <span>$requests</span><span>;
</span><span>30</span> <span>        }
</span><span>31</span> 
<span>32</span>         <span>$this</span>->ch = curl_init(<span>$request</span><span>);
</span><span>33</span>         curl_setopt(<span>$this</span>->ch, CURLOPT_RETURNTRANSFER, 1);<span>//</span><span> 设置不显示结果,储存入变量</span>
<span>34</span>         curl_setopt(<span>$this</span>->ch, CURLOPT_TIMEOUT, self::TIMEOUT); <span>//</span><span> 设置超时限制防止死循环
</span><span>35</span> 
<span>36</span> <span>        // 判断是否以https方式访问</span>
<span>37</span>         <span>if</span> (<span>$https</span><span>) {
</span><span>38</span>             curl_setopt(<span>$this</span>->ch, CURLOPT_SSL_VERIFYPEER, 0); <span>//</span><span> 对认证证书来源的检查</span>
<span>39</span>             curl_setopt(<span>$this</span>->ch, CURLOPT_SSL_VERIFYHOST, 0); <span>//</span><span> 从证书中检查SSL加密算法是否存在</span>
<span>40</span> <span>        }
</span><span>41</span> 
<span>42</span>         <span>if</span> (<span>$method</span> == 'post') {        <span>//</span><span> 以post方式提交
</span><span>43</span> <span>            //curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false);     //php 5.6文件上传必加内容,5.4不需要</span>
<span>44</span>             curl_setopt(<span>$this</span>->ch, CURLOPT_POST, 1); <span>//</span><span> 发送一个常规的Post请求</span>
<span>45</span>             curl_setopt(<span>$this</span>->ch, CURLOPT_POSTFIELDS, <span>$params</span>); <span>//</span><span> Post提交的数据包</span>
<span>46</span>             curl_setopt(<span>$this</span>->ch, CURLOPT_RETURNTRANSFER, 1<span>);
</span><span>47</span> <span>        }
</span><span>48</span>         
<span>49</span>         <span>$tmpInfo</span> = curl_exec(<span>$this</span>->ch); <span>//</span><span> 执行操作</span>
<span>50</span>         <span>if</span> (curl_errno(<span>$this</span>-><span>ch)) {
</span><span>51</span>             <span>echo</span> 'Errno:'.curl_error(<span>$this</span>->ch);<span>//</span><span>捕抓异常</span>
<span>52</span> <span>        }
</span><span>53</span>         curl_close(<span>$this</span>->ch); <span>//</span><span> 关闭CURL会话
</span><span>54</span> <span>        //var_dump($tmpInfo);exit;</span>
<span>55</span>         <span>return</span> <span>$tmpInfo</span>; <span>//</span><span> 返回数据</span>
<span>56</span> <span>    }
</span><span>57</span> 
<span>58</span>     <span>/*</span><span>*
</span><span>59</span> <span>     * 生成url
</span><span>60</span>      <span>*/</span>
<span>61</span>     <span>public</span> <span>function</span> create_url(<span>$data</span><span>) {
</span><span>62</span>         <span>$temp</span> = '?'<span>;
</span><span>63</span>         <span>foreach</span> (<span>$data</span> <span>as</span> <span>$key</span> => <span>$item</span><span>) {
</span><span>64</span>             <span>$temp</span> = <span>$temp</span> . <span>$key</span> . '=' . <span>$item</span> . '&'<span>;
</span><span>65</span> <span>        }
</span><span>66</span>         <span>return</span> <span>substr</span>(<span>$temp</span>, 0, -1<span>);
</span><span>67</span> <span>    }
</span><span>68</span> }
<span>关于curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false)会在微信图片资源上传博文中详细讲述它出现的心酸史,这里暂时用不到,不做解释</span>

c. Authorization base class

<span> 1</span> <?<span>php  
</span><span> 2</span> <span>namespace common\tools\wechat;
</span><span> 3</span> 
<span> 4</span> <span>use</span><span> common\tools\wechat\ConfigTool;
</span><span> 5</span> <span>use</span><span> common\tools\HttpsTool;
</span><span> 6</span> <span>/*</span><span>*
</span><span> 7</span> <span>* Weixin_oauth 类库
</span><span> 8</span> <span>*/</span>
<span> 9</span> <span>class</span><span> OauthTool {
</span><span>10</span> 
<span>11</span>     <span>public</span> <span>$conf</span><span>;
</span><span>12</span> 
<span>13</span>     <span>public</span> <span>function</span><span> __construct(){
</span><span>14</span>         <span>$re</span> = <span>new</span><span> ConfigTool;               
</span><span>15</span>         <span>$this</span>->conf = <span>$re</span>-><span>setConfig();
</span><span>16</span> <span>    } 
</span><span>17</span> 
<span>18</span>     <span>/*</span><span>*
</span><span>19</span> <span>     * 生成用户授权的地址
</span><span>20</span> <span>     * @param string 自定义需要保持的信息
</span><span>21</span> <span>     * @param sting 请求的路由
</span><span>22</span> <span>     * @param bool 是否是通过公众平台方式认真
</span><span>23</span>      <span>*/</span>
<span>24</span>     <span>public</span> <span>function</span> authorize_addr(<span>$route</span>, <span>$state</span>='', <span>$mp</span>=<span>false</span><span>) {
</span><span>25</span> 
<span>26</span>         <span>if</span> (<span>$mp</span><span>) {
</span><span>27</span>             <span>$data</span> =<span> [
</span><span>28</span>                 'appid' => <span>$this</span>->conf['appid'],
<span>29</span>                 'secret' => <span>$this</span>->conf['token'],
<span>30</span>                 'grant_type' => 'client_credential'
<span>31</span> <span>            ];
</span><span>32</span>             <span>$url</span> = <span>$this</span>->conf['mp_authorize_url'<span>];
</span><span>33</span>         } <span>else</span><span> {
</span><span>34</span>             <span>$data</span> =<span> [
</span><span>35</span>                 'appid' => <span>$this</span>->conf['appid'],                                <span>//</span><span>公众号唯一标识</span>
<span>36</span>                 'redirect_uri' => <span>urlencode</span>(<span>$this</span>->conf['redirect_uri'] . <span>$route</span>),       <span>//</span><span>授权后重定向的回调链接地址</span>
<span>37</span>                 'response_type' => 'code',                                      <span>//</span><span>返回类型,此处填写code</span>
<span>38</span>                 'scope'=><span>$this</span>->conf['scope'],                                  <span>//</span><span>应用授权作用域</span>
<span>39</span>                 'state'=><span>$state</span>,                                                <span>//</span><span>重定向后带上state参数,开发者可以填写任意参数</span>
<span>40</span>                 '#wechat_redirect'=>''                                          <span>//</span><span>直接在微信打开链接,可不填,做页面302重定向时必须带此参数</span>
<span>41</span> <span>            ];
</span><span>42</span>             <span>$url</span> = <span>$this</span>->conf['authorize_url'<span>];
</span><span>43</span> <span>        }
</span><span>44</span>         
<span>45</span>         <span>$send</span> = <span>new</span><span> HttpsTool;
</span><span>46</span>         <span>//</span><span>var_dump($url . $send->create_url($data));exit;</span>
<span>47</span>         <span>return</span>  <span>$url</span> . <span>$send</span>->create_url(<span>$data</span><span>);
</span><span>48</span> <span>    }
</span><span>49</span> 
<span>50</span>     <span>/*</span><span>*
</span><span>51</span> <span>     * 获取 access token
</span><span>52</span> <span>     * @param string 用于换取access token的code,微信提供
</span><span>53</span>      <span>*/</span>
<span>54</span>     <span>public</span> <span>function</span> access_token(<span>$code</span><span>) {
</span><span>55</span> 
<span>56</span>         <span>$data</span> =<span> [
</span><span>57</span>             'appid' => <span>$this</span>->conf['appid'],
<span>58</span>             'secret' => <span>$this</span>->conf['secret'],
<span>59</span>             'code' => <span>$code</span>,
<span>60</span>             'grant_type' => 'authorization_code'
<span>61</span> <span>        ];
</span><span>62</span>         <span>//</span><span> 生成授权url</span>
<span>63</span>         <span>$url</span> = <span>$this</span>->conf['access_token_url'<span>];
</span><span>64</span>         
<span>65</span>         <span>$send</span> = <span>new</span><span> HttpsTool;
</span><span>66</span>         <span>return</span> <span>$send</span>->send_request(<span>$url</span>, <span>$data</span><span>);
</span><span>67</span> <span>    }
</span><span>68</span> 
<span>69</span>     <span>/*</span><span>*
</span><span>70</span> <span>     * 获取用户信息
</span><span>71</span> <span>     * @param string access token
</span><span>72</span> <span>     * @param string 用户的open id
</span><span>73</span>      <span>*/</span>
<span>74</span>     <span>public</span> <span>function</span> userinfo(<span>$token</span>, <span>$openid</span><span>) {
</span><span>75</span> 
<span>76</span>         <span>$data</span> =<span> [
</span><span>77</span>             'access_token' => <span>$token</span>,
<span>78</span>             'openid' => <span>$openid</span>,
<span>79</span>             'lang' => <span>$this</span>->conf['lang'<span>]
</span><span>80</span> <span>        ];
</span><span>81</span>         <span>//</span><span> 生成授权url</span>
<span>82</span>         <span>$url</span> = <span>$this</span>->conf['userinfo_url'<span>];
</span><span>83</span> 
<span>84</span>         <span>$send</span> = <span>new</span><span> HttpsTool;
</span><span>85</span>         <span>return</span> <span>$send</span>->send_request(<span>$url</span>, <span>$data</span><span>);
</span><span>86</span> <span>    }
</span><span>87</span> 
<span>88</span> }

                                        d. Authorization base class call and user data processing (user data is stored or updated before the controller is called)

<span> 1</span> <?<span>php
</span><span> 2</span> <span>namespace wechat\controllers\classes;
</span><span> 3</span> 
<span> 4</span> <span>use</span><span> common\tools\wechat\OauthTool;
</span><span> 5</span> <span>use</span><span> common\models\User;
</span><span> 6</span> <span>use</span><span> common\tools\EmojiTool;
</span><span> 7</span> 
<span> 8</span> <span>/*</span><span>*
</span><span> 9</span> <span> * 微信用户基本信息获取
</span><span>10</span>  <span>*/</span>
<span>11</span> <span>class</span><span> UserinfoClass {
</span><span>12</span>  
<span>13</span>     <span>/*</span><span>*
</span><span>14</span> <span>     * 用户授权并获取code 
</span><span>15</span> <span>     * @return string 用户code
</span><span>16</span>      <span>*/</span>
<span>17</span>     <span>public</span> <span>function</span> getCode(<span>$route</span>, <span>$state</span><span>){
</span><span>18</span> 
<span>19</span>         <span>$re</span> = <span>new</span><span> OauthTool;
</span><span>20</span>         <span>$request</span> = <span>$re</span>->authorize_addr(<span>$route</span>, <span>$state</span><span>);
</span><span>21</span>         <span>$code</span> = <span>isset</span>(<span>$_GET</span>['code']) ? <span>$_GET</span>['code'] : ''<span>;
</span><span>22</span>         
<span>23</span>         <span>return</span> [<span>$request</span>,<span>$code</span><span>];
</span><span>24</span> <span>    }
</span><span>25</span>     
<span>26</span>     <span>/*</span><span>*
</span><span>27</span> <span>     * 获取用户信息并写入数据库(之后加参数传给code)
</span><span>28</span>      <span>*/</span>
<span>29</span>     <span>public</span> <span>function</span> info(<span>$code</span><span>) {
</span><span>30</span>         <span>$re</span> = <span>new</span><span> OauthTool;
</span><span>31</span>         <span>//</span><span>获取access token</span>
<span>32</span>         <span>$access</span> = <span>$re</span>->access_token(<span>$code</span><span>);
</span><span>33</span>         <span>$token</span> = json_decode(<span>$access</span>,<span>true</span><span>);
</span><span>34</span>         <span>//</span><span>header("Content-type: text/html; charset=gbk"); 
</span><span>35</span> <span>        //获取用户信息</span>
<span>36</span>         <span>if</span>(<span>count</span>(<span>$token</span>) != 2<span>) {
</span><span>37</span>             <span>$response</span> = <span>$re</span>->userinfo(<span>$token</span>['access_token'], <span>$token</span>['openid'<span>]);
</span><span>38</span>             <span>$user</span> = json_decode(<span>$response</span>,<span>true</span><span>);
</span><span>39</span>             <span>//</span><span>用户昵称转换
</span><span>40</span> <span>            //$user['nickname'] = EmojiTool::emoji_trans($user['nickname']);</span>
<span>41</span> 
<span>42</span>             <span>if</span>(<span>$model</span> = User::findOne(['openid' => <span>$user</span>['openid'] ])) {            <span>//</span><span>用户已存在更新数据</span>
<span>43</span>                 <span>$model</span>->attributes = <span>$user</span><span>;
</span><span>44</span>                 <span>$model</span>->modify_time = <span>time</span><span>();
</span><span>45</span>                 <span>$model</span>->save(<span>false</span><span>);
</span><span>46</span>             }<span>else</span>{                                                                  <span>//</span><span>用户不存在写入</span>
<span>47</span>                 <span>$model</span> = <span>new</span><span> User;
</span><span>48</span>                 <span>$model</span>->attributes = <span>$user</span><span>;
</span><span>49</span>                 <span>$model</span>->create_time = <span>time</span><span>();
</span><span>50</span>                 <span>$model</span>->save(<span>false</span><span>);
</span><span>51</span> <span>            }
</span><span>52</span> <span>        }
</span><span>53</span>         <span>return</span> <span>isset</span>(<span>$model</span>->id) ? <span>$model</span>->id : ''<span>;
</span><span>54</span> <span>    }
</span><span>55</span>     
<span>56</span> }
                                                                                                                                                                                     

e. Controller call (only one of the methods is posted here)

<span> 1</span> <span>/*</span><span>*
</span><span> 2</span> <span>     * 产品列表
</span><span> 3</span> <span>     * @return object 所有可用产品信息
</span><span> 4</span>      <span>*/</span>
<span> 5</span>     <span>public</span> <span>function</span><span> actionIndex(){
</span><span> 6</span>         <span>//</span><span>判断页面是否自动刷新</span>
<span> 7</span>         <span>if</span>(<span>isset</span>(<span>$_GET</span>['state'<span>])) {
</span><span> 8</span>             <span>$refresh</span> = 0<span>;
</span><span> 9</span>         }<span>else</span><span>{
</span><span>10</span>             <span>$refresh</span> = 1<span>;
</span><span>11</span> <span>        }
</span><span>12</span> 
<span>13</span>         <span>//</span><span>获取用户code</span>
<span>14</span>         <span>$user</span> = <span>new</span><span> UserinfoClass;
</span><span>15</span>         <span>$request</span> = <span>$user</span>->getCode('r=store/index', 1<span>);
</span><span>16</span> 
<span>17</span>         <span>//</span><span>该用户userid</span>
<span>18</span>         <span>$userid</span> = <span>$user</span>->info(<span>$request</span>[1<span>]);
</span><span>19</span> 
<span>20</span>         <span>$model</span> = <span>new</span><span> Product;
</span><span>21</span>         <span>$list</span> = <span>$model</span>->find()->where(['status' => 1])-><span>all();
</span><span>22</span> 
<span>23</span>         <span>return</span> <span>$this</span>->render('index',['list' => <span>$list</span>, 'refresh' => <span>$refresh</span>, 'userid' => <span>$userid</span>, 'request' => <span>$request</span><span>]);
</span><span>24</span>     }
                                                                                                                                                                                                                                                                 

$refresh: Determine whether the page is refreshed. Since the page is opened for the first time without oauth verification, verification is automatically requested to avoid repeated refreshes. Here, the state parameter of the callback is used as the basis for judgment and state=1 (if there is a specific Parameters need to be able to assign the state to the required value);

$request: This is the verification request address

f. View automatically refreshes

Just add the following js code to the view

<span> 1</span> <script type="text/javascript">
<span> 2</span> 
<span> 3</span>     <span>//</span><span>自动请求获取code</span>
<span> 4</span>     $(<span>function</span><span>(){
</span><span> 5</span>         <span>var</span> refresh = <?= <span>$refresh</span>; ?><span>;
</span><span> 6</span>         <span>var</span> request = '<?= $request[0]; ?>'<span>;
</span><span> 7</span>         <span>if</span>(refresh == 1<span>){
</span><span> 8</span>             console.<span>log</span>(1<span>);
</span><span> 9</span>             location =<span> request;
</span><span>10</span> <span>        }
</span><span>11</span> <span>    });
</span><span>12</span> </script>

 

特此声明:相关文章均为查阅资料、阅读大神博文后结合实际开发情况遇到的问题整理而成,能找到原博的必会署名,找不到原博而引用的内容还望原博主海涵

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1089036.htmlTechArticle(二)网页授权获取用户基本信息,获取基本信息 在公众号的配置过程中,许多开发者会在菜单中加入HTML5页面,有时在页面内需要访问页...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn