企微短剧业务系统

WelcomeMsgService.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\DjUser;
  5. use App\Models\MassMsgLinkRecord;
  6. use App\Models\System\Users;
  7. use App\Models\WelcomeMsg;
  8. use App\Models\WelcomeMsgRelation;
  9. use Illuminate\Support\Facades\DB;
  10. class WelcomeMsgService
  11. {
  12. /**
  13. * 设置新用户欢迎语发送规则
  14. * */
  15. public static function setRule($ruleId, $params)
  16. {
  17. try {
  18. # 验证欢迎语内容
  19. $msgData = isset($params['msg_data']) ? $params['msg_data'] : [];
  20. if(!is_array($msgData)) $msgData = json_decode($msgData, true);
  21. $errno = WelcomeMsgService::msgDataVerify($msgData);
  22. if($errno) return $errno;
  23. $adminId = $params['admin_id'];
  24. $corpid = $params['corpid'];
  25. $isForAll = $params['is_for_all'];
  26. $users = $params['users'];
  27. $name = $params['name'];
  28. $sysGroupId = $params['sys_group_id'];
  29. if(!$isForAll && empty($users)) {
  30. return 2204;
  31. }
  32. DB::beginTransaction();
  33. # 写入数据到主表
  34. $relationId = WelcomeMsgRelation::setMsgRelation($ruleId, $name, $corpid, $adminId, $isForAll, $users, $sysGroupId);
  35. if(!$relationId) {
  36. DB::rollBack();
  37. return 2203;
  38. }
  39. # 设置时间规则及消息内容到消息表
  40. foreach ($msgData as $msgRule) {
  41. $msgId = isset($msgRule['msg_id']) ? $msgRule['msg_id'] : 0;
  42. $weeks = $msgRule['weeks'];
  43. $isDayParting = $msgRule['is_day_parting'];
  44. $startTime = $msgRule['start_time'];
  45. $endTime = $msgRule['end_time'];
  46. $content = $msgRule['content'];
  47. $attachments = html_entity_decode($msgRule['attachments']);
  48. $operate = isset($msgRule['operate']) ? $msgRule['operate'] : '';
  49. if($msgId && $operate=='del') { // 删除子规则操作
  50. $result = WelcomeMsg::where('id', $msgId)->update(['enable' => 0]);
  51. if(!$result) {
  52. DB::rollBack();
  53. return 2203;
  54. }
  55. continue;
  56. }
  57. $result = WelcomeMsg::setMsg(
  58. $msgId, $weeks, $startTime, $endTime, $content, $attachments, $relationId, $isDayParting, $sysGroupId
  59. );
  60. if(!$result) {
  61. DB::rollBack();
  62. return 2203;
  63. }
  64. }
  65. DB::commit();
  66. } catch (\Exception $e) {
  67. Log::logError('设置新用户欢迎语发送规则过程发生异常', [
  68. 'line' => $e->getLine(),
  69. 'msg' => $e->getMessage(),
  70. 'data' => $params
  71. ], 'WelcomeMsgRuleSet-Exception');
  72. return 2202;
  73. }
  74. return 0;
  75. }
  76. /**
  77. * 获取新消息欢迎语规则列表
  78. * @param $corpid string 企业ID
  79. * @param $page integer 当前页码数
  80. * @param $pageSize integer 每页显示条数
  81. * */
  82. public static function ruleList($corpid, $userId, $status, $page, $pageSize, &$errno)
  83. {
  84. try {
  85. list($list, $count) = WelcomeMsgRelation::getRuleLists($corpid, $userId, $status, $page, $pageSize);
  86. # 获取创建人信息
  87. $adminIds = $list->pluck('admin_id');
  88. $adminData = Users::select(['id','name'])->whereIn('id', $adminIds)->get();
  89. foreach ($list as $datum) {
  90. # 创建人信息
  91. $adminInfo = $adminData->where('id', $datum->admin_id)->first();
  92. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  93. # 使用成员信息
  94. $users = $datum->users;
  95. $isForAll = $datum->is_for_all;
  96. if($isForAll) {
  97. $userList = DjUser::where('corpid', $corpid)->pluck('name')->toArray();
  98. } else {
  99. $userIds = explode(',', trim($users, ','));
  100. $userList = DjUser::selectRaw("distinct(name)")->whereIn('user_id', $userIds)
  101. ->where('corpid', $corpid)
  102. ->get();
  103. $userList = $userList->pluck('name');
  104. }
  105. $datum->user_list = $userList;
  106. $datum->rule_id = $datum->id;
  107. unset($datum->admin_id, $datum->id);
  108. }
  109. } catch (\Exception $e) {
  110. Log::logError('获取新消息欢迎语规则列表发生异常', [
  111. 'line' => $e->getLine(),
  112. 'msg' => $e->getMessage()
  113. ], 'WelcomeMsgList-Exception');
  114. $errno = 2205;
  115. return [[], 0];
  116. }
  117. return [$list, $count];
  118. }
  119. /**
  120. * 获取欢迎语详情
  121. * */
  122. public static function ruleDetail($corpid, $ruleId, &$errno)
  123. {
  124. try{
  125. $detail = WelcomeMsgRelation::selectRaw('id as rule_id, admin_id, corpid, name, is_for_all, users, enable')
  126. ->where('corpid', $corpid)->where('id', $ruleId)
  127. ->where('is_del', 0)
  128. ->first();
  129. if(empty($detail)) return [];
  130. # 获取消息列表
  131. $msgList = WelcomeMsg::selectRaw("id as msg_id, weeks, start_time, end_time, content, attachments, is_day_parting")
  132. ->where('relation_id', $detail->rule_id)->where('enable', 1)->get();
  133. # 附件信息处理
  134. foreach($msgList as $msg) {
  135. $attachments = json_decode($msg->attachments, true);
  136. if(!empty($attachments)) {
  137. foreach ($attachments as $key=>&$attachment) {
  138. if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显
  139. $radarId = $attachment['radar']['radar_id'] ?? 0;
  140. $radarInfo = RadarService::getRadarContent($corpid, $radarId);
  141. if(empty($radarInfo)) {
  142. unset($attachment[$key]);
  143. continue;
  144. }
  145. $attachment['radar'] = $radarInfo;
  146. }
  147. }
  148. }
  149. $msg->attachments = json_encode($attachments, 256);
  150. }
  151. $detail->msg_list = $msgList;
  152. } catch (\Exception $e) {
  153. Log::logError('欢迎语详情获取过程发生异常', [
  154. 'line' => $e->getLine(),
  155. 'msg' => $e->getMessage(),
  156. 'rule_id' => $ruleId
  157. ], 'ruleDetail');
  158. $errno = 2206;
  159. return [];
  160. }
  161. return $detail;
  162. }
  163. /**
  164. * 删除新用户欢迎语
  165. * */
  166. public static function updateStatus($corpid, $ruleId, $status)
  167. {
  168. try {
  169. # 验证规则是否存在
  170. $isExist = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->exists();
  171. if(!$isExist) return 2207;
  172. DB::beginTransaction();
  173. # 变更规则状态
  174. $result = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->update(['enable' => $status]);
  175. if(!$result) {
  176. DB::rollBack();
  177. return 2208;
  178. }
  179. if(!$status)
  180. # 变更剧集组配置
  181. MassMsgLinkRecord::where('msg_type', 2)->where('rule_id', $ruleId)->update(['enable' => $status]);
  182. DB::commit();
  183. } catch (\Exception $e) {
  184. DB::rollBack();
  185. Log::logError('欢迎语状态变更过程发生异常', [
  186. 'line' => $e->getLine(),
  187. 'msg' => $e->getMessage()
  188. ], 'WelcomeMsgRuleUpdateStatus');
  189. return 2208;
  190. }
  191. return 0;
  192. }
  193. public static function ruleDel($corpid, $ruleId)
  194. {
  195. try {
  196. # 验证规则是否存在
  197. $isExist = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->exists();
  198. if(!$isExist) return 2207;
  199. DB::beginTransaction();
  200. # 软删除
  201. $result = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->update(['is_del' => 1]);
  202. if(!$result) {
  203. DB::rollBack();
  204. return 2209;
  205. }
  206. # 变更剧集组配置
  207. MassMsgLinkRecord::where('msg_type', 2)->where('rule_id', $ruleId)->update(['enable' => 0]);
  208. DB::commit();
  209. } catch (\Exception $e) {
  210. DB::rollBack();
  211. Log::logError('欢迎语删除过程发生异常', [
  212. 'line' => $e->getLine(),
  213. 'msg' => $e->getMessage()
  214. ], 'WelcomeMsgRuleDel');
  215. return 2209;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * 校验新用户欢迎语消息体
  221. * */
  222. public static function msgDataVerify(&$msgData)
  223. {
  224. if(!empty($msgData)) {
  225. foreach ($msgData as $key=>&$msg) {
  226. # 判断附件格式是否合法
  227. if(!empty($msg['attachments'])) {
  228. $attachments = json_decode(html_entity_decode($msg['attachments']), true);
  229. if(!is_array($attachments)) return 2210;
  230. }
  231. # 判断是否是分时段欢迎语
  232. $isDayParting = isset($msg['is_day_parting']) ? $msg['is_day_parting'] : false;
  233. if($isDayParting === false) {
  234. unset($msgData[$key]);
  235. }
  236. }
  237. } else {
  238. return 2201;
  239. }
  240. if(empty($msgData)) return 2201;
  241. return 0;
  242. }
  243. }