Usercenter.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Cache;
  5. use think\Db;
  6. /**
  7. * 会员中心,不是个人中心
  8. */
  9. class Usercenter extends Api
  10. {
  11. protected $noNeedLogin = ['test','testredis'];
  12. protected $noNeedRight = '*';
  13. protected $allowFields = [
  14. 'id',
  15. 'username',
  16. 'nickname',
  17. 'email',
  18. 'mobile',
  19. 'avatar',
  20. 'real_status',
  21. 'gender',
  22. 'height',
  23. 'weight',
  24. 'birthday',
  25. 'bio',
  26. 'audio_bio',
  27. 'idcard_status',
  28. 'is_active',
  29. 'longitude',
  30. 'latitude',
  31. 'cityname',
  32. 'photo_images',
  33. 'education',
  34. 'hobby',
  35. 'job',
  36. 'marital',
  37. 'tag',
  38. 'wages',
  39. 'hometown_cityid',
  40. 'character',
  41. 'constellation',
  42. 'stature',
  43. 'is_appointment',
  44. 'chat_price',
  45. 'voice_price',
  46. 'video_price',
  47. 'greet_voice',
  48. 'greet_chat',
  49. 'is_kefu',
  50. 'is_hideaddress',
  51. 'is_recommend','is_cohabit', 'live', 'is_house', 'car', 'chest', 'waist',
  52. 'suqiu'
  53. ];
  54. //获取他人用户信息,留下足迹
  55. public function getuserinfo(){
  56. $uid = input_post('uid',0);
  57. $userinfo = Db::name('user')->field($this->allowFields)->where('id',$uid)->find();
  58. if(!$userinfo){
  59. $this->error('不存在的用户');
  60. }
  61. if($uid != $this->auth->id){
  62. $user_remark = Db::name('user_remark')->where('to_user_id',$uid)->where('user_id',$this->auth->id)->find();
  63. $userinfo['nickname'] = !empty($user_remark['nickname_remark']) ? $user_remark['nickname_remark'] : $userinfo['nickname'];
  64. }
  65. //用户数据
  66. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  67. $new_data = [
  68. 'age' => birthtime_to_age($userinfo['birthday']),
  69. ];
  70. if ($this->auth->gender == 1) {
  71. $qinmi_sum = Db::name('user_intimacy')->where(['uid' => $this->auth->id, 'other_uid' => $uid])->value('value');
  72. } elseif ($this->auth->gender == 0) {
  73. $qinmi_sum = Db::name('user_intimacy')->where(['uid' => $uid, 'other_uid' => $this->auth->id])->value('value');
  74. } else {
  75. $qinmi_sum = 0;
  76. }
  77. $new_data['qinmi_sum'] = $qinmi_sum ? :0;
  78. //合并
  79. $userinfo = array_merge($userinfo,$new_data);
  80. //枚举
  81. //家乡
  82. $userinfo['hometown_city'] = Db::name('area')->where('id',$userinfo['hometown_cityid'])->value('name');
  83. $userinfo['hometown_city'] = ($userinfo['hometown_city'] && $userinfo['is_hideaddress'] == 0) ? $userinfo['hometown_city'] : '';
  84. //vip
  85. $userwallet = Db::name('user_wallet')->where('user_id',$uid)->find();
  86. $userinfo['is_vip'] = $this->is_vip($userwallet['vip_endtime'],$userwallet['vip_level']);
  87. $userinfo['comein_gift'] = '';
  88. if($userinfo['is_vip'] == 1){
  89. $userinfo['comein_gift'] = localpath_to_netpath(config('site.vip_comein_gift'));
  90. }
  91. if($userinfo['is_vip'] == 2){
  92. $userinfo['comein_gift'] = localpath_to_netpath(config('site.svip_comein_gift'));
  93. }
  94. //是否喜欢和关注
  95. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  96. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  97. /*$is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  98. $userinfo['is_like'] = $is_like ? 1 : 0;*/
  99. //是否拉黑
  100. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  101. $userinfo['is_black'] = $is_black ? 1 : 0;
  102. //关注人数,粉丝人数
  103. $follow_num = Db::name('user_follow')->where(['uid'=>$uid])->count('id');
  104. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$uid])->count('id');
  105. $userinfo['follow_num'] = $follow_num;
  106. $userinfo['fans_num'] = $fans_num;
  107. //访客数
  108. $visit_num = Db::name('user_visit')->where(['to_uid'=>$uid])->count('id');
  109. $userinfo['visit_num'] = $visit_num;
  110. //查询是否打过招呼
  111. $count = Db::name('user_greet')->where(['user_id' => $this->auth->id, 'user_to_id' => $uid])->count('id');
  112. if ($count) {
  113. $userinfo['is_chat'] = 1; //是否打过招呼: 1是 0否
  114. } else {
  115. $userinfo['is_chat'] = 0; //是否打过招呼: 1是 0否
  116. }
  117. //守护数量和是否已经守护
  118. $userinfo['guard_num'] = Db::name('user_guard')->where(['user_to_id' => $uid])->count('id');
  119. $userinfo['is_guard'] = Db::name('user_guard')->where(['user_id' => $this->auth->id, 'user_to_id' => $uid])->count('id');
  120. //查询财富等级和魅力等级
  121. $wallet_info = Db::name('user_wallet')->where(['user_id' => $uid])->find();
  122. $wealth_level = Db::name('wealth_level')->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  123. if ($wealth_level) {
  124. $userinfo['wealth_level'] = localpath_to_netpath($wealth_level['image']);
  125. } else {
  126. $userinfo['wealth_level'] = '';
  127. }
  128. $charm_level = Db::name('charm_level')->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  129. if ($charm_level) {
  130. $userinfo['charm_level'] = localpath_to_netpath($charm_level['image']);
  131. } else {
  132. $userinfo['charm_level'] = '';
  133. }
  134. //开通守护消耗金币数量
  135. $userinfo['guard_price'] = config('site.guard_price') ? : 0;
  136. //男性非会员是否已开通聊天
  137. // $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  138. // $user_chat_unlock = Db::name('user_chat_unlock')->where(['user_id' => $this->auth->id, 'user_to_id' => $uid])->count('id');
  139. // if ($this->auth->gender == 0 || $vip_endtime > time() || $user_chat_unlock) {
  140. $userinfo['is_chat_unlock'] = 1; //已解锁
  141. // } else {
  142. // $userinfo['is_chat_unlock'] = 0;
  143. // }
  144. //男性非会员解锁聊天所需金币数量
  145. $userinfo['unlock_chat_gold'] = config('site.unlock_chat_gold') > 0 ? config('site.unlock_chat_gold') : 10;
  146. //动态信息
  147. $dongtai = Db::name('topic_dongtai')->field('type, images')->where(['user_id' => $uid, 'is_hidden' => 0, 'type' => ['in', [1, 2]], 'status' => 0, 'auit_status' => 1])->order('id desc')->limit(3)->select();
  148. if ($dongtai) {
  149. $dongtai_image = [];
  150. foreach ($dongtai as &$v) {
  151. $arr['type'] = $v['type'];
  152. if ($v['type'] == 1) {
  153. //图片
  154. $images_url = explode(',', $v['images']);
  155. foreach ($images_url as &$value) {
  156. $arr['images'] = one_domain_image($value);
  157. array_push($dongtai_image, $arr);
  158. if (count($dongtai_image) == 4) {
  159. break 2;
  160. }
  161. }
  162. } else {
  163. //视频
  164. $images_url = explode('.', $v['images']);
  165. unset($images_url[count($images_url) - 1]);
  166. $arr['images'] = join('.', $images_url) . '_0.jpg';
  167. $arr['images'] = one_domain_image($arr['images']);
  168. array_push($dongtai_image, $arr);
  169. if (count($dongtai_image) == 4) {
  170. break;
  171. }
  172. }
  173. }
  174. $userinfo['dongtai_image'] = $dongtai_image;
  175. // $dongtai = join(',', $dongtai);
  176. // $dongtai = explode(',', $dongtai);
  177. // $dongtai = array_slice($dongtai, 0, 3);
  178. // $dongtai = join(',', $dongtai);
  179. // $userinfo['dongtai_image'] = one_domain_image($dongtai);
  180. // $userinfo['dongtai_image'] = explode(',', $userinfo['dongtai_image']);
  181. } else {
  182. $userinfo['dongtai_image'] = [];
  183. }
  184. if ($this->auth->id != $uid) {
  185. //查看别人信息,就要留下痕迹
  186. $data = [
  187. 'uid' => $this->auth->id,
  188. 'to_uid' => $uid,
  189. ];
  190. $check = Db::name('user_visit')->where($data)->find();
  191. if ($check) {
  192. Db::name('user_visit')->where($data)->update(['number' => $check['number'] + 1, 'updatetime' => time()]);
  193. } else {
  194. $data['number'] = 1;
  195. $data['updatetime'] = time();
  196. Db::name('user_visit')->insertGetId($data);
  197. }
  198. }
  199. //礼物墙
  200. $userinfo['gift_wall'] = $this->giftwall($uid);
  201. //动态数量
  202. $userinfo['dongtai_num'] = Db::name('topic_dongtai')->where(['user_id' => $uid, 'is_hidden' => 0, 'type' => ['in', [1, 2]], 'status' => 0, 'auit_status' => 1])->count();
  203. $this->success('success',$userinfo);
  204. }
  205. //某用户的礼物墙
  206. private function giftwall($uid){
  207. //$uid = $this->auth->id;
  208. //聊天礼物
  209. $gift_user_typing = Db::name('gift_user_typing')->alias('log')
  210. ->join('gift', 'gift.id = log.gift_id', 'LEFT')
  211. ->field('log.gift_id,log.gift_name,sum(log.number) as number,gift.image')
  212. ->where(['log.user_to_id' => $uid])
  213. ->group('log.gift_id')
  214. ->order('gift.price desc')
  215. ->column('log.gift_id,log.gift_name,sum(log.number) as number,gift.image');
  216. $gift_user_typing = list_domain_image($gift_user_typing,['image']);
  217. //dump($gift_user_typing);
  218. //动态礼物
  219. $gift_user_dongtai = Db::name('gift_user_dongtai')->alias('log')
  220. ->join('gift', 'gift.id = log.gift_id', 'LEFT')
  221. ->field('log.gift_id,log.gift_name,sum(log.number) as number,gift.image')
  222. ->where(['log.user_to_id' => $uid])
  223. ->group('log.gift_id')
  224. ->order('gift.price desc')
  225. ->column('log.gift_id,log.gift_name,sum(log.number) as number,gift.image');
  226. $gift_user_dongtai = list_domain_image($gift_user_dongtai,['image']);
  227. //dump($gift_user_dongtai);
  228. //为空
  229. if(empty($gift_user_typing) && empty($gift_user_dongtai)){
  230. return [];
  231. }
  232. if(empty($gift_user_typing) && !empty($gift_user_dongtai)){
  233. foreach($gift_user_dongtai as $key => $val){
  234. $result[] = $val;
  235. }
  236. return $result;
  237. }
  238. if(!empty($gift_user_typing) && empty($gift_user_dongtai)){
  239. foreach($gift_user_typing as $key => $val){
  240. $result[] = $val;
  241. }
  242. return $result;
  243. }
  244. //合并
  245. foreach($gift_user_typing as $key => $val){
  246. foreach($gift_user_dongtai as $k => $v){
  247. if($key == $k){
  248. $gift_user_typing[$key]['number'] += $v['number'];
  249. }
  250. if(!isset($gift_user_typing[$k])){
  251. $gift_user_typing[$k] = $v;
  252. }
  253. }
  254. }
  255. //dump($gift_user_typing);
  256. //结果
  257. $result = [];
  258. foreach($gift_user_typing as $key => $val){
  259. $result[] = $val;
  260. }
  261. return $result;
  262. }
  263. //开通守护
  264. public function addguard() {
  265. exit;
  266. // 接口防并发
  267. if (!$this->apiLimit(1, 1)) {
  268. $this->error(__('Operation frequently'));
  269. }
  270. $user_id = input('user_id', 0, 'intval');// 守护对象
  271. if (!$user_id) {
  272. $this->error();
  273. }
  274. // 不可以守护给自己
  275. if($this->auth->id == $user_id) {
  276. $this->error("不可以守护自己");
  277. }
  278. //查询是否开通守护
  279. $count = Db::name('user_guard')->where(['user_id' => $this->auth->id, 'user_to_id' => $user_id])->count('id');
  280. if ($count) {
  281. $this->error('已经开通守护了');
  282. }
  283. $giftvalue = config('site.guard_price');
  284. if ($giftvalue <= 0) {
  285. $this->error('守护金额异常~');
  286. }
  287. //被守护人信息
  288. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  289. if (!$touserinfo) {
  290. $this->error("不存在的用户");
  291. }
  292. // 判断当前用户余额
  293. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  294. if($user_gold < $giftvalue) {
  295. $this->error("您的金币不足");
  296. }
  297. Db::startTrans();
  298. // 添加礼物守护记录表
  299. $data = [
  300. 'user_id' => $this->auth->id,
  301. 'user_to_id' => $user_id,
  302. 'price' => $giftvalue,
  303. 'createtime' => time(),
  304. ];
  305. $log_id = Db::name('user_guard')->insertGetId($data);
  306. if(!$log_id){
  307. Db::rollback();
  308. $this->error('守护失败');
  309. }
  310. if($giftvalue > 0){
  311. // 扣除当前用户余额
  312. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,81,'开通守护','user_guard',$log_id);
  313. if($wallet_rs['status'] === false){
  314. Db::rollback();
  315. $this->error($wallet_rs['msg']);
  316. }
  317. // 添加守护用户余额
  318. $money_to_gold = config('site.money_to_gold');
  319. $gift_plat_scale = config('site.gift_plat_scale');
  320. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  321. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  322. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,82,'被守护收益','user_guard',$log_id,2);
  323. if($wallet_rs['status'] === false){
  324. Db::rollback();
  325. $this->error($wallet_rs['msg']);
  326. }
  327. //增加赠送用户上级余额
  328. if ($touserinfo['intro_uid']) {
  329. //获取返利比率
  330. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  331. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  332. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  333. //上级获得金额
  334. $intro_uid_money = bcdiv(bcmul($money,$intro_income_rebate_rate,2),100,2);
  335. if ($intro_uid_money > 0) {
  336. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人开通守护获赠奖励','user_guard',$log_id);
  337. if($intro_result['status']===false)
  338. {
  339. Db::rollback();
  340. $this->error($intro_result['msg']);
  341. }
  342. }
  343. }
  344. }
  345. }
  346. //tag任务守护金币
  347. //搭讪奖励
  348. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  349. // if($task_rs === false){
  350. // Db::rollback();
  351. // $this->error('完成任务守护奖励失败');
  352. // }
  353. Db::commit();
  354. $this->success('守护成功');
  355. }
  356. //守护列表
  357. public function guardlist(){
  358. exit;
  359. $user_id = input('user_id', 0, 'intval');
  360. if (!$user_id) {
  361. $this->error('您的网络开小差啦~');
  362. }
  363. $list = Db::name('user_guard')
  364. ->alias('a')
  365. ->join('user','a.user_id = user.id','LEFT')
  366. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  367. ->field('a.createtime,user.id,user.nickname,user.avatar,user.real_status,ur.nickname_remark')
  368. ->where('a.user_to_id',$user_id)->order('a.id desc')->autopage()->select();
  369. if (!$list) {
  370. $this->success('success', $list);
  371. }
  372. $list = list_domain_image($list,['avatar']);
  373. $time = time();
  374. foreach ($list as &$val) {
  375. $val['createtime'] = ceil(($time - $val['createtime']) / 86400);
  376. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  377. }
  378. $this->success('success',$list);
  379. }
  380. //我的守护
  381. public function myguardlist(){
  382. exit;
  383. $type = input('type', 0, 'intval'); //类型: 1我守护的 2守护我的
  384. if (!in_array($type, [1, 2])) {
  385. $this->error('您的网络开小差了');
  386. }
  387. $where = [];
  388. if ($type == 1) {
  389. $join = 'a.user_to_id = user.id';
  390. $where['a.user_id'] = $this->auth->id;
  391. } else {
  392. $join = 'a.user_id = user.id';
  393. $where['a.user_to_id'] = $this->auth->id;
  394. }
  395. $list = Db::name('user_guard')
  396. ->alias('a')
  397. ->join('user',$join,'LEFT')
  398. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  399. ->field('a.createtime,user.id,user.nickname,user.avatar,user.real_status,ur.nickname_remark')
  400. ->where($where)->order('a.id desc')->autopage()->select();
  401. if (!$list) {
  402. $this->success('success', $list);
  403. }
  404. $list = list_domain_image($list,['avatar']);
  405. $time = time();
  406. foreach ($list as &$val) {
  407. $val['createtime'] = ceil(($time - $val['createtime']) / 86400);
  408. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  409. }
  410. $this->success('success',$list);
  411. }
  412. //打字聊天每句话调用一次
  413. public function chat_once(){
  414. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  415. $this->success('success',['money' => 'free']);
  416. }
  417. if ($this->auth->gender == 0) { //女生不花钱
  418. $this->error('您的网络开小差啦~');
  419. }
  420. //检测用户
  421. $to_user_id = input_post('to_user_id');
  422. $to_user_info = Db::name('user')->field('id,intro_uid,real_status,gender,free_video,free_audio,free_typing,chat_price,is_kefu')->where('id',$to_user_id)->find();
  423. if(!$to_user_info){
  424. $this->error('不存在的用户');
  425. }
  426. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  427. $this->success('success',['money' => 'free']);
  428. }
  429. if ($to_user_info['gender'] != 0) {
  430. $this->error('同性不能聊天~');
  431. }
  432. //验证金额
  433. $wallet_info = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  434. $is_vip = $this->is_vip($wallet_info['vip_endtime'],$wallet_info['vip_level']);
  435. if ($is_vip != 0) { //会员
  436. $price = $to_user_info['chat_price'];//config('site.typing_min_price'); //扣费金币
  437. //获取折扣
  438. $vip_chat_discount = config('site.vip_chat_discount');
  439. if ($vip_chat_discount >= 0 && $vip_chat_discount <= 10) {
  440. $price = bcdiv(bcmul($price,$vip_chat_discount,2),10,0);
  441. }
  442. } else {
  443. $price = $to_user_info['chat_price'];//config('site.typing_min_price'); //扣费金币
  444. }
  445. //正常价格
  446. $bili = config('site.money_to_gold'); //兑换比例
  447. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  448. $money = bcdiv($price,$bili,2); //对应人民币
  449. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  450. $resArray['money'] = $money; //返回给前端的计算结果
  451. Db::startTrans();
  452. //记录日志
  453. $data = [
  454. 'user_id' => $this->auth->id,
  455. 'price' => $price,
  456. 'createtime' => time(),
  457. 'to_user_id' => $to_user_id,
  458. 'money' => $money,
  459. ];
  460. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  461. if(!$log_id){
  462. Db::rollback();
  463. $this->error('扣费失败');
  464. }
  465. //有性别差,扣费
  466. if($price > 0){
  467. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$to_user_id,'gold',-$price,13,'文字聊天消费','user_match_typing_log',$log_id);
  468. if($rs['status'] === false){
  469. Db::rollback();
  470. $this->error($rs['msg']);
  471. }
  472. }
  473. //另一方加钱,0收费
  474. if($money > 0){
  475. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,$this->auth->id,'money',$money,23,'文字聊天收益','user_match_typing_log',$log_id,2);
  476. if($rs['status'] === false){
  477. Db::rollback();
  478. $this->error($rs['msg']);
  479. }
  480. $touserinfo = $to_user_info;
  481. //增加赠送用户上级余额
  482. if ($touserinfo['intro_uid']) {
  483. //获取返利比率
  484. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  485. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  486. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  487. //上级获得金额
  488. $intro_uid_money = bcdiv(bcmul($money,$intro_income_rebate_rate,2),100,2);
  489. if ($intro_uid_money > 0) {
  490. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$to_user_id,'money',$intro_uid_money,68, '邀请人文字聊天获赠奖励','user_match_typing_log',$log_id);
  491. if($intro_result['status']===false)
  492. {
  493. Db::rollback();
  494. $this->error($intro_result['msg']);
  495. }
  496. }
  497. }
  498. }
  499. }
  500. //增加亲密度
  501. $user_intimacy_rs = addintimacy($this->auth->id, $to_user_id, $price);
  502. if (!$user_intimacy_rs['status']) {
  503. Db::rollback();
  504. $this->error('您的网络开小差啦~');
  505. }
  506. //tag任务赠送金币
  507. //私信5名异性奖励
  508. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,23);
  509. if($task_rs === false){
  510. Db::rollback();
  511. $this->error('完成任务赠送奖励失败');
  512. }
  513. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,23);
  514. if($task_rs === false){
  515. Db::rollback();
  516. $this->error('完成任务赠送奖励失败');
  517. }
  518. Db::commit();
  519. //发送消息
  520. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  521. $tenim = new \app\api\controller\Tenim;
  522. $tenim->sendMessageToUser($this->auth->id, $to_user_id, $user_intimacy_rs['level_remark'], 1);
  523. }
  524. $this->success('success',$resArray);
  525. }
  526. //语音通话每分钟调用一次
  527. public function voice_onemin(){
  528. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  529. $this->success('success',['money' => 'free']);
  530. }
  531. if ($this->auth->gender == 0) { //女生不花钱
  532. $this->error('您的网络开小差啦~');
  533. }
  534. //检测用户
  535. $to_user_id = input_post('to_user_id');
  536. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  537. $to_user_info = Db::name('user')->field('id,intro_uid,real_status,gender,free_video,free_audio,free_typing,voice_price,is_kefu')->where('id',$to_user_id)->find();
  538. if(!$to_user_info){
  539. $this->error('不存在的用户');
  540. }
  541. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  542. $this->success('success',['money' => 'free']);
  543. }
  544. if ($to_user_info['gender'] != 0) {
  545. $this->error('同性不能聊天~');
  546. }
  547. //验证金额
  548. $wallet_info = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  549. $is_vip = $this->is_vip($wallet_info['vip_endtime'],$wallet_info['vip_level']);
  550. if($is_vip == 0){
  551. $this->error('VIP才能语音聊天哦');
  552. }
  553. if ($is_vip == 2) { //SVIP会员
  554. $price = $to_user_info['voice_price'];//config('site.video_min_price'); //扣费金币
  555. //获取折扣
  556. $vip_voice_discount = config('site.vip_voice_discount');
  557. if ($vip_voice_discount >= 0 && $vip_voice_discount <= 10) {
  558. $price = bcdiv(bcmul($price,$vip_voice_discount,2),10,0);
  559. }
  560. } else {
  561. $price = $to_user_info['voice_price'];//config('site.video_min_price'); //扣费金币
  562. }
  563. //正常价格
  564. $bili = config('site.money_to_gold'); //兑换比例
  565. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  566. $money = bcdiv($price,$bili,2); //对应人民币
  567. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  568. Db::startTrans();
  569. //查询是否有匹配记录
  570. $user_match_audio_log_info = [];
  571. if ($request_id) {
  572. $user_match_audio_log_info = Db::name('user_match_audio_log')->where(['user_id' => $this->auth->id, 'to_user_id' => $to_user_id, 'request_id' => $request_id])->find();
  573. }
  574. if ($user_match_audio_log_info) {
  575. //修改记录日志
  576. $data = [
  577. 'price' => bcadd($user_match_audio_log_info['price'],$price,0),
  578. 'createtime' => time(),
  579. 'money' => bcadd($user_match_audio_log_info['money'],$money,2),
  580. 'call_minutes' => $user_match_audio_log_info['call_minutes'] + 1
  581. ];
  582. $log_rs = Db::name('user_match_audio_log')->where(['id' => $user_match_audio_log_info['id'], 'createtime' => $user_match_audio_log_info['createtime']])->setField($data);
  583. if (!$log_rs) {
  584. Db::rollback();
  585. $this->error('扣费失败');
  586. }
  587. $log_id = $user_match_audio_log_info['id'];
  588. } else {
  589. //添加记录日志
  590. $data = [
  591. 'user_id' => $this->auth->id,
  592. 'price' => $price,
  593. 'createtime' => time(),
  594. 'to_user_id' => $to_user_id,
  595. 'money' => $money,
  596. 'request_id' => $request_id,
  597. 'call_minutes' => 1
  598. ];
  599. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  600. if (!$log_id) {
  601. Db::rollback();
  602. $this->error('扣费失败');
  603. }
  604. }
  605. //有性别差,扣费
  606. if($price > 0){
  607. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$to_user_id,'gold',-$price,12,'语音通话消费' . $data['call_minutes'] . '分钟','user_match_audio_log',$log_id, 0, $request_id);
  608. if($rs['status'] === false){
  609. Db::rollback();
  610. $this->error($rs['msg']);
  611. }
  612. }
  613. //另一方加钱,0收费
  614. if($money > 0){
  615. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,$this->auth->id,'money',$money,22,'语音通话收益' . $data['call_minutes'] . '分钟','user_match_audio_log',$log_id,2, $request_id);
  616. if($rs['status'] === false){
  617. Db::rollback();
  618. $this->error($rs['msg']);
  619. }
  620. $touserinfo = $to_user_info;
  621. //增加赠送用户上级余额
  622. if ($touserinfo['intro_uid']) {
  623. //获取返利比率
  624. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  625. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  626. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  627. //上级获得金额
  628. $intro_uid_money = bcdiv(bcmul($money,$intro_income_rebate_rate,2),100,2);
  629. if ($intro_uid_money > 0) {
  630. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$to_user_id,'money',$intro_uid_money,68, '邀请人语音聊天获赠奖励' . $data['call_minutes'] . '分钟','user_match_audio_log',$log_id, 0, $request_id);
  631. if($intro_result['status']===false)
  632. {
  633. Db::rollback();
  634. $this->error($intro_result['msg']);
  635. }
  636. }
  637. }
  638. }
  639. }
  640. //增加亲密度
  641. $user_intimacy_rs = addintimacy($this->auth->id, $to_user_id, $price);
  642. if (!$user_intimacy_rs['status']) {
  643. Db::rollback();
  644. $this->error('您的网络开小差啦~');
  645. }
  646. //tag任务赠送金币
  647. //与1名异性语音通话奖励
  648. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,25);
  649. if($task_rs === false){
  650. Db::rollback();
  651. $this->error('完成任务赠送奖励失败');
  652. }
  653. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,25);
  654. if($task_rs === false){
  655. Db::rollback();
  656. $this->error('完成任务赠送奖励失败');
  657. }
  658. //发送消息
  659. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  660. $tenim = new \app\api\controller\Tenim;
  661. $tenim->sendMessageToUser($this->auth->id, $to_user_id, $user_intimacy_rs['level_remark'], 1);
  662. }
  663. Db::commit();
  664. $rs = [
  665. 'money' => $data['money'],
  666. ];
  667. $this->success('success',$rs);
  668. }
  669. //视频通话每分钟调用一次
  670. public function videochat_onemin(){
  671. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  672. $this->success('success',['money' => 'free']);
  673. }
  674. if ($this->auth->gender == 0) { //女生不花钱
  675. $this->error('您的网络开小差啦~');
  676. }
  677. //检测用户
  678. $to_user_id = input_post('to_user_id');
  679. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  680. $to_user_info = Db::name('user')->field('id,intro_uid,real_status,gender,free_video,free_audio,free_typing,video_price,is_kefu')->where('id',$to_user_id)->find();
  681. if(!$to_user_info){
  682. $this->error('不存在的用户');
  683. }
  684. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  685. $this->success('success',['money' => 'free']);
  686. }
  687. if ($to_user_info['gender'] != 0) {
  688. $this->error('同性不能聊天~');
  689. }
  690. /* //检查是否免费过
  691. $count = Db::name('user_match_video_log_free')->where(['user_id' => $this->auth->id])->count('id');
  692. if (!$count) {
  693. //记录日志
  694. $data = [
  695. 'user_id' => $this->auth->id,
  696. 'price' => 0,
  697. 'createtime' => time(),
  698. 'to_user_id' => $to_user_id,
  699. 'money' => 0,
  700. ];
  701. $log_id = Db::name('user_match_video_log_free')->insertGetId($data);
  702. if(!$log_id){
  703. $this->error('扣费失败');
  704. }
  705. $this->success('success');
  706. }*/
  707. //验证金额
  708. $wallet_info = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  709. $is_vip = $this->is_vip($wallet_info['vip_endtime'],$wallet_info['vip_level']);
  710. if($is_vip != 2){
  711. $this->error('SVIP才能视频聊天哦');
  712. }
  713. if ($is_vip == 2) { //SVIP会员
  714. $price = $to_user_info['video_price'];//config('site.video_min_price'); //扣费金币
  715. //获取折扣
  716. $vip_video_discount = config('site.vip_video_discount');
  717. if ($vip_video_discount >= 0 && $vip_video_discount <= 10) {
  718. $price = bcdiv(bcmul($price,$vip_video_discount,2),10,0);
  719. }
  720. } else {
  721. $price = $to_user_info['video_price'];//config('site.video_min_price'); //扣费金币
  722. }
  723. //正常价格
  724. $bili = config('site.money_to_gold'); //兑换比例
  725. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  726. $money = bcdiv($price,$bili,2); //对应人民币
  727. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  728. Db::startTrans();
  729. //查询是否有匹配记录
  730. $user_match_video_log_info = [];
  731. if ($request_id) {
  732. $user_match_video_log_info = Db::name('user_match_video_log')->where(['user_id' => $this->auth->id, 'to_user_id' => $to_user_id, 'request_id' => $request_id])->find();
  733. }
  734. if ($user_match_video_log_info) {
  735. //修改记录日志
  736. $data = [
  737. 'price' => bcadd($user_match_video_log_info['price'],$price,0),
  738. 'createtime' => time(),
  739. 'money' => bcadd($user_match_video_log_info['money'],$money,2),
  740. 'call_minutes' => $user_match_video_log_info['call_minutes'] + 1
  741. ];
  742. $log_rs = Db::name('user_match_video_log')->where(['id' => $user_match_video_log_info['id'], 'createtime' => $user_match_video_log_info['createtime']])->setField($data);
  743. if (!$log_rs) {
  744. Db::rollback();
  745. $this->error('扣费失败');
  746. }
  747. $log_id = $user_match_video_log_info['id'];
  748. } else {
  749. //添加记录日志
  750. $data = [
  751. 'user_id' => $this->auth->id,
  752. 'price' => $price,
  753. 'createtime' => time(),
  754. 'to_user_id' => $to_user_id,
  755. 'money' => $money,
  756. 'request_id' => $request_id,
  757. 'call_minutes' => 1
  758. ];
  759. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  760. if (!$log_id) {
  761. Db::rollback();
  762. $this->error('扣费失败');
  763. }
  764. }
  765. //有性别差,扣费
  766. if($price > 0){
  767. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$to_user_id,'gold',-$price,11,'视频通话消费' . $data['call_minutes'] . '分钟','user_match_video_log',$log_id, 0, $request_id);
  768. if($rs['status'] === false){
  769. Db::rollback();
  770. $this->error($rs['msg']);
  771. }
  772. }
  773. //另一方加钱,0收费
  774. if($money > 0){
  775. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,$this->auth->id,'money',$money,21,'视频通话收益' . $data['call_minutes'] . '分钟','user_match_video_log',$log_id,2, $request_id);
  776. if($rs['status'] === false){
  777. Db::rollback();
  778. $this->error($rs['msg']);
  779. }
  780. $touserinfo = $to_user_info;
  781. //增加赠送用户上级余额
  782. if ($touserinfo['intro_uid']) {
  783. //获取返利比率
  784. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  785. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  786. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  787. //上级获得金额
  788. $intro_uid_money = bcdiv(bcmul($money,$intro_income_rebate_rate,2),100,2);
  789. if ($intro_uid_money > 0) {
  790. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$to_user_id,'money',$intro_uid_money,68, '邀请人视频聊天获赠奖励' . $data['call_minutes'] . '分钟','user_match_video_log',$log_id, 0, $request_id);
  791. if($intro_result['status']===false)
  792. {
  793. Db::rollback();
  794. $this->error($intro_result['msg']);
  795. }
  796. }
  797. }
  798. }
  799. }
  800. //增加亲密度
  801. $user_intimacy_rs = addintimacy($this->auth->id, $to_user_id, $price);
  802. if (!$user_intimacy_rs['status']) {
  803. Db::rollback();
  804. $this->error('您的网络开小差啦~');
  805. }
  806. //tag任务赠送金币
  807. //与1名异性视频通话奖励
  808. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,26);
  809. if($task_rs === false){
  810. Db::rollback();
  811. $this->error('完成任务赠送奖励失败');
  812. }
  813. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,26);
  814. if($task_rs === false){
  815. Db::rollback();
  816. $this->error('完成任务赠送奖励失败');
  817. }
  818. Db::commit();
  819. //发送消息
  820. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  821. $tenim = new \app\api\controller\Tenim;
  822. $tenim->sendMessageToUser($this->auth->id, $to_user_id, $user_intimacy_rs['level_remark'], 1);
  823. }
  824. $rs = [
  825. 'money' => $data['money'],
  826. ];
  827. $this->success('success',$rs);
  828. }
  829. //消息列表通话
  830. public function calllist() {
  831. if ($this->auth->gender == 1) {
  832. $list = Db::name('user_gold_log')->where(['user_id' => $this->auth->id, 'log_type' => ['in', [11, 12]]])->order('id desc')->autopage()->select();
  833. } elseif ($this->auth->gender == 0) {
  834. $list = Db::name('user_money_log')->where(['user_id' => $this->auth->id, 'log_type' => ['in', [21, 22]]])->order('id desc')->autopage()->select();
  835. } else {
  836. $this->success('success', []);
  837. }
  838. if (!$list) {
  839. $this->success('success', $list);
  840. }
  841. $arr = [];
  842. $mt_user_match_audio_log = Db::name('user_match_audio_log');
  843. $mt_user_match_video_log = Db::name('user_match_video_log');
  844. $mt_user = Db::name('user');
  845. foreach ($list as &$v) {
  846. $data = [];
  847. if ($this->auth->gender == 1) {
  848. if ($v['log_type'] == 11) { //视频通话
  849. $log_info = $mt_user_match_video_log->field('to_user_id user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  850. $data['desc'] = '视频时长' . $log_info['call_minutes'] . '分钟';
  851. } else { //语音通话
  852. $log_info = $mt_user_match_audio_log->field('to_user_id user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  853. $data['desc'] = '语音时长' . $log_info['call_minutes'] . '分钟';
  854. }
  855. } else {
  856. if ($v['log_type'] == 21) { //视频通话
  857. $log_info = $mt_user_match_video_log->field('user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  858. $data['desc'] = '视频时长' . $log_info['call_minutes'] . '分钟';
  859. } else { //语音通话
  860. $log_info = $mt_user_match_audio_log->field('user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  861. $data['desc'] = '语音时长' . $log_info['call_minutes'] . '分钟';
  862. }
  863. }
  864. $user_info = $mt_user->field('nickname, avatar,is_active')->where(['id' => $log_info['user_id']])->find();
  865. $data['user_id'] = $log_info['user_id'];
  866. $data['nickname'] = $user_info['nickname'];
  867. $data['avatar'] = one_domain_image($user_info['avatar']);
  868. $data['createtime'] = get_last_time($log_info['createtime']);
  869. $data['is_active'] = $user_info['is_active'];
  870. $arr[] = $data;
  871. }
  872. $this->success('success', $arr);
  873. }
  874. //查询对方用户收费价格和我的余额
  875. public function otheruserinfo(){
  876. $user_id = input('user_id', 0, 'intval');
  877. if (!$user_id) {
  878. $this->error('您的网络开小差啦~');
  879. }
  880. $field = [
  881. 'chat_price','voice_price','video_price','gender','is_kefu', 'open_match_audio', 'open_match_video'
  882. ];
  883. $info = Db::name('user')->field($field)->where('id',$user_id)->find();
  884. if ($info['is_kefu'] != 1 && $this->auth->is_kefu != 1) { //不是客服
  885. if ($info['gender'] == $this->auth->gender || $info['gender'] == -1 || $this->auth->gender == -1) {
  886. $this->error('性别存在问题');
  887. }
  888. }
  889. if ($this->auth->gender == 0) { //收费价格按照女号
  890. $info['chat_price'] = $this->auth->chat_price;
  891. $info['voice_price'] = $this->auth->voice_price;
  892. $info['video_price'] = $this->auth->video_price;
  893. $info['open_match_audio'] = $this->auth->open_match_audio;
  894. $info['open_match_video'] = $this->auth->open_match_video;
  895. }
  896. $info['chat_price_vip'] = $info['chat_price']; //vip价格
  897. $info['voice_price_vip'] = $info['voice_price'];
  898. $info['video_price_vip'] = $info['video_price'];
  899. //获取聊天折扣
  900. $vip_chat_discount = config('site.vip_chat_discount');
  901. if ($vip_chat_discount >= 0 && $vip_chat_discount <= 10) {
  902. $info['vip_chat_discount'] = $vip_chat_discount;
  903. } else {
  904. $info['vip_chat_discount'] = 10;
  905. }
  906. //获取视频折扣
  907. $vip_video_discount = config('site.vip_video_discount');
  908. if ($vip_video_discount >= 0 && $vip_video_discount <= 10) {
  909. $info['vip_video_discount'] = config('site.vip_video_discount'); //视频
  910. } else {
  911. $info['vip_video_discount'] = 10;
  912. }
  913. //获取语音折扣
  914. $vip_voice_discount = config('site.vip_voice_discount');
  915. if ($vip_voice_discount >= 0 && $vip_voice_discount <= 10) {
  916. $info['vip_voice_discount'] = config('site.vip_voice_discount'); //语音
  917. } else {
  918. $info['vip_voice_discount'] = 10;
  919. }
  920. //获取聊天折扣
  921. $info['chat_price_vip'] = ceil($info['chat_price'] * $info['vip_chat_discount'] / 10);
  922. //获取语音折扣
  923. $info['voice_price_vip'] = ceil($info['voice_price'] * $info['vip_voice_discount'] / 10);
  924. //获取视频折扣
  925. $info['video_price_vip'] = ceil($info['video_price'] * $info['vip_video_discount'] / 10);
  926. if ($this->auth->gender == 0) { //查询男号余额和vip
  927. $wallet_info = Db::name('user_wallet')->where(['user_id' => $user_id])->find();
  928. } else {
  929. $wallet_info = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  930. }
  931. $info['is_vip'] = $this->is_vip($wallet_info['vip_endtime'],$wallet_info['vip_level']);
  932. $info['gold'] = $wallet_info['gold'];
  933. $info['plat_scale'] = config('site.gift_plat_scale'); //平台抽成百分比
  934. $info['money_to_gold'] = config('site.money_to_gold'); //
  935. $this->success('success',$info);
  936. }
  937. //语音视频随聊
  938. public function getrandomuser() {
  939. $type = input('type', 0, 'intval'); //类型:1语音 2视频
  940. if (!in_array($type, [1, 2, 3])) {
  941. $this->error('您的网络开小差了');
  942. }
  943. $size = input('size',0,'intval');// 是否查 10 条
  944. $size = $size > 0 ? 1 : 0;
  945. //给出备选用户
  946. $map = [
  947. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  948. 'is_active' => 1, //在线的
  949. 'status' =>1, //未封禁用户
  950. 'is_kefu' => 0, //不是客服
  951. ];
  952. if ($this->auth->gender == 1) {
  953. $map['real_status|idcard_status'] = 1; //真人认证或实名认证
  954. }
  955. if ($type == 1) {
  956. $map['open_match_audio'] = 1;
  957. } elseif ($type == 2) {
  958. $map['open_match_video'] = 1;
  959. }
  960. $key = md5(json_encode($map));// 根据搜索条件做Key
  961. $lists = Cache::remember("getrandomuser_{$key}_{$size}",function () use ($map,$size){
  962. $query = Db::name('user')
  963. ->field('id,nickname,username,avatar')
  964. ->where($map)
  965. ->order('is_active desc, logintime desc');
  966. if ($size == 1){
  967. $query->limit(10);
  968. }else{
  969. $query->page($this->page,100);
  970. }
  971. $lists = $query->select();
  972. $lists = list_domain_image($lists,['avatar,audio_bio']);
  973. $mt_user_greet_content = Db::name('user_greet_content');
  974. foreach ($lists as &$v) {
  975. $info = $mt_user_greet_content->where(['user_id' => $v['id'], 'is_default' => 1])->find();
  976. if(!$info){
  977. $this->add_girl_greet($v['id']);
  978. $info = $mt_user_greet_content->where(['user_id' => $v['id'], 'is_default' => 1])->find();
  979. }
  980. if ($info) {
  981. if ($info['type'] == 0) {
  982. $v['greet_content'] = $info['content'];
  983. } elseif ($info['type'] == 1) {
  984. $v['greet_content'] = '[语音]';
  985. } elseif ($info['type'] == 2) {
  986. $v['greet_content'] = '[图片]';
  987. }
  988. } else {
  989. $v['greet_content'] = '';
  990. }
  991. }
  992. return $lists;
  993. },2);
  994. $count = Db::name('user')->where(['is_active' => 1])->count('id');
  995. $return_data['count'] = config('site.randomuser_num') > 0 ? config('site.randomuser_num') + $count : $count;
  996. $return_data['list'] = $lists;
  997. $this->success('success',$return_data);
  998. }
  999. private function add_girl_greet($user_id){
  1000. $girl_greet = [
  1001. [
  1002. 'user_id' => $user_id,
  1003. 'title' => '默认',
  1004. 'content' => '你好,想认识你,可以聊聊吗?',
  1005. 'is_default' => 1,
  1006. 'createtime' => time(),
  1007. ],
  1008. [
  1009. 'user_id' => $user_id,
  1010. 'title' => '默认',
  1011. 'content' => '听说主动打招呼的人运气都不会太差,所以我来试试!',
  1012. 'createtime' => time(),
  1013. ],
  1014. [
  1015. 'user_id' => $user_id,
  1016. 'title' => '默认',
  1017. 'content' => '嗨!看到你的资料觉得挺有意思的,认识一下?',
  1018. 'createtime' => time(),
  1019. ],
  1020. [
  1021. 'user_id' => $user_id,
  1022. 'title' => '默认',
  1023. 'content' => '我猜你收到很多消息,但我的这条一定最特别!',
  1024. 'createtime' => time(),
  1025. ],
  1026. ];
  1027. Db::name('user_greet_content')->insertAll($girl_greet);
  1028. }
  1029. //查询免费体验视频通话用户信息
  1030. public function getfreevideouserinfo() {
  1031. $to_user_id = input('to_user_id', 0, 'intval');
  1032. $to_user_info = Db::name('user')->field('id,gender,nickname,avatar,birthday,height,bio')->where(['id' => $to_user_id, 'is_kefu' => 0])->find();
  1033. if(!$to_user_info){
  1034. $this->error('不存在的用户');
  1035. }
  1036. if ($to_user_info['gender'] == $this->auth->gender) {
  1037. $this->error('同性不能聊天~');
  1038. }
  1039. $to_user_info['desc'] = '';
  1040. $age = birthtime_to_age($to_user_info['birthday']);
  1041. if ($age > 0) {
  1042. $to_user_info['desc'] = $age . '岁';
  1043. } else {
  1044. $to_user_info['desc'] = '18岁';
  1045. }
  1046. if ($to_user_info['height']) {
  1047. $to_user_info['desc'] .= ' | ' . $to_user_info['height'];
  1048. }
  1049. //查询是否免费过
  1050. $to_user_info['free_count'] = 1;//Db::name('user_match_video_log_free')->where(['user_id' => $this->auth->id])->count('id');
  1051. $this->success('信息', $to_user_info);
  1052. }
  1053. //免费体验视频通话1分钟
  1054. public function videochat_onemin_free(){
  1055. $this->success('success');
  1056. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  1057. $this->success('success');
  1058. }
  1059. //检查是否免费过
  1060. $count = Db::name('user_match_video_log_free')->where(['user_id' => $this->auth->id])->count('id');
  1061. if ($count) {
  1062. $this->error('已经免费体验过');
  1063. }
  1064. //检测用户
  1065. $to_user_id = input_post('to_user_id');
  1066. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing,video_price,is_kefu')->where('id',$to_user_id)->find();
  1067. if(!$to_user_info){
  1068. $this->error('不存在的用户');
  1069. }
  1070. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  1071. $this->success('success');
  1072. }
  1073. if ($to_user_info['gender'] == $this->auth->gender) {
  1074. $this->error('同性不能聊天~');
  1075. }
  1076. //记录日志
  1077. $data = [
  1078. 'user_id' => $this->auth->id,
  1079. 'price' => 0,
  1080. 'createtime' => time(),
  1081. 'to_user_id' => $to_user_id,
  1082. 'money' => 0,
  1083. ];
  1084. $log_id = Db::name('user_match_video_log_free')->insertGetId($data);
  1085. if(!$log_id){
  1086. $this->error('扣费失败');
  1087. }
  1088. $this->success('success');
  1089. }
  1090. //男性非会员解锁聊天
  1091. public function unlockchat() {
  1092. }
  1093. /**
  1094. * 是否关注
  1095. */
  1096. public function isFollows() {
  1097. $user_id = $this->request->request("user_id",0,"intval");
  1098. if (!$user_id || $user_id<=0) {
  1099. $this->error(__('Invalid parameters'));
  1100. }
  1101. $map = [
  1102. 'uid' => $this->auth->id,
  1103. 'follow_uid' => $user_id,
  1104. ];
  1105. $check = Db::name('user_follow')->where($map)->find();
  1106. $data = [];
  1107. if($check){
  1108. $data["is_show_follow"] = 0;
  1109. }else{
  1110. $data["is_show_follow"] = 1;
  1111. }
  1112. $this->success("获取成功!",$data);
  1113. }
  1114. //我的礼物榜
  1115. public function gift_ranklist(){
  1116. //日榜
  1117. $sql1 = Db::name('gift_user_typing') ->field('user_id,total_price')->where('user_to_id',$this->auth->id)->whereTime('createtime','today')->select(false);
  1118. $sql2 = Db::name('gift_user_dongtai')->field('user_id,total_price')->where('user_to_id',$this->auth->id)->whereTime('createtime','today')->select(false);
  1119. $sql = 'select log.user_id,sum(log.total_price) sum_price,user.nickname,user.avatar from ('.$sql1.' UNION ALL '.$sql2.') as log LEFT JOIN mt_user user on log.user_id = user.id group by log.user_id order by sum_price desc limit 10';
  1120. $rs_day = Db::query($sql);
  1121. //周榜
  1122. $sql1 = Db::name('gift_user_typing') ->field('user_id,total_price')->where('user_to_id',$this->auth->id)->whereTime('createtime','week')->select(false);
  1123. $sql2 = Db::name('gift_user_dongtai')->field('user_id,total_price')->where('user_to_id',$this->auth->id)->whereTime('createtime','week')->select(false);
  1124. $sql = 'select log.user_id,sum(log.total_price) sum_price,user.nickname,user.avatar from ('.$sql1.' UNION ALL '.$sql2.') as log LEFT JOIN mt_user user on log.user_id = user.id group by log.user_id order by sum_price desc limit 10';
  1125. $rs_week = Db::query($sql);
  1126. //月榜
  1127. $sql1 = Db::name('gift_user_typing') ->field('user_id,total_price')->where('user_to_id',$this->auth->id)->whereTime('createtime','month')->select(false);
  1128. $sql2 = Db::name('gift_user_dongtai')->field('user_id,total_price')->where('user_to_id',$this->auth->id)->whereTime('createtime','month')->select(false);
  1129. $sql = 'select log.user_id,sum(log.total_price) sum_price,user.nickname,user.avatar from ('.$sql1.' UNION ALL '.$sql2.') as log LEFT JOIN mt_user user on log.user_id = user.id group by log.user_id order by sum_price desc limit 10';
  1130. $rs_month = Db::query($sql);
  1131. //
  1132. $rs = [
  1133. 'day' => $rs_day,
  1134. 'week' => $rs_week,
  1135. 'month' => $rs_month,
  1136. ];
  1137. $this->success(1,$rs);
  1138. }
  1139. /////////////////////////////////////////////////////
  1140. /**
  1141. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  1142. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  1143. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  1144. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  1145. * @return float | false | string
  1146. */
  1147. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  1148. if( empty( $point_1 ) || empty( $point_2 ) ){
  1149. return false;
  1150. }
  1151. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  1152. $p1_x = $point_1[0];
  1153. $p1_y = $point_1[1];
  1154. $p2_x = $point_2[0];
  1155. $p2_y = $point_2[1];
  1156. if(
  1157. $p1_x < -180 || $p1_x > 180
  1158. || $p2_x < -180 || $p2_x > 180
  1159. || $p1_y < -90 || $p1_y > 90
  1160. || $p2_y < -90 || $p2_y > 90
  1161. ){
  1162. return '0公里';
  1163. }
  1164. // 根据2点各自的坐标,计算2点之间直线距离的公式
  1165. $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);
  1166. // 是否计算为字符串公里距离
  1167. if( !$calc_as_string ){
  1168. return (string)round( $distance / 1000 , 1 ) . '公里';
  1169. }
  1170. // 如果计算为字符串公里距离
  1171. if( $distance / 1000 > 1 ){
  1172. $k = (string)round( $distance / 1000 , 1 );
  1173. $m = (string)$distance % 1000 ;
  1174. $distance = "{$k}公里{$m}米";
  1175. }
  1176. else{
  1177. $distance = "{$distance}米";
  1178. }
  1179. return $distance;
  1180. }
  1181. //地图api,根据两地坐标,获得两地距离,打卡用的
  1182. //type=0直线,type=1开车
  1183. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  1184. $result = 0;
  1185. $apiurl = 'https://restapi.amap.com/v3/distance?';
  1186. $param = [
  1187. 'key' => '398c424811d1a59beac2f915323d334e',
  1188. 'origins' => $start_lon.','.$start_lat,
  1189. 'destination' => $end_lon.','.$end_lat,
  1190. 'type' => $type,
  1191. 'output' => 'json',
  1192. ];
  1193. $apiurl .= http_build_query($param);
  1194. $request_rs = json_decode(curl_get($apiurl),true);
  1195. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  1196. if(isset($request_rs['results'][0]['distance']))
  1197. {
  1198. $result = $request_rs['results'][0]['distance'];
  1199. }
  1200. }
  1201. //dump($result);
  1202. return $result;
  1203. }
  1204. public function distance()
  1205. {
  1206. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  1207. dump($a);
  1208. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  1209. dump($a);
  1210. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  1211. dump($b);
  1212. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  1213. dump($b);
  1214. }
  1215. }