Usercenter.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\model\wallet;
  6. use Redis;
  7. /**
  8. * 会员中心,不是个人中心
  9. */
  10. class Usercenter extends Api
  11. {
  12. protected $noNeedLogin = ['test','testredis'];
  13. protected $noNeedRight = '*';
  14. protected $allowFields = [
  15. 'id',
  16. 'username',
  17. 'nickname',
  18. //'truename',//
  19. 'email',
  20. 'mobile',
  21. 'avatar',
  22. 'real_status',
  23. 'gender',
  24. 'height',
  25. 'weight',
  26. 'birthday',
  27. 'bio',
  28. 'audio_bio',
  29. //'alipay_account',//
  30. 'idcard_status',
  31. 'longitude',
  32. 'latitude',
  33. 'cityname',
  34. 'photo_images',
  35. 'education_id',
  36. 'hobby_ids',
  37. 'job_id',
  38. 'marital_id',
  39. 'tag_ids',
  40. 'wages_id',
  41. 'hometown_cityid',
  42. 'wechat_account',
  43. 'secretvideo_status',
  44. ];
  45. public function testredis(){
  46. //redis_matching_set(1,125);
  47. dump(redis_matching_get(1));
  48. $a = cache('?mt_matching_uid_1','',['type'=>'Redis']);
  49. dump($a);
  50. }
  51. public function test(){
  52. $a = [1,4,10,22,66,36,102,45,23,52,35,76,7];
  53. $b = [1,10,7,102];
  54. $c = [10,102];
  55. //dump(array_intersect($a,$b));
  56. $data = array_merge($c,$b);
  57. dump($data);
  58. dump(array_flip(array_flip($data)));
  59. dump(array_flip([]));
  60. }
  61. //获取他人用户信息,留下足迹
  62. public function getuserinfo(){
  63. $uid = input_post('uid',0);
  64. $userinfo = Db::name('user')->field($this->allowFields)->where('id',$uid)->find();
  65. if(!$userinfo){
  66. $this->error('不存在的用户');
  67. }
  68. //用户数据
  69. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  70. $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$uid)->find();
  71. $secretvideo = Db::name('user_secretvideo')->where('user_id',$uid)->find();
  72. $new_data = [
  73. 'age' => birthtime_to_age($userinfo['birthday']),
  74. 'truename' => ($userinfo['idcard_status'] == 1 && isset($idcard_confirm['truename'])) ? $idcard_confirm['truename'] : '',
  75. 'alipay_account' => ($userinfo['idcard_status'] == 1 && isset($idcard_confirm['alipay_account'])) ? $idcard_confirm['alipay_account'] : '',
  76. 'secretvideo' => ($userinfo['secretvideo_status'] == 1 && isset($secretvideo['secretvideo'])) ? $secretvideo['secretvideo'] : '',
  77. ];
  78. //亲密度,登录用户给当前用户花了多少钱
  79. $gift_map = [
  80. 'user_id' => $this->auth->id,
  81. 'user_to_id' => $uid,
  82. ];
  83. $gift_user_typing = Db::name('gift_user_typing')->where($gift_map)->sum('price');//文字聊天送礼物
  84. $gift_user_party = Db::name('gift_user_party')->where($gift_map)->sum('value');//派对音聊送礼物
  85. $gift_user_livebc = Db::name('gift_user_livebc')->where($gift_map)->sum('price');//直播送礼物
  86. $match_map_str = '(user_id = '.$this->auth->id.' and to_user_id = '.$uid.') or (user_id = '.$uid.' and to_user_id = '.$this->auth->id.')';
  87. $match_typing = Db::name('user_match_typing_log')->where($match_map_str)->sum('price'); //聊天
  88. $match_audio = Db::name('user_match_audio_log')->where($match_map_str)->sum('price'); //语音
  89. $match_video = Db::name('user_match_video_log')->where($match_map_str)->sum('price'); //视频
  90. $new_data['qinmi_sum'] = $qinmi_sum = $gift_user_typing + $gift_user_party + $gift_user_livebc + $match_typing + $match_audio + $match_video;
  91. //解锁开关
  92. $new_data['isshow_unlock_wechataccount'] = $qinmi_sum >= 15 ? 1 : 0; //满足15亲密值才展示
  93. if(config('site.open_unlock_wechataccount') == 0){
  94. $new_data['isshow_unlock_wechataccount'] = 0; //总开关,强制隐藏
  95. }
  96. $new_data['isshow_unlock_secretvideo'] = $qinmi_sum >= 15 ? 1 : 0; //满足15亲密值才展示
  97. //上次解锁过期日期
  98. $check_map = [
  99. 'user_id' => $this->auth->id,
  100. 'to_user_id' => $uid,
  101. ];
  102. $order_secretvideo = Db::name('order_secretvideo')->where($check_map)->order('id desc')->value('endtime');
  103. $order_wechataccount = Db::name('order_wechataccount')->where($check_map)->order('id desc')->value('endtime');
  104. $new_data['unlock_secretvideo_endtime'] = !empty($order_secretvideo) ? $order_secretvideo : 0;
  105. $new_data['unlock_secretvideo_status'] = $new_data['unlock_secretvideo_endtime'] > time() ? 1 : 0;
  106. $new_data['unlock_wechataccount_endtime'] = !empty($order_wechataccount) ? $order_wechataccount : 0;
  107. $new_data['unlock_wechataccount_status'] = $new_data['unlock_wechataccount_endtime'] > time() ? 1 : 0;
  108. //提示语而已
  109. $new_data['unlock_secretvideo_notice'] = '是否花费'.config('site.unlock_secretvideo').'金币解锁一周(或年费vip可每日免费解锁一次)?';
  110. $new_data['unlock_wechataccount_notice'] = '是否花费'.config('site.unlock_wechataccount').'金币解锁一周(或年费vip可每日免费解锁一次)?';
  111. //合并
  112. $userinfo = array_merge($userinfo,$new_data);
  113. //枚举
  114. $userinfo['education'] = Db::name('enum_education')->where('id',$userinfo['education_id'])->value('name');
  115. $userinfo['hobby'] = Db::name('enum_hobby')->where('id','IN',$userinfo['hobby_ids'])->field(['id','name'])->select();
  116. $userinfo['job'] = Db::name('enum_job')->where('id',$userinfo['job_id'])->value('name');
  117. $userinfo['marital'] = Db::name('enum_marital')->where('id',$userinfo['marital_id'])->value('name');
  118. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  119. $userinfo['wages'] = Db::name('enum_wages')->where('id',$userinfo['wages_id'])->value('name');
  120. //家乡
  121. $userinfo['hometown_city'] = Db::name('area')->where('id',$userinfo['hometown_cityid'])->value('name');
  122. //vip
  123. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  124. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  125. //是否喜欢和关注
  126. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  127. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  128. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  129. $userinfo['is_like'] = $is_like ? 1 : 0;
  130. //是否拉黑
  131. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  132. $userinfo['is_black'] = $is_black ? 1 : 0;
  133. //关注人数,粉丝人数
  134. $follow_num = Db::name('user_follow')->where(['uid'=>$this->auth->id])->count('id');
  135. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$this->auth->id])->count('id');
  136. $userinfo['follow_num'] = $follow_num;
  137. $userinfo['fans_num'] = $fans_num;
  138. //查看别人信息,就要留下痕迹
  139. $data = [
  140. 'uid' => $this->auth->id,
  141. 'to_uid' => $uid,
  142. ];
  143. $check = Db::name('user_visit')->where($data)->find();
  144. if($check){
  145. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  146. }else{
  147. $data['number'] = 1;
  148. $data['updatetime'] = time();
  149. Db::name('user_visit')->insertGetId($data);
  150. }
  151. $this->success('success',$userinfo);
  152. }
  153. //同城
  154. public function samecity(){
  155. $gender = input_post('gender','all');
  156. $agemin = input_post('agemin',0);
  157. $agemax = input_post('agemax',100);
  158. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  159. // $this->success('success',[]);
  160. }
  161. $map = [
  162. 'user.status' => 1,
  163. // 'user.cityname' => $this->auth->cityname,
  164. 'user.id' => ['neq',$this->auth->id],
  165. // 'user.longitude' => ['neq',''],
  166. // 'user.latitude' => ['neq',''],
  167. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  168. // 'user.is_active' => 1,//完全不考虑直播与语聊的权重,只用活跃做排序
  169. ];
  170. if($gender != 'all'){
  171. $map['user.gender'] = $gender;
  172. }
  173. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  174. //dump($map);
  175. $field = [
  176. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  177. ];
  178. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('is_active desc,active_time desc')->autopage()->select();
  179. //dump($list);
  180. $list = list_domain_image($list,['avatar']);
  181. foreach($list as $key => $one){
  182. $one['age'] = birthtime_to_age($one['birthday']);
  183. // $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  184. $one['distance'] = rand(0,10).'km';
  185. //状态
  186. $one['active_text'] = '在线';
  187. $list[$key] = $one;
  188. }
  189. $this->success('success',$list);
  190. }
  191. //附近
  192. public function nearuser(){
  193. $gender = input_post('gender','all');
  194. $agemin = input_post('agemin',0);
  195. $agemax = input_post('agemax',100);
  196. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  197. // $this->success('success',[]);
  198. }
  199. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  200. $map = [
  201. 'user.status' => 1,
  202. ////'user.cityname' => $this->auth->cityname,
  203. 'user.id' => ['neq',$this->auth->id],
  204. // 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  205. // 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  206. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  207. //'user.is_active' => 1,//完全不考虑直播与语聊的权重,只用活跃做排序
  208. ];
  209. if($gender != 'all'){
  210. $map['user.gender'] = $gender;
  211. }
  212. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  213. //dump($map);
  214. $field = [
  215. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  216. ];
  217. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('is_active desc,active_time desc')->autopage()->select();
  218. //dump($list);exit;
  219. $list = list_domain_image($list,['avatar']);
  220. foreach($list as $key => $one){
  221. $one['age'] = birthtime_to_age($one['birthday']);
  222. //$one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  223. $one['distance'] = rand(0,10).'km';
  224. //状态
  225. $one['active_text'] = '在线';
  226. $list[$key] = $one;
  227. }
  228. $this->success('success',$list);
  229. }
  230. //因为接受者拿不到发起者的uid,废弃了
  231. //视频和语音,接收方使用,如果是性别劣势方,检查钱是否够用
  232. public function video_audio_moneycheck(){
  233. //检测用户,发起方的uid
  234. $to_user_id = input_post('from_user_id');
  235. $to_user_info = Db::name('user')->field('id,real_status,gender')->where('id',$to_user_id)->find();
  236. if(!$to_user_info){
  237. $this->error('不存在的用户');
  238. }
  239. //扣费金币
  240. $type = input_post('type','video'); //类型
  241. $price = $type == 'video' ? config('site.video_min_price') : config('site.audio_min_price');
  242. //发起用户的分数,被发起用户的分数。按性别给分
  243. $auth_level = 0;
  244. $tous_level = 0;
  245. //打分
  246. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  247. $auth_level = 30;//实名女最高
  248. }
  249. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  250. $auth_level = 20;//未实名女次之
  251. }
  252. if($this->auth->gender == 1){
  253. $auth_level = 10;//男性最低
  254. }
  255. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  256. $tous_level = 30;
  257. }
  258. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  259. $tous_level = 20;
  260. }
  261. if($to_user_info['gender'] == 1){
  262. $tous_level = 10;
  263. }
  264. //同性不收钱
  265. //都是男的,不扣钱
  266. //都是实名认证的女性,不扣钱
  267. //都是未实名认证的女性,不扣钱
  268. if($auth_level == $tous_level){
  269. $price = 0;
  270. $this->success('success');
  271. }
  272. //扣钱uid,收钱uid,收钱free_video
  273. //分数少扣钱,分数多收益
  274. if($auth_level < $tous_level){
  275. $kou_user = $this->auth->id;
  276. }else{
  277. $kou_user = $to_user_info['id'];
  278. }
  279. //需要扣我的(接收方的)钱,判断钱是否够
  280. if($price > 0 && $kou_user == $this->auth->id){
  281. Db::startTrans();
  282. $gold = model('wallet')->getWallet($kou_user,'gold');
  283. if(bccomp($price,$gold) == 1){
  284. Db::rollback();
  285. $this->error('金币不足');
  286. }
  287. Db::commit();
  288. }
  289. $this->success('success');
  290. }
  291. //视频通话每分钟调用一次
  292. public function video_onemin(){
  293. //检测用户
  294. $to_user_id = input_post('to_user_id');
  295. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  296. if(!$to_user_info){
  297. $this->error('不存在的用户');
  298. }
  299. //正常价格
  300. $price = config('site.video_min_price'); //扣费金币
  301. $bili = config('site.money_to_gold'); //兑换比例
  302. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  303. $money = bcdiv($price,$bili,2); //对应人民币
  304. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  305. //发起用户的分数,被发起用户的分数。按性别给分
  306. $auth_level = 0;
  307. $tous_level = 0;
  308. //打分
  309. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  310. $auth_level = 30;//实名女最高
  311. }
  312. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  313. $auth_level = 20;//未实名女次之
  314. }
  315. if($this->auth->gender == 1){
  316. $auth_level = 10;//男性最低
  317. }
  318. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  319. $tous_level = 30;
  320. }
  321. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  322. $tous_level = 20;
  323. }
  324. if($to_user_info['gender'] == 1){
  325. $tous_level = 10;
  326. }
  327. //同性不收钱
  328. //都是男的,不扣钱
  329. //都是实名认证的女性,不扣钱
  330. //都是未实名认证的女性,不扣钱
  331. if($auth_level == $tous_level){
  332. $price = 0;$money = 0;
  333. }
  334. Db::startTrans();
  335. //记录日志
  336. $data = [
  337. 'user_id' => $this->auth->id,
  338. 'price' => $price,
  339. 'createtime' => time(),
  340. 'to_user_id' => $to_user_id,
  341. 'money' => $money,
  342. ];
  343. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  344. if(!$log_id){
  345. Db::rollback();
  346. $this->error('扣费失败');
  347. }
  348. //同性别,提前结束
  349. if($auth_level == $tous_level){
  350. Db::commit();
  351. $this->success('success');
  352. }
  353. //扣钱uid,收钱uid,收钱free_video
  354. //分数少扣钱,分数多收益
  355. if($auth_level < $tous_level){
  356. $kou_user = $this->auth->id;
  357. $get_user = $to_user_info['id'];
  358. $get_user_free = $to_user_info['free_video'];
  359. }else{
  360. $kou_user = $to_user_info['id'];
  361. $get_user = $this->auth->id;
  362. $get_user_free = $this->auth->free_video;
  363. }
  364. //需要扣别人的钱,判断钱是否购
  365. if($price > 0 && $kou_user != $this->auth->id){
  366. $gold = model('wallet')->getWallet($kou_user,'gold');
  367. if(bccomp($price,$gold) == 1){
  368. Db::rollback();
  369. $this->error('对方金币不足');
  370. }
  371. }
  372. //有性别差,扣费
  373. if($price > 0){
  374. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,11,'','user_match_video_log',$log_id);
  375. if($rs['status'] === false){
  376. Db::rollback();
  377. $this->error($rs['msg']);
  378. }
  379. }
  380. //另一方加钱,0收费
  381. if($money > 0 && $get_user_free == 0){
  382. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,21,'','user_match_video_log',$log_id);
  383. if($rs['status'] === false){
  384. Db::rollback();
  385. $this->error($rs['msg']);
  386. }
  387. }
  388. Db::commit();
  389. $this->success('success');
  390. }
  391. //语音通话每分钟调用一次
  392. public function audio_onemin(){
  393. //检测用户
  394. $to_user_id = input_post('to_user_id');
  395. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  396. if(!$to_user_info){
  397. $this->error('不存在的用户');
  398. }
  399. //正常价格
  400. $price = config('site.audio_min_price'); //扣费金币
  401. $bili = config('site.money_to_gold'); //兑换比例
  402. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  403. $money = bcdiv($price,$bili,2); //对应人民币
  404. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  405. //发起用户的分数,被发起用户的分数。按性别给分
  406. $auth_level = 0;
  407. $tous_level = 0;
  408. //打分
  409. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  410. $auth_level = 30;//实名女最高
  411. }
  412. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  413. $auth_level = 20;//未实名女次之
  414. }
  415. if($this->auth->gender == 1){
  416. $auth_level = 10;//男性最低
  417. }
  418. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  419. $tous_level = 30;
  420. }
  421. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  422. $tous_level = 20;
  423. }
  424. if($to_user_info['gender'] == 1){
  425. $tous_level = 10;
  426. }
  427. //同性不收钱
  428. //都是男的,不扣钱
  429. //都是实名认证的女性,不扣钱
  430. //都是未实名认证的女性,不扣钱
  431. if($auth_level == $tous_level){
  432. $price = 0;$money = 0;
  433. }
  434. Db::startTrans();
  435. //记录日志
  436. $data = [
  437. 'user_id' => $this->auth->id,
  438. 'price' => $price,
  439. 'createtime' => time(),
  440. 'to_user_id' => $to_user_id,
  441. 'money' => $money,
  442. ];
  443. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  444. if(!$log_id){
  445. Db::rollback();
  446. $this->error('扣费失败');
  447. }
  448. //同性别,提前结束
  449. if($auth_level == $tous_level){
  450. Db::commit();
  451. $this->success('success');
  452. }
  453. //扣钱uid,收钱uid,收钱free_video
  454. //分数少扣钱,分数多收益
  455. if($auth_level < $tous_level){
  456. $kou_user = $this->auth->id;
  457. $get_user = $to_user_info['id'];
  458. $get_user_free = $to_user_info['free_audio'];
  459. }else{
  460. $kou_user = $to_user_info['id'];
  461. $get_user = $this->auth->id;
  462. $get_user_free = $this->auth->free_audio;
  463. }
  464. //需要扣别人的钱,判断钱是否购
  465. if($price > 0 && $kou_user != $this->auth->id){
  466. $gold = model('wallet')->getWallet($kou_user,'gold');
  467. if(bccomp($price,$gold) == 1){
  468. Db::rollback();
  469. $this->error('对方金币不足');
  470. }
  471. }
  472. //有性别差,扣费
  473. if($price > 0){
  474. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,12,'','user_match_audio_log',$log_id);
  475. if($rs['status'] === false){
  476. Db::rollback();
  477. $this->error($rs['msg']);
  478. }
  479. }
  480. //另一方加钱,0收费
  481. if($money > 0 && $get_user_free == 0){
  482. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,22,'','user_match_audio_log',$log_id);
  483. if($rs['status'] === false){
  484. Db::rollback();
  485. $this->error($rs['msg']);
  486. }
  487. }
  488. Db::commit();
  489. $this->success('success');
  490. }
  491. //打字聊天每句话调用一次
  492. public function typing_once(){
  493. //检测用户
  494. $to_user_id = input_post('to_user_id');
  495. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  496. if(!$to_user_info){
  497. $this->error('不存在的用户');
  498. }
  499. //正常价格
  500. $price = config('site.typing_min_price'); //扣费金币
  501. $bili = config('site.money_to_gold'); //兑换比例
  502. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  503. $money = bcdiv($price,$bili,2); //对应人民币
  504. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  505. //发起用户的分数,被发起用户的分数。按性别给分
  506. $auth_level = 0;
  507. $tous_level = 0;
  508. //打分
  509. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  510. $auth_level = 30;//实名女最高
  511. }
  512. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  513. $auth_level = 20;//未实名女次之
  514. }
  515. if($this->auth->gender == 1){
  516. $auth_level = 10;//男性最低
  517. }
  518. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  519. $tous_level = 30;
  520. }
  521. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  522. $tous_level = 20;
  523. }
  524. if($to_user_info['gender'] == 1){
  525. $tous_level = 10;
  526. }
  527. //同性不收钱
  528. //都是男的,不扣钱
  529. //都是实名认证的女性,不扣钱
  530. //都是未实名认证的女性,不扣钱
  531. if($auth_level == $tous_level){
  532. $price = 0;$money = 0;
  533. }
  534. //性别优势的人发起,免费
  535. if($auth_level > $tous_level){
  536. $price = 0;$money = 0;
  537. }
  538. Db::startTrans();
  539. //记录日志
  540. $data = [
  541. 'user_id' => $this->auth->id,
  542. 'price' => $price,
  543. 'createtime' => time(),
  544. 'to_user_id' => $to_user_id,
  545. 'money' => $money,
  546. ];
  547. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  548. if(!$log_id){
  549. Db::rollback();
  550. $this->error('扣费失败');
  551. }
  552. //同性别,提前结束
  553. if($auth_level == $tous_level){
  554. Db::commit();
  555. $this->success('success');
  556. }
  557. //零消费,零收益消费,提前结束,其实这条没必要,下面金钱操作还会过滤一次
  558. if($price == 0 && $money == 0){
  559. Db::commit();
  560. $this->success('success');
  561. }
  562. //扣钱uid,收钱uid,收钱free_video
  563. //分数少扣钱,分数多收益
  564. if($auth_level < $tous_level){
  565. $kou_user = $this->auth->id;
  566. $get_user = $to_user_info['id'];
  567. $get_user_free = $to_user_info['free_typing'];
  568. }else{
  569. //这种已经没有了
  570. $kou_user = $to_user_info['id'];
  571. $get_user = $this->auth->id;
  572. $get_user_free = $this->auth->free_typing;
  573. }
  574. //有性别差,扣费
  575. if($price > 0){
  576. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,13,'','user_match_typing_log',$log_id);
  577. if($rs['status'] === false){
  578. Db::rollback();
  579. $this->error($rs['msg']);
  580. }
  581. }
  582. //另一方加钱,0收费
  583. if($money > 0 && $get_user_free == 0){
  584. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,23,'','user_match_typing_log',$log_id);
  585. if($rs['status'] === false){
  586. Db::rollback();
  587. $this->error($rs['msg']);
  588. }
  589. }
  590. //tag任务赠送金币
  591. //搭讪奖励
  592. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  593. if($task_rs === false){
  594. Db::rollback();
  595. $this->error('完成任务赠送奖励失败');
  596. }
  597. Db::commit();
  598. $this->success('success');
  599. }
  600. //语音匹配
  601. public function getaudiouser(){
  602. //判断资格
  603. /*$start = strtotime(date('Y-m-d'));
  604. $end = $start + 86399;
  605. $map = [
  606. 'user_id' => $this->auth->id,
  607. 'createtime' => ['between',[$start,$end]],
  608. 'price' => 0,
  609. ];
  610. $check = Db::name('user_match_audio_log')->where($map)->find();*/
  611. $check = true;
  612. //已经用掉免费的了,判断金额
  613. if($check){
  614. $price = config('site.audio_min_price');
  615. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  616. if($gold < $price){
  617. $this->error('您的金币已经不足,请充值');
  618. }
  619. }
  620. //找到互关的人,排除
  621. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  622. //dump($follow_me);
  623. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  624. //dump($my_follow);exit;
  625. //给出备选用户
  626. $map = [
  627. 'status' =>1, //未封禁用户
  628. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  629. 'is_online' => 0, //不在语聊间的
  630. 'is_livebc' => 0, //不在直播的
  631. 'is_active' => 1, //在线的
  632. //'real_status' => 1, //真人认证
  633. //'idcard_status' => 1, //实名认证
  634. 'open_match_audio' => 1, //打开语聊开关
  635. //'id' => ['NOT IN',$my_follow] //不是好友的
  636. ];
  637. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  638. $lists = $this->fliter_user($lists,10);
  639. $result = [];
  640. if(!empty($lists)){
  641. foreach($lists as $key => $val){
  642. $result[] = ['id'=>$val];
  643. }
  644. }
  645. //tag任务赠送金币
  646. //语音匹配奖励 +5金币
  647. if(!empty($result)){
  648. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  649. if($task_rs === false){
  650. $this->error('完成任务赠送奖励失败');
  651. }
  652. }
  653. $this->success('success',$result);
  654. }
  655. //视频匹配
  656. public function getvideouser(){
  657. //判断资格
  658. /*$start = strtotime(date('Y-m-d'));
  659. $end = $start + 86399;
  660. $map = [
  661. 'user_id' => $this->auth->id,
  662. 'createtime' => ['between',[$start,$end]],
  663. 'price' => 0,
  664. ];
  665. $check = Db::name('user_match_video_log')->where($map)->find();*/
  666. $check = true;
  667. //已经用掉免费的了,判断金额
  668. if($check){
  669. $price = config('site.video_min_price');
  670. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  671. if($gold < $price){
  672. $this->error('您的金币已经不足,请充值');
  673. }
  674. }
  675. //找到互关的人,排除
  676. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  677. //dump($follow_me);
  678. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  679. //dump($my_follow);exit;
  680. //给出备选用户
  681. $map = [
  682. 'status' =>1, //未封禁用户
  683. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  684. 'is_online' => 0, //不在语聊间的
  685. 'is_livebc' => 0, //不在直播的
  686. 'is_active' => 1, //在线的
  687. //'real_status' => 1, //真人认证
  688. //'idcard_status' => 1, //实名认证
  689. 'open_match_video' => 1, //打开视频开关的
  690. // 'id' => ['NOT IN',$my_follow] //不是好友的
  691. ];
  692. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  693. $lists = $this->fliter_user($lists,10);
  694. $result = [];
  695. if(!empty($lists)){
  696. foreach($lists as $key => $val){
  697. $result[] = ['id'=>$val];
  698. }
  699. }
  700. //tag任务赠送金币
  701. //视频匹配奖励 +5金币
  702. if(!empty($result)){
  703. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,12);
  704. if($task_rs === false){
  705. $this->error('完成任务赠送奖励失败');
  706. }
  707. }
  708. $this->success('success',$result);
  709. }
  710. //聊天匹配
  711. public function gettypinguser(){
  712. //找到互关的人,排除
  713. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  714. //dump($follow_me);
  715. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  716. //dump($my_follow);exit;
  717. //给出备选用户
  718. $map = [
  719. 'status' =>1, //未封禁用户
  720. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  721. //'real_status' => 1, //真人认证
  722. //'idcard_status' => 1, //实名认证
  723. //'is_active' => 1, //在线的
  724. //打开聊天开关的
  725. 'open_match_typing' => 1, //打开文字聊天开关的
  726. //'id' => ['NOT IN',$my_follow] //不是好友的
  727. ];
  728. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  729. //$lists = $this->fliter_user($lists,100);
  730. $lists = array_column($lists,'id');
  731. $result = [];
  732. if(!empty($lists)){
  733. /*foreach($lists as $key => $val){
  734. $result[] = ['id'=>$val];
  735. }*/
  736. $result = Db::name('user')->field('id,nickname,username,avatar,audio_bio')->where(['id'=>['IN',$lists]])->select();
  737. $result = list_domain_image($result,['avatar,audio_bio']);
  738. }
  739. //tag任务赠送金币
  740. //缘分匹配奖励 +5金币
  741. if(!empty($result)){
  742. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,10);
  743. if($task_rs === false){
  744. $this->error('完成任务赠送奖励失败');
  745. }
  746. }
  747. $this->success('success',$result);
  748. }
  749. //过滤规则
  750. private function fliter_user($lists,$number = 1){
  751. if(empty($lists)){
  752. return $lists;
  753. }
  754. //dump($lists);
  755. //过滤掉通话中的
  756. foreach($lists as $key => $val){
  757. if(redis_matching_get($val['id']) == 1){
  758. unset($lists[$key]);
  759. }
  760. }
  761. //预留全部
  762. $all_result = array_column($lists,'id');
  763. //dump($all_result);
  764. //提取同城的
  765. $citydata = [];
  766. foreach($lists as $key => $val){
  767. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  768. $citydata[] = $val['id'];
  769. }
  770. }
  771. //dump($citydata);
  772. //有标签交集的
  773. $tagdata = [];
  774. foreach($lists as $key => $val){
  775. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  776. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  777. $val_tag_ids = explode(',',$val['tag_ids']);
  778. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  779. $tagdata[] = $val['id'];
  780. }
  781. }
  782. }
  783. //dump($tagdata);
  784. //双条件都满足
  785. $double_data = [];
  786. if(!empty($citydata) && !empty($tagdata)){
  787. $double_data = array_intersect($citydata,$tagdata);
  788. }
  789. //dump($double_data);
  790. if(count($double_data) >= $number){
  791. return $double_data;
  792. }
  793. //两种条件合并,去重。空数组合并没影响
  794. $merge_data = array_merge($citydata,$tagdata);
  795. $merge_data = array_flip(array_flip($merge_data));
  796. //dump($merge_data);
  797. if(count($merge_data) >= $number){
  798. return $merge_data;
  799. }
  800. return $all_result;
  801. }
  802. /**
  803. * 是否关注
  804. */
  805. public function isFollows() {
  806. $user_id = $this->request->request("user_id",0,"intval");
  807. if (!$user_id || $user_id<=0) {
  808. $this->error(__('Invalid parameters'));
  809. }
  810. $map = [
  811. 'uid' => $this->auth->id,
  812. 'follow_uid' => $user_id,
  813. ];
  814. $check = Db::name('user_follow')->where($map)->find();
  815. $data = [];
  816. if($check){
  817. $data["is_show_follow"] = 0;
  818. }else{
  819. $data["is_show_follow"] = 1;
  820. }
  821. $this->success("获取成功!",$data);
  822. }
  823. /**
  824. * 获取主播技能分类
  825. */
  826. public function getAnchorType() {
  827. $this->success("获取成功!",\app\common\model\UserAnchorType::select());
  828. }
  829. /**
  830. * 主播申请
  831. */
  832. public function anchorApply() {
  833. $this->error('不需要申请');
  834. /*$type_id = $this->request->request('type_id'); // 技能分类ID
  835. $desc = $this->request->request('desc'); // 申请备注
  836. if (!$type_id && !$desc) {
  837. $this->error(__('Invalid parameters'));
  838. }
  839. $useranchorModel = new \app\common\model\UserAnchor();
  840. $data = [];
  841. $data["user_id"] = $this->auth->id;
  842. $data["type_id"] = $type_id;
  843. if($useranchorModel->where($data)->find()) $this->error(__('您已申请过该类型的主播,请勿重复申请!'));
  844. $data["desc"] = $desc;
  845. $data["createtime"] = time();
  846. $res = $useranchorModel->insertGetId($data);
  847. if($res) {
  848. \app\common\model\User::update(["is_anchor"=>1],["id"=>$this->auth->id]);
  849. $this->success("申请发送成功!");
  850. } else {
  851. $this->error("网络错误,请稍后重试");
  852. }*/
  853. }
  854. /**
  855. * 获取当前用户信息
  856. */
  857. public function getMyUserInfo() {
  858. $userInfo = $this->auth->getUserinfo();
  859. //当月消费金额
  860. $redis = new Redis();
  861. $redisconfig = config("redis");
  862. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  863. $user_renew2 = $redis->get('user_renew2_'.$this->auth->id);
  864. $userInfo['user_renew2'] = empty($user_renew2)?0:intval($user_renew2);
  865. $this->success("获取成功!",$userInfo);
  866. }
  867. /**
  868. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  869. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  870. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  871. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  872. * @return float | false | string
  873. */
  874. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  875. if( empty( $point_1 ) || empty( $point_2 ) ){
  876. return false;
  877. }
  878. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  879. $p1_x = $point_1[0];
  880. $p1_y = $point_1[1];
  881. $p2_x = $point_2[0];
  882. $p2_y = $point_2[1];
  883. if(
  884. $p1_x < -180 || $p1_x > 180
  885. || $p2_x < -180 || $p2_x > 180
  886. || $p1_y < -90 || $p1_y > 90
  887. || $p2_y < -90 || $p2_y > 90
  888. ){
  889. return '0公里';
  890. }
  891. // 根据2点各自的坐标,计算2点之间直线距离的公式
  892. $distance = round(6378.138*2*asin(sqrt(pow(sin(( $p1_x *pi()/180-$p2_x*pi()/180)/2),2)+cos( $p1_x *pi()/180)*cos($p2_x*pi()/180)* pow(sin(( $p1_y *pi()/180-$p2_y*pi()/180)/2),2)))*1000);
  893. // 是否计算为字符串公里距离
  894. if( !$calc_as_string ){
  895. return (string)round( $distance / 1000 , 1 ) . '公里';
  896. }
  897. // 如果计算为字符串公里距离
  898. if( $distance / 1000 > 1 ){
  899. $k = (string)round( $distance / 1000 , 1 );
  900. $m = (string)$distance % 1000 ;
  901. $distance = "{$k}公里{$m}米";
  902. }
  903. else{
  904. $distance = "{$distance}米";
  905. }
  906. return $distance;
  907. }
  908. //地图api,根据两地坐标,获得两地距离,打卡用的
  909. //type=0直线,type=1开车
  910. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  911. $result = 0;
  912. $apiurl = 'https://restapi.amap.com/v3/distance?';
  913. $param = [
  914. 'key' => '398c424811d1a59beac2f915323d334e',
  915. 'origins' => $start_lon.','.$start_lat,
  916. 'destination' => $end_lon.','.$end_lat,
  917. 'type' => $type,
  918. 'output' => 'json',
  919. ];
  920. $apiurl .= http_build_query($param);
  921. $request_rs = json_decode(curl_get($apiurl),true);
  922. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  923. if(isset($request_rs['results'][0]['distance']))
  924. {
  925. $result = $request_rs['results'][0]['distance'];
  926. }
  927. }
  928. //dump($result);
  929. return $result;
  930. }
  931. public function distance()
  932. {
  933. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  934. dump($a);
  935. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  936. dump($a);
  937. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  938. dump($b);
  939. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  940. dump($b);
  941. }
  942. }