ThinkPHP微信demo实例
这个是我自己做的,用麦当苗儿的微信类做的~
先说说详细做出来的效果吧:RBAC权限控制,还有就是一些api效果实线:(小黄鸡聊天,快递查询,天气查询,翻译查询,会员模块,优惠信息后台控制)。都是一些很简单的效果!
如图:,
,因为微信只会给你保存信息是5天,公司要用到公众平台,就不得不进行开发了,第一点,就是把用户发送的数据写入数据库。我是直接在苗儿的weixin控制器里面的index方法直接加了一句写入数据库的操作,
我们先不商量这里写入是否合理,反正是可以写入就对了~
然后我的后台就可以接收了:
在这里我说一句,在这里,微信不会给你到微信用户的用户名,他在他的api接口的文档上面说明的是发送方帐号(一个OpenID) ,然后我们继续延续到下一个话题,也就是会员模块,第一:加入企业用户就可以直接获取用户的资料了,第二,用户申请资料的时候也会传入一个发送方帐号(一个OpenID) ,这样我们把之前的表用视图模型关联就可以实线想我的那个图一样的效果,就是假如会员有注册,就是显示用户名注册的用户名,假如没注册就是显示发送方帐号(一个OpenID) ,这个是个取舍的办法,暂时我只想到了这个方式去获到用户名。
这个是我获取到的用户:
然后在微信信息管理里面只要是他们用微信发送消息过来的就是是显示他们的用户名了,不然就还是openid,如果要是有人用别的方式能获取到用户名,希望大湿开光指点一下小弟!
然后就是给大家api接口了,这个应该算个小小的福利吧,只要你有公众帐号就直接可以用,
<?php
/***微信api接口处理函数****/
//天气查询函数
function _getWeather($keyword){
$cityname = trim(substr($keyword,6,strlen($keyword)-6));
$url = "http://api.map.baidu.com/telematics/v2/weather?location={$cityname}&ak=1a3cde429f38434f1811a75e1a90310c";
$fa=file_get_contents($url);
$f=simplexml_load_string($fa);
$city=$f->currentCity;
$da1=$f->results->result[0]->date;
$da2=$f->results->result[1]->date;
$da3=$f->results->result[2]->date;
$w1=$f->results->result[0]->weather;
$w2=$f->results->result[1]->weather;
$w3=$f->results->result[2]->weather;
$p1=$f->results->result[0]->wind;
$p2=$f->results->result[1]->wind;
$p3=$f->results->result[2]->wind;
$q1=$f->results->result[0]->temperature;
$q2=$f->results->result[1]->temperature;
$q3=$f->results->result[2]->temperature;
$d1=$cityname.$da1.$w1.$p1.$q1;
$d2=$cityname.$da2.$w2.$p2.$q2;
$d3=$cityname.$da3.$w3.$p3.$q3;
$msg =<<<str
$d1
$d2
$d3
str;
return $msg;
}
//翻译函数
function _fanyi($keyword){
$keyword = trim(substr($keyword,6,strlen($keyword)-6));
$tranurl="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=9peNkh97N6B9GGj9zBke9tGQ&q={$keyword}&from=auto&to=auto";//百度翻译地址
$transtr=file_get_contents($tranurl);//读入文件
$transon=json_decode($transtr);//json解析
//print_r($transon);
$contentStr = $transon->trans_result[0]->dst;//读取翻译内容
return $contentStr;
}
//快递查询函数
function _getDindan($keyword){
$keyword = trim(substr($keyword,6,strlen($keyword)-6));
$status=array('0'=>'查询出错','1'=>'暂无记录','2'=>'在途中','3'=>'派送中','4'=>'已签收','5'=>'拒收','6'=>'疑难件','7'=>'退回');//构建快递状态数组
$kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$keyword}&id=shentong&show=xml";//快递地址
$kuaidistr=file_get_contents($kuaidiurl);//读入文件
$kuaidiobj=simplexml_load_string($kuaidistr);//xml解析
$kuaidistatus = $kuaidiobj->Status;//获取快递状态
$kuaistr=strval($kuaidistatus);//对象转换为字符串
$contentStr0 =$status[$kuaistr];//根据数组返回
foreach ($kuaidiobj->Data->Order as $a)
{
foreach ($a->Time as $b)
{
foreach ($a->Content as $c)
{$m.="{$b}{$c}";}
}
}
//遍历获取快递时间和事件
$contentStr="你的快递单号{$keyword}{$contentStr0}{$m}";
return $contentStr;
}
//小黄鸡函数
function _xiaohuangji($keyword){
$keyword = trim(substr($keyword,1,strlen($keyword)-1));
$strurl="http://sandbox.api.simsimi.com/request.p?key=e0f1c913-fe3a-40ad-904f-5467677a38b7&lc=ch&text='{$keyword}'";
$fa=file_get_contents($strurl);
$strjson=json_decode($fa);
$contentStr = $strjson->response;
return $contentStr;
}
?>
复制代码
复制代码
用的方法就是
//微信首页方法
public function index(){
/* 加载微信SDK */
import('@.ORG.ThinkWechat');
$weixin = new ThinkWechat('yangyifan');
/* 获取请求信息 */
$data = $weixin->request();
/* 获取回复信息 */
list($content, $type) = $this->reply($data);
M('info')->data($data)->add();
//天气
if(substr($data['Content'],0,6) == '天气'){
$content = _getWeather($data['Content']);
$type = 'text';
//翻译
}else if(substr($data['Content'],0,6) == '翻译'){
$content = _fanyi($data['Content']);
$type = 'text';
//快递
}else if(substr($data['Content'],0,6) == '快递'){
$content = _getDindan($data['Content']);
$type = 'text';
//小黄鸡
}else if(substr($data['Content'],0,1) == '@'){
$content = _xiaohuangji($data['Content']);
$type = 'text';
//帮助
}else{
$content = '';
}
$weixin->response($content, $type);
}
下一篇:程序员必须掌握的职场黑话