WechatKeywordsService.php 9.5 KB

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