xhTMessageService.php 5.2 KB

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