WechatKeywordsService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. return $list;
  49. }
  50. private static function getQuery(array $param)
  51. {
  52. $sql = WechatKeywords::query()->where('is_del',0);
  53. if (getProp($param, 'puser_id')) {
  54. $sql->where('puser_id', $param['puser_id']);
  55. }
  56. if (getProp($param, 'user_id')) {
  57. $sql->where('user_id', $param['user_id']);
  58. }
  59. if (getProp($param, 'keyword')) {
  60. $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
  61. }
  62. if(getProp($param,'wechat_authorization_info_id')){
  63. $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
  64. }
  65. return $sql;
  66. }
  67. /**
  68. * 详情
  69. * name: detail
  70. * @param $id
  71. * date 2023/07/06 13:55
  72. */
  73. public static function detail($id)
  74. {
  75. return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts']));
  76. }
  77. /**
  78. * 保存关键字
  79. * name: updateKeyWords
  80. * @param $id
  81. * @param mixed $param
  82. * @return string
  83. * date 2023/07/06 14:51
  84. */
  85. public static function updateKeyWords($id, mixed $param)
  86. {
  87. $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
  88. if (is_empty($info)) {
  89. self::throwErrMsg('关键词不存在或已删除');
  90. }
  91. $appIds = [];
  92. if ($param['keyword'] != $info->keyword) {
  93. // 修改关键词,需要查询词关键词是否已配置
  94. $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('wechat_authorization_info_id')->toArray();
  95. }
  96. WechatKeywords::query()->where('id',$id)->update($param);
  97. if ($appIds){
  98. WechatAccountKeywordLog::where('weacht_keyworld_id',$info->id)->update(['status' => 0]);
  99. self::allocation($info->id,$appIds);
  100. }
  101. return "操作成功";
  102. }
  103. /**
  104. * 配置公众号列表
  105. * name: WechaAccountAuthListInfo
  106. * @param $id
  107. * @param mixed $userId
  108. * date 2023/07/06 15:31
  109. */
  110. public static function WechaAccountAuthListInfo($id, mixed $userId)
  111. {
  112. $list = DB::table('wechat_authorization_infos')
  113. ->where('user_id', $userId)
  114. ->select('id', 'nick_name', 'is_enabled')->get();
  115. $authList = WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->get();
  116. if (!$list->isEmpty()) {
  117. foreach ($list as $val) {
  118. $val->is_auth = 0;
  119. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  120. if (getProp($info, 'status') == 1) {
  121. $val->is_auth = 1;
  122. }
  123. if ($val->is_enabled != 1) {
  124. $val->nick_name .= "(取消授权)";
  125. }
  126. unset($val->is_enabled);
  127. }
  128. }
  129. return $list;
  130. }
  131. /**
  132. * 删除关键词
  133. * name: delKeywords
  134. * @param array $ids
  135. * date 2023/07/06 15:54
  136. */
  137. public static function delKeywords(array $ids)
  138. {
  139. if (empty($ids)) {
  140. self::throwErrMsg('要删除的数据不能为空');
  141. }
  142. DB::beginTransaction();
  143. try {
  144. WechatKeywords::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  145. WechatAccountKeywordLog::query()->whereIn('weacht_keyworld_id', $ids)->update(['status' => 0]);
  146. DB::commit();
  147. } catch (\Exception $exception) {
  148. DB::rollBack();
  149. self::throwErrMsg('删除失败');
  150. }
  151. return "操作成功";
  152. }
  153. public static function allocation($id, $wxAuthIds)
  154. {
  155. $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
  156. if (is_empty($info)) {
  157. self::throwErrMsg('关键词不存在或已删除');
  158. }
  159. $data = [];
  160. $list = [];
  161. $errMsg = "";
  162. if (empty($wxAuthIds)){
  163. $data['wechat_accounts'] = [];
  164. }else{
  165. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  166. ->whereIn('id',$wxAuthIds)
  167. ->where('is_enabled',1)
  168. ->where('user_id',$info->user_id)
  169. ->get();
  170. if($wechatAccountInfos->isEmpty()){
  171. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  172. }
  173. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  174. $canNotUsed = array_diff($wxAuthIds,$canNotUsed);
  175. if (count($canNotUsed) > 0){
  176. self::throwErrMsg("优化师对id:为:".implode(',',$canNotUsed)."的公众号没有使用权限");
  177. }
  178. $keywords = explode(',',$info->keyword);
  179. $allSet = WechatAccountKeywordLog::where('user_id',$info->user_id)->get();
  180. foreach ($wechatAccountInfos as $val){
  181. $data['wechat_accounts'][] = [
  182. 'id' => $val->id,
  183. 'appid' =>$val->authorizer_appid,
  184. 'nick_name' => $val->nick_name,
  185. 'component_appid' => $val->component_appid
  186. ];
  187. foreach ($keywords as $kval){
  188. $has = $allSet->where( 'keyword',$kval)
  189. ->where( 'wechat_authorization_info_id', $val->id)
  190. ->where( 'status', 1)
  191. ->where('weacht_keyworld_id','<>',$info->id)->first();
  192. if ($has){
  193. $errMsg .= "{$val->nick_name}已重新配置关键词{$kval};";
  194. }
  195. $list[] = [
  196. 'weacht_keyworld_id' => $info->id,
  197. 'user_id' => $info->user_id,
  198. 'puser_id' => $info->puser_id,
  199. 'miniprogram_id' => $info->miniprogram_id,
  200. 'appid' => $val->authorizer_appid,
  201. 'wechat_authorization_info_id'=> $val->id,
  202. 'nick_name' => $val->nick_name,
  203. 'keyword' => $kval,
  204. 'content' => $info->send_content,
  205. 'status' => 1,
  206. ];
  207. }
  208. }
  209. unset($wechatAccountInfos,$allSet);
  210. }
  211. DB::beginTransaction();
  212. try {
  213. WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]);
  214. if (!empty($list)){
  215. WechatAccountKeywordLog::where('user_id',$info->user_id)
  216. ->whereIn("keyword",$keywords)->whereIn('wechat_authorization_info_id',$wxAuthIds)->update(['status' => 0]);
  217. foreach ($list as $val){
  218. WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'keyword' => $val['keyword'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val);
  219. }
  220. }
  221. WechatKeywords::where('id',$id)->update($data);
  222. DB::commit();
  223. }catch (\Exception $exception){
  224. self::throwErrMsg('操作失败');
  225. }
  226. return "操作成功".$errMsg;
  227. }
  228. public static function getConfig($userId, $puserId, $miniprogramId)
  229. {
  230. $set = MiniprogramWechatGlobalConfig::where(['user_id' => $userId,'puser_id' => $puserId,'miniprogram_id' => $miniprogramId])
  231. ->where('type',self::KEYWORD_SET_KEY)->value('value');
  232. return $set == 1 ? 1 :0;
  233. }
  234. public static function setConfig($param)
  235. {
  236. $param['type'] = self::KEYWORD_SET_KEY;
  237. $res = MiniprogramWechatGlobalConfig::updateOrCreate(
  238. ['user_id' => $param['user_id'],'puser_id' => $param['puser_id'],'miniprogram_id' => $param['miniprogram_id'],'type' => self::KEYWORD_SET_KEY],
  239. $param
  240. );
  241. if($res){
  242. return "操作成功";
  243. }
  244. return "操作失败";
  245. }
  246. }