還有解決不了的歡迎留言,也可以訪問我的微博,知無不言,菜鳥一枚,感謝觀看? ? http://weibo.com/rosekissyou
1 這里首先在服務(wù)器新建數(shù)據(jù)庫? wechart_official? 采用默認(rèn)的utf8編碼
然后建立測試的數(shù)據(jù)表
這里新建了表?? weae_user? 新建了一條數(shù)據(jù)
user_id (INT10) user_name(varchar60)? createtime (datetime)
2 修改代碼的數(shù)據(jù)庫配置文件?? Common/Conf/config.php
'DB_TYPE'=>'mysql',// 數(shù)據(jù)庫類型
'DB_HOST'=>'127.0.0.1',// 服務(wù)器地址
'DB_NAME'=>'wechart_official',// 數(shù)據(jù)庫名
'DB_USER'=>'root',// 用戶名
'DB_PWD'=>'自己的密碼',// 密碼
'DB_PORT'=>'3306',// 端口
'DB_PREFIX'=>'weae_',// 數(shù)據(jù)庫表前綴
'DB_PARAMS'=>array(),// 數(shù)據(jù)庫連接參數(shù)
'DB_DEBUG'=>TRUE,// 數(shù)據(jù)庫調(diào)試模式 開啟后可以記錄SQL日志
'DB_FIELDS_CACHE'=>true,// 啟用字段緩存 , 在調(diào)試模式下字段緩存和數(shù)據(jù)庫緩存都不起作用
'DB_CHARSET'=>'utf8',// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'DB_DEPLOY_TYPE'=>0,// 數(shù)據(jù)庫部署方式:0 集中式(單一服務(wù)器),1 分布式(主從服務(wù)器)
'DB_RW_SEPARATE'=>false,// 數(shù)據(jù)庫讀寫是否分離 主從式有效
'DB_MASTER_NUM'=>1,// 讀寫分離后 主服務(wù)器數(shù)量
'DB_SLAVE_NO'=>'',// 指定從服務(wù)器序號
/*模板引擎更換
// 布局設(shè)置? 可以更改為smarty引擎
'TMPL_ENGINE_TYPE'? ? ? =>? 'Think',? ? // 默認(rèn)模板引擎 以下設(shè)置僅對使用Think模板引擎有效
*/
// 布局設(shè)置
'TMPL_ENGINE_TYPE'=>'Smarty',// 默認(rèn)模板引擎 以下設(shè)置僅對使用Think模板引擎有效
上面我直接就把模板引擎改成了smarty
3 添加model模塊,在根目錄下面新建Model ,然后分別新建IndexModel.class.php MenuModel.class.php UserModel.class.php
這里我主要使用IndexModel做測試,所以先在這里添加命名空間和繼承父類Model
namespaceModel;
useThink\Model;
classIndexModelextendsModel{
}
接著我們在IndexController里面添加調(diào)用
user Model/IndexModel;
然后就可以開始測試了;
4 接下來開始連接服務(wù)器測試,先在IndexController控制器里面添加一個方法測試服務(wù)器
public functionuser_sql(){
$sql_user=new\Model\UserModel();
$select_user=$sql_user->select();
var_dump($select_user);
$this->display('index');
}
返回如下內(nèi)容
這里也就表示數(shù)據(jù)庫連接是正常的,下面開始在微信公眾號里面使用數(shù)據(jù)庫連接;
這里添加當(dāng)用戶輸入 '測試數(shù)據(jù)庫連接' 的時候調(diào)用數(shù)據(jù)庫,就會顯示博客里面的標(biāo)題
實(shí)現(xiàn)效果如上圖, 源碼在下面直接給出,歡迎吐槽,也歡迎交流,一起共同進(jìn)步,謝謝
namespaceAdmin\Controller;
useModel\ArticlesModel;
useModel\IndexModel;
useThink\Controller;
classIndexControllerextendsController {
//進(jìn)行微信測試,跳過驗(yàn)證
public functionindex(){
//? ? $this->display();
self::responseMsg();
}
public functionvalid()
{
$echoStr=$_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo$echoStr;
exit;
}
}
//消息回復(fù)
public functionresponseMsg()
{
//get post data, May be due to the different environments
$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if(!empty($postStr)){
$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername=$postObj->FromUserName;
$toUsername=$postObj->ToUserName;
$keyword=trim($postObj->Content);
$input_type=$postObj->MsgType;// 分類獲取不同的輸入信息
$loc_x=$postObj->Location_X;
$loc_y=$postObj->Location_Y;
$time=time();
$textTpl="
%s
0
";
// 1 獲取關(guān)注后的動作
$ev=$postObj->Event;
if($ev=="subscribe")
{
$msgType="text";
$contentStr="感謝你的關(guān)注百姓堂公眾號,只為給您更好更健康的身體而存在!";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
}
// 2 分類解析不同類型的輸入信息
// 2.1 文本消息
if($input_type=="image") {
$msgType="text";
$contentStr="掐指一算,你今天缺我";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
}
//1? 發(fā)送定位返回附件的酒店
if($input_type=="location")
{
$textTpl="
%s
4
<![CDATA[你周邊附近的酒店如下]]>
<![CDATA[%s]]>
<![CDATA[%s]]>
<![CDATA[%s]]>
1
";
$url="http://api.map.baidu.com/telematics/v2/local?location={$loc_y},{$loc_x}&keyWord=酒店&number=3&ak=1a3cde429f38434f1811a75e1a90310c";
$fa=file_get_contents($url);
$f=simplexml_load_string($fa);
$d1=$f->poiList->point[0]->name;
$d2=$f->poiList->point[1]->name;
$d3=$f->poiList->point[2]->name;
$w1=$f->poiList->point[0]->address;
$w2=$f->poiList->point[1]->address;
$w3=$f->poiList->point[2]->address;
$p1=$f->poiList->point[0]->telephone;
$p2=$f->poiList->point[1]->telephone;
$p3=$f->poiList->point[2]->telephone;
$q1=$f->poiList->point[0]->distance;
$q2=$f->poiList->point[1]->distance;
$q3=$f->poiList->point[2]->distance;
$m1="{$d1}地址{$w1}電話{$p1}距離{$q1}米";
$m2="{$d2}地址{$w2}電話{$p2}距離{$q2}米";
$m3="{$d3}地址{$w3}電話{$p3}距離{$q3}米";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$m1,$m2,$m3);
echo$resultStr;
}
if(!empty($keyword))
{
$msgType="text";
switch($keyword)
{
case'測試數(shù)據(jù)庫連接':
//測試調(diào)用數(shù)據(jù)庫
$sql_articles=new\Model\ArticlesModel();
$articles=$sql_articles->select();
$contentStr='';
foreach($articlesas$article){
echo$article['title'];
$contentStr.=$article['title'].'? ';
}
//? $contentStr = "請發(fā)送定位信息給我";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
case'1';
$textTpl="
%s
<![CDATA[年輪]]>
0
";
// $contentStr = "請搜索小程序 百姓堂 允許獲取定位來使用該功能";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);
echo$resultStr;
break;
case'2':
$contentStr="您有什么健康問題呢?請發(fā)送關(guān)鍵詞,或者關(guān)注公眾號: hello kitty 獲取更多相關(guān)健康問題";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
case'3':
$contentStr="請發(fā)送定位信息給我";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
case'5':
$contentStr="發(fā)送你的靚照給我,立刻給你算算鴻運(yùn),道破吉兇";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
case"6";
$textTpl="
%s
1
<![CDATA[易企秀]]>
1
";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);
echo$resultStr;
break;
case'7':
$textTpl="
%s
1
<![CDATA[新年賀卡]]>
1
";
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);
echo$resultStr;
break;
default:
$contentStr=<<< MESG
歡迎訪問百年臻陽方,本公眾號由廣東泓然堂醫(yī)藥有限公司提供,為您的健康提供全方位保障,點(diǎn)擊關(guān)注有驚喜.
1? 歌曲 張碧晨-年輪
2? 咨詢健康問題
3? 發(fā)送定位尋找附件的酒店 指導(dǎo)您來到本店
5? 發(fā)送相片給我,半仙幫你看相
6? 易企秀
7? 賀卡制作
MESG;
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
}
//? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//? ? ? ? ? ? ? ? echo $resultStr;
}else{
echo"Input something...";
}
}else{
echo"";
exit;
}
}
//? 微信驗(yàn)證
private functioncheckSignature()
{
$signature=$_GET["signature"];
$timestamp=$_GET["timestamp"];
$nonce=$_GET["nonce"];
$token=TOKEN;
$tmpArr=array($token,$timestamp,$nonce);
sort($tmpArr);
$tmpStr=implode($tmpArr);
$tmpStr=sha1($tmpStr);
if($tmpStr==$signature){
return true;
}else{
return false;
}
}
public functionsql_test(){
$sql_articles=new\Model\ArticlesModel();
$articles=$sql_articles->select();
foreach($articlesas$article){
echo$article['title'];
}
$this->assign('articles',$articles);
//? var_dump($articles);
$this->display('index');
}
public functionuser_sql(){
$sql_user=new\Model\UserModel();
$select_user=$sql_user->select();
var_dump($select_user);
$this->display('index');
}
}
還有解決不了的歡迎留言,也可以訪問我的微博,知無不言,菜鳥一枚,感謝觀看? ? http://weibo.com/rosekissyou