WechatSubscribeService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. *
  4. * @file:WechatSubscribeService.php
  5. * @Date: 2023/7/10
  6. * @Time: 09:35
  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\WechatAccountSubscribeDetail;
  13. use Modules\WechatPlatform\Models\WechatSubscribeMsg;
  14. class WechatSubscribeService extends BaseService
  15. {
  16. const KEYWORD_SET_KEY = "miniprogram_wechat_subscribe";
  17. protected static array $hideField = ['updated_at', 'is_del', 'del_at', 'user_id', 'puser_id','send_total', 'send_content']; // 公用的影藏字段
  18. /**
  19. * 获取配置
  20. * name: getConfig
  21. * @param $userId
  22. * @param $puserId
  23. * @param $miniprogramId
  24. * @return int
  25. * date 2023/07/10 09:40
  26. */
  27. public static function getConfig($userId, $puserId, $miniprogramId)
  28. {
  29. $set = MiniprogramWechatGlobalConfig::where(['user_id' => $userId,'puser_id' => $puserId,'miniprogram_id' => $miniprogramId])
  30. ->where('type',self::KEYWORD_SET_KEY)->value('value');
  31. return ['data' => $set == 1 ? 1 : 0];
  32. }
  33. /**
  34. * 保存配置
  35. * name: setConfig
  36. * @param $param
  37. * @return string
  38. * date 2023/07/10 09:39
  39. */
  40. public static function setConfig($param)
  41. {
  42. $param['type'] = self::KEYWORD_SET_KEY;
  43. $res = MiniprogramWechatGlobalConfig::updateOrCreate(
  44. ['user_id' => $param['user_id'],'puser_id' => $param['puser_id'],'miniprogram_id' => $param['miniprogram_id'],'type' => self::KEYWORD_SET_KEY],
  45. $param
  46. );
  47. if($res){
  48. return ['msg' => "操作成功"];
  49. }
  50. self::throwErrMsg("操作失败");
  51. }
  52. public static function addSubscribe(mixed $param)
  53. {
  54. $res = WechatSubscribeMsg::create($param);
  55. if ($res) {
  56. return ['msg' => "操作成功"];
  57. }
  58. self::throwErrMsg("操作失败");
  59. }
  60. /**
  61. * 获取详情
  62. * name: getDetail
  63. * @param $id
  64. * date 2023/07/10 10:08
  65. */
  66. public static function getDetail($id)
  67. {
  68. return WechatSubscribeMsg::query()->where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts']));
  69. }
  70. public static function allocation($id, $wxAuthIds)
  71. {
  72. $info = WechatSubscribeMsg::query()->where('id', $id)->where('is_del', 0)->first();
  73. if (is_empty($info)) {
  74. self::throwErrMsg('该关注回复不存在或已删除');
  75. }
  76. $data = [];
  77. $list = [];
  78. $appIds =[];
  79. if (empty($wxAuthIds)){
  80. $data['wechat_accounts'] = [];
  81. }else {
  82. $wechatAccountInfos = DB::table('wechat_authorization_infos')
  83. ->whereIn('id', $wxAuthIds)
  84. ->where('is_enabled', 1)
  85. ->where('user_id', $info->user_id)
  86. ->get();
  87. if ($wechatAccountInfos->isEmpty()) {
  88. self::throwErrMsg("优化师对提交的公众号没有使用权限");
  89. }
  90. $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
  91. $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
  92. if (count($canNotUsed) > 0) {
  93. self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
  94. }
  95. foreach ($wechatAccountInfos as $val){
  96. $data['wechat_accounts'][] = [
  97. 'id' => $val->id,
  98. 'appid' =>$val->authorizer_appid,
  99. 'nick_name' => $val->nick_name,
  100. 'component_appid' => $val->component_appid
  101. ];
  102. $appIds[] = $val->authorizer_appid;
  103. $list[] = [
  104. 'subscribe_id' => $info->id,
  105. 'user_id' => $info->user_id,
  106. 'puser_id' => $info->puser_id,
  107. 'miniprogram_id' => $info->miniprogram_id,
  108. 'appid' => $val->authorizer_appid,
  109. 'wechat_authorization_info_id'=> $val->id,
  110. 'nick_name' => $val->nick_name,
  111. 'content' => $info->send_content,
  112. 'status' => 1,
  113. ];
  114. }
  115. unset($wechatAccountInfos);
  116. }
  117. DB::beginTransaction();
  118. try {
  119. if (!empty($appIds)){
  120. // 关闭所有要配置的公众号关注回复
  121. WechatAccountSubscribeDetail::whereIn('appid',$appIds)->update(['status' => 0]);
  122. foreach ($list as $val){
  123. WechatAccountSubscribeDetail::updateOrCreate(
  124. [
  125. 'subscribe_id' => $val['subscribe_id'],
  126. 'miniprogram_id' => $val['miniprogram_id'],
  127. 'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
  128. ],$val);
  129. }
  130. }else{
  131. // 关闭当前回复所涉及的公众号关注回复
  132. WechatAccountSubscribeDetail::where('subscribe_id',$id)->update(['status' => 0]);
  133. }
  134. WechatSubscribeMsg::where('id',$id)->update($data);
  135. DB::commit();
  136. }catch (\Exception $exception){
  137. self::throwErrMsg('操作失败');
  138. }
  139. return "操作成功";
  140. }
  141. /**
  142. * 编辑
  143. * name: updateSubscribe
  144. * @param $id
  145. * @param mixed $param
  146. * @return string
  147. * date 2023/07/10 11:03
  148. */
  149. public static function updateSubscribe($id, mixed $param)
  150. {
  151. $info = WechatSubscribeMsg::query()->where('id', $id)->where('is_del', 0)->first();
  152. if (is_empty($info)) {
  153. self::throwErrMsg('该关注回复不存在或已删除');
  154. }
  155. DB::beginTransaction();
  156. try {
  157. WechatSubscribeMsg::query()->where('id',$id)->update($param);
  158. WechatAccountSubscribeDetail::where('subscribe_id',$info->id)->update(['content' => $param['send_content']]);
  159. DB::commit();
  160. }catch (\Exception $exception){
  161. self::throwErrMsg('操作失败');
  162. }
  163. return "操作成功";
  164. }
  165. /**
  166. * 删除
  167. * name: del
  168. * @param array $ids
  169. * date 2023/07/10 11:03
  170. */
  171. public static function del(array $ids)
  172. {
  173. if (empty($ids)) {
  174. self::throwErrMsg('要删除的数据不能为空');
  175. }
  176. DB::beginTransaction();
  177. try {
  178. WechatSubscribeMsg::query()->whereIn('id',$ids)->update(['is_del' => 1, 'del_at' => get_date()]);
  179. WechatAccountSubscribeDetail::query()->whereIn('subscribe_id',$ids)->update(['status' => 0]);
  180. DB::commit();
  181. }catch (\Exception $exception){
  182. self::throwErrMsg('操作失败');
  183. }
  184. return "操作成功";
  185. }
  186. public static function WechaAccountAuthListInfo($id, mixed $userId)
  187. {
  188. $list = DB::table('wechat_authorization_infos')
  189. ->where('user_id', $userId)
  190. ->select('id', 'nick_name', 'is_enabled')->get();
  191. $authList = WechatAccountSubscribeDetail::where('subscribe_id', $id)->get();
  192. if (!$list->isEmpty()) {
  193. foreach ($list as $val) {
  194. $val->is_auth = 0;
  195. $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
  196. if (getProp($info, 'status') == 1) {
  197. $val->is_auth = 1;
  198. }
  199. if ($val->is_enabled != 1) {
  200. $val->nick_name .= "(取消授权)";
  201. }
  202. unset($val->is_enabled);
  203. }
  204. }
  205. return $list;
  206. }
  207. /**
  208. * 关注回复列表
  209. * name: list
  210. * @param array $param
  211. * date 2023/07/10 11:38
  212. */
  213. public static function list(array $param)
  214. {
  215. $sql = self::getQuery($param)->orderBy('id','desc');
  216. $isAll = getProp($param,'is_all',false);
  217. if($isAll){
  218. $list = $sql->get();
  219. }else{
  220. $list = $sql->paginate(getProp($param,'limit',10));
  221. }
  222. $list->makeHidden(array_merge(self::$hideField));
  223. foreach ($list as $val){
  224. $val->wechat_accounts = WechatAccountSubscribeDetail::query()->where(
  225. ['subscribe_id' => $val->id,
  226. 'status' => 1
  227. ])->select('wechat_authorization_info_id as id',"appid",'nick_name')->get();
  228. }
  229. return $list;
  230. }
  231. private static function getQuery(array $param)
  232. {
  233. $sql = WechatSubscribeMsg::query()->where('is_del',0);
  234. if (getProp($param, 'puser_id')) {
  235. $sql->where('puser_id', $param['puser_id']);
  236. }
  237. if (getProp($param, 'user_id')) {
  238. $sql->where('user_id', $param['user_id']);
  239. }
  240. if (getProp($param, 'keyword')) {
  241. $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
  242. }
  243. if(getProp($param,'miniprogram_id')){
  244. $sql->where('miniprogram_id', $param['miniprogram_id']);
  245. }
  246. if(getProp($param,'wechat_authorization_info_id')){
  247. $ids = WechatAccountSubscribeDetail::query()->where([
  248. 'wechat_authorization_info_id' => $param['wechat_authorization_info_id'],
  249. 'status' => 1,
  250. ])->pluck('weacht_keyworld_id')->toArray();
  251. $sql->whereIn('id',$ids);
  252. }
  253. return $sql;
  254. }
  255. /**
  256. * 获取关注回复内容
  257. * name: getContent
  258. * @param $wechatAppId
  259. * @return mixed|string
  260. * date 2023/07/10 18:37
  261. */
  262. public static function getContent($wechatAppId)
  263. {
  264. $info = WechatAccountSubscribeDetail::where([
  265. 'status' => 1,
  266. 'wechat_authorization_info_id' => $wechatAppId,
  267. ])->first();
  268. if (is_empty($info)) {
  269. return "";
  270. }
  271. $set = self::getConfig($info->user_id,$info->puser_id,$info->miniprogram_id);
  272. if (getProp($set,'data') == 0){
  273. return '';
  274. }
  275. WechatAccountSubscribeDetail::where('id', $info->id)->increment('send_total', 1);
  276. WechatSubscribeMsg::where('id', $info->weacht_keyworld_id)->increment('send_total', 1);
  277. return $info->content;
  278. }
  279. }