weiphp3.0(thinkphp)的调整之路

weiphp是在thinkphp的基础上开发的简洁强大开源的微信公众平台开发框架,其宣称微信功能插件化开发,多公众号管理,配置简单。

但是关于weiphp的文档使用一是内容真不多,二是写的也不详细。

 

说说我的需求吧,weiphp安装到一台独立的php机器上,本机使用nginx+fast-cgi方式代理访问。现在想把我们另一个主站点的域名挂一个location解析到这台weiphp的机器上,因为主站点是https的,所以调试更麻烦了些。

遇到的问题是,

1. 做的微信互动应用需要获取微信用户信息授权,微信公众号的域名已被配置为主域名,不能修改为搭建的weiphp站点的新域名。

2. 我们的配置主域名是https的,需要修改weiphp里的源码适应https访问,加载资源。

调整:

1、nginx配置。

weiphp为独立php环境,机器ip为:10.10.10.1,机器本上已有nginx+fast-cgi在运行,需要大前端nginx配置location代理到这台机器上。

  

upstream weiphp{

          server 10.10.10.1:80;

  }

  location /weiphp {

              proxy_set_header    Host    $host;

              proxy_set_header    X-Real-IP       $remote_addr;

              proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_pass http://weiphp/;

        }

        location /Public {

                proxy_set_header    Host    $host;

              proxy_set_header    X-Real-IP       $remote_addr;

              proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_pass http://weiphp/Public;

        }

        location /Uploads {

                proxy_set_header    Host    $host;

              proxy_set_header    X-Real-IP       $remote_addr;

              proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_pass http://weiphp/Uploads;

        }

        location /Addons {

                proxy_set_header    Host    $host;

              proxy_set_header    X-Real-IP       $remote_addr;

              proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_pass http://weiphp/Addons;

        }

        location ~ \.php$ {

              proxy_set_header    Host    $host;

              proxy_set_header    X-Real-IP       $remote_addr;

              proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

              proxy_pass http://weiphp;

        }

2. 源码调整。

为啥?因为微信站点的域名是https的,必须要weiphp适应为https。但是thinkphp的机制是默认适应http协议。

a,Application/Home/View/default/Public/mobile_head.html

修改微信饮用js地址 http://res.wx.qq.com/open/js/jweixin-1.0.0.js 为 https://res.wx.qq.com/open/js/jweixin-1.0.0.js。

修改zepto引用地址 http://yaotv.qq.com/shake_tv/include/js/lib/zepto.1.1.4.min.js 为 https://yaotv.qq.com/shake_tv/include/js/lib/zepto.1.1.4.min.js。

b,ThinkPHP/ThinkPHP.php

先添加常量

//self define by pautcher

define ('DX_DOMAIN','h5.mydomain.com');

 

将原来SITE_URL的定义取消,修改为如下

//define ( 'SITE_URL', 'http://' . SITE_DOMAIN . __ROOT__ );

if (stristr(SITE_DOMAIN,DX_DOMAIN)!=FALSE) {

        define ( 'SITE_URL', 'https://' . SITE_DOMAIN . __ROOT__ );

}

if (! defined ( 'SITE_URL' )) {

        define ( 'SITE_URL', 'http://' . SITE_DOMAIN . __ROOT__ );

}

 

 

c. ThinkPHP/Common/functions.php

修改U方法,在return $url之前添加如下语句。

if(stristr($url,DX_DOMAIN)!=FALSE){

        $url = str_replace('http:','https:',$url);

    }

 

 

d. ThinkPHP/Library/Think/YunVerify.class.php 

查找 $url = "http://" . self::$ip . ":" . self::$port . $uri;    //生成最后的URL

找到后,在语句后面追加下面一段代码。

 if(stristr($url,DX_DOMAIN)!=false){

                $url = "https://" . self::$ip . ":" . self::$port . $uri;

        }

 

注意,这个文件里有两处代码。

 

e. Application/Home/View/default/Addons/preview.html

修改下方一段script语句为如下,

<script type="text/javascript">
        var preurl = "{$url}";
        if(preurl.indexOf("h5.daxia520.com")>-1 && preurl.indexOf("https")==-1){
                preurl = preurl.replace("http","https");
        }    
        var url = preurl+"&publicid={$public_info.id}";
        $('#frame').attr('src',preurl);
        $('#canvas').qrcode({width:200,height:200,text:url}); 
    </script>

 

f. Public/scene/index.html

在下面这段代码后面添加一段代码:

var PREFIX_URL = "http://"+window.location.host+"/json/";
  var VIEW_URL = "http://"+window.location.host+"/";
  var JSON_URL = "http://"+window.location.host+"/index.php";
  var PREFIX_S1_URL = "http://"+window.location.host+"/";
  var PREFIX_HOST = "http://"+window.location.host+"/";
  var SYS_FILE_HOST = "http://"+window.location.host+"/Uploads/syspic/";
  var PREFIX_FILE_HOST = "http://"+window.location.host+"/userfiles/";
  var PREFIXSYS_FILE_HOST = "http://"+window.location.host+"/Uploads/";
  var USER_FILE_HOST = PREFIX_FILE_HOST;
  var CLIENT_CDN = "http://"+window.location.host+"/Public/css/";

修改后如下所示:

  var DX_DOMAIN = "h5.daxia520.com";
  var PREFIX_URL = "http://"+window.location.host+"/json/";
  var VIEW_URL = "http://"+window.location.host+"/";
  var JSON_URL = "http://"+window.location.host+"/index.php";
  var PREFIX_S1_URL = "http://"+window.location.host+"/";
  var PREFIX_HOST = "http://"+window.location.host+"/";
  var SYS_FILE_HOST = "http://"+window.location.host+"/Uploads/syspic/";
  var PREFIX_FILE_HOST = "http://"+window.location.host+"/userfiles/";
  var PREFIXSYS_FILE_HOST = "http://"+window.location.host+"/Uploads/";
  var USER_FILE_HOST = PREFIX_FILE_HOST;
  var CLIENT_CDN = "http://"+window.location.host+"/Public/css/";
  if(window.location.host==DX_DOMAIN){
    PREFIX_URL = "https://"+window.location.host+"/json/";
    VIEW_URL = "https://"+window.location.host+"/";
    JSON_URL = "https://"+window.location.host+"/index.php";
    PREFIX_S1_URL = "https://"+window.location.host+"/";
    PREFIX_HOST = "https://"+window.location.host+"/";
    SYS_FILE_HOST = "https://"+window.location.host+"/Uploads/syspic/";
    PREFIX_FILE_HOST = "https://"+window.location.host+"/userfiles/";
    PREFIXSYS_FILE_HOST = "https://"+window.location.host+"/Uploads/";
    CLIENT_CDN = "https://"+window.location.host+"/Public/css/";
  }

 

然后,惊奇的发现,新的域名location配置支持了,https的请求也支持了。

posted @ 2015-12-29 15:08  pautcher  阅读(3150)  评论(0编辑  收藏  举报