| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace common\services;
- use biz\tm\models\TMessage;
- use bizHd\message\classes\TMessageClass;
- use bizHd\wx\classes\WxOpenClass;
- use Yii;
- use common\models\xhTMessage;
- use common\components\wxUtil;
- use common\components\dict;
- class xhTMessageService
- {
- //取所有的模板消息
- public static function getList($sjId)
- {
- $tm = xhTMessage::getAllByCondition(['sjId' => $sjId]);
- $list = [];
- if (!empty($tm)) {
- foreach ($tm as $val) {
- $list[$val['shortTemplateId']] = $val['templateId'];
- }
- }
- return $list;
- }
- public static function add($data)
- {
- $tm = xhTMessage::add($data);
- return $tm;
- }
- public static function batchAdd($data)
- {
- xhTMessage::batchAdd($data);
- }
- public static function delByMerchantId($sjId)
- {
- xhTMessage::deleteByCondition(['sjId' => $sjId]);
- }
- public static function delTMessage($merchant)
- {
- $sjId = $merchant['id'];
- $preTMessage = wxUtil::getAllTMessage($merchant);//删除原有的
- if (!empty($preTMessage)) {
- if (isset($preTMessage['template_list'])) {
- foreach ($preTMessage['template_list'] as $key => $val) {
- $template_id = $val['template_id'];
- wxUtil::delTMessage($merchant, $template_id);
- }
- }
- }
- $message = self::getList($sjId);
- if (!empty($message)) {
- self::delByMerchantId($sjId);
- }
- }
- //商家模板初始化 ssh 2020.2.9
- public static function init($merchant)
- {
- //设置模板消息所属行业:IT科技 互联网/电子商务 消费品 消费品
- wxUtil::setTMIndustry($merchant, 1, 31);
- $sjId = $merchant['id'];
- $original = dict::getDict('tMessage');
- if (empty($original)) {
- return false;
- }
- $preTMessage = wxUtil::getAllTMessage($merchant);//删除原有的
- if (!empty($preTMessage)) {
- if (isset($preTMessage['template_list'])) {
- foreach ($preTMessage['template_list'] as $key => $val) {
- $template_id = $val['template_id'];
- wxUtil::delTMessage($merchant, $template_id);
- }
- }
- }
- $message = self::getList($sjId);
- if (!empty($message)) {
- self::delByMerchantId($sjId);
- }
- $time = time();
- foreach ($original as $key => $val) {//重新添加
- $wxMessage = wxUtil::tMessageCreate($merchant, $key);
- if (isset($wxMessage['errcode']) && $wxMessage['errcode'] == 0) {
- $templateId = $wxMessage['template_id'];
- $data = [
- 'shortTemplateId' => $key,
- 'templateId' => $templateId,
- 'templateName' => $val,
- 'sjId' => $sjId,
- 'addTime' => $time,
- 'createTime' => date("Y-m-d H:i:s"),
- ];
- TMessageClass::add($data);
- }
- }
- xhMerchantExtendService::updateBySjId($sjId, ['wxTMessageInit' => 1]);
- }
- //发送到手机上 ssh 2019.8.11
- public static function sendUrlToMobile($url, $openId, $merchant)
- {
- $sjId = $merchant['id'];
- $allTM = xhTMessageService::getList($sjId);
- $shortTempId = 'OPENTM207430125';
- if (isset($allTM[$shortTempId])) {
- $tempId = $allTM[$shortTempId];
- $data = [
- "touser" => $openId, "template_id" => $tempId, "url" => $url,
- "data" => ["first" => ["value" => '亲,您的链接已经生成了哦', "color" => "#173177"],
- "keyword1" => ["value" => '访问链接', "color" => "#173177"],
- "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
- "remark" => ["value" => "点此直接访问~", "color" => "#173177"]]];
- wxUtil::sendTaskInform($data, $merchant);
- }
- }
- //发送链接到手机上
- public static function sendUrl($url, $name = '访问链接', $merchant, $admin)
- {
- $sjId = $merchant['id'];
- $openId = $admin['openId'];
- $allTM = xhTMessageService::getList($sjId);
- $shortTempId = 'OPENTM207430125';
- if (isset($allTM[$shortTempId])) {
- $tempId = $allTM[$shortTempId];
- $data = [
- "touser" => $openId, "template_id" => $tempId, "url" => $url,
- "data" => ["first" => ["value" => '亲,您的链接已经生成了哦', "color" => "#173177"],
- "keyword1" => ["value" => $name, "color" => "#173177"],
- "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
- "remark" => ["value" => "点此直接访问~", "color" => "#173177"]]];
- wxUtil::sendTaskInform($data, $merchant);
- }
- }
- //补充缺失的模板 ssh 2020.2.9
- public static function supplement($merchant, $isOpen)
- {
- $sjId = $merchant['id'];
- $tMessageList = dict::getDict('tMessage');
- $tMessageList = array_flip($tMessageList);
- $tm = self::getList($sjId);
- $existsTM = [];
- if (!empty($tm)) {
- foreach ($tm as $tmKey => $tmVal) {
- $existsTM[$tmVal] = $tmKey;
- }
- }
- $addData = array_diff($tMessageList, $existsTM);
- if (empty($addData)) {
- return ['code' => 'A0001', 'msg' => '没有模板需要添加'];
- }
- foreach ($addData as $tKey => $tVal) {
- $wxReturn = wxUtil::tMessageCreate($merchant, $tVal, $isOpen);
- if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
- $templateId = $wxReturn['template_id'];
- $data = [
- 'shortTemplateId' => $tVal,
- 'templateId' => $templateId,
- 'templateName' => $tKey,
- 'sjId' => $sjId,
- 'createTime' => date("Y-m-d H:i:s"),
- ];
- self::add($data);
- Yii::warning("add tmessage:" . $tVal . " success\n");
- } else {
- Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
- }
- }
- }
- }
|