WechatKeywordsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\MiniprogramWechatGlobalConfig;
  12. use Modules\WechatPlatform\Models\WechatAccountKeywordLog;
  13. use Modules\WechatPlatform\Models\WechatKeywords;
  14. class WechatKeywordsService extends BaseService
  15. {
  16. protected static array $hideField = ['updated_at', 'is_del', 'del_at', 'user_id', 'puser_id', 'send_content']; // 公用的影藏字段
  17. const KEYWORD_SET_KEY = "miniprogram_wechat_keyword";
  18. /**
  19. * 添加关键字
  20. * name: addKeyword
  21. * @param mixed $param
  22. * date 2023/07/06 10:11
  23. */
  24. public static function addKeyword(mixed $param)
  25. {
  26. $res = WechatKeywords::create($param);
  27. if ($res) {
  28. return "操作成功";
  29. }
  30. return "操作失败";
  31. }
  32. /**
  33. * 关键词列表
  34. * name: getKeywordsList
  35. * @param array $param
  36. * date 2023/07/06 13:53
  37. */
  38. public static function getKeywordsList(array $param)
  39. {
  40. $sql = self::getQuery($param)->orderBy('id', 'desc');
  41. $isAll = getProp($param, 'is_all', false);
  42. if ($isAll) {
  43. $list = $sql->get();
  44. } else {
  45. $list = $sql->paginate(getProp($param, 'limit', 10));
  46. }
  47. $list->makeHidden(self::$hideField);
  48. foreach ($list as $val){
  49. $val->wechat_accounts = WechatAccountKeywordLog::query()->where(
  50. ['weacht_keyworld_id' => $val->id,
  51. 'status' => 1
  52. ])->select('wechat_authorization_info_id as id',"appid",'nick_name')->get();;
  53. }
  54. return $list;
  55. }
  56. private static function getQuery(array $param)
  57. {
  58. $sql = WechatKeywords::query()->where('is_del', 0);
  59. if (getProp($param, 'puser_id')) {
  60. $sql->where('puser_id', $param['puser_id']);
  61. }
  62. if (getProp($param, 'user_id')) {
  63. $sql->where('user_id', $param['user_id']);
  64. }
  65. if (getProp($param, 'keyword')) {
  66. $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
  67. }
  68. if (getProp($param, 'miniprogram_id')) {
  69. $sql->where('miniprogram_id', $param['miniprogram_id']);
  70. }
  71. if (getProp($param, 'wechat_authorization_info_id')) {
  72. $ids = WechatAccountKeywordLog::query()->where([
  73. 'wechat_authorization_info_id' => $param['wechat_authorization_info_id'],
  74. 'status' => 1,
  75. ])->pluck('weacht_keyworld_id')->toArray();
  76. $sql->whereIn('id',$ids);
  77. }
  78. return $sql;
  79. }
  80. /**
  81. * 详情
  82. * name: detail
  83. * @param $id
  84. * date 2023/07/06 13:55
  85. */
  86. public static function detail($id)
  87. {
  88. return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField, ['wechat_accounts']));
  89. }
  90. /**
  91. * 保存关键字
  92. * name: updateKeyWords
  93. * @param $id
  94. * @param mixed $param
  95. * @return string
  96. * date 2023/07/06 14:51
  97. */
  98. public static function updateKeyWords($id, mixed $param)
  99. {
  100. $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
  101. if (is_empty($info)) {
  102. self::throwErrMsg('关键词不存在或已删除');
  103. }
  104. $appIds = [];
  105. if ($param['keyword'] != $info->keyword) {
  106. // 修改关键词,需要查询词关键词是否已配置
  107. $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('wechat_authorization_info_id')->toArray();
  108. }
  109. WechatKeywords::query()->where('id', $id)->update($param);
  110. if ($appIds) {
  111. WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update(['status' => 0]);
  112. self::allocation($info->id, $appIds);
  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 ['msg' => "操作成功"];
  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. $errMsg = "";
  175. if (empty($wxAuthIds)) {
  176. $data['wechat_accounts'] = [];
  177. } else {
  178. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  179. ->whereIn('id', $wxAuthIds)
  180. ->where('is_enabled', 1)
  181. ->where('user_id', $info->user_id)
  182. ->get();
  183. if ($wechatAccountInfos->isEmpty()) {
  184. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  185. }
  186. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  187. $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
  188. if (count($canNotUsed) > 0) {
  189. self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
  190. }
  191. $keywords = explode(',', $info->keyword);
  192. $allSet = WechatAccountKeywordLog::where('user_id', $info->user_id)->get();
  193. foreach ($wechatAccountInfos as $val) {
  194. $data['wechat_accounts'][] = [
  195. 'id' => $val->id,
  196. 'appid' => $val->authorizer_appid,
  197. 'nick_name' => $val->nick_name,
  198. 'component_appid' => $val->component_appid
  199. ];
  200. foreach ($keywords as $kval) {
  201. $has = $allSet->where('keyword', $kval)
  202. ->where('wechat_authorization_info_id', $val->id)
  203. ->where('status', 1)
  204. ->where('weacht_keyworld_id', '<>', $info->id)->first();
  205. if ($has) {
  206. $errMsg .= "{$val->nick_name}已重新配置关键词{$kval};";
  207. }
  208. $list[] = [
  209. 'weacht_keyworld_id' => $info->id,
  210. 'user_id' => $info->user_id,
  211. 'puser_id' => $info->puser_id,
  212. 'miniprogram_id' => $info->miniprogram_id,
  213. 'appid' => $val->authorizer_appid,
  214. 'wechat_authorization_info_id' => $val->id,
  215. 'nick_name' => $val->nick_name,
  216. 'keyword' => $kval,
  217. 'content' => $info->send_content,
  218. 'status' => 1,
  219. ];
  220. }
  221. }
  222. unset($wechatAccountInfos, $allSet);
  223. }
  224. DB::beginTransaction();
  225. try {
  226. WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->update(['status' => 0]);
  227. if (!empty($list)) {
  228. WechatAccountKeywordLog::where('user_id', $info->user_id)
  229. ->whereIn("keyword", $keywords)->whereIn('wechat_authorization_info_id', $wxAuthIds)->update(['status' => 0]);
  230. foreach ($list as $val) {
  231. WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'], 'keyword' => $val['keyword'], 'wechat_authorization_info_id' => $val['wechat_authorization_info_id']], $val);
  232. }
  233. }
  234. WechatKeywords::where('id', $id)->update($data);
  235. DB::commit();
  236. } catch (\Exception $exception) {
  237. self::throwErrMsg('操作失败');
  238. }
  239. return "操作成功" . $errMsg;
  240. }
  241. public static function getConfig($userId, $puserId, $miniprogramId)
  242. {
  243. $set = MiniprogramWechatGlobalConfig::where(['user_id' => $userId, 'puser_id' => $puserId, 'miniprogram_id' => $miniprogramId])
  244. ->where('type', self::KEYWORD_SET_KEY)->value('value');
  245. return ['data' => $set == 1 ? 1 : 0];
  246. }
  247. public static function setConfig($param)
  248. {
  249. $param['type'] = self::KEYWORD_SET_KEY;
  250. $res = MiniprogramWechatGlobalConfig::updateOrCreate(
  251. ['user_id' => $param['user_id'], 'puser_id' => $param['puser_id'], 'miniprogram_id' => $param['miniprogram_id'], 'type' => self::KEYWORD_SET_KEY],
  252. $param
  253. );
  254. if ($res) {
  255. return ['msg' => "操作成功"];
  256. }
  257. return "操作失败";
  258. }
  259. /**
  260. * 更新关键词,授权公众号id匹配关键词
  261. * name: getKeywordsByWords
  262. * @param $keyword
  263. * @param $wechatAppId
  264. * @return mixed|string
  265. * date 2023/07/10 16:31
  266. */
  267. public static function getKeywordsByWords($keyword, $wechatAppId)
  268. {
  269. // 匹配关键词
  270. $info = WechatAccountKeywordLog::where([
  271. "keyword" => $keyword,
  272. 'status' => 1,
  273. 'wechat_authorization_info_id' => $wechatAppId,
  274. ])->first();
  275. if (is_empty($info)) {
  276. return "";
  277. }
  278. $set = self::getConfig($info->user_id,$info->puser_id,$info->miniprogram_id);
  279. if (getProp($set,'data') == 0){
  280. return '';
  281. }
  282. WechatAccountKeywordLog::where('id', $info->id)->increment('send_total', 1);
  283. WechatKeywords::where('id', $info->weacht_keyworld_id)->increment('send_total', 1);
  284. return $info->content;
  285. }
  286. }