xhTMessageService.php 6.3 KB

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