转载请说明出处
根据微信文档此文件名称为wx.php
<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use wx\wxBase;
use wx\wechatCallbackapiTest;
class Wx extends Controller
{
protected $weObj;
public function index() {
}
//微信对接地址写到这
public function requestwx() {
$config = Db::name('wxconfig')->where( array("id" => 1) )->find();
$apiUrlPrefix = 'https://api.weixin.qq.com/cgi-bin';
// if( $config['localtoken'] == $ltoken ) {
$this->weObj = new wechatCallbackapiTest($config);
$this->openid = $this->weObj->getValue('FromUserName');
switch($this->weObj->getValue('MsgType')) {
case 'text':
$this->reply();
break;
case 'image'://搞了个人脸识别
$img = $this->weObj->getValue('PicUrl');
$params['image_url'] = $img;
$facecon = Db::name('face')->where('id',1)->find();
$api_key = $facecon['appkey'];
$api_secret = $facecon['appsecret'];
$server = "https://api-cn.faceplusplus.com/facepp/v3/detect";
$params['api_key'] = $api_key;
$params['api_secret'] = $api_secret;
$params['return_attributes'] = 'gender,age,headpose,blur,emotion,beauty,eyegaze,skinstatus';
$result = $this->http_post($server,$params);
Db::name('log')->insert(['admin_name'=>$result]);
if($result){
$info = json_decode($result,true);
Db::name('log')->insert(['admin_name'=>$info]);
//性别//颜值
if($info['faces']['0']['attributes']['gender'] == 'Male'){
$sex = '男';
$beauty = $info['faces']['0']['attributes']['beauty']['female_score'];
}else{
$sex = '女';
$beauty = $info['faces']['0']['attributes']['beauty']['male_score'];
}
$age = $info['faces']['0']['attributes']['age']['value'];
//健康
$health = $info['faces']['0']['attributes']['skinstatus']['health'];
//色斑
$stain = $info['faces']['0']['attributes']['skinstatus']['stain'];
//青舂痘
$acne = $info['faces']['0']['attributes']['skinstatus']['acne'];
//黑眼圈
$dark_circle = $info['faces']['0']['attributes']['skinstatus']['dark_circle'];
//心情
$emotion['sadness'] = $info['faces']['0']['attributes']['emotion']['sadness'];
$emotion['neutral'] = $info['faces']['0']['attributes']['emotion']['neutral'];
$emotion['disgust'] = $info['faces']['0']['attributes']['emotion']['disgust'];
$emotion['anger'] = $info['faces']['0']['attributes']['emotion']['anger'];
$emotion['surprise'] = $info['faces']['0']['attributes']['emotion']['surprise'];
$emotion['fear'] = $info['faces']['0']['attributes']['emotion']['fear'];
$emotion['happiness'] = $info['faces']['0']['attributes']['emotion']['happiness'];
$key = array_search(max($emotion),$emotion);
if($key == 'anger'){
$xinqing = '愤怒';
}else if($key == 'disgust'){
$xinqing = 'disgust';
}else if($key == 'fear'){
$xinqing = '恐惧';
}else if($key == 'happiness'){
$xinqing = '高兴';
}else if($key == 'neutral'){
$xinqing = '平静';
}else if($key == 'sadness'){
$xinqing = '伤心';
}else if($key == 'surprise'){
$xinqing = '惊讶';
}
// $data = array(
// '性别:' => $sex,
// '年龄:' => $age,
// '颜值:' => $beauty,
// '健康:' => $health,
// '色斑:' => $stain,
// '青舂痘:' => $acne,
// '黑眼圈' => $dark_circle
// );
$aa = "性别:".$sex."\n"."年龄:".$age."\n"."颜值:".$beauty."\n"."健康:".$health."\n"."色斑:".$stain."\n"."青舂痘:".$acne."\n"."黑眼圈:".$dark_circle."\n"."心情:".$xinqing;
$this->weObj->text($aa);
}
break;
case 'voice':
$this->reply();
break;
case 'event':
switch ($this->weObj->getValue('Event')) {
case "subscribe": // 关注
$EventKey = $this->weObj->getValue('EventKey'); //二维码参数值
if(!empty($EventKey)) { //扫描带参数二维码,进行关注后的事件推送
} else {
//关注判断是否存在此用户,不存在插入
$openid = $this->openid;
$info = Db::name('wxuser')->where('openid',$openid)->find();
if(empty($info)) {
$data = array();
if($user = $this->weObj->getUserinfo()) {
$data = array(
'headimg' => $user['headimgurl'],
'nickname' => $user['nickname'],
'sex' => $user['sex'],
'city' => $user['city'],
'country' => $user['country'],
'province' => $user['province'],
'openid' => $openid,
'is_follow' => 1,
);
Db::name('wxuser')->insert($data);
$info['userid'] = Db::name('wxuser')->getLastInsID();
}
} else {
if($info['is_follow'] == 0) {
Db::name('wxuser')->where('userid',$info['userid'])->update(['is_follow' => 1]);
}
}
//若果后台设置回复关键字请在数据库查找
Db::name('Log')->insert(['admin_id'=>1]);
$this->reply('关注');
}
break;
case "unsubscribe": //取消关注
//业务处理...
Db::name('wxuser')->where('userid',$info['userid'])->update(['is_follow' => 0]);
break;
}
default:
echo '';
break;
}
// } else {
// echo '';
// }
}
//关键词回复
private function reply($keyword='') {
$keyword = $keyword == '' ? $this->weObj->getValue('Content') : $keyword;
if($replay = Db::name("wxkeyreply")->where(array("keyword"=>$keyword))->select()) {
$type = $replay[0]['type'];
if($type == 1) { //文本
$this->weObj->text(stripslashes(htmlspecialchars_decode($replay[0]['descs'])));
} elseif($type == 2) { // 图文
$newsArr = array();
$appUrl = 'http://' . $_SERVER['HTTP_HOST'] .'/uploads/images/';
foreach ($replay as $v) {
$newsArr[] = array(
'Title' => $v["title"],
'Description' => $v["sm"],
'PicUrl' => $appUrl . $v["pic"],
'Url' => $v["url"]
);
}
$this->weObj->news($newsArr);
}
}
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
protected function http_post($url,$param,$post_file=false){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach($param as $key=>$val){
$aPOST[] = $key."=".urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST,true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
}