Heim > php教程 > php手册 > (二)网页授权获取用户基本信息,获取基本信息

(二)网页授权获取用户基本信息,获取基本信息

WBOY
Freigeben: 2016-06-13 08:48:14
Original
1258 Leute haben es durchsucht

(二)网页授权获取用户基本信息,获取基本信息

在公众号的配置过程中,许多开发者会在菜单中加入HTML5页面,有时在页面内需要访问页面的用户信息,此时就需要网页授权获取用户基本信息

PS:本博文所阐述的微信开发基于Yii2.0框架

 

1、设置授权回调域名:开发 ---> 接口权限

  找到“网页授权获取用户基本信息”,点击后面对应的“修改”,在弹框响应位置填写授权回调域名即可,此处的域名不需要加http://  (关于网页授权回调域名的说明详情可参考公众平台开发者文档)

 

 

2、获取授权

  关于OAuth2.0博主参考的是方倍工作室的博文http://www.cnblogs.com/txw1958/p/weixin71-oauth20.html(PS:方倍是一个微信开发大神,其中的微信开发内容还是比较详细的,推荐参考),其中详细剖析了微信官方文档的相关内容,也提供了获取授权的更详细思路和方案。

  实际上,获取用户信息的关键在于获取用户的openid。博主想要实现用户点击公众号菜单打开页面即可自动授权,从而针对该用户进行数据库操作,于是有下面两种方式: 

  (1)利用自定义菜单请求授权页面

    自定义菜单后面会单独写一篇博文,在这里先简述一下通过自定义菜单进行授权,该方法需要高级接口权限,且局限于关注公众号的用户直接从菜单进入页面。

<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> }';
Nach dem Login kopieren

  需要授权的view直接在url处填写微信提供的授权请求地址,其中:

  • appid:填写微信公众平台基本配置中的AppID;
  • redirect_uri:填写授权完成后跳转的页面地址,即自己的html5页面;
  • state:跳转至回调页面所带参数;
  • response_type:网页授权的两种scope,微信官方文档中说明如下:
    1<span>、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)
    </span>2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。
    Nach dem Login kopieren

    按照此方法点击“商城”即可接收到返回的openid,继而进行下一步用户信息的获取。 

  (2)利用JS自动请求授权页面

    这个方法相对而言比较笨拙,步骤略复杂,但目前能解决需求还没有研究简化方法,且由于页面的跳转多数情况下访问页面的时间会增加,但相比于前一个方法,该方法可以获取到非关注用户的基本信息。有些程序可能涉及到页面分享,程序没有强制关注但其他用户通过分享直接进入页面也需要记录用户信息,此时可以考虑该方法。(微信开发相关的代码博主封装成工具类调用,这里先贴用到的部分,以后整理完成会全部贴出来并附下载链接)

    该方法的思路为:js请求链接获取code ---> 利用code换取openid ---> 得到用户基本信息

a. 编辑配置

为了方便把用到的一些微信参数单独写入一个类,方便修改添加及调用

<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> }
Nach dem Login kopieren

b. https请求工具

<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> }
Nach dem Login kopieren
<span>关于curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false)会在微信图片资源上传博文中详细讲述它出现的心酸史,这里暂时用不到,不做解释</span>
Nach dem Login kopieren

c. 授权基类

<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> }
Nach dem Login kopieren

d. 授权基类调用及用户数据处理(在控制器调用前,先对用户数据存入或更新)

<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> }
Nach dem Login kopieren

e. 控制器调用(这里只贴其中一个方法)

<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>     }
Nach dem Login kopieren

程序要求用户打开产品列表即获取用户信息并存入数据库,其中设计了几个变量作用如下:

$refresh:判断页面是否刷新,由于首次打开页面未进行oauth验证时才自动请求验证,避免反复刷新,这里用回调的state参数作为判断依据且设state=1(若有特定参数需要可将state赋值为所需值);

$request:即为验证请求地址

f. 视图自动刷新

只需要在视图中添加以下js代码即可

<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>
Nach dem Login kopieren

 

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

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage