日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

发模板消息( 自己项目 )

發(fā)布時(shí)間:2023/12/29 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 发模板消息( 自己项目 ) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 在控制器或者model里面調(diào)用model里下面這個(gè)方法

//到賬提醒
? ?public function filmquantity($cardid,$mvnum){
? ? ? ? try {
? ? ? ? ? ? $mMsg=new MmsgModel();
? ? ? ? ? ? $ret=$mMsg->accountnotice($cardid,$mvnum);

? ? ? ? ? ? if ($ret){//生成成功
? ? ? ? ? ? ? ? //調(diào)用后臺(tái)發(fā)送通知
? ? ? ? ? ? ? ? GetData2(Constants::serverHTTP."admin/SendWxTplMsg?id=".$ret);
? ? ? ? ? ? }

? ? ? ? ? ? response($ret, Constants::RESPONESE_SUCCESS, '');
? ? ? ? } catch (Exception $e) {
? ? ? ? ? ? response_catch($e);
? ? ? ? }

? ? }


2. 這個(gè)方法調(diào)用了MmsgModel里的accountnotice方法(這一步在準(zhǔn)備數(shù)據(jù),然后調(diào)用了wxMsg公共方法,在functions.php)

//到賬提醒
? ? public function accountnotice($cardid,$mvnum){
? ? ? ? $res=M('acard')->field('fdcNick')->where(['ID'=>$cardid])->select();
? ? ? ? if($res) {
? ? ? ? ? ? $data = [
? ? ? ? ? ? ? ? 'key1' => '您好!您領(lǐng)取的的電影票已到賬!',//標(biāo)題
? ? ? ? ? ? ? ? 'key2' => $res[0]['fdcNick'],//訂單號(hào)
? ? ? ? ? ? ? ? 'key3' => $mvnum . '張',//訂單類型
? ? ? ? ? ? ? ? 'key4' => date('Y-m-d H:i:s'),//訂單說(shuō)明
? ? ? ? ? ? ? ? 'key5' => '請(qǐng)點(diǎn)擊進(jìn)入預(yù)約界面!當(dāng)前電影票需預(yù)約后才可觀看!',//備注
? ? ? ? ? ? ];
? ? ? ? }
? ? ? ? //模版消息確認(rèn)url 需要全寫 http://yc.wzjo2o.com

? ? ? ? ?$url="http://yc.wzjo2o.com/index/goto?url=".urlencode('mine/gift?shopID=all');
? ? ? ? ?$ret =wxMsg(1102,$cardid,$data,$url);
? ? ? ? ?return $ret;

? ? }


3. wxMsg公共方法先從數(shù)據(jù)庫(kù)取出模板(這個(gè)是對(duì)應(yīng)微信上的格式[寫法在5]手動(dòng)添加到數(shù)據(jù)庫(kù)的), 然后把模板里面固定的字替換成剛剛上方準(zhǔn)備的數(shù)據(jù). 然后回到最外面的(model里的?filmquantity方法 )方法調(diào)用GetData2公共方法(寫法在下方)去調(diào)用后臺(tái)發(fā)送消息模板.

/*
?*? 消息模板
?*/
function wxMsg($type,$cardid,$keys,$url){
? ? $data=M('aformat')->where('id='.$type)->find();????????//取出模板
? ? if($data){
? ? ? ? $format=$data['fdcFormat'];
? ? ? ? //標(biāo)題&訂單號(hào)&訂單類型&訂單說(shuō)明&備注
? ? ? ? //電影票發(fā)放確認(rèn)消息
? ? ? ? if($type==1101) {
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[訂單號(hào)]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[訂單類型]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[訂單說(shuō)明]', $keys['key4'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key5'], $format);
? ? ? ? }
? ? ? ? //標(biāo)題&賬戶名&數(shù)量&時(shí)間&備注
? ? ? ? //電影票到賬確認(rèn)消息
? ? ? ? else if($type==1102) {
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[賬戶名]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[數(shù)量]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[時(shí)間]', $keys['key4'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key5'], $format);
? ? ? ? }
? ? ? ? else if($type==1103) {
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[消費(fèi)項(xiàng)目]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[消費(fèi)時(shí)間]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key4'], $format);
? ? ? ? }
? ? ? ? else if($type==1104) {
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[活動(dòng)]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[獎(jiǎng)品]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[時(shí)間]', $keys['key4'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key5'], $format);
? ? ? ? }
? ? ? ? else if($type==1105) {
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[到賬時(shí)間]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[到賬金額]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[收益產(chǎn)品]', $keys['key4'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key5'], $format);
? ? ? ? }
? ? ? ? else if ($type==101) {
? ? ? ? ? ? $format = str_replace('[金額]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[時(shí)間]', date('y-m-d H:i:s', time()), $format);
? ? ? ? ? ? // $format = str_replace('[傭金類型]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key2'], $format);
? ? ? ? }else if ($type==102){? ? // 邀請(qǐng)注冊(cè)成功
? ? ? ? ? ? $format = str_replace('[會(huì)員昵稱]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[會(huì)員幸運(yùn)號(hào)]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[時(shí)間]', date('y-m-d H:i:s', time()), $format);
? ? ? ? }else if ($type==103){? ? //收益提醒
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['first'], $format);
? ? ? ? ? ? $format = str_replace('[收益類型]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[收益時(shí)間]', date('y-m-d H:i:s', time()), $format);
? ? ? ? }else if ($type==104){? ? //拼團(tuán)成功
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[名稱]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[價(jià)格]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[人數(shù)]', $keys['key4'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key5'], $format);
? ? ? ? }else if ($type==105){? ? //訂單狀態(tài)變更
? ? ? ? ? ? $format = str_replace('[標(biāo)題]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[訂單編號(hào)]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[訂單狀態(tài)]', $keys['key3'], $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key4'], $format);
? ? ? ? }else if ($type==106){? ? //兌換券使用通知
? ? ? ? ? ? $format = str_replace('[優(yōu)惠標(biāo)題]', '優(yōu)惠標(biāo)題', $format);
? ? ? ? ? ? $format = str_replace('[名稱]', $keys['key1'], $format);
? ? ? ? ? ? $format = str_replace('[編號(hào)]', $keys['key2'], $format);
? ? ? ? ? ? $format = str_replace('[數(shù)量]', '1張', $format);
? ? ? ? ? ? $format = str_replace('[備注]', $keys['key3'], $format);
? ? ? ? }else{
? ? ? ? ? ? throw new Exception('模版消息設(shè)置出錯(cuò)!', 0 );
? ? ? ? }

? ? ? ? $ins=[
? ? ? ? ? ? 'fdbWx'=>1,
? ? ? ? ? ? 'fdlFormatID'=>$type,
? ? ? ? ? ? 'fdcTplID'=>$data['fdcTplID'],
? ? ? ? ? ? 'fdiType'=>$data['fdiType'],
? ? ? ? ? ? 'fdcTitle'=>$data['fdcName'],
? ? ? ? ? ? 'fdlCardID'=>$cardid,
? ? ? ? ? ? 'fdtMsg'=>$format,
? ? ? ? ? ? 'fdcUrl'=>$url,
? ? ? ? ? ? 'created_at'=>date('Y-m-d H:i:s')
? ? ? ? ];
? ? ? ? $ret=M('mmsgdetail')->add($ins);
? ? ? ? return $ret;
? ? }else{
? ? ? ? throw new Exception('模版消息設(shè)置出錯(cuò)!', 0 );
? ? }

}


4.?GetData2公共方法的寫法

function GetData2($url){
? ? $ch = curl_init($url);// 初始化一個(gè) cURL 對(duì)象


? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER,1) ;
? ? curl_setopt($ch, CURLOPT_TIMEOUT,1) ; //設(shè)置為1 異步執(zhí)行
? ? curl_setopt ( $ch, CURLOPT_HEADER, 0);? ? //設(shè)置請(qǐng)求選項(xiàng)


? ? curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;//在啟用 CURLOPT_RETURNTRANSFER 時(shí)候?qū)@取數(shù)據(jù)返回
? ? $output = curl_exec($ch);//執(zhí)行Curl會(huì)話獲取相關(guān)回復(fù)
? ? curl_close($ch);//釋放Curl 句柄, 關(guān)閉Cur會(huì)話
? ? if (empty($output)) { return ;}
? ? $result = json_decode($output,true);
? ? return $result;

}

5.GetData2(Constants::serverHTTP."admin/SendWxTplMsg?id=".$ret);?????? ? //通過(guò)GetData2這個(gè)方法可調(diào)用后臺(tái)的admin控制器里SendWxTplMsg這個(gè)方法,?

//調(diào)用api發(fā)送消息
? ? public function SendWxTplMsgAction()
? ? {
? ? ? ? $get=I('get.');
? ? ? ? $id=$get['id'];
? ? ? ? $msg=new AdminModel();

? ? ? ? $interval=3;//秒鐘?
? ? ? ? $ret=true;
? ? ? ? while($ret){
? ? ? ? ? ? $ret=$msg->SendWxTplMsg($id);?
? ? ? ? ? ? $id=null;?????????????????????????????????????????????????? ? //為了把以前沒(méi)發(fā)的也發(fā)出去
? ? ? ? ? ? if ($ret==false) break;
? ? ? ? ? ? sleep($interval);
? ? ? ? };
? ? ? ? response($ret, Constants::ERROR_CODE, '列表數(shù)據(jù)');

? ? }


6. 在這個(gè)model里面調(diào)用微信插件發(fā)送模板消息

