微信公众号开发10数据库修改

来源:互联网 发布:网络通信工程师考试 编辑:程序博客网 时间:2024/06/09 16:23

上面测试了数据库的显示数据, 这里开始做数据库的修改,首先还是测试能不能添加数据库



接下来访问该方法添加数据到数据库



  插入成功后开始访问显示页面



这里测试完成后就开始用微信公众号添加数据

调用用户访问





这里给出所有源码



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;

}

}

//消息回复

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 获取关注后的动作

$ev=$postObj->Event;

if($ev=="subscribe")

{

$msgType="text";

$contentStr="感谢你的关注百姓堂公众号,只为给您更好更健康的身体而存在!";

$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  发送定位返回附件的酒店

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'测试数据库连接':

//测试调用数据库 显示数据

$sql_articles=new\Model\ArticlesModel();

$articles=$sql_articles->select();

$contentStr='';

foreach($articlesas$article){

$contentStr.=$article['title'].'  ';

}

//编写插入数据, 这里调用

$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'].'创建时间: '.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='用户名不存在,可以创建';

$insert_user=$sql_user->data($data)->add();

if($insert_user){

$contentStr.='--创建成功--';

}else{

$contentStr='创建失败';

}

}else{

$contentStr='用户名已经存在';

}

$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="您有什么健康问题呢?请发送关键词,或者关注公众号: hello kitty 获取更多相关健康问题";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

case'3':

$contentStr="请发送定位信息给我";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

case'5':

$contentStr="发送你的靓照给我,立刻给你算算鸿运,道破吉凶";

$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

欢迎访问百年臻阳方,本公众号由广东泓然堂医药有限公司提供,为您的健康提供全方位保障,点击关注有惊喜.

1  歌曲 张碧晨-年轮

2  咨询健康问题

3  发送定位寻找附件的酒店 指导您来到本店

5  发送相片给我,半仙帮你看相

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'用户名不存在,可以创建';

$insert_user=$sql_user->data($data)->add();

if($insert_user){

echo'创建成功';

}else{

echo'创建失败';

}

}else{

echo'用户名已经存在';

}

$this->display('index');

}

}

还有解决不了的欢迎留言,也可以访问我的微博,知无不言,菜鸟一枚,感谢观看    http://weibo.com/rosekissyou

0 0
原创粉丝点击