AggregateController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Jobs\Order\OrderDeliverInfoManage;
  4. use App\Jobs\Shop\ShopOrderDeliverInfoManage;
  5. use App\Lib\WeApp\WeApp;
  6. use App\Models\Shop\WxShopOrder;
  7. use App\Models\User\WxUser;
  8. use App\Models\User\WxUserCoinRecord;
  9. use App\Models\WxDomainAuth;
  10. use App\Models\WxOrder;
  11. use App\Wen\Utils\Settings;
  12. use App\Wen\Utils\UserUtils;
  13. use Carbon\Carbon;
  14. use Illuminate\Http\Request;
  15. use Intervention\Image\Facades\Image;
  16. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  17. class AggregateController extends BaseController
  18. {
  19. /**
  20. * 对用户聚合
  21. * @param Request $request
  22. */
  23. public function user_hanlder(Request $request){
  24. $action = $request->action;
  25. if(_empty_($action)){
  26. return $this->fail(200001);
  27. }
  28. switch ($action) {
  29. case "mute":
  30. return $this->mute_user($request);
  31. break;
  32. case "unmute":
  33. return $this->unmute_user($request);
  34. break;
  35. case "ban":
  36. return $this->ban_user($request);
  37. break;
  38. case "unban":
  39. return $this->unban_user($request);
  40. break;
  41. case "authorization":
  42. return $this->get_user_authorization($request);
  43. break;
  44. case "mini_deliver_info_confirm_first_order":
  45. return $this->mini_deliver_info_confirm_first_order($request);
  46. break;
  47. case "ban_mute_detail":
  48. return $this->ban_mute_detail($request);
  49. break;
  50. default:
  51. echo "Your favorite color is neither red, blue, nor green!";
  52. }
  53. }
  54. private function ban_mute_detail(Request &$request){
  55. $user_id = $request->user_id;
  56. if(_empty_($user_id)){
  57. return $this->fail(503002);
  58. }
  59. $the_user = WxUser::find($user_id);
  60. if(_empty_($the_user)){
  61. return $this->fail(200003);
  62. }
  63. if($the_user->user_state === 2){
  64. $ban_until = get_user_meta($the_user->id, 'ban_until', 's');
  65. if(time() > $ban_until){
  66. // 解除封号
  67. if(UserUtils::unlock_user($the_user, 2)){
  68. $the_user->state_tip = '正常用户';
  69. $the_user->user_state = 0;
  70. }
  71. }
  72. if($ban_until && $the_user->user_state === 2){
  73. $ban_until_ = Carbon::createFromTimestamp($ban_until);
  74. $the_user->state_tip = '该用户被封号至'.$ban_until_->toDateTimeString();
  75. $the_user->ban_until = $ban_until_->toDateTimeString();
  76. $secondsDifference = $ban_until_->diffInSeconds(Carbon::now());
  77. $hoursDifference = round($secondsDifference / 3600, 2);
  78. global $__MINI_GLOBAL_TENANT_ID__;
  79. $tmp_tenant_id = -1;
  80. if($__MINI_GLOBAL_TENANT_ID__ != $the_user->tenant_id){
  81. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  82. $__MINI_GLOBAL_TENANT_ID__ = $the_user->tenant_id;
  83. }
  84. $the_user->cost_balance = round($hoursDifference * Settings::get('app_unban_one_hour_balance', 1.0, true), 2);
  85. if($tmp_tenant_id > 0){
  86. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  87. }
  88. }
  89. }else if($the_user->user_state === 3){
  90. $mute_until = get_user_meta($the_user->id, 'mute_until', 's');
  91. if(time() > $mute_until){
  92. // 解除禁言
  93. if(UserUtils::unlock_user($the_user, 3)){
  94. $the_user->state_tip = '正常用户';
  95. $the_user->user_state = 0;
  96. }
  97. }
  98. if($mute_until && $the_user->user_state === 3){
  99. $mute_until_ = Carbon::createFromTimestamp($mute_until);
  100. $the_user->state_tip = '该用户被禁言至'.$mute_until_->toDateTimeString();
  101. $the_user->mute_until = $mute_until_->toDateTimeString();
  102. $secondsDifference = $mute_until_->diffInSeconds(Carbon::now());
  103. $hoursDifference = round($secondsDifference / 3600, 2);
  104. global $__MINI_GLOBAL_TENANT_ID__;
  105. $tmp_tenant_id = -1;
  106. if($__MINI_GLOBAL_TENANT_ID__ != $the_user->tenant_id){
  107. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  108. $__MINI_GLOBAL_TENANT_ID__ = $the_user->tenant_id;
  109. }
  110. $the_user->cost_balance = round($hoursDifference * Settings::get('app_unmute_one_hour_balance', 0.5, true), 2);
  111. if($tmp_tenant_id > 0){
  112. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  113. }
  114. }
  115. }
  116. return $this->success($the_user);
  117. }
  118. /** 获取配置
  119. * @param Request $request
  120. */
  121. public function config(Request $request){
  122. $type = _empty_default_($request->type, '');
  123. if(_empty_($type)){
  124. return $this->fail(200001);
  125. }
  126. switch ($type) {
  127. case "inspire":
  128. return $this->inspire_config($request);
  129. break;
  130. case "jssdk":
  131. return $this->get_jssdk_config($request);
  132. break;
  133. case "voter-template-type":
  134. return $this->get_voter_template_type($request);
  135. break;
  136. case "get-withpara-mpservicer-code":
  137. return $this->get_withpara_mpservicer_code($request);
  138. break;
  139. case 'note-config':
  140. return $this->get_note_config($request);
  141. break;
  142. default:
  143. echo "Your favorite color is neither red, blue, nor green!";
  144. }
  145. }
  146. private function get_note_config(Request &$request){
  147. $config = [
  148. 'middle' => [
  149. 'box' => Settings::get('app_note_middle_box', [])
  150. ],
  151. 'bottom' => [
  152. 'box' => Settings::get('app_note_bottom_box', [])
  153. ]
  154. ];
  155. return $this->success($config);
  156. }
  157. private function get_user_authorization(Request &$request){
  158. $uid = $request->uid;
  159. $user_id = _empty_default_($request->user_id, 0);
  160. $domain = _empty_default_($request->domain, '');
  161. if(_empty_($uid) && _empty_($domain) && _empty_($user_id)){
  162. return $this->fail(503002);
  163. }
  164. if(!_empty_($user_id)){
  165. $auth = WxDomainAuth::where('user_id', $user_id)->first();
  166. if($auth){
  167. return $this->success(['id'=>$auth->id, 'user_id'=>$auth->user_id]);
  168. }else{
  169. return $this->fail(200003);
  170. }
  171. }else if(!_empty_($domain)){
  172. $auth = WxDomainAuth::where('domain_1', $domain)->orWhere('domain_2', $domain)
  173. ->orWhere('domain_3', $domain)->orWhere('domain_4', $domain)
  174. ->orWhere('domain_5', $domain)->orWhere('domain_6', $domain)
  175. ->orWhere('domain_7', $domain)->first();
  176. }else{
  177. $auth = WxDomainAuth::where('user_id', $uid)->first();
  178. }
  179. if($auth){
  180. return $this->success($auth);
  181. }
  182. return $this->fail(200003);
  183. }
  184. private function get_withpara_mpservicer_code(Request &$request){
  185. $uid = $request->uid;
  186. if(_empty_($uid)){
  187. return $this->fail(200000);
  188. }
  189. global $__MINI_GLOBAL_TENANT_ID__;
  190. $action = _empty_default_($request->code_action, 'bind');
  191. $scene = _empty_default_($request->scene, '');
  192. $scene_id = _empty_default_($request->scene_id, '');
  193. if($action == 'scene'){
  194. if(_empty_($scene)){
  195. return $this->fail(200001);
  196. }
  197. $scene_str = 'user:scene:'.$uid.':'.$scene.':'.$__MINI_GLOBAL_TENANT_ID__;
  198. if($scene_id > 0){
  199. $scene_str .= ':'.$scene_id;
  200. }
  201. }else{
  202. $scene_str = 'user:bind:'.$uid;
  203. }
  204. $weapp = new WeApp('mp');
  205. $mpServicer = $weapp->getMpServicer();
  206. try {
  207. $url = $mpServicer->getQrCodeWithPara(0, $scene_str);
  208. }catch (\Exception $e){
  209. return $this->fail(200006, [], '生成公众号二维码失败,请检查[全局配置-支付-微信-公众号]');
  210. }
  211. if($url){
  212. $qrCode = QrCode::format('png')->size(300)->generate($url);
  213. $image = Image::make($qrCode);
  214. if($action == 'bind'){
  215. $image->resizeCanvas(300, 300 + 60, 'bottom', false, '#ffffff'); // 增加高度,保持宽高比,背景色为白色
  216. $mp_app_name = Settings::get('mp_app_name', '');
  217. $text_tip = '关注'.$mp_app_name.'公众号,可以更方便通知到您';
  218. if(mb_strlen($mp_app_name) > 4){
  219. $text_tip = '关注'.$mp_app_name.'公众号';
  220. }
  221. $image->text($text_tip, 150, 40, function($font){
  222. $font->file(public_path('storage/font/DingTalk_JinBuTi_Regular.ttf')); // 字体文件路径
  223. $font->size(13); // 字体大小
  224. $font->color('#000000'); // 字体颜色
  225. $font->align('center'); // 水平对齐
  226. $font->valign('top'); // 垂直对齐
  227. });
  228. }
  229. $base64 = 'data:image/png;base64,' . base64_encode($image->encode('png')->getEncoded());
  230. return $this->success($base64);
  231. }
  232. return $this->fail(200006);
  233. }
  234. private function get_voter_template_type(Request &$request){
  235. return $this->success([
  236. [
  237. "id" => 0,
  238. "name" => "默认"
  239. ]
  240. ]);
  241. }
  242. private function get_jssdk_config(Request &$request){
  243. $url = _empty_default_($request->url, Settings::get('app_h5_home', 'https://mini.h5.minisns.cn'));
  244. $weapp = new WeApp('mp');
  245. $mps = $weapp->getMpServicer();
  246. if($mps){
  247. return $this->success($mps->getJsSdkConfig($url));
  248. }
  249. return $this->fail(200008, [], '管理员未配置公众号的appid或appsecret');
  250. }
  251. private function mini_deliver_info_confirm_first_order(Request &$request){
  252. $uid = $request->uid;
  253. if(_empty_($uid)){
  254. return $this->fail(200000);
  255. }
  256. $order = WxOrder::where('user_id', $uid)->whereNotNull('order_serial_number')->where('order_serial_number', '<>', '')->where('order_serial_platform', 0)
  257. ->where('order_serial_platform_type', 'mini')->where('order_state', 5)->orderBy('id', 'desc')->first();
  258. if($order){
  259. OrderDeliverInfoManage::dispatch('query', $order->id);
  260. return $this->success([
  261. 'type' => 'order',
  262. 'transaction_id' => $order->order_serial_number,
  263. ]);
  264. }else{
  265. $shop_order = WxShopOrder::withTrashed()->where('user_id', $uid)->where('pay_status', 2)->whereNull('parent_order_id')->whereIn('status', [8, 10])->whereNotNull('serial_number')->where('serial_number', '<>', '')->where('serial_platform', 0)
  266. ->where('serial_platform_type', 'mini')->orderBy('id', 'desc')->first();
  267. if($shop_order){
  268. if($shop_order->status == 8){
  269. ShopOrderDeliverInfoManage::dispatch('query', $shop_order->id, null);
  270. }else if($shop_order->status == 10){
  271. ShopOrderDeliverInfoManage::dispatch('virtual-query', $shop_order->id, null);
  272. }else if($shop_order->status == 9){
  273. ShopOrderDeliverInfoManage::dispatch('virtual-upload', $shop_order->id, null);
  274. }
  275. return $this->success([
  276. 'type' => 'shop_order',
  277. 'transaction_id' => $shop_order->serial_number,
  278. ]);
  279. }
  280. }
  281. return $this->fail(200003);
  282. }
  283. private function unban_user(Request &$request){
  284. $uid = $request->uid;
  285. if(_empty_($uid)){
  286. return $this->fail(200000);
  287. }
  288. if(!UserUtils::is_mini_supder_admin($uid)){
  289. return $this->fail(200000);
  290. }
  291. // 参数
  292. $user_id = _empty_default_($request->user_id, 0);
  293. if(_empty_($user_id)){
  294. return $this->fail(200001);
  295. }
  296. $the_user = WxUser::find($user_id);
  297. if(UserUtils::unlock_user($the_user, 2)){
  298. return $this->success([], 200, '已解除');
  299. }else{
  300. return $this->fail(200002);
  301. }
  302. }
  303. private function unmute_user(Request &$request){
  304. $uid = $request->uid;
  305. if(_empty_($uid)){
  306. return $this->fail(200000);
  307. }
  308. if(!UserUtils::is_mini_supder_admin($uid)){
  309. return $this->fail(200000);
  310. }
  311. // 参数
  312. $user_id = _empty_default_($request->user_id, 0);
  313. if(_empty_($user_id)){
  314. return $this->fail(200001);
  315. }
  316. $the_user = WxUser::find($user_id);
  317. if(UserUtils::unlock_user($the_user, 3)){
  318. return $this->success([], 200, '已解除');
  319. }else{
  320. return $this->fail(200002);
  321. }
  322. }
  323. private function ban_user(Request &$request){
  324. $uid = $request->uid;
  325. if(_empty_($uid)){
  326. return $this->fail(200000);
  327. }
  328. if(!UserUtils::is_mini_supder_admin($uid)){
  329. return $this->fail(200000);
  330. }
  331. // 参数
  332. $user_id = _empty_default_($request->user_id, 0);
  333. if(_empty_($user_id)){
  334. return $this->fail(200001);
  335. }
  336. $days = _empty_default_($request->days, 0);
  337. if(_empty_($days)){
  338. return $this->fail(200001);
  339. }
  340. if(!in_array($days, [7, 30, 90, 3650])){
  341. return $this->fail(200004);
  342. }
  343. $the_user = WxUser::find($user_id);
  344. if(UserUtils::lock_user_incre($the_user, 2, $days * 3600 * 24)){
  345. return $this->success([], 200, '已将该用户封号'.$days.'天');
  346. }else{
  347. return $this->fail(200002);
  348. }
  349. }
  350. private function mute_user(Request &$request){
  351. $uid = $request->uid;
  352. if(_empty_($uid)){
  353. return $this->fail(200000);
  354. }
  355. if(!UserUtils::is_mini_supder_admin($uid)){
  356. return $this->fail(200000);
  357. }
  358. // 参数
  359. $user_id = _empty_default_($request->user_id, 0);
  360. if(_empty_($user_id)){
  361. return $this->fail(200001);
  362. }
  363. $days = _empty_default_($request->days, 0);
  364. if(_empty_($days)){
  365. return $this->fail(200001);
  366. }
  367. if(!in_array($days, [3, 7, 30, 90])){
  368. return $this->fail(200004);
  369. }
  370. $the_user = WxUser::find($user_id);
  371. if(UserUtils::lock_user_incre($the_user, 3, $days * 3600 * 24)){
  372. return $this->success([], 200, '已将该用户禁言'.$days.'天');
  373. }else{
  374. return $this->fail(200002);
  375. }
  376. }
  377. private function inspire_config(Request &$request){
  378. global $__MINI_GLOBAL_CURRENT_USER_ID__;
  379. if(_empty_($__MINI_GLOBAL_CURRENT_USER_ID__)){
  380. return $this->fail(503002);
  381. }
  382. global $__MINI_GLOBAL_DEVICE__;
  383. if($request->isMethod('GET')){
  384. // 总共观看了多少次,获得了多少金币,今天观看了几次,今天上限次数
  385. $today_count = WxUserCoinRecord::where([
  386. ['user_id', '=', $__MINI_GLOBAL_CURRENT_USER_ID__],
  387. ['type', '=', 10],
  388. ['created_at', '>=', Carbon::today()]
  389. ])->count();
  390. $total_count = WxUserCoinRecord::where([
  391. ['user_id', '=', $__MINI_GLOBAL_CURRENT_USER_ID__],
  392. ['type', '=', 10]
  393. ])->count();
  394. $total_coin_count = WxUserCoinRecord::where([
  395. ['user_id', '=', $__MINI_GLOBAL_CURRENT_USER_ID__],
  396. ['type', '=', 10]
  397. ])->sum('incre');
  398. if($__MINI_GLOBAL_DEVICE__ == 'mp'){
  399. $every_day_max = Settings::get('ad_mp_reward_every_day_times', 10);
  400. }else{
  401. $every_day_max = Settings::get('ad_uni_reward_every_day_times', 10);
  402. }
  403. return $this->success([
  404. 'total_times' => $total_count,
  405. 'today_times' => $today_count,
  406. 'total_coins' => $total_coin_count,
  407. 'every_day_max' => $every_day_max
  408. ]);
  409. }else if($request->isMethod('POST')){
  410. }
  411. }
  412. }