KfService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace App\Services\App;
  3. use App\Http\Controllers\Miniprogram\Core;
  4. use App\Http\Controllers\Wechat\Media;
  5. use App\Models\Common\MediaModel;
  6. use App\Models\Fpdx\WxkfModel;
  7. use App\Models\User\UserModel;
  8. use App\Services\Service;
  9. use Illuminate\Support\Facades\Cache;
  10. /**
  11. * Class KfService
  12. * @package App\Services\App
  13. */
  14. class KfService extends Service
  15. {
  16. /**
  17. * 获取小程序客服资源
  18. * @param string $key
  19. * @return bool|string
  20. */
  21. public function getMedia(string $key)
  22. {
  23. $public_id = config('miniprogram.public_id');
  24. $mm = new MediaModel();
  25. $km = $mm->where([['key', $key], ['public_id', $public_id]])->first();
  26. if (collect($km)->isEmpty()) {
  27. return false;
  28. }
  29. if (time() - $km->toArray()['updated_at'] > 86400) {
  30. $resp = Media::uploadImg($km->url, $km->public_id);
  31. if ($resp['code'] == 0) {
  32. $km->fill([
  33. 'updated_at' => time(),
  34. 'value' => $resp['data']['media_id'],
  35. ]);
  36. $km->save();
  37. return $resp['data']['media_id'];
  38. } else {
  39. return false;
  40. }
  41. } else {
  42. return $km->value;
  43. }
  44. }
  45. /**
  46. * @param string $key
  47. * @return bool|mixed
  48. */
  49. public function getOriginMedia(string $key)
  50. {
  51. $public_id = config('miniprogram.public_id');
  52. $mm = new MediaModel();
  53. $km = $mm->where([['key', $key], ['public_id', $public_id]])->first();
  54. if (collect($km)->isEmpty()) {
  55. return false;
  56. }
  57. return $km->url;
  58. }
  59. /**
  60. * hash映射活动微信客服
  61. * @param $uid
  62. * @return string
  63. */
  64. public function hashWxkf($uid)
  65. {
  66. $user = UserModel::findOrFail($uid);
  67. $wxm = new WxkfModel();
  68. if (!empty($user->wxkf)) {
  69. $wxkf = $wxm->where('wxid', $user->wxkf)->first();
  70. if (collect($wxkf)->isEmpty()) {
  71. return false;
  72. }
  73. if ("活动" == $wxkf->type && $wxkf->state == 1) {
  74. return $user->wxkf;
  75. } else {
  76. return false;
  77. }
  78. }
  79. $tmps = array();
  80. $wxs = $wxm->where(array(
  81. ['state', 1],
  82. ['sex', '!=', abs($user->sex - 3)],
  83. ['hour_queue_cnt', '>', 0],
  84. ['type', '活动'],
  85. ))->get();
  86. foreach ($wxs as $wx) {
  87. if ($wx->friend_cnt > 5000) {
  88. continue;
  89. }
  90. if ($wx->today_cnt >= $wx->day_limit) {
  91. continue;
  92. }
  93. if ($wx->today_add_cnt >= $wx->day_add_limit) {
  94. continue;
  95. }
  96. $tmps[] = array(
  97. 'wxid' => $wx->wxid,
  98. );
  99. }
  100. if (count($tmps) == 0) {
  101. return false;
  102. }
  103. $key = $uid % count($tmps);
  104. $user->wxkf = $tmps[$key]['wxid'];
  105. $wxm->where('wxid', $user->wxkf)->increment('today_cnt', 1);
  106. $wxm->where('wxid', $user->wxkf)->decrement('hour_queue_cnt', 1);
  107. if ($user->save()) {
  108. return $user->wxkf;
  109. } else {
  110. return false;
  111. }
  112. }
  113. /**
  114. * hash映射微信审核专员
  115. * @param $uid
  116. * @return string
  117. * @throws \Psr\SimpleCache\InvalidArgumentException
  118. */
  119. public function hashWxshzy($uid)
  120. {
  121. $user = UserModel::findOrFail($uid);
  122. $wxm = new WxkfModel();
  123. if (!empty($user->wxkf)) {
  124. $wxkf = $wxm->where('wxid', $user->wxkf)->first();
  125. if ($wxkf->state == 1 && $wxkf->type == "审核") {
  126. return $user->wxkf;
  127. }
  128. }
  129. $tmps = array();
  130. $wxs = $wxm->where(array(
  131. ['state', 1],
  132. ['sex', '!=', abs($user->sex - 3)],
  133. ['type', '审核'],
  134. ))->get();
  135. list($two, $three, $four, $five) = array("✅", "✅", "✅", "✅");
  136. foreach ($wxs as $wx) {
  137. if ($wx->hour_queue_cnt <= 0) {
  138. $two = "❌";
  139. continue;
  140. }
  141. $two = "✅";
  142. if ($wx->today_cnt >= $wx->day_limit) {
  143. $three = "❌";
  144. continue;
  145. }
  146. $three = "✅";
  147. if ($wx->today_add_cnt >= $wx->day_add_limit) {
  148. $four = "❌";
  149. continue;
  150. }
  151. $four = "✅";
  152. if ($wx->friend_cnt > 5000) {
  153. $five = "❌";
  154. continue;
  155. }
  156. $five = "✅";
  157. $tmps[] = array(
  158. 'wxid' => $wx->wxid,
  159. );
  160. }
  161. if (count($tmps) == 0) {
  162. try {
  163. $ispush = true;
  164. Cache::put("hashwxshzy:{$user->sex}", true);
  165. if ($ispush) {
  166. $one = $wxm->where(array(
  167. ['state', 1],
  168. ['sex', '!=', abs($user->sex - 3)],
  169. ['type', '审核'],
  170. ))->exists() ? "✅" : "❌";
  171. $admins = array(10002, 98047, 103143);
  172. foreach ($admins as $admin) {
  173. $public_id = \config('wechat.fpdx.public_id');
  174. $template_id = "ugxHaIxl5sFf2T00QZdCc0l4qELRUfEtw1HnCE7PDww";
  175. $data = [
  176. 'first' => [
  177. 'value' => "警告⚠️审核专员异常\n 请按照以下步骤排查:\n\n 【男】号【女】号都存在,且处于【上线】状态,并且功能都为【审核】 {$one}\n",
  178. ],
  179. 'keyword1' => [
  180. 'value' => "「小时发码上限」小于 0 {$two}\n",
  181. ],
  182. 'keyword2' => [
  183. 'value' => "「今日发码数」不大于「每日发码上限」{$three}\n",
  184. ],
  185. 'keyword3' => [
  186. 'value' => "「今日好友数」不大于「每日添加好友上限」{$four}\n",
  187. ],
  188. 'keyword4' => [
  189. 'value' => "「好友总数」小于 5000 {$five}\n",
  190. ],
  191. 'remark' => [
  192. 'value' => " 联系开发人员",
  193. "color" => "#FF7E98",
  194. ],
  195. ];
  196. $core = new Core();
  197. $core->template($admin, $template_id, $public_id, "", $data);
  198. }
  199. }
  200. } catch (\Exception $exception) {
  201. }
  202. return false;
  203. }
  204. if (Cache::get("hashwxshzy:{$user->sex}")) {
  205. Cache::delete("hashwxshzy:{$user->sex}");
  206. try {
  207. $admins = array(10002, 98047, 795723, 103143);
  208. foreach ($admins as $admin) {
  209. $public_id = \config('wechat.fpdx.public_id');
  210. $template_id = "ugxHaIxl5sFf2T00QZdCc0l4qELRUfEtw1HnCE7PDww";
  211. $sex = $user->sex == 1 ? "男" : "女";
  212. $data = [
  213. 'first' => [
  214. 'value' => "✅「{$sex}」审核专员恢复正常",
  215. ],
  216. 'keyword1' => [
  217. 'value' => "",
  218. ],
  219. 'keyword2' => [
  220. 'value' => "",
  221. ],
  222. 'keyword3' => [
  223. 'value' => "",
  224. ],
  225. 'keyword4' => [
  226. 'value' => "",
  227. ],
  228. 'remark' => [
  229. 'value' => "",
  230. "color" => "#FF7E98",
  231. ],
  232. ];
  233. $core = new Core();
  234. $core->template($admin, $template_id, $public_id, "", $data);
  235. }
  236. } catch (\Exception $exception) {
  237. }
  238. }
  239. $key = $uid % count($tmps);
  240. $user->wxkf = $tmps[$key]['wxid'];
  241. $wxm->where('wxid', $user->wxkf)->increment('today_cnt', 1);
  242. $wxm->where('wxid', $user->wxkf)->decrement('hour_queue_cnt', 1);
  243. return $tmps[$key]['wxid'];
  244. }
  245. /**
  246. * 清理并记录每日数据
  247. * @return boolean
  248. */
  249. public function clearWxkf()
  250. {
  251. try {
  252. $wxkfmodel = new WxkfModel();
  253. $wxkfs = $wxkfmodel->get();
  254. $data = array();
  255. foreach ($wxkfs as $wxkf) {
  256. $data[] = array(
  257. 'date' => date('Y-m-d'),
  258. 'wxid' => $wxkf->wxid,
  259. 'today_cnt' => $wxkf->today_cnt,
  260. 'today_add_cnt' => $wxkf->today_add_cnt,
  261. 'replay_cnt' => $wxkf->replay_cnt,
  262. );
  263. }
  264. \DB::table('kdgx_fpdx_wxkf_log')->insert($data);
  265. } catch (\Exception $e) {
  266. }
  267. \DB::table('kdgx_fpdx_wxkf')->update(['today_cnt' => 0, 'today_add_cnt' => 0, 'replay_cnt' => 0]);
  268. return true;
  269. }
  270. /**
  271. * 清理每小时数据
  272. * @return boolean
  273. */
  274. public function wxkfHourQueue()
  275. {
  276. \DB::table('kdgx_fpdx_wxkf')->where('type', '活动')->update(['hour_queue_cnt' => 50]);
  277. \DB::table('kdgx_fpdx_wxkf')->where('type', '审核')->update(['hour_queue_cnt' => 200]);
  278. return true;
  279. }
  280. }