WechatKeywordsService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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')->toArray();
  93. if ($appIds) {
  94. $hasUsed = WechatAccountKeywordLog::where('keyword', $param['keyword'])->whereIn('appid', $appIds)->where('status', 1)->pluck('nick_name')->toArray();
  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. 'miniprogram_id' => $info->miniprogram_id,
  108. ];
  109. WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update($data);
  110. DB::commit();
  111. } catch (\Exception $exception) {
  112. DB::rollBack();
  113. self::throwErrMsg('编辑失败');
  114. }
  115. return "操作成功";
  116. }
  117. /**
  118. * 配置公众号列表
  119. * name: WechaAccountAuthListInfo
  120. * @param $id
  121. * @param mixed $userId
  122. * date 2023/07/06 15:31
  123. */
  124. public static function WechaAccountAuthListInfo($id, mixed $userId)
  125. {
  126. $list = DB::table('wechat_authorization_infos')
  127. ->where('user_id', $userId)
  128. ->select('id', 'nick_name', 'is_enabled')->get();
  129. $authList = WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->get();
  130. if (!$list->isEmpty()) {
  131. foreach ($list as $val) {
  132. $val->is_auth = 0;
  133. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  134. if (getProp($info, 'status') == 1) {
  135. $val->is_auth = 1;
  136. }
  137. if ($val->is_enabled != 1) {
  138. $val->nick_name .= "(取消授权)";
  139. }
  140. unset($val->is_enabled);
  141. }
  142. }
  143. return $list;
  144. }
  145. /**
  146. * 删除关键词
  147. * name: delKeywords
  148. * @param array $ids
  149. * date 2023/07/06 15:54
  150. */
  151. public static function delKeywords(array $ids)
  152. {
  153. if (empty($ids)) {
  154. self::throwErrMsg('要删除的数据不能为空');
  155. }
  156. DB::beginTransaction();
  157. try {
  158. WechatKeywords::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  159. WechatAccountKeywordLog::query()->whereIn('weacht_keyworld_id', $ids)->update(['status' => 0]);
  160. DB::commit();
  161. } catch (\Exception $exception) {
  162. DB::rollBack();
  163. self::throwErrMsg('删除失败');
  164. }
  165. return "操作成功";
  166. }
  167. public static function allocation($id, $wxAuthIds)
  168. {
  169. $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
  170. if (is_empty($info)) {
  171. self::throwErrMsg('关键词不存在或已删除');
  172. }
  173. $data = [];
  174. $list = [];
  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. foreach ($wechatAccountInfos as $val){
  192. $data['wechat_accounts'][] = [
  193. 'id' => $val->id,
  194. 'appid' =>$val->authorizer_appid,
  195. 'nick_name' => $val->nick_name,
  196. 'component_appid' => $val->component_appid
  197. ];
  198. $list[] = [
  199. 'weacht_keyworld_id' => $info->id,
  200. 'appid' => $val->authorizer_appid,
  201. 'wechat_authorization_info_id'=> $val->id,
  202. 'nick_name' => $val->nick_name,'keyword' => $info->keyword,
  203. 'content' => $info->send_content,
  204. 'status' => 1,
  205. ];
  206. }
  207. }
  208. DB::beginTransaction();
  209. try {
  210. if (!empty($list)){
  211. foreach ($list as $val){
  212. WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val);
  213. }
  214. }else{
  215. WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]);
  216. }
  217. WechatKeywords::where('id',$id)->update($data);
  218. DB::commit();
  219. }catch (\Exception $exception){
  220. DB::rollBack();
  221. self::throwErrMsg('操作失败');
  222. }
  223. return "操作成功";
  224. }
  225. }