Vbot.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace addons\vbot;
  3. use addons\vbot\library\VbotLib;
  4. use app\common\library\Menu;
  5. use think\Addons;
  6. use think\Db;
  7. /**
  8. * 微信通知机器人
  9. */
  10. class Vbot extends Addons
  11. {
  12. /**
  13. * 通过模板发送通知消息
  14. * @param string $template 消息模板Id或Code
  15. * @param array $tpl_data 要覆盖的模板数据 (参见:\application\admin\controller\dinghorn\Example.php 的 example2 方法)
  16. * @param array $tpl_variable 模板变量键值对,若模板变量已预先定义,则会自动获取值,无需在此传递
  17. * @return array 发送结果
  18. */
  19. public function vbotSendMsg($template, $tpl_data = array(), $tpl_variable = array())
  20. {
  21. $now_time = time();
  22. // 查询对应模板
  23. $template = Db::name('vbot_template')
  24. ->where('id = :tpl OR code = :tpl_code', ['tpl' => $template, 'tpl_code' => $template])
  25. ->where('deletetime', null)
  26. ->find();
  27. if (!$template || !$template['openswitch']) {
  28. $log_arr = [
  29. 'robot_id' => 0,
  30. 'template_id' => 0,
  31. 'template_data' => 0,
  32. 'errmsg' => '消息模板未找到或已禁用!(-2)',
  33. 'status' => '0',
  34. 'createtime' => $now_time
  35. ];
  36. Db::name('vbot_msglog')->insert($log_arr);
  37. return array('errcode' => -2, 'errmsg' => '消息模板未找到或已禁用!');
  38. }
  39. $VbotLib = new VbotLib($template, $tpl_data, $tpl_variable);
  40. // 获取模板内的机器人
  41. $robot_ids = isset($tpl_data['robot_ids']) ? $tpl_data['robot_ids'] : $template['robot_ids'];
  42. $robots = Db::name('vbot_robot')
  43. ->whereIn('id', $robot_ids)
  44. ->where('openswitch', 1)
  45. ->where('deletetime', null)
  46. ->select();
  47. // 组装消息数据
  48. $msg_data = $VbotLib->deal_with_msg();
  49. $log_arr = [];//要插入的日志
  50. $vbot_config = get_addon_config('vbot');
  51. $is_err = false;//是否有错误,用于判定本次操作的返回值
  52. // 循环所有要发送的机器人
  53. foreach ($robots as $key => $value) {
  54. $res = $VbotLib->msgSend($value['webhook'], $msg_data);
  55. if ($res['errcode'] == 0 && $vbot_config['success_log']) {
  56. $log_arr[] = array(
  57. 'robot_id' => $value['id'],
  58. 'template_id' => $template['id'],
  59. 'template_data' => json_encode($msg_data),
  60. 'errmsg' => '',
  61. 'status' => '1',
  62. 'createtime' => $now_time
  63. );
  64. } elseif ($res['errcode'] != 0) {
  65. $is_err = true;
  66. if ($vbot_config['error_log']) {
  67. $log_arr[] = array(
  68. 'robot_id' => $value['id'],
  69. 'template_id' => $template['id'],
  70. 'template_data' => json_encode($msg_data),
  71. 'errmsg' => $res['errmsg'] . '(' . $res['errcode'] . ')',
  72. 'status' => '0',
  73. 'createtime' => $now_time
  74. );
  75. }
  76. }
  77. }
  78. if ($log_arr) {
  79. Db::startTrans();
  80. try {
  81. Db::name('vbot_msglog')->insertAll($log_arr);
  82. Db::commit();
  83. } catch (\Exception $e) {
  84. Db::rollback();
  85. }
  86. }
  87. return !$is_err ? ['errcode' => 0] : ['errcode' => -3, 'errmsg' => '消息发送失败,请查看日志'];
  88. }
  89. /**
  90. * 插件安装方法
  91. * @return bool
  92. */
  93. public function install()
  94. {
  95. $menu = [
  96. [
  97. 'name' => 'vbot',
  98. 'title' => '微信通知机器人',
  99. 'icon' => 'fa fa-volume-up',
  100. 'sublist' => [
  101. [
  102. 'name' => 'vbot/robot',
  103. 'title' => '机器人管理',
  104. 'icon' => 'fa fa-circle-o',
  105. 'sublist' => [
  106. ['name' => 'vbot/robot/index', 'title' => '查看'],
  107. ['name' => 'vbot/robot/add', 'title' => '添加'],
  108. ['name' => 'vbot/robot/edit', 'title' => '编辑'],
  109. ['name' => 'vbot/robot/del', 'title' => '删除'],
  110. ['name' => 'vbot/robot/multi', 'title' => '批量更新'],
  111. ['name' => 'vbot/robot/recyclebin', 'title' => '回收站'],
  112. ['name' => 'vbot/robot/destroy', 'title' => '真实删除'],
  113. ['name' => 'vbot/robot/restore', 'title' => '还原'],
  114. ]
  115. ],
  116. [
  117. 'name' => 'vbot/variable',
  118. 'title' => '模板变量管理',
  119. 'icon' => 'fa fa-circle-o',
  120. 'remark' => '在此处设置变量,随后可在模板通知内使用变量,变量值可来源于方法返回值或SQL查询结果',
  121. 'sublist' => [
  122. ['name' => 'vbot/variable/index', 'title' => '查看'],
  123. ['name' => 'vbot/variable/add', 'title' => '添加'],
  124. ['name' => 'vbot/variable/edit', 'title' => '编辑'],
  125. ['name' => 'vbot/variable/del', 'title' => '删除'],
  126. ['name' => 'vbot/variable/multi', 'title' => '批量更新'],
  127. ['name' => 'vbot/variable/recyclebin', 'title' => '回收站'],
  128. ['name' => 'vbot/variable/destroy', 'title' => '真实删除'],
  129. ['name' => 'vbot/variable/restore', 'title' => '还原'],
  130. ['name' => 'vbot/variable/view_variable', 'title' => '计算变量值'],
  131. ]
  132. ],
  133. [
  134. 'name' => 'vbot/template',
  135. 'title' => '通知模板管理',
  136. 'icon' => 'fa fa-circle-o',
  137. 'sublist' => [
  138. ['name' => 'vbot/template/index', 'title' => '查看'],
  139. ['name' => 'vbot/template/add', 'title' => '添加'],
  140. ['name' => 'vbot/template/edit', 'title' => '编辑'],
  141. ['name' => 'vbot/template/del', 'title' => '删除'],
  142. ['name' => 'vbot/template/multi', 'title' => '批量更新'],
  143. ['name' => 'vbot/template/recyclebin', 'title' => '回收站'],
  144. ['name' => 'vbot/template/destroy', 'title' => '真实删除'],
  145. ['name' => 'vbot/template/restore', 'title' => '还原'],
  146. ]
  147. ],
  148. [
  149. 'name' => 'vbot/msglog',
  150. 'title' => '通知发送日志管理',
  151. 'icon' => 'fa fa-circle-o',
  152. 'sublist' => [
  153. ['name' => 'vbot/msglog/index', 'title' => '查看'],
  154. ['name' => 'vbot/msglog/edit', 'title' => '编辑'],
  155. ['name' => 'vbot/msglog/del', 'title' => '删除'],
  156. ['name' => 'vbot/msglog/multi', 'title' => '批量更新'],
  157. ['name' => 'vbot/msglog/recyclebin', 'title' => '回收站'],
  158. ['name' => 'vbot/msglog/destroy', 'title' => '真实删除'],
  159. ['name' => 'vbot/msglog/restore', 'title' => '还原'],
  160. ]
  161. ],
  162. ]
  163. ]
  164. ];
  165. Menu::create($menu);
  166. return true;
  167. }
  168. /**
  169. * 插件卸载方法
  170. * @return bool
  171. */
  172. public function uninstall()
  173. {
  174. Menu::delete('vbot');
  175. return true;
  176. }
  177. /**
  178. * 插件启用方法
  179. * @return bool
  180. */
  181. public function enable()
  182. {
  183. Menu::enable('vbot');
  184. return true;
  185. }
  186. /**
  187. * 插件禁用方法
  188. * @return bool
  189. */
  190. public function disable()
  191. {
  192. Menu::disable('vbot');
  193. return true;
  194. }
  195. }