WechatKeywordsService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. *
  4. * @file:WechatKeywordsService.php
  5. * @Date: 2023/7/5
  6. * @Time: 16:31
  7. */
  8. namespace Modules\WechatPlatform\Services;
  9. use Illuminate\Support\Facades\DB;
  10. use Modules\Common\Services\BaseService;
  11. use Modules\WechatPlatform\Models\WechatAccountKeywordLog;
  12. use Modules\WechatPlatform\Models\WechatKeywords;
  13. class WechatKeywordsService extends BaseService
  14. {
  15. protected static array $hideField = ['updated_at', 'is_del', 'del_at', 'user_id', 'puser_id', 'send_content']; // 公用的影藏字段
  16. /**
  17. * 添加关键字
  18. * name: addKeyword
  19. * @param mixed $param
  20. * date 2023/07/06 10:11
  21. */
  22. public static function addKeyword(mixed $param)
  23. {
  24. $res = WechatKeywords::create($param);
  25. if ($res) {
  26. return "操作成功";
  27. }
  28. return "操作失败";
  29. }
  30. /**
  31. * 关键词列表
  32. * name: getKeywordsList
  33. * @param array $param
  34. * date 2023/07/06 13:53
  35. */
  36. public static function getKeywordsList(array $param)
  37. {
  38. $sql = self::getQuery($param)->orderBy('id','desc');
  39. $isAll = getProp($param,'is_all',false);
  40. if($isAll){
  41. $list = $sql->get();
  42. }else{
  43. $list = $sql->paginate(getProp($param,'limit',10));
  44. }
  45. $list->makeHidden(array_merge(self::$hideField,['content']));
  46. return $list;
  47. }
  48. private static function getQuery(array $param)
  49. {
  50. $sql = WechatKeywords::query()->where('is_del',0);
  51. if (getProp($param, 'puser_id')) {
  52. $sql->where('puser_id', $param['puser_id']);
  53. }
  54. if (getProp($param, 'user_id')) {
  55. $sql->where('user_id', $param['user_id']);
  56. }
  57. if (getProp($param, 'keyword')) {
  58. $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
  59. }
  60. if(getProp($param,'wechat_authorization_info_id')){
  61. $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
  62. }
  63. return $sql;
  64. }
  65. /**
  66. * 详情
  67. * name: detail
  68. * @param $id
  69. * date 2023/07/06 13:55
  70. */
  71. public static function detail($id)
  72. {
  73. return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts']));
  74. }
  75. /**
  76. * 保存关键字
  77. * name: updateKeyWords
  78. * @param $id
  79. * @param mixed $param
  80. * @return string
  81. * date 2023/07/06 14:51
  82. */
  83. public static function updateKeyWords($id, mixed $param)
  84. {
  85. $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
  86. if (is_empty($info)) {
  87. self::throwErrMsg('关键词不存在或已删除');
  88. }
  89. if ($param['keyword'] != $info->keyword) {
  90. // 修改关键词,需要查询词关键词是否已配置
  91. if ($info->wechat_accounts) {
  92. $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('appid')->get();
  93. if ($appIds) {
  94. $hasUsed = WechatAccountKeywordLog::where('keyword', $param['keyword'])->whereIn('appid', $appIds)->where('status', 1)->pluck('nick_name')->get();
  95. if ($hasUsed) {
  96. self::throwErrMsg("关键字{$param['keyword']}已配置了以下公众号" . implode(',', $hasUsed) . "请先解绑后再编辑");
  97. }
  98. }
  99. }
  100. }
  101. DB::beginTransaction();
  102. try {
  103. $info->save($param);
  104. $data = [
  105. 'content' => $param['send_content'],
  106. 'keyword' => $param['keyword'],
  107. ];
  108. WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update($data);
  109. DB::commit();
  110. } catch (\Exception $exception) {
  111. DB::rollBack();
  112. self::throwErrMsg('编辑失败');
  113. }
  114. return "操作成功";
  115. }
  116. /**
  117. * 配置公众号列表
  118. * name: WechaAccountAuthListInfo
  119. * @param $id
  120. * @param mixed $userId
  121. * date 2023/07/06 15:31
  122. */
  123. public static function WechaAccountAuthListInfo($id, mixed $userId)
  124. {
  125. $list = DB::table('wechat_authorization_infos')
  126. ->where('user_id', $userId)
  127. ->select('id', 'nick_name', 'is_enabled')->get();
  128. $authList = WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->get();
  129. if (!$list->isEmpty()) {
  130. foreach ($list as $val) {
  131. $val->is_auth = 0;
  132. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  133. if (getProp($info, 'status') == 1) {
  134. $val->is_auth = 1;
  135. }
  136. if ($val->is_enabled != 1) {
  137. $val->nick_name .= "(取消授权)";
  138. }
  139. unset($val->is_enabled);
  140. }
  141. }
  142. return $list;
  143. }
  144. /**
  145. * 删除关键词
  146. * name: delKeywords
  147. * @param array $ids
  148. * date 2023/07/06 15:54
  149. */
  150. public static function delKeywords(array $ids)
  151. {
  152. if (empty($ids)) {
  153. self::throwErrMsg('要删除的数据不能为空');
  154. }
  155. DB::beginTransaction();
  156. try {
  157. WechatKeywords::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  158. WechatAccountKeywordLog::query()->whereIn('weacht_keyworld_id', $ids)->update(['status' => 0]);
  159. DB::commit();
  160. } catch (\Exception $exception) {
  161. DB::rollBack();
  162. self::throwErrMsg('删除失败');
  163. }
  164. return "操作成功";
  165. }
  166. public static function allocation($id, $wxAuthIds)
  167. {
  168. $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
  169. if (is_empty($info)) {
  170. self::throwErrMsg('关键词不存在或已删除');
  171. }
  172. $data = [];
  173. $list = [];
  174. if (empty($wxAuthIds)){
  175. $data['wechat_accounts'] = [];
  176. }else{
  177. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  178. ->whereIn('id',$wxAuthIds)
  179. ->where('is_enabled',1)
  180. ->where('user_id',$info->user_id)
  181. ->get();
  182. if($wechatAccountInfos->isEmpty()){
  183. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  184. }
  185. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  186. $canNotUsed = array_diff($wxAuthIds,$canNotUsed);
  187. if (count($canNotUsed) > 0){
  188. self::throwErrMsg("优化师对id:为:".implode(',',$canNotUsed)."的公众号没有使用权限");
  189. }
  190. foreach ($wechatAccountInfos as $val){
  191. $data['wechat_accounts'][] = [
  192. 'id' => $val->id,
  193. 'appid' =>$val->authorizer_appid,
  194. 'nick_name' => $val->nick_name,
  195. 'component_appid' => $val->component_appid
  196. ];
  197. $list[] = [
  198. 'weacht_keyworld_id' => $info->id,
  199. 'appid' => $val->authorizer_appid,
  200. 'wechat_authorization_info_id'=> $val->id,
  201. 'nick_name' => $val->nick_name,'keyword' => $info->keyword,
  202. 'content' => $info->send_content,
  203. 'status' => 1,
  204. ];
  205. }
  206. }
  207. DB::beginTransaction();
  208. try {
  209. if (!empty($list)){
  210. foreach ($list as $val){
  211. WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val);
  212. }
  213. }else{
  214. WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]);
  215. }
  216. WechatKeywords::where('id',$id)->update($data);
  217. DB::commit();
  218. }catch (\Exception $exception){
  219. DB::rollBack();
  220. self::throwErrMsg('操作失败');
  221. }
  222. return "操作成功";
  223. }
  224. }