xhTMessageService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. print_r($batchData);
  71. if(!empty($batchData)){
  72. self::batchAdd($batchData);
  73. }
  74. }
  75. xhMerchantExtendService::updateByMerchantId($merchantId, ['wxTMessageInit' => 1]);
  76. }
  77. //发送到手机上 shish 2019.8.11
  78. public static function sendUrlToMobile($url, $openId, $merchant)
  79. {
  80. $merchantId = $merchant['id'];
  81. $allTM = xhTMessageService::getList($merchantId);
  82. $shortTempId = 'OPENTM207430125';
  83. if (isset($allTM[$shortTempId])) {
  84. $tempId = $allTM[$shortTempId];
  85. $data = [
  86. "touser" => $openId, "template_id" => $tempId, "url" => $url,
  87. "data" => ["first" => ["value" => '亲,您的链接已经生成了哦', "color" => "#173177"],
  88. "keyword1" => ["value" => '访问链接', "color" => "#173177"],
  89. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  90. "remark" => ["value" => "点此直接访问~", "color" => "#173177"]]];
  91. wxUtil::sendTaskInform($data, $merchant);
  92. }
  93. }
  94. //发送链接到手机上
  95. public static function sendUrl($url, $name = '访问链接', $merchant, $admin)
  96. {
  97. $merchantId = $merchant['id'];
  98. $openId = $admin['openId'];
  99. $allTM = xhTMessageService::getList($merchantId);
  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" => $name, "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. //补充缺失的模板 shish 2020.2.9
  113. public static function supplement($merchant)
  114. {
  115. $merchantId = $merchant['id'];
  116. $tMessageList = configDict::getConfig('tMessage');
  117. $tMessageList = array_flip($tMessageList);
  118. $tm = self::getList($merchantId);
  119. $existsTM = [];
  120. if (!empty($tm)) {
  121. foreach ($tm as $tmKey => $tmVal) {
  122. $existsTM[$tmVal] = $tmKey;
  123. }
  124. }
  125. $addData = array_diff($tMessageList, $existsTM);
  126. if (empty($addData)) {
  127. return ['code' => 'A0001', 'msg' => '没有模板需要添加'];
  128. }
  129. foreach ($addData as $tKey => $tVal) {
  130. $wxReturn = wxUtil::tMessageCreate($merchant, $tVal);
  131. if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
  132. $templateId = $wxReturn['template_id'];
  133. $data = [
  134. 'shortTemplateId' => $tVal,
  135. 'templateId' => $templateId,
  136. 'templateName' => $tKey,
  137. 'merchantId' => $merchantId,
  138. 'createTime' => date("Y-m-d H:i:s"),
  139. ];
  140. self::add($data);
  141. Yii::warning("add tmessage:" . $tVal . " success\n");
  142. } else {
  143. Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
  144. }
  145. }
  146. }
  147. }