xhTMessageService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\models\xhTMessage;
  5. use common\components\wxUtil;
  6. use common\components\configDict;
  7. class xhTMessageService
  8. {
  9. //取所有的模板消息
  10. public static function getList($merchantId)
  11. {
  12. $tm = xhTMessage::getAllByCondition(['merchantId' => $merchantId]);
  13. $list = [];
  14. if (!empty($tm)) {
  15. foreach ($tm as $val) {
  16. $list[$val['shortTemplateId']] = $val['templateId'];
  17. }
  18. }
  19. return $list;
  20. }
  21. public static function add($data)
  22. {
  23. $tm = xhTMessage::add($data);
  24. return $tm;
  25. }
  26. public static function batchAdd($data)
  27. {
  28. xhTMessage::batchAdd($data);
  29. }
  30. public static function delByMerchantId($merchantId)
  31. {
  32. xhTMessage::deleteByCondition(['merchantId' => $merchantId]);
  33. }
  34. //商家模板初始化 shish 2020.2.9
  35. public static function init($merchant)
  36. {
  37. //设置模板消息所属行业 IT科技 互联网/电子商务 消费品 消费品
  38. wxUtil::setTMIndustry($merchant, 1, 31);
  39. $merchantId = $merchant['id'];
  40. $original = configDict::getConfig('tMessage');
  41. if (!empty($original)) {
  42. $preTMessage = wxUtil::getAllTMessage($merchant);//删除原有的
  43. if (!empty($preTMessage)) {
  44. if (isset($preTMessage['template_list'])) {
  45. foreach ($preTMessage['template_list'] as $key => $val) {
  46. $template_id = $val['template_id'];
  47. wxUtil::delTMessage($merchant, $template_id);
  48. }
  49. }
  50. }
  51. $message = self::getList($merchantId);
  52. if (!empty($message)) {
  53. self::delByMerchantId($merchantId);
  54. }
  55. $batchData = [];
  56. foreach ($original as $key => $val) {//重新添加
  57. $wxMessage = wxUtil::tMessageCreate($merchant, $key);
  58. if (isset($wxMessage['errcode']) && $wxMessage['errcode'] == 0) {
  59. $templateId = $wxMessage['template_id'];
  60. $data = [
  61. 'shortTemplateId' => $key,
  62. 'templateId' => $templateId,
  63. 'templateName' => $val,
  64. 'merchantId' => $merchantId,
  65. 'createTime' => date("Y-m-d H:i:s"),
  66. ];
  67. $batchData[] = $data;
  68. }
  69. }
  70. if(!empty($batchData)){
  71. self::batchAdd($batchData);
  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. }