$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"); } } } }