From eb01bd5ecda0455efd0e3ea28e18abfc2d3b44dd Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 21 Jan 2019 15:42:23 +0800 Subject: [PATCH] kefu updata --- webapp/controller/KefuController.class.php | 168 ++++++++++++++++----- 1 file changed, 130 insertions(+), 38 deletions(-) diff --git a/webapp/controller/KefuController.class.php b/webapp/controller/KefuController.class.php index 8b64791..eb2324f 100644 --- a/webapp/controller/KefuController.class.php +++ b/webapp/controller/KefuController.class.php @@ -52,7 +52,7 @@ class KefuController { private function checkAccountKefuExist($accountid) { - $conn = $this->getMysql($accountid); + $conn = $this->getMysql(); $select_tbl_name = 'accounts_kefu'; $row = $conn->execQueryOne('SELECT accountid ' . @@ -68,6 +68,43 @@ class KefuController { } } + private function saveAwardTimeToDB($accountid, $time) + { + $conn = $this->getMysql(); + $select_tbl_name = 'accounts_kefu'; + $row = $conn->execScript("UPDATE $select_tbl_name SET " . + ' awardtime=:awardtime ' . + 'WHERE accountid=:accountid; ', + array( + ':accountid' => $accountid, + ':awardtime' => $time + ) + ); + error_log('award_check_row:' . json_encode($row)); + return $row; + + } + + private function getAwardtimeFromDB($accountid) + { + + $conn = $this->getMysql(); + $select_tbl_name = 'accounts_kefu'; + $row = $conn->execQueryOne('SELECT awardtime ' . + "FROM $select_tbl_name WHERE accountid = :accountid ;", + array( + ':accountid' => $accountid, + )); + + error_log('checkAward_row:' . json_encode($row)); + if ($row) { + return $row['awardtime']; + } else { + return ''; + } + + } + private function getAwardConfig($gameid, $condition) { @@ -101,7 +138,6 @@ class KefuController { { $attachments = ''; error_log('config:' . json_encode($config)); - error_log('attachment0:' . $config[0]); foreach ($config as $attachment) { error_log('attachment:'. json_encode($attachment)); $attachments .= $attachment['item_id'] . ':' . $attachment['count'] . ';'; @@ -109,6 +145,19 @@ class KefuController { return $attachments; } + + + private function checkAward($accountid) + { + $awardtime = $this->getAwardtimeFromDB($accountid); + if (isset($awardtime) and time() - $awardtime > 60 * 60 * 24 * 1) { + return true; + }else{ + return false; + } + } + + private function sendAward($accountid, $config) { @@ -118,7 +167,7 @@ class KefuController { $url = 'https://gamemail.kingsome.cn/webapp/index.php?'; } $to = $accountid; - $from = 'from'; + $from = 'kefu'; $mail_type = '1'; $mail_subtype = '0'; $subject = '奖励'; @@ -137,14 +186,14 @@ class KefuController { error_log('sendAward.url:' . $url); $response = ''; - error_log(); + if (!phpcommon\HttpClient::get($url, array(), $response)) { error_log('邮件发送失败'); return; } $data = json_decode($response, true); error_log('邮件返回:' . $response); - if(isset($data) && $data['ErrorCode'] == 0) { + if(isset($data) && $data['errcode'] == 0) { return true; } else{ return; @@ -202,7 +251,7 @@ class KefuController { } } - private function sendMsg() + private function encryptJson() { $pc2 = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID); $encryptMsg = ''; @@ -224,6 +273,48 @@ class KefuController { } + private function sendMsg($data) + { + $postarray = json_encode( $data ,JSON_UNESCAPED_UNICODE); + error_log($postarray); + + //POST发送https请求客服接口api + $access_token = $this->getAccessToken($openid, $accountid); + $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; + error_log('aturl:' . $url); + $response = ''; + if (!phpcommon\HttpClient::post($url, $postarray, $response)) { + phpcommon\sendError(ERR_RETRY, '系统繁忙'); + return; + } + $ret_info = json_decode($response, true); + error_log('sendMsg:' . $response); + if ($ret_info['errcode'] == 0) { + exit ; + } else { + error_log($response); + echo(json_encode(array( + 'errcode' => 0, + 'errmsg' => '' + ))); + + } + } + + public function mytest() + { + error_log('mytest'); + $content = 'hahah'; + $data = array ( + "touser"=> $_REQUEST['openid'], + "msgtype"=>"text", + "text"=> array ("content" => $content) + ); + + $this->sendMsg($data); + } + + private function disposeText($postArr, $accountid) { error_log('text'); @@ -234,16 +325,39 @@ class KefuController { $awardConfig = $this->getAwardConfig($gameid, $test); error_log('awardConfig:' . json_encode($awardConfig)); if (isset($awardConfig)) { - $awardRet = $this->sendAward($accountid, $awardConfig); - if ($awardRet) { - error_log('发送奖励成功!'); + $checkTimeRet = $this->checkAward($accountid); + if ($checkTimeRet) + { + $awardRet = $this->sendAward($accountid, $awardConfig); + if ($awardRet) { + $time = time(); + $save_ret = $this->saveAwardTimeToDB($accountid, $time); + if ($save_ret) { + error_log('save_ret:' . $save_ret); + $content = '奖励领取成功,请查收邮件!'; + $data = array ( + "touser" => $openid, + "msgtype" => "text", + "text" => array ("content" => $content) + ); + $this->sendMsg($data); + } + } + } else { + $content = '一天内只能领取一次奖励!'; + $data = array ( + "touser" => $openid, + "msgtype" => "text", + "text" => array ("content" => $content) + ); + $this->sendMsg($data); } } } private function disposeImage($postArr, $accountid) { - $openid = $postArr['FromUserName']; + $openid = $postArr['FromUserName']; $CreateTime = $postArr['CreateTime']; @@ -253,7 +367,7 @@ class KefuController { { error_log('disposeEvent'); - $openid = $postArr['FromUserName']; + $openid = $postArr['FromUserName']; $CreateTime = $postArr['CreateTime']; if($postArr['Event'] == 'user_enter_tempsession') @@ -273,36 +387,14 @@ class KefuController { ); $this->insertKefuInfoToDB($accountid, $kefu_info); } - $content = '您好,有什么能帮助你? 回复【1】领取奖励。 ' ; + $content = '您好,有什么能帮助你? 回复【111】领取奖励。 ' ; $data = array ( - "touser"=> $openid, - "msgtype"=>"text", - "text"=> array ("content" => $content) + "touser" => $openid, + "msgtype" => "text", + "text" => array ("content" => $content) ); - $postarray = json_encode( $data ,JSON_UNESCAPED_UNICODE); - error_log($postarray); + $this->sendMsg($data); - //POST发送https请求客服接口api - $access_token = $this->getAccessToken($openid, $accountid); - $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; - error_log('aturl:' . $url); - $response = ''; - if (!phpcommon\HttpClient::post($url, $postarray, $response)) { - phpcommon\sendError(ERR_RETRY, '系统繁忙'); - return; - } - $ret_info = json_decode($response, true); - error_log($response); - if ($ret_info['errcode'] == 0) { - echo(json_encode(array( - 'errcode' => 0, - 'errmsg' => '' - ))); - exit ; - } else { - phpcommon\sendError(ERR_RETRY, 'token失败'); - error_log($response); - } } }