xhTMessageService.php 5.4 KB

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