Usercenter.php 39 KB

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