上面測試了數(shù)據(jù)庫的顯示數(shù)據(jù), 這里開始做數(shù)據(jù)庫的修改,首先還是測試能不能添加數(shù)據(jù)庫
接下來訪問該方法添加數(shù)據(jù)到數(shù)據(jù)庫
調(diào)用用戶訪問
這里給出所有源碼
namespaceAdmin\Controller;
useModel\ArticlesModel;
useModel\IndexModel;
useThink\Controller;
classIndexControllerextendsController {
//進行微信測試,跳過驗證
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ù)庫 顯示數(shù)據(jù)
$sql_articles=new\Model\ArticlesModel();
$articles=$sql_articles->select();
$contentStr='';
foreach($articlesas$article){
$contentStr.=$article['title'].'? ';
}
//編寫插入數(shù)據(jù), 這里調(diào)用
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
case'顯示用戶':
$sql_user=new\Model\UserModel();
$users=$sql_user->select();
$contentStr='';
foreach($usersas$user){
$contentStr.='用戶名:'.$user['user_name'].'創(chuàng)建時間: '.date('Y-m-d H:i:s',$user['create_time']);
}
$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo$resultStr;
break;
case'插入用戶':
$contentStr='';
$sql_user=new\Model\UserModel();
//插入用戶
$data=array(
'user_name'=>'mock1',
'create_time'=>time()
);
//檢查用戶名是否存在? "? user_name =? $data['user_name'] "
$check_user_name=$sql_user->where(" user_name = '%s'",array($data['user_name']))->select();
if(empty($check_user_name)){
$contentStr='用戶名不存在,可以創(chuàng)建';
$insert_user=$sql_user->data($data)->add();
if($insert_user){
$contentStr.='--創(chuàng)建成功--';
}else{
$contentStr='創(chuàng)建失敗';
}
}else{
$contentStr='用戶名已經(jīng)存在';
}
$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ā)送你的靚照給我,立刻給你算算鴻運,道破吉兇";
$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ī)藥有限公司提供,為您的健康提供全方位保障,點擊關(guān)注有驚喜.
1? 歌曲 張碧晨-年輪
2? 咨詢健康問題
3? 發(fā)送定位尋找附件的酒店 指導您來到本店
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;
}
}
//? 微信驗證
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();
//顯示所有的用戶列表
$this->assign('users',$select_user);
// var_dump($select_user);
$this->display('user');
}
public functionuser_add(){
$sql_user=new\Model\UserModel();
//插入用戶
$data=array(
'user_name'=>'mock1',
'create_time'=>time()
);
//檢查用戶名是否存在? "? user_name =? $data['user_name'] "
$check_user_name=$sql_user->where(" user_name = '%s'",array($data['user_name']))->select();
if(empty($check_user_name)){
echo'用戶名不存在,可以創(chuàng)建';
$insert_user=$sql_user->data($data)->add();
if($insert_user){
echo'創(chuàng)建成功';
}else{
echo'創(chuàng)建失敗';
}
}else{
echo'用戶名已經(jīng)存在';
}
$this->display('index');
}
}
還有解決不了的歡迎留言,也可以訪問我的微博,知無不言,菜鳥一枚,感謝觀看? ? http://weibo.com/rosekissyou