Replay.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\wechat\model;
  11. use app\model\BaseModel;
  12. /**
  13. * 微信回复
  14. */
  15. class Replay extends BaseModel
  16. {
  17. /*******************************************************************************微信回复开始*****************************************************/
  18. /**
  19. * 添加微信关键词回复
  20. * @param array $data
  21. */
  22. public function addRule($data)
  23. {
  24. $res = model('wechat_replay_rule')->add($data);
  25. if ($res === false) {
  26. return $this->error($res, 'SAVE_FAIL');
  27. }
  28. return $this->success($res, 'SAVE_SUCCESS');
  29. }
  30. /**
  31. * 修改微信关键词回复
  32. * @param array $data
  33. * @param array $condition
  34. * @return multitype:string mixed
  35. */
  36. public function editRule($data, $condition)
  37. {
  38. $res = model('wechat_replay_rule')->update($data, $condition);
  39. if ($res === false) {
  40. return $this->error($res, 'SAVE_FAIL');
  41. }
  42. return $this->success($res, 'SAVE_SUCCESS');
  43. }
  44. /**
  45. * 删除微信关键词回复
  46. * @param array $condition
  47. * @return multitype:string mixed
  48. */
  49. public function deleteRule($condition)
  50. {
  51. $res = model('wechat_replay_rule')->delete($condition);
  52. if ($res === false) {
  53. return $this->error($res, 'DELETE_FAIL');
  54. }
  55. return $this->success($res, 'DELETE_SUCCESS');
  56. }
  57. /**
  58. * 获取微信关键词回复信息
  59. * @param array $condition
  60. * @param string $field
  61. * @return multitype:string mixed
  62. */
  63. public function getRuleInfo($condition, $field = '*')
  64. {
  65. $info = model('wechat_replay_rule')->getInfo($condition, $field);
  66. return $this->success($info);
  67. }
  68. /**
  69. * 获取回复列表
  70. * @param array $condition
  71. * @param number $page
  72. * @param string $page_size
  73. * @param string $order
  74. * @param string $field
  75. * @return multitype:string mixed
  76. */
  77. public function getReplayPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  78. {
  79. $list = model('wechat_replay_rule')->pageList($condition, $field, $order, $page, $page_size);
  80. return $this->success($list);
  81. }
  82. /**
  83. * 获取站点关键字回复
  84. * @param unknown $site_id
  85. * @param unknown $keyWords
  86. */
  87. public function getSiteWechatKeywordsReplay($keywords, $site_id)
  88. {
  89. $list = model('wechat_replay_rule')->getList([['rule_type', '=', 'KEYWORDS'], ['site_id', '=', $site_id]]);
  90. $default_list = model('wechat_replay_rule')->getList([['rule_type', '=', 'DEFAULT'], ['site_id', '=', $site_id]]);
  91. $rule_list = array();
  92. $text = '';
  93. foreach ($list as $k => $v) {
  94. $kewords_array = json_decode($v['keywords_json'], true);
  95. $replay_array = json_decode($v['replay_json'], true);
  96. if (!empty($kewords_array) && !empty($replay_array)) {
  97. foreach ($kewords_array as $k_key => $v_key) {
  98. $text = $v_key['keywords_name'] . ',' . $text;
  99. if (($v_key['keywords_type'] == 0 && $v_key['keywords_name'] == $keywords) || ($v_key['keywords_type'] == 1 && (strpos($keywords, $v_key['keywords_name']) !== false))) {
  100. $num = rand(0, count($replay_array) - 1);
  101. $rule_list[] = $replay_array[$num];
  102. }
  103. }
  104. }
  105. }
  106. if (empty($rule_list)) {
  107. $default_rule_list = array();
  108. foreach ($default_list as $kk => $vv) {
  109. $default_replay_array = json_decode($vv['replay_json'], true);
  110. if (!empty($default_replay_array)) {
  111. $num = rand(0, count($replay_array) - 1);
  112. $default_rule_list[] = $default_replay_array[$num];
  113. }
  114. }
  115. if (!empty($default_rule_list)){
  116. $rule = $default_rule_list[0];
  117. }else{
  118. $rule = [];
  119. }
  120. } else {
  121. $rule = $rule_list[0];
  122. }
  123. return $this->success($rule);
  124. }
  125. /**
  126. * 获取微信关注回复
  127. * @param unknown $from_user
  128. * @param unknown $to_user
  129. * @param unknown $site_id
  130. * @param unknown $keywords
  131. */
  132. function getWechatFollowReplay($site_id)
  133. {
  134. $follow_info = model('wechat_replay_rule')->getInfo([['rule_type', '=', 'AFTER'], ['site_id', '=', $site_id]]);
  135. $replay_content = '';
  136. if (!empty($follow_info['replay_json'])) {
  137. $replay_info = json_decode($follow_info['replay_json'], true);
  138. switch ($replay_info[0]['type']) {
  139. case 'text' :
  140. $replay_content = $replay_info[0]['reply_content'];
  141. break;
  142. case 'articles' :
  143. $replay_content = '';
  144. break;
  145. }
  146. }
  147. return $this->success($replay_content);
  148. }
  149. /*******************************************************************************微信回复结束*****************************************************/
  150. }