xhTMessageService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace common\services;
  3. use biz\message\classes\TMessageClass;
  4. use Yii;
  5. use common\models\xhTMessage;
  6. use common\components\wxUtil;
  7. use common\components\configDict;
  8. class xhTMessageService
  9. {
  10. //取所有的模板消息
  11. public static function getList($merchantId)
  12. {
  13. $tm = xhTMessage::getAllByCondition(['merchantId' => $merchantId]);
  14. $list = [];
  15. if (!empty($tm)) {
  16. foreach ($tm as $val) {
  17. $list[$val['shortTemplateId']] = $val['templateId'];
  18. }
  19. }
  20. return $list;
  21. }
  22. public static function add($data)
  23. {
  24. $tm = xhTMessage::add($data);
  25. return $tm;
  26. }
  27. public static function batchAdd($data)
  28. {
  29. xhTMessage::batchAdd($data);
  30. }
  31. public static function delByMerchantId($merchantId)
  32. {
  33. xhTMessage::deleteByCondition(['merchantId' => $merchantId]);
  34. }
  35. //商家模板初始化 shish 2020.2.9
  36. public static function init($merchant)
  37. {
  38. //设置模板消息所属行业:IT科技 互联网/电子商务 消费品 消费品
  39. wxUtil::setTMIndustry($merchant, 1, 31);
  40. $merchantId = $merchant['id'];
  41. $original = configDict::getConfig('tMessage');
  42. if (empty($original)) {
  43. return false;
  44. }
  45. $preTMessage = wxUtil::getAllTMessage($merchant);//删除原有的
  46. if (!empty($preTMessage)) {
  47. if (isset($preTMessage['template_list'])) {
  48. foreach ($preTMessage['template_list'] as $key => $val) {
  49. $template_id = $val['template_id'];
  50. wxUtil::delTMessage($merchant, $template_id);
  51. }
  52. }
  53. }
  54. $message = self::getList($merchantId);
  55. if (!empty($message)) {
  56. self::delByMerchantId($merchantId);
  57. }
  58. $time = time();
  59. foreach ($original as $key => $val) {//重新添加
  60. $wxMessage = wxUtil::tMessageCreate($merchant, $key);
  61. if (isset($wxMessage['errcode']) && $wxMessage['errcode'] == 0) {
  62. $templateId = $wxMessage['template_id'];
  63. $data = [
  64. 'shortTemplateId' => $key,
  65. 'templateId' => $templateId,
  66. 'templateName' => $val,
  67. 'merchantId' => $merchantId,
  68. 'addTime' => $time,
  69. 'createTime' => date("Y-m-d H:i:s"),
  70. ];
  71. TMessageClass::add($data);
  72. }
  73. }
  74. xhMerchantExtendService::updateByMerchantId($merchantId, ['wxTMessageInit' => 1]);
  75. }
  76. //发送到手机上 shish 2019.8.11
  77. public static function sendUrlToMobile($url, $openId, $merchant)
  78. {
  79. $merchantId = $merchant['id'];
  80. $allTM = xhTMessageService::getList($merchantId);
  81. $shortTempId = 'OPENTM207430125';
  82. if (isset($allTM[$shortTempId])) {
  83. $tempId = $allTM[$shortTempId];
  84. $data = [
  85. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  86. "data" => ["first" => ["value" => '亲,您的链接已经生成了哦', "color" => "#173177"],
  87. "keyword1" => ["value" => '访问链接', "color" => "#173177"],
  88. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  89. "remark" => ["value" => "点此直接访问~", "color" => "#173177"]]];
  90. wxUtil::sendTaskInform($data, $merchant);
  91. }
  92. }
  93. //发送链接到手机上
  94. public static function sendUrl($url, $name = '访问链接', $merchant, $admin)
  95. {
  96. $merchantId = $merchant['id'];
  97. $openId = $admin['openId'];
  98. $allTM = xhTMessageService::getList($merchantId);
  99. $shortTempId = 'OPENTM207430125';
  100. if (isset($allTM[$shortTempId])) {
  101. $tempId = $allTM[$shortTempId];
  102. $data = [
  103. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  104. "data" => ["first" => ["value" => '亲,您的链接已经生成了哦', "color" => "#173177"],
  105. "keyword1" => ["value" => $name, "color" => "#173177"],
  106. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  107. "remark" => ["value" => "点此直接访问~", "color" => "#173177"]]];
  108. wxUtil::sendTaskInform($data, $merchant);
  109. }
  110. }
  111. //补充缺失的模板 shish 2020.2.9
  112. public static function supplement($merchant)
  113. {
  114. $merchantId = $merchant['id'];
  115. $tMessageList = configDict::getConfig('tMessage');
  116. $tMessageList = array_flip($tMessageList);
  117. $tm = self::getList($merchantId);
  118. $existsTM = [];
  119. if (!empty($tm)) {
  120. foreach ($tm as $tmKey => $tmVal) {
  121. $existsTM[$tmVal] = $tmKey;
  122. }
  123. }
  124. $addData = array_diff($tMessageList, $existsTM);
  125. if (empty($addData)) {
  126. return ['code' => 'A0001', 'msg' => '没有模板需要添加'];
  127. }
  128. foreach ($addData as $tKey => $tVal) {
  129. $wxReturn = wxUtil::tMessageCreate($merchant, $tVal);
  130. if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
  131. $templateId = $wxReturn['template_id'];
  132. $data = [
  133. 'shortTemplateId' => $tVal,
  134. 'templateId' => $templateId,
  135. 'templateName' => $tKey,
  136. 'merchantId' => $merchantId,
  137. 'createTime' => date("Y-m-d H:i:s"),
  138. ];
  139. self::add($data);
  140. Yii::warning("add tmessage:" . $tVal . " success\n");
  141. } else {
  142. Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
  143. }
  144. }
  145. }
  146. }