初始化代码
This commit is contained in:
215
application/api/wxapi/Wxtemp.php
Normal file
215
application/api/wxapi/Wxtemp.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
namespace app\api\wxapi;
|
||||
|
||||
use app\api\wxapi\WxClient;
|
||||
use app\api\wxapi\pay\WxPayConfig;
|
||||
use app\api\wxapi\request\AccessTokenRequest;
|
||||
use app\api\wxapi\request\TemplateSendRequest;
|
||||
use think\Db;
|
||||
|
||||
class Wxtemp extends Base{
|
||||
|
||||
public $WxClient='';
|
||||
|
||||
public $error='';
|
||||
|
||||
public function getAccessToken(){
|
||||
$access_token_config=DB::name('config')->where(['name'=>'access_token',])->find();
|
||||
$access_token=$access_token_config['value'];
|
||||
|
||||
/* $appID=DB::name('config')->where('name', 'wx_appid')->value('value');
|
||||
|
||||
if(!strlen($appID)){
|
||||
$this->error='appid不能为空';
|
||||
return false;
|
||||
}
|
||||
|
||||
$appsecret=DB::name('config')->where('name', 'wx_secret')->value('value');
|
||||
if(!strlen($appsecret)){
|
||||
$this->error='appsecret不能为空';
|
||||
return false;
|
||||
}
|
||||
|
||||
$WxClient =new WxClient();
|
||||
$WxClient->appID=$appID;
|
||||
$WxClient->appsecret=$appsecret;*/
|
||||
$WxPayConfig = new WxPayConfig();
|
||||
$WxClient =new WxClient();
|
||||
$WxClient->appID=$WxPayConfig->GetAppId();
|
||||
$WxClient->appsecret=$WxPayConfig->GetAppSecret();
|
||||
|
||||
$this->WxClient=$WxClient;
|
||||
if(time()>$access_token_config['ctime']){
|
||||
$AccessTokenRequest=new AccessTokenRequest();
|
||||
$AccessTokenRequest->setAppId($WxClient->appID);
|
||||
$AccessTokenRequest->setSecret($WxClient->appsecret);
|
||||
|
||||
$WxClient->setTypeCurl(WxClient::TYPE_CURL_GET);
|
||||
$AccessTokenRequest=$WxClient->execute($AccessTokenRequest);
|
||||
$access_token=$AccessTokenRequest->access_token;
|
||||
DB::name('config')->where(['name'=>'access_token',])->update(['value'=>$access_token,'ctime'=>time()+7200,]);
|
||||
}
|
||||
if(!strlen($access_token)){
|
||||
$this->error='access_token不能为空';
|
||||
return false;
|
||||
}
|
||||
return $access_token;
|
||||
}
|
||||
|
||||
public function sendTempMsg($info=[]){
|
||||
$access_token=$this->getAccessToken();
|
||||
if(!$access_token){
|
||||
return false;
|
||||
}
|
||||
$WxClient=$this->WxClient;
|
||||
$tempArray=[
|
||||
'buy_order'=>'450UXsKV5qdrogsAwjb0zyr8hMwxzCKenx2S4UiXq2w',
|
||||
|
||||
];
|
||||
|
||||
if(!isset($info['temp_type']) || !$info['temp_type'] || !in_array($info['temp_type'], array_keys($tempArray))){
|
||||
$this->error='temp_type消息模板配制异常';
|
||||
return false;
|
||||
}
|
||||
$tempid=$tempArray[$info['temp_type']];
|
||||
|
||||
//$url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
|
||||
//$user_info=DB::name('users')->where(['id'=>,])->find();
|
||||
|
||||
if(!isset($info['touser']) || !$info['touser']){
|
||||
$this->error='touser消息接收人不能为空';
|
||||
return false;
|
||||
}
|
||||
if(is_string($info['touser'])){
|
||||
$touser[]=$info['touser'];
|
||||
}else{
|
||||
$touser=$info['touser'];
|
||||
}
|
||||
|
||||
$_TemplateSendRequest=new TemplateSendRequest();
|
||||
foreach($touser as $u){
|
||||
$TemplateSendRequest=clone $_TemplateSendRequest;
|
||||
$TemplateSendRequest->setAccessToken($access_token);
|
||||
$TemplateSendRequest->setToUser($u);
|
||||
$TemplateSendRequest->setTemplateId($tempid);
|
||||
//$TemplateSendRequest->setUrl($url);
|
||||
$TemplateSendRequest->setTopColor('#FF0000');
|
||||
|
||||
switch($info['temp_type']){
|
||||
case 'buy_order':
|
||||
$data=$this->buy_order($info);
|
||||
break;
|
||||
/* case 'fans_temp':
|
||||
$data=$this->fans_temp($info);
|
||||
break;
|
||||
case 'daili_temp':
|
||||
$data=$this->daili_temp($info);
|
||||
break;*/
|
||||
}
|
||||
if(empty($data)){
|
||||
return false;
|
||||
}
|
||||
|
||||
$TemplateSendRequest->setData($data);
|
||||
$WxClient->setTypeCurl(WxClient::TYPE_CURL_JSON);
|
||||
|
||||
$req=$WxClient->execute($TemplateSendRequest);
|
||||
/* print_r($touser);
|
||||
print_r($req);
|
||||
*/ }
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buy_order($info=[]){
|
||||
$title=isset($info['title'])?$info['title']:'';
|
||||
$name=isset($info['name'])?$info['name']:'';
|
||||
$ordersn=isset($info['ordersn'])?$info['ordersn']:'';
|
||||
$content=isset($info['content'])?$info['content']:'';
|
||||
$remark=isset($info['remark'])?$info['remark']:'';
|
||||
/*您已预订成功,请在24小时内完成支付。
|
||||
产品名称:20134-0815
|
||||
订单编号:A39393939331
|
||||
通知内容:预订时间 2015-08-01 15:34
|
||||
如有疑问,请拨打咨询热线*/
|
||||
$data = [
|
||||
'first' => [
|
||||
'value' => $title,
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword1' => [
|
||||
'value' => $content,
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword2' => [
|
||||
'value' => $name.'-'.$ordersn,
|
||||
'color' => '#173177'
|
||||
],
|
||||
|
||||
'remark' => [
|
||||
'value' => $remark,
|
||||
'color' => '#173177'
|
||||
],
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function fans_temp($info=[]){
|
||||
$nick_name=isset($info['nick_name'])?$info['nick_name']:'';
|
||||
$title=isset($info['title'])?$info['title']:'';
|
||||
$remark=isset($info['remark'])?$info['remark']:'';
|
||||
$order_sn=isset($info['order_sn'])?$info['order_sn']:'';
|
||||
$price=isset($info['price'])?$info['price']:'';
|
||||
$pay_type=isset($info['pay_type'])?$info['pay_type']:'';
|
||||
|
||||
$data = [
|
||||
'first' => [
|
||||
'value' => '粉丝【' . $nick_name . '】成功的完成了一笔消费!',
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword1' => [
|
||||
'value' => $price.'元',
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword2' => [
|
||||
'value' => $pay_type,
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword3' => [
|
||||
'value' => $title,
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword4' => [
|
||||
'value' => $order_sn,
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword5' => [
|
||||
'value' => $remark,
|
||||
'color' => '#173177'
|
||||
],
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function daili_temp($info=[]){
|
||||
$nick_name=isset($info['nick_name'])?$info['nick_name']:'';
|
||||
$price=isset($info['price'])?$info['price']:'';
|
||||
$data = [
|
||||
'first' => [
|
||||
'value' => '尊敬的会员【' . $nick_name . '】您好,您有一笔收益到帐提醒!',
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword1' => [
|
||||
'value' => '粉丝收益',
|
||||
'color' => '#173177'
|
||||
],
|
||||
'keyword2' => [
|
||||
'value' => $price.'元',
|
||||
'color' => '#173177'
|
||||
],
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user