$merchantId]); $list = []; if (!empty($tm)) { foreach ($tm as $val) { $list[$val['shortTemplateId']] = $val['templateId']; } } return $list; } public static function add($data) { $tm = xhTMessage::add($data); $merchantId = isset($tm['merchantId']) ? $tm['merchantId'] : 0; self::refresh($merchantId); return $tm; } public static function batch_add($data, $merchantId) { xhTMessage::batchAdd($data); self::refresh($merchantId); } public static function delByMerchantId($merchantId) { xhTMessage::deleteByCondition(['merchantId' => $merchantId]); self::refresh($merchantId); } public static function refresh($merchantId) { $cacheKey = configDict::getCacheKey('tMessage') . $merchantId; Yii::$app->redis->executeCommand('DEL', [$cacheKey]); $tm = xhTMessage::getAllByCondition(['merchantId' => $merchantId]); $list = []; if (!empty($tm)) { foreach ($tm as $val) { $list[$val['shortTemplateId']] = $val['templateId']; } } $sring = json_encode($list); Yii::$app->redis->executeCommand('SET', [$cacheKey, $sring]); } /** * 初始化 商家 的模板消息 */ public static function init($merchant) { $merchantId = $merchant['id']; weixinUtil::setTMIndustry($merchant, 1, 31);//设置模板消息所属行业 IT科技 互联网/电子商务 消费品 消费品 $original = configDict::getConfig('tMessage'); if (!empty($original)) { $preTMessage = weixinUtil::getAllTMessage($merchant);//删除原有的 if (!empty($preTMessage)) { if (isset($preTMessage['template_list'])) { foreach ($preTMessage['template_list'] as $key => $val) { $template_id = $val['template_id']; weixinUtil::delTMessage($merchant, $template_id); } } } $message = self::getList($merchantId); if (!empty($message)) { self::delByMerchantId($merchantId); } $batchData = []; foreach ($original as $key => $val) {//重新添加 $wxMessage = weixinUtil::tMessageCreate($merchant, $key); if (isset($wxMessage['errcode']) && $wxMessage['errcode'] == 0) { $templateId = $wxMessage['template_id']; $data = [ 'shortTemplateId' => $key, 'templateId' => $templateId, 'templateName' => $val, 'merchantId' => $merchantId, 'createTime' => date("Y-m-d H:i:s"), ]; $batchData[] = $data; } } self::batch_add($batchData, $merchantId); } xhMerchantExtendService::updateByMerchantId($merchantId, ['wxTMessageInit' => 1]);//完成初始化 } //发送到手机上 shish 2019.8.11 public static function sendUrlToMobile($url, $openId, $merchant) { $merchantId = $merchant['id']; $allTM = xhTMessageService::getList($merchantId); $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"]]]; weixinUtil::sendTaskInform($data, $merchant); } } /** * 发送链接到手机上 */ public static function sendUrl($url, $name = '访问链接', $merchant, $admin) { $merchantId = $merchant['id']; $openId = $admin['openId']; $allTM = xhTMessageService::getList($merchantId); $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"]]]; weixinUtil::sendTaskInform($data, $merchant); } } public static function supplement($merchant) { $merchantId = $merchant['id']; $tMessageList = configDict::getConfig('tMessage'); $tMessageList = array_flip($tMessageList); $tm = self::getList($merchantId); $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 = weixinUtil::tMessageCreate($merchant, $tVal); if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) { $templateId = $wxReturn['template_id']; $data = [ 'shortTemplateId' => $tVal, 'templateId' => $templateId, 'templateName' => $tKey, 'merchantId' => $merchantId, '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"); } } return ['code' => 'A0001', 'msg' => 'success']; } }