public function SendWxTplMsg($id = "")
? ? {
? ? ? ? try {
? ? ? ? ? ??
? ? ? ? ? ? $where = [
? ? ? ? ? ? ? ? 'fdbWx' => 1,
? ? ? ? ? ? ? ? 'fdiSend' => 0,
? ? ? ? ? ? ];
? ? ? ? ? ? if ($id) {
? ? ? ? ? ? ? ? $where['ID'] = $id;
? ? ? ? ? ? }
? ? ? ? ? ? sleep(8);? ? ? ?//等待8秒,不然數(shù)據(jù)沒(méi)建好
? ? ? ? ? ? $data = M('mmsgdetail')->where($where)->field(['ID', 'fdlCardID', 'fdtMsg', 'fdcTplID', 'fdcUrl'])->find();

? ? ? ? ? ? if ($data) {
? ? ? ? ? ? ? ? M('mmsgdetail')->where(['ID' => $data['ID']])->save(['fdiSend' => 1]);
? ? ? ?????$res = wechat()->tplMsg($data['fdlCardID'], json_decode($data['fdtMsg'], true), $data['fdcTplID'], $data['fdcUrl']);????????? ? //在這里調(diào)用微信插件發(fā)送模板消息
? ? ? ? ? ? ? ? if ($res['errmsg'] != 'ok') {
? ? ? ? ? ? ? ? ? ? M('mmsgdetail')->where(['ID' => $data['ID']])->save(['fdiSend' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $this->MmsgtoBase($data['ID']);
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }


? ? ? ? } catch (Exception $e) {
? ? ? ? ? ? return true;
? ? ? ? ? ? //發(fā)送有問(wèn)題
? ? ? ? ? ? M('mmsgdetail')->where(['ID' => $data['ID']])->save(['fdiSend' => 3, 'updated_at' => date('Y-m-d H:i:s')]);
? ? ? ? }


? ? }



7. 必須在微信上找微信一個(gè)模板消息, 然后在后臺(tái)對(duì)應(yīng)他才有效 , 先登錄公眾號(hào) -> 功能 ->模板消息 ->從模板庫(kù)里面用搜索找 -> 找到合適后點(diǎn)添加 ->然后手動(dòng)添加到下面這張表



建表:

????Schema::create('aformat', function (Blueprint $table) {
? ? ????$table->BigInteger('ID',true,false)->unique('aformat_PK');
? ? ????$table->bigInteger('fdiType')->comment('類型')->default(0);
? ? ????$table->bigInteger('fdbWx')->comment('0:平臺(tái)自身消息? 1:微信消息模板')->default(0);
? ? ????$table->string('fdcName',100)->comment('微信模板消息名稱(對(duì)應(yīng)微信標(biāo)題)')->nullable();
? ? ????$table->string('fdcVar',255)->comment('微信需要替換的內(nèi)容')->nullable();
? ? ????$table->string('fdcFormat',255)->comment('替換模板')->nullable();
? ? ????$table->string('fdcTplID',255)->comment('微信消息模板ID')->nullable();
? ? ????$table->string('fdcWxNo',20)->comment('微信消息模板編號(hào)')->nullable();
? ? ????$table->string('fdcWxMsg',200)->comment('微信消息模板詳細(xì)內(nèi)容')->nullable();
? ? ????$table->timestamps();

????});


????Schema::create('mMsgDetail', function (Blueprint $table) {
? ? ? ? ? ? $table->BigInteger('ID',true,false)->unique('mMsgDetail_PK');
? ? ? ? ? ? $table->boolean('fdbWx')->comment('是否為微信消息')->default(false);?
? ? ? ? ? ? $table->string('fdcTplID',255)->comment('模版ID')->nullable();?
? ? ? ? ? ? $table->integer('fdiType')->comment('消息類型')->default(0);
? ? ? ? ? ? $table->string('fdlCardID',255)->comment('用戶,0、系統(tǒng)消息')->index('mMsgDetail_idx')->nullable();?
? ? ? ? ? ? $table->string('fdcTitle',255)->comment('消息標(biāo)題')->nullable();?
? ? ? ? ? ? $table->mediumText('fdtMsg')->comment('系統(tǒng)消息')->nullable();?
? ? ? ? ? ? $table->integer('fdiSend')->comment('消息類型0:未處理 1:發(fā)送成功 2:發(fā)送失敗')->default(0);
? ? ? ? ? ? $table->boolean('fdbRead')->comment('是否已讀')->default(false);?
? ? ? ? ? ? $table->bigInteger('fdlFormatID')->comment('推廣設(shè)置ID')->default(0);??
? ? ? ? ? ? $table->bigInteger('fdlSrcID')->comment('srcid')->default(0);?
? ? ? ? ? ? $table->string('fdcUrl',255)->comment('連接url')->nullable();?
? ? ? ? ? ? $table->timestamps();
? ? ? ? });

總結(jié)

以上是生活随笔為你收集整理的发模板消息( 自己项目 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。