Usercenter.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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. //'truename',//
  18. 'email',
  19. 'mobile',
  20. 'avatar',
  21. 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'audio_bio',
  28. 'idcard_status',
  29. 'is_active',
  30. 'longitude',
  31. 'latitude',
  32. 'cityname',
  33. 'photo_images',
  34. 'education_id',
  35. 'hobby_ids',
  36. 'job_id',
  37. 'marital_id',
  38. 'tag_ids',
  39. 'wages_id',
  40. 'hometown_cityid',
  41. 'wechat_account',
  42. 'secretvideo_status',
  43. 'character_id',
  44. 'constellation_id',
  45. 'stature_id',
  46. 'is_appointment',
  47. 'chat_price',
  48. 'voice_price',
  49. 'video_price',
  50. 'greet_voice',
  51. 'greet_chat',
  52. 'is_kefu',
  53. 'is_hideaddress', 'is_recommend','is_cohabit', 'live_id', 'is_house', 'car_id', 'chest_id', 'waist'
  54. ];
  55. //获取他人用户信息,留下足迹
  56. public function getuserinfo(){
  57. $uid = input_post('uid',0);
  58. $userinfo = Db::name('user')->field($this->allowFields)->where('id',$uid)->find();
  59. if(!$userinfo){
  60. $this->error('不存在的用户');
  61. }
  62. //用户数据
  63. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  64. $new_data = [
  65. 'age' => birthtime_to_age($userinfo['birthday']),
  66. ];
  67. if ($this->auth->gender == 1) {
  68. $qinmi_sum = Db::name('user_intimacy')->where(['uid' => $this->auth->id, 'other_uid' => $uid])->value('value');
  69. } elseif ($this->auth->gender == 0) {
  70. $qinmi_sum = Db::name('user_intimacy')->where(['uid' => $uid, 'other_uid' => $this->auth->id])->value('value');
  71. } else {
  72. $qinmi_sum = 0;
  73. }
  74. $new_data['qinmi_sum'] = $qinmi_sum ? :0;
  75. //合并
  76. $userinfo = array_merge($userinfo,$new_data);
  77. //枚举
  78. $userinfo['education'] = Db::name('enum_education')->where('id',$userinfo['education_id'])->value('name');
  79. $userinfo['education'] = $userinfo['education'] ? : '';
  80. $userinfo['hobby'] = Db::name('enum_hobby')->where('id','IN',$userinfo['hobby_ids'])->field(['id','name'])->select();
  81. $userinfo['job'] = Db::name('enum_job')->where('id',$userinfo['job_id'])->value('name');
  82. $userinfo['job'] = $userinfo['job'] ? : '';
  83. $userinfo['marital'] = Db::name('enum_marital')->where('id',$userinfo['marital_id'])->value('name');
  84. $userinfo['marital'] = $userinfo['marital'] ? : '';
  85. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  86. $userinfo['wages'] = Db::name('enum_wages')->where('id',$userinfo['wages_id'])->value('name');
  87. $userinfo['wages'] = $userinfo['wages'] ? : '';
  88. $userinfo['character'] = Db::name('enum_character')->where('id',$userinfo['character_id'])->value('name');
  89. $userinfo['character'] = $userinfo['character'] ? : '';
  90. $userinfo['constellation'] = Db::name('enum_constellation')->where('id',$userinfo['constellation_id'])->value('name');
  91. $userinfo['constellation'] = $userinfo['constellation'] ? : '';
  92. $userinfo['stature'] = Db::name('enum_stature')->where('id',$userinfo['stature_id'])->value('name');
  93. $userinfo['stature'] = $userinfo['stature'] ? : '';
  94. $userinfo['live'] = Db::name('enum_live')->where('id',$userinfo['live_id'])->value('name');
  95. $userinfo['live'] = $userinfo['live'] ? : '';
  96. $userinfo['car'] = Db::name('enum_car')->where('id',$userinfo['car_id'])->value('name');
  97. $userinfo['car'] = $userinfo['car'] ? : '';
  98. $userinfo['chest'] = Db::name('enum_chest')->where('id',$userinfo['chest_id'])->value('name');
  99. $userinfo['chest'] = $userinfo['chest'] ? : '';
  100. //家乡
  101. $userinfo['hometown_city'] = Db::name('area')->where('id',$userinfo['hometown_cityid'])->value('name');
  102. $userinfo['hometown_city'] = ($userinfo['hometown_city'] && $userinfo['is_hideaddress'] == 0) ? $userinfo['hometown_city'] : '';
  103. //vip
  104. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  105. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  106. //是否喜欢和关注
  107. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  108. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  109. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  110. $userinfo['is_like'] = $is_like ? 1 : 0;
  111. //是否拉黑
  112. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  113. $userinfo['is_black'] = $is_black ? 1 : 0;
  114. //关注人数,粉丝人数
  115. $follow_num = Db::name('user_follow')->where(['uid'=>$this->auth->id])->count('id');
  116. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$this->auth->id])->count('id');
  117. $userinfo['follow_num'] = $follow_num;
  118. $userinfo['fans_num'] = $fans_num;
  119. //访客数
  120. $visit_num = Db::name('user_visit')->where(['to_uid'=>$this->auth->id])->count('id');
  121. $userinfo['visit_num'] = $visit_num;
  122. //查询是否打过招呼
  123. $count = Db::name('user_greet')->where(['user_id' => $this->auth->id, 'user_to_id' => $uid])->count('id');
  124. if ($count) {
  125. $userinfo['is_chat'] = 1; //是否打过招呼: 1是 0否
  126. } else {
  127. $userinfo['is_chat'] = 0; //是否打过招呼: 1是 0否
  128. }
  129. //守护数量和是否已经守护
  130. $userinfo['guard_num'] = Db::name('user_guard')->where(['user_to_id' => $uid])->count('id');
  131. $userinfo['is_guard'] = Db::name('user_guard')->where(['user_id' => $this->auth->id, 'user_to_id' => $uid])->count('id');
  132. //查询财富等级和魅力等级
  133. $wallet_info = Db::name('user_wallet')->where(['user_id' => $uid])->find();
  134. $wealth_level = Db::name('wealth_level')->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  135. if ($wealth_level) {
  136. $userinfo['wealth_level'] = localpath_to_netpath($wealth_level['image']);
  137. } else {
  138. $userinfo['wealth_level'] = '';
  139. }
  140. $charm_level = Db::name('charm_level')->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  141. if ($charm_level) {
  142. $userinfo['charm_level'] = localpath_to_netpath($charm_level['image']);
  143. } else {
  144. $userinfo['charm_level'] = '';
  145. }
  146. //开通守护消耗金币数量
  147. $userinfo['guard_price'] = config('site.guard_price') ? : 0;
  148. //男性非会员是否已开通聊天
  149. // $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  150. // $user_chat_unlock = Db::name('user_chat_unlock')->where(['user_id' => $this->auth->id, 'user_to_id' => $uid])->count('id');
  151. // if ($this->auth->gender == 0 || $vip_endtime > time() || $user_chat_unlock) {
  152. $userinfo['is_chat_unlock'] = 1; //已解锁
  153. // } else {
  154. // $userinfo['is_chat_unlock'] = 0;
  155. // }
  156. //男性非会员解锁聊天所需金币数量
  157. $userinfo['unlock_chat_gold'] = config('site.unlock_chat_gold') > 0 ? config('site.unlock_chat_gold') : 10;
  158. //动态信息
  159. $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();
  160. if ($dongtai) {
  161. $dongtai_image = [];
  162. foreach ($dongtai as &$v) {
  163. $arr['type'] = $v['type'];
  164. if ($v['type'] == 1) {
  165. //图片
  166. $images_url = explode(',', $v['images']);
  167. foreach ($images_url as &$value) {
  168. $arr['images'] = one_domain_image($value);
  169. array_push($dongtai_image, $arr);
  170. if (count($dongtai_image) == 3) {
  171. break 2;
  172. }
  173. }
  174. } else {
  175. //视频
  176. $images_url = explode('.', $v['images']);
  177. unset($images_url[count($images_url) - 1]);
  178. $arr['images'] = join('.', $images_url) . '_0.jpg';
  179. $arr['images'] = one_domain_image($arr['images']);
  180. array_push($dongtai_image, $arr);
  181. if (count($dongtai_image) == 3) {
  182. break;
  183. }
  184. }
  185. }
  186. $userinfo['dongtai_image'] = $dongtai_image;
  187. // $dongtai = join(',', $dongtai);
  188. // $dongtai = explode(',', $dongtai);
  189. // $dongtai = array_slice($dongtai, 0, 3);
  190. // $dongtai = join(',', $dongtai);
  191. // $userinfo['dongtai_image'] = one_domain_image($dongtai);
  192. // $userinfo['dongtai_image'] = explode(',', $userinfo['dongtai_image']);
  193. } else {
  194. $userinfo['dongtai_image'] = [];
  195. }
  196. if ($this->auth->id != $uid) {
  197. //查看别人信息,就要留下痕迹
  198. $data = [
  199. 'uid' => $this->auth->id,
  200. 'to_uid' => $uid,
  201. ];
  202. $check = Db::name('user_visit')->where($data)->find();
  203. if ($check) {
  204. Db::name('user_visit')->where($data)->update(['number' => $check['number'] + 1, 'updatetime' => time()]);
  205. } else {
  206. $data['number'] = 1;
  207. $data['updatetime'] = time();
  208. Db::name('user_visit')->insertGetId($data);
  209. }
  210. }
  211. //礼物墙
  212. $userinfo['gift_wall'] = $this->giftwall($uid);
  213. $this->success('success',$userinfo);
  214. }
  215. //某用户的礼物墙
  216. private function giftwall($uid){
  217. //$uid = $this->auth->id;
  218. //聊天礼物
  219. $gift_user_typing = Db::name('gift_user_typing')->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_typing = list_domain_image($gift_user_typing,['image']);
  227. //dump($gift_user_typing);
  228. //动态礼物
  229. $gift_user_dongtai = Db::name('gift_user_dongtai')->alias('log')
  230. ->join('gift', 'gift.id = log.gift_id', 'LEFT')
  231. ->field('log.gift_id,log.gift_name,sum(log.number) as number,gift.image')
  232. ->where(['log.user_to_id' => $uid])
  233. ->group('log.gift_id')
  234. ->order('gift.price desc')
  235. ->column('log.gift_id,log.gift_name,sum(log.number) as number,gift.image');
  236. $gift_user_dongtai = list_domain_image($gift_user_dongtai,['image']);
  237. //dump($gift_user_dongtai);
  238. //为空
  239. if(empty($gift_user_typing) && empty($gift_user_dongtai)){
  240. return [];
  241. }
  242. if(empty($gift_user_typing) && !empty($gift_user_dongtai)){
  243. return $gift_user_dongtai;
  244. }
  245. if(!empty($gift_user_typing) && empty($gift_user_dongtai)){
  246. return $gift_user_typing;
  247. }
  248. //合并
  249. foreach($gift_user_typing as $key => $val){
  250. foreach($gift_user_dongtai as $k => $v){
  251. if($key == $k){
  252. $gift_user_typing[$key]['number'] += $v['number'];
  253. }
  254. if(!isset($gift_user_typing[$k])){
  255. $gift_user_typing[$k] = $v;
  256. }
  257. }
  258. }
  259. //dump($gift_user_typing);
  260. //结果
  261. $result = [];
  262. foreach($gift_user_typing as $key => $val){
  263. $result[] = $val;
  264. }
  265. return $result;
  266. }
  267. //这里不用连user_active表,完全使用user表的active_time,user_active表只做离线用
  268. //同城
  269. public function samecity(){
  270. $gender = input_post('gender','all');
  271. $agemin = input_post('agemin',0);
  272. $agemax = input_post('agemax',100);
  273. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  274. // $this->success('success',[]);
  275. }
  276. $map = [
  277. 'user.status' => 1,
  278. // 'user.cityname' => $this->auth->cityname,
  279. 'user.id' => ['neq',$this->auth->id],
  280. // 'user.longitude' => ['neq',''],
  281. // 'user.latitude' => ['neq',''],
  282. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  283. 'user.active_time' => ['gt',time()-86400],
  284. ];
  285. if($gender != 'all'){
  286. $map['user.gender'] = $gender;
  287. }
  288. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  289. //dump($map);
  290. $field = [
  291. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender','user.active_time'
  292. ];
  293. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('user.active_time desc')->autopage()->select();
  294. //dump($list);
  295. $list = list_domain_image($list,['avatar']);
  296. foreach($list as $key => $one){
  297. $one['age'] = birthtime_to_age($one['birthday']);
  298. // $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  299. $one['distance'] = rand(0,10).'km';
  300. //状态
  301. $one['active_text'] = '刚刚离线';
  302. if($one['active_time'] > time()-21600){
  303. $one['active_text'] = '1小时内在线';
  304. }
  305. if($one['active_time'] > time()-3600){
  306. $one['active_text'] = '在线';
  307. }
  308. $list[$key] = $one;
  309. }
  310. $this->success('success',$list);
  311. }
  312. //附近
  313. public function nearuser(){
  314. $gender = input_post('gender','all');
  315. $agemin = input_post('agemin',0);
  316. $agemax = input_post('agemax',100);
  317. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  318. // $this->success('success',[]);
  319. }
  320. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  321. $map = [
  322. 'user.status' => 1,
  323. ////'user.cityname' => $this->auth->cityname,
  324. 'user.id' => ['neq',$this->auth->id],
  325. // 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  326. // 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  327. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  328. 'user.active_time' => ['gt',time()-86400],
  329. ];
  330. if($gender != 'all'){
  331. $map['user.gender'] = $gender;
  332. }
  333. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  334. //dump($map);
  335. $field = [
  336. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender','user.active_time'
  337. ];
  338. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('user.active_time desc')->autopage()->select();
  339. //dump($list);exit;
  340. $list = list_domain_image($list,['avatar']);
  341. foreach($list as $key => $one){
  342. $one['age'] = birthtime_to_age($one['birthday']);
  343. //$one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  344. $one['distance'] = rand(0,10).'km';
  345. //状态
  346. $one['active_text'] = '刚刚离线';
  347. if($one['active_time'] > time()-21600){
  348. $one['active_text'] = '1小时内在线';
  349. }
  350. if($one['active_time'] > time()-3600){
  351. $one['active_text'] = '在线';
  352. }
  353. $list[$key] = $one;
  354. }
  355. $this->success('success',$list);
  356. }
  357. //因为接受者拿不到发起者的uid,废弃了
  358. //视频和语音,接收方使用,如果是性别劣势方,检查钱是否够用
  359. public function video_audio_moneycheck(){
  360. //检测用户,发起方的uid
  361. $to_user_id = input_post('from_user_id');
  362. $to_user_info = Db::name('user')->field('id,real_status,gender')->where('id',$to_user_id)->find();
  363. if(!$to_user_info){
  364. $this->error('不存在的用户');
  365. }
  366. //扣费金币
  367. $type = input_post('type','video'); //类型
  368. $price = $type == 'video' ? config('site.video_min_price') : config('site.audio_min_price');
  369. //发起用户的分数,被发起用户的分数。按性别给分
  370. $auth_level = 0;
  371. $tous_level = 0;
  372. //打分
  373. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  374. $auth_level = 30;//实名女最高
  375. }
  376. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  377. $auth_level = 20;//未实名女次之
  378. }
  379. if($this->auth->gender == 1){
  380. $auth_level = 10;//男性最低
  381. }
  382. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  383. $tous_level = 30;
  384. }
  385. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  386. $tous_level = 20;
  387. }
  388. if($to_user_info['gender'] == 1){
  389. $tous_level = 10;
  390. }
  391. //同性不收钱
  392. //都是男的,不扣钱
  393. //都是实名认证的女性,不扣钱
  394. //都是未实名认证的女性,不扣钱
  395. if($auth_level == $tous_level){
  396. $price = 0;
  397. $this->success('success');
  398. }
  399. //扣钱uid,收钱uid,收钱free_video
  400. //分数少扣钱,分数多收益
  401. if($auth_level < $tous_level){
  402. $kou_user = $this->auth->id;
  403. }else{
  404. $kou_user = $to_user_info['id'];
  405. }
  406. //需要扣我的(接收方的)钱,判断钱是否够
  407. if($price > 0 && $kou_user == $this->auth->id){
  408. Db::startTrans();
  409. $gold = model('wallet')->getWallet($kou_user,'gold');
  410. if(bccomp($price,$gold) == 1){
  411. Db::rollback();
  412. $this->error('金币不足');
  413. }
  414. Db::commit();
  415. }
  416. $this->success('success');
  417. }
  418. /**
  419. * 是否关注
  420. */
  421. public function isFollows() {
  422. $user_id = $this->request->request("user_id",0,"intval");
  423. if (!$user_id || $user_id<=0) {
  424. $this->error(__('Invalid parameters'));
  425. }
  426. $map = [
  427. 'uid' => $this->auth->id,
  428. 'follow_uid' => $user_id,
  429. ];
  430. $check = Db::name('user_follow')->where($map)->find();
  431. $data = [];
  432. if($check){
  433. $data["is_show_follow"] = 0;
  434. }else{
  435. $data["is_show_follow"] = 1;
  436. }
  437. $this->success("获取成功!",$data);
  438. }
  439. /**
  440. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  441. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  442. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  443. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  444. * @return float | false | string
  445. */
  446. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  447. if( empty( $point_1 ) || empty( $point_2 ) ){
  448. return false;
  449. }
  450. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  451. $p1_x = $point_1[0];
  452. $p1_y = $point_1[1];
  453. $p2_x = $point_2[0];
  454. $p2_y = $point_2[1];
  455. if(
  456. $p1_x < -180 || $p1_x > 180
  457. || $p2_x < -180 || $p2_x > 180
  458. || $p1_y < -90 || $p1_y > 90
  459. || $p2_y < -90 || $p2_y > 90
  460. ){
  461. return '0公里';
  462. }
  463. // 根据2点各自的坐标,计算2点之间直线距离的公式
  464. $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);
  465. // 是否计算为字符串公里距离
  466. if( !$calc_as_string ){
  467. return (string)round( $distance / 1000 , 1 ) . '公里';
  468. }
  469. // 如果计算为字符串公里距离
  470. if( $distance / 1000 > 1 ){
  471. $k = (string)round( $distance / 1000 , 1 );
  472. $m = (string)$distance % 1000 ;
  473. $distance = "{$k}公里{$m}米";
  474. }
  475. else{
  476. $distance = "{$distance}米";
  477. }
  478. return $distance;
  479. }
  480. //地图api,根据两地坐标,获得两地距离,打卡用的
  481. //type=0直线,type=1开车
  482. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  483. $result = 0;
  484. $apiurl = 'https://restapi.amap.com/v3/distance?';
  485. $param = [
  486. 'key' => '398c424811d1a59beac2f915323d334e',
  487. 'origins' => $start_lon.','.$start_lat,
  488. 'destination' => $end_lon.','.$end_lat,
  489. 'type' => $type,
  490. 'output' => 'json',
  491. ];
  492. $apiurl .= http_build_query($param);
  493. $request_rs = json_decode(curl_get($apiurl),true);
  494. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  495. if(isset($request_rs['results'][0]['distance']))
  496. {
  497. $result = $request_rs['results'][0]['distance'];
  498. }
  499. }
  500. //dump($result);
  501. return $result;
  502. }
  503. public function distance()
  504. {
  505. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  506. dump($a);
  507. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  508. dump($a);
  509. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  510. dump($b);
  511. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  512. dump($b);
  513. }
  514. //开通守护
  515. public function addguard() {
  516. // 接口防并发
  517. if (!$this->apiLimit(1, 1)) {
  518. $this->error(__('Operation frequently'));
  519. }
  520. $user_id = input('user_id', 0, 'intval');// 守护对象
  521. if (!$user_id) {
  522. $this->error();
  523. }
  524. // 不可以守护给自己
  525. if($this->auth->id == $user_id) {
  526. $this->error("不可以守护自己");
  527. }
  528. //查询是否开通守护
  529. $count = Db::name('user_guard')->where(['user_id' => $this->auth->id, 'user_to_id' => $user_id])->count('id');
  530. if ($count) {
  531. $this->error('已经开通守护了');
  532. }
  533. $giftvalue = config('site.guard_price');
  534. if ($giftvalue <= 0) {
  535. $this->error('守护金额异常~');
  536. }
  537. //被守护人信息
  538. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  539. if (!$touserinfo) {
  540. $this->error("不存在的用户");
  541. }
  542. // 判断当前用户余额
  543. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  544. if($user_gold < $giftvalue) {
  545. $this->error("您的金币余额不足");
  546. }
  547. Db::startTrans();
  548. // 添加礼物守护记录表
  549. $data = [
  550. 'user_id' => $this->auth->id,
  551. 'user_to_id' => $user_id,
  552. 'price' => $giftvalue,
  553. 'createtime' => time(),
  554. ];
  555. $log_id = Db::name('user_guard')->insertGetId($data);
  556. if(!$log_id){
  557. Db::rollback();
  558. $this->error('守护失败');
  559. }
  560. if($giftvalue > 0){
  561. // 扣除当前用户余额
  562. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,81,'开通守护','user_guard',$log_id);
  563. if($wallet_rs['status'] === false){
  564. Db::rollback();
  565. $this->error($wallet_rs['msg']);
  566. }
  567. // 添加守护用户余额
  568. $money_to_gold = config('site.money_to_gold');
  569. $gift_plat_scale = config('site.gift_plat_scale');
  570. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  571. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  572. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,82,'被守护收益','user_guard',$log_id,2);
  573. if($wallet_rs['status'] === false){
  574. Db::rollback();
  575. $this->error($wallet_rs['msg']);
  576. }
  577. //增加赠送用户上级余额
  578. if ($touserinfo['intro_uid']) {
  579. //获取返利比率
  580. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  581. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  582. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  583. //上级获得金额
  584. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  585. if ($intro_uid_money > 0) {
  586. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人开通守护获赠奖励','gift_user_typing',$log_id);
  587. if($intro_result['status']===false)
  588. {
  589. Db::rollback();
  590. $this->error($intro_result['msg']);
  591. }
  592. }
  593. }
  594. }
  595. }
  596. //tag任务守护金币
  597. //搭讪奖励
  598. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  599. // if($task_rs === false){
  600. // Db::rollback();
  601. // $this->error('完成任务守护奖励失败');
  602. // }
  603. Db::commit();
  604. $this->success('守护成功');
  605. }
  606. //守护列表
  607. public function guardlist(){
  608. $user_id = input('user_id', 0, 'intval');
  609. if (!$user_id) {
  610. $this->error('您的网络开小差啦~');
  611. }
  612. $list = Db::name('user_guard')
  613. ->alias('a')
  614. ->join('user','a.user_id = user.id','LEFT')
  615. ->field('a.createtime,user.id,user.nickname,user.avatar,user.real_status')
  616. ->where('a.user_to_id',$user_id)->order('a.id desc')->autopage()->select();
  617. if (!$list) {
  618. $this->success('success', $list);
  619. }
  620. $list = list_domain_image($list,['avatar']);
  621. $time = time();
  622. foreach ($list as &$v) {
  623. $v['createtime'] = ceil(($time - $v['createtime']) / 86400);
  624. }
  625. $this->success('success',$list);
  626. }
  627. //我的守护
  628. public function myguardlist(){
  629. $type = input('type', 0, 'intval'); //类型: 1我守护的 2守护我的
  630. if (!in_array($type, [1, 2])) {
  631. $this->error('您的网络开小差了');
  632. }
  633. $where = [];
  634. if ($type == 1) {
  635. $join = 'a.user_to_id = user.id';
  636. $where['a.user_id'] = $this->auth->id;
  637. } else {
  638. $join = 'a.user_id = user.id';
  639. $where['a.user_to_id'] = $this->auth->id;
  640. }
  641. $list = Db::name('user_guard')
  642. ->alias('a')
  643. ->join('user',$join,'LEFT')
  644. ->field('a.createtime,user.id,user.nickname,user.avatar,user.real_status')
  645. ->where($where)->order('a.id desc')->autopage()->select();
  646. if (!$list) {
  647. $this->success('success', $list);
  648. }
  649. $list = list_domain_image($list,['avatar']);
  650. $time = time();
  651. foreach ($list as &$v) {
  652. $v['createtime'] = ceil(($time - $v['createtime']) / 86400);
  653. }
  654. $this->success('success',$list);
  655. }
  656. //打字聊天每句话调用一次
  657. public function chat_once(){
  658. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  659. $this->success('success',array('money'=>''));
  660. }
  661. if ($this->auth->gender == 0) { //女生不花钱
  662. $this->error('您的网络开小差啦~');
  663. }
  664. //检测用户
  665. $to_user_id = input_post('to_user_id');
  666. $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();
  667. if(!$to_user_info){
  668. $this->error('不存在的用户');
  669. }
  670. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  671. $this->success('success',array('money'=>''));
  672. }
  673. if ($to_user_info['gender'] != 0) {
  674. $this->error('同性不能聊天~');
  675. }
  676. //验证金额
  677. $wallet_info = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  678. if ($wallet_info['vip_endtime'] >= time()) { //会员
  679. $price = $to_user_info['chat_price'];//config('site.typing_min_price'); //扣费金币
  680. //获取折扣
  681. $vip_chat_discount = config('site.vip_chat_discount');
  682. if ($vip_chat_discount >= 0 && $vip_chat_discount <= 10) {
  683. $price = ceil($price * $vip_chat_discount / 10);
  684. }
  685. } else {
  686. $price = $to_user_info['chat_price'];//config('site.typing_min_price'); //扣费金币
  687. }
  688. //正常价格
  689. $bili = config('site.money_to_gold'); //兑换比例
  690. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  691. $money = bcdiv($price,$bili,2); //对应人民币
  692. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  693. $resArray['money'] = $money; //返回给前端的计算结果
  694. Db::startTrans();
  695. //记录日志
  696. $data = [
  697. 'user_id' => $this->auth->id,
  698. 'price' => $price,
  699. 'createtime' => time(),
  700. 'to_user_id' => $to_user_id,
  701. 'money' => $money,
  702. ];
  703. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  704. if(!$log_id){
  705. Db::rollback();
  706. $this->error('扣费失败');
  707. }
  708. //有性别差,扣费
  709. if($price > 0){
  710. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$to_user_id,'gold',-$price,13,'文字聊天消费','user_match_typing_log',$log_id);
  711. if($rs['status'] === false){
  712. Db::rollback();
  713. $this->error($rs['msg']);
  714. }
  715. }
  716. //另一方加钱,0收费
  717. if($money > 0){
  718. $rs = model('wallet')->lockChangeAccountRemain($to_user_id,$this->auth->id,'money',$money,23,'文字聊天收益','user_match_typing_log',$log_id,2);
  719. if($rs['status'] === false){
  720. Db::rollback();
  721. $this->error($rs['msg']);
  722. }
  723. $touserinfo = $to_user_info;
  724. //增加赠送用户上级余额
  725. if ($touserinfo['intro_uid']) {
  726. //获取返利比率
  727. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  728. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  729. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  730. //上级获得金额
  731. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  732. if ($intro_uid_money > 0) {
  733. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$to_user_id,'money',$intro_uid_money,68, '邀请人文字聊天获赠奖励','gift_user_typing',$log_id);
  734. if($intro_result['status']===false)
  735. {
  736. Db::rollback();
  737. $this->error($intro_result['msg']);
  738. }
  739. }
  740. }
  741. }
  742. }
  743. //增加亲密度
  744. $user_intimacy_rs = addintimacy($this->auth->id, $to_user_id, $price);
  745. if (!$user_intimacy_rs['status']) {
  746. Db::rollback();
  747. $this->error('您的网络开小差啦~');
  748. }
  749. //tag任务赠送金币
  750. //私信5名异性奖励
  751. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,23);
  752. if($task_rs === false){
  753. Db::rollback();
  754. $this->error('完成任务赠送奖励失败');
  755. }
  756. /*$task_rs = \app\common\model\TaskLog::tofinish($to_user_id,23);
  757. if($task_rs === false){
  758. Db::rollback();
  759. $this->error('完成任务赠送奖励失败');
  760. }*/
  761. Db::commit();
  762. //发送消息
  763. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  764. $tenim = new \app\api\controller\Tenim;
  765. $tenim->sendMessageToUser($this->auth->id, $to_user_id, $user_intimacy_rs['level_remark'], 1);
  766. }
  767. $this->success('success',$resArray);
  768. }
  769. //语音通话每分钟调用一次
  770. public function voice_onemin(){
  771. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  772. $this->success('success');
  773. }
  774. if ($this->auth->gender == 0) { //女生不花钱
  775. $this->error('您的网络开小差啦~');
  776. }
  777. //检测用户
  778. $to_user_id = input_post('to_user_id');
  779. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  780. $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();
  781. if(!$to_user_info){
  782. $this->error('不存在的用户');
  783. }
  784. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  785. $this->success('success');
  786. }
  787. if ($to_user_info['gender'] != 0) {
  788. $this->error('同性不能聊天~');
  789. }
  790. //验证金额
  791. $wallet_info = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  792. if ($wallet_info['vip_endtime'] >= time()) { //会员
  793. $price = $to_user_info['voice_price'];//config('site.video_min_price'); //扣费金币
  794. //获取折扣
  795. $vip_video_discount = config('site.vip_video_discount');
  796. if ($vip_video_discount >= 0 && $vip_video_discount <= 10) {
  797. $price = ceil($price * $vip_video_discount / 10);
  798. }
  799. } else {
  800. $price = $to_user_info['voice_price'];//config('site.video_min_price'); //扣费金币
  801. }
  802. //正常价格
  803. $bili = config('site.money_to_gold'); //兑换比例
  804. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  805. $money = bcdiv($price,$bili,2); //对应人民币
  806. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  807. Db::startTrans();
  808. //查询是否有匹配记录
  809. $user_match_audio_log_info = [];
  810. if ($request_id) {
  811. $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();
  812. }
  813. if ($user_match_audio_log_info) {
  814. //修改记录日志
  815. $data = [
  816. 'price' => $user_match_audio_log_info['price'] + $price,
  817. 'createtime' => time(),
  818. 'money' => $user_match_audio_log_info['money'] + $money,
  819. 'call_minutes' => $user_match_audio_log_info['call_minutes'] + 1
  820. ];
  821. $log_id = Db::name('user_match_audio_log')->where(['id' => $user_match_audio_log_info['id'], 'createtime' => $user_match_audio_log_info['createtime']])->setField($data);
  822. if (!$log_id) {
  823. Db::rollback();
  824. $this->error('扣费失败');
  825. }
  826. } else {
  827. //添加记录日志
  828. $data = [
  829. 'user_id' => $this->auth->id,
  830. 'price' => $price,
  831. 'createtime' => time(),
  832. 'to_user_id' => $to_user_id,
  833. 'money' => $money,
  834. 'request_id' => $request_id,
  835. 'call_minutes' => 1
  836. ];
  837. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  838. if (!$log_id) {
  839. Db::rollback();
  840. $this->error('扣费失败');
  841. }
  842. }
  843. //有性别差,扣费
  844. if($price > 0){
  845. $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);
  846. if($rs['status'] === false){
  847. Db::rollback();
  848. $this->error($rs['msg']);
  849. }
  850. }
  851. //另一方加钱,0收费
  852. if($money > 0){
  853. $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);
  854. if($rs['status'] === false){
  855. Db::rollback();
  856. $this->error($rs['msg']);
  857. }
  858. $touserinfo = $to_user_info;
  859. //增加赠送用户上级余额
  860. if ($touserinfo['intro_uid']) {
  861. //获取返利比率
  862. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  863. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  864. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  865. //上级获得金额
  866. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  867. if ($intro_uid_money > 0) {
  868. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$to_user_id,'money',$intro_uid_money,68, '邀请人语音聊天获赠奖励' . $data['call_minutes'] . '分钟','gift_user_typing',$log_id, 0, $request_id);
  869. if($intro_result['status']===false)
  870. {
  871. Db::rollback();
  872. $this->error($intro_result['msg']);
  873. }
  874. }
  875. }
  876. }
  877. }
  878. //增加亲密度
  879. $user_intimacy_rs = addintimacy($this->auth->id, $to_user_id, $price);
  880. if (!$user_intimacy_rs['status']) {
  881. Db::rollback();
  882. $this->error('您的网络开小差啦~');
  883. }
  884. //tag任务赠送金币
  885. //与1名异性语音通话奖励
  886. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,25);
  887. if($task_rs === false){
  888. Db::rollback();
  889. $this->error('完成任务赠送奖励失败');
  890. }
  891. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,25);
  892. if($task_rs === false){
  893. Db::rollback();
  894. $this->error('完成任务赠送奖励失败');
  895. }
  896. //发送消息
  897. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  898. $tenim = new \app\api\controller\Tenim;
  899. $tenim->sendMessageToUser($this->auth->id, $to_user_id, $user_intimacy_rs['level_remark'], 1);
  900. }
  901. Db::commit();
  902. $this->success('success');
  903. }
  904. //视频通话每分钟调用一次
  905. public function videochat_onemin(){
  906. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  907. $this->success('success');
  908. }
  909. if ($this->auth->gender == 0) { //女生不花钱
  910. $this->error('您的网络开小差啦~');
  911. }
  912. //检测用户
  913. $to_user_id = input_post('to_user_id');
  914. $request_id = input('request_id', '', 'trim'); //唯一请求标识
  915. $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();
  916. if(!$to_user_info){
  917. $this->error('不存在的用户');
  918. }
  919. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  920. $this->success('success');
  921. }
  922. if ($to_user_info['gender'] != 0) {
  923. $this->error('同性不能聊天~');
  924. }
  925. /* //检查是否免费过
  926. $count = Db::name('user_match_video_log_free')->where(['user_id' => $this->auth->id])->count('id');
  927. if (!$count) {
  928. //记录日志
  929. $data = [
  930. 'user_id' => $this->auth->id,
  931. 'price' => 0,
  932. 'createtime' => time(),
  933. 'to_user_id' => $to_user_id,
  934. 'money' => 0,
  935. ];
  936. $log_id = Db::name('user_match_video_log_free')->insertGetId($data);
  937. if(!$log_id){
  938. $this->error('扣费失败');
  939. }
  940. $this->success('success');
  941. }*/
  942. //验证金额
  943. $wallet_info = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  944. if ($wallet_info['vip_endtime'] >= time()) { //会员
  945. $price = $to_user_info['video_price'];//config('site.video_min_price'); //扣费金币
  946. //获取折扣
  947. $vip_video_discount = config('site.vip_video_discount');
  948. if ($vip_video_discount >= 0 && $vip_video_discount <= 10) {
  949. $price = ceil($price * $vip_video_discount / 10);
  950. }
  951. } else {
  952. $price = $to_user_info['video_price'];//config('site.video_min_price'); //扣费金币
  953. }
  954. //正常价格
  955. $bili = config('site.money_to_gold'); //兑换比例
  956. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  957. $money = bcdiv($price,$bili,2); //对应人民币
  958. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  959. Db::startTrans();
  960. //查询是否有匹配记录
  961. $user_match_video_log_info = [];
  962. if ($request_id) {
  963. $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();
  964. }
  965. if ($user_match_video_log_info) {
  966. //修改记录日志
  967. $data = [
  968. 'price' => $user_match_video_log_info['price'] + $price,
  969. 'createtime' => time(),
  970. 'money' => $user_match_video_log_info['money'] + $money,
  971. 'call_minutes' => $user_match_video_log_info['call_minutes'] + 1
  972. ];
  973. $log_id = Db::name('user_match_video_log')->where(['id' => $user_match_video_log_info['id'], 'createtime' => $user_match_video_log_info['createtime']])->setField($data);
  974. if (!$log_id) {
  975. Db::rollback();
  976. $this->error('扣费失败');
  977. }
  978. } else {
  979. //添加记录日志
  980. $data = [
  981. 'user_id' => $this->auth->id,
  982. 'price' => $price,
  983. 'createtime' => time(),
  984. 'to_user_id' => $to_user_id,
  985. 'money' => $money,
  986. 'request_id' => $request_id,
  987. 'call_minutes' => 1
  988. ];
  989. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  990. if (!$log_id) {
  991. Db::rollback();
  992. $this->error('扣费失败');
  993. }
  994. }
  995. //有性别差,扣费
  996. if($price > 0){
  997. $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);
  998. if($rs['status'] === false){
  999. Db::rollback();
  1000. $this->error($rs['msg']);
  1001. }
  1002. }
  1003. //另一方加钱,0收费
  1004. if($money > 0){
  1005. $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);
  1006. if($rs['status'] === false){
  1007. Db::rollback();
  1008. $this->error($rs['msg']);
  1009. }
  1010. $touserinfo = $to_user_info;
  1011. //增加赠送用户上级余额
  1012. if ($touserinfo['intro_uid']) {
  1013. //获取返利比率
  1014. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  1015. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  1016. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  1017. //上级获得金额
  1018. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  1019. if ($intro_uid_money > 0) {
  1020. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$to_user_id,'money',$intro_uid_money,68, '邀请人视频聊天获赠奖励' . $data['call_minutes'] . '分钟','gift_user_typing',$log_id, 0, $request_id);
  1021. if($intro_result['status']===false)
  1022. {
  1023. Db::rollback();
  1024. $this->error($intro_result['msg']);
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }
  1030. //增加亲密度
  1031. $user_intimacy_rs = addintimacy($this->auth->id, $to_user_id, $price);
  1032. if (!$user_intimacy_rs['status']) {
  1033. Db::rollback();
  1034. $this->error('您的网络开小差啦~');
  1035. }
  1036. //tag任务赠送金币
  1037. //与1名异性视频通话奖励
  1038. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,26);
  1039. if($task_rs === false){
  1040. Db::rollback();
  1041. $this->error('完成任务赠送奖励失败');
  1042. }
  1043. $task_rs = \app\common\model\TaskLog::tofinish($to_user_id,26);
  1044. if($task_rs === false){
  1045. Db::rollback();
  1046. $this->error('完成任务赠送奖励失败');
  1047. }
  1048. Db::commit();
  1049. //发送消息
  1050. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  1051. $tenim = new \app\api\controller\Tenim;
  1052. $tenim->sendMessageToUser($this->auth->id, $to_user_id, $user_intimacy_rs['level_remark'], 1);
  1053. }
  1054. $this->success('success');
  1055. }
  1056. //消息列表好友/密友
  1057. public function intimacyfriends() {
  1058. $type = input('type', 0, 'intval'); //类型:0消息列表好友 1密友 2好友
  1059. $where = [];
  1060. /*if ($type == 0) {
  1061. $list = Db::name('user_follow')->field('follow_uid user_id')->where(['uid' => $this->auth->id])->autopage()->order('id desc')->select();
  1062. $mt_user = Db::name('user');
  1063. // $mt_user_follow = Db::name('user_follow');
  1064. $mt_user_intimacy = Db::name('user_intimacy');
  1065. foreach ($list as &$v) {
  1066. //查询亲密度
  1067. if ($this->auth->gender == 1) {
  1068. $where['uid'] = $this->auth->id;
  1069. $where['other_uid'] = $v['user_id'];
  1070. } else {
  1071. $where['other_uid'] = $this->auth->id;
  1072. $where['uid'] = $v['user_id'];
  1073. }
  1074. $user_intimacy = $mt_user_intimacy->field('value, updatetime')->where($where)->find();
  1075. $v['value'] = $user_intimacy ? $user_intimacy['value'] : 0;
  1076. $v['updatetime'] = $user_intimacy ? $user_intimacy['updatetime'] : 0;
  1077. //查询对方信息
  1078. $user_info = $mt_user->field('nickname, avatar,birthday,gender,real_status')->where(['id' => $v['user_id']])->find();
  1079. $v['nickname'] = $user_info['nickname'];
  1080. $v['avatar'] = one_domain_image($user_info['avatar']);
  1081. $v['updatetime'] = get_last_time($v['updatetime']);
  1082. $v['age'] = birthtime_to_age($user_info['birthday']);
  1083. $v['gender'] = $user_info['gender'];
  1084. $v['real_status'] = $user_info['real_status'];
  1085. // $v['status'] = $mt_user_follow->where(['uid' => $this->auth->id, 'follow_uid' => $v['user_id']])->count('id');
  1086. $v['status'] = 1;
  1087. }
  1088. } else {*/
  1089. if ($this->auth->gender == 1) {
  1090. $where['uid'] = $this->auth->id;
  1091. $field = 'other_uid user_id, value, updatetime';
  1092. } else {
  1093. $where['other_uid'] = $this->auth->id;
  1094. $field = 'uid user_id, value, updatetime';
  1095. }
  1096. if ($type == 1) { //密友
  1097. $where['value'] = ['egt', 1000];
  1098. } elseif ($type == 2) { //好友
  1099. $where['value'] = ['egt', 100];
  1100. } else { //消息列表好友
  1101. $where['value'] = ['egt', 10];
  1102. }
  1103. $list = Db::name('user_intimacy')->field($field)->where($where)->autopage()->order('updatetime desc')->select();
  1104. $mt_user = Db::name('user');
  1105. $mt_user_follow = Db::name('user_follow');
  1106. foreach ($list as &$v) {
  1107. $user_info = $mt_user->field('nickname, avatar,birthday,gender,real_status,is_active')->where(['id' => $v['user_id']])->find();
  1108. $v['nickname'] = $user_info['nickname'];
  1109. $v['avatar'] = one_domain_image($user_info['avatar']);
  1110. $v['updatetime'] = get_last_time($v['updatetime']);
  1111. $v['age'] = birthtime_to_age($user_info['birthday']);
  1112. $v['gender'] = $user_info['gender'];
  1113. $v['real_status'] = $user_info['real_status'];
  1114. $v['status'] = $mt_user_follow->where(['uid' => $this->auth->id, 'follow_uid' => $v['user_id']])->count('id');
  1115. $v['is_active'] = $user_info['is_active'];
  1116. }
  1117. // }
  1118. $this->success('success',$list);
  1119. }
  1120. //消息列表通话
  1121. public function calllist() {
  1122. if ($this->auth->gender == 1) {
  1123. $list = Db::name('user_gold_log')->where(['user_id' => $this->auth->id, 'log_type' => ['in', [11, 12]]])->order('id desc')->autopage()->select();
  1124. } elseif ($this->auth->gender == 0) {
  1125. $list = Db::name('user_money_log')->where(['user_id' => $this->auth->id, 'log_type' => ['in', [21, 22]]])->order('id desc')->autopage()->select();
  1126. } else {
  1127. $this->success('success', []);
  1128. }
  1129. if (!$list) {
  1130. $this->success('success', $list);
  1131. }
  1132. $arr = [];
  1133. $mt_user_match_audio_log = Db::name('user_match_audio_log');
  1134. $mt_user_match_video_log = Db::name('user_match_video_log');
  1135. $mt_user = Db::name('user');
  1136. foreach ($list as &$v) {
  1137. $data = [];
  1138. if ($this->auth->gender == 1) {
  1139. if ($v['log_type'] == 11) { //视频通话
  1140. $log_info = $mt_user_match_video_log->field('to_user_id user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  1141. $data['desc'] = '视频时长' . $log_info['call_minutes'] . '分钟';
  1142. } else { //语音通话
  1143. $log_info = $mt_user_match_audio_log->field('to_user_id user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  1144. $data['desc'] = '语音时长' . $log_info['call_minutes'] . '分钟';
  1145. }
  1146. } else {
  1147. if ($v['log_type'] == 21) { //视频通话
  1148. $log_info = $mt_user_match_video_log->field('user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  1149. $data['desc'] = '视频时长' . $log_info['call_minutes'] . '分钟';
  1150. } else { //语音通话
  1151. $log_info = $mt_user_match_audio_log->field('user_id, createtime, call_minutes')->where(['id' => $v['table_id']])->find();
  1152. $data['desc'] = '语音时长' . $log_info['call_minutes'] . '分钟';
  1153. }
  1154. }
  1155. $user_info = $mt_user->field('nickname, avatar,is_active')->where(['id' => $log_info['user_id']])->find();
  1156. $data['user_id'] = $log_info['user_id'];
  1157. $data['nickname'] = $user_info['nickname'];
  1158. $data['avatar'] = one_domain_image($user_info['avatar']);
  1159. $data['createtime'] = get_last_time($log_info['createtime']);
  1160. $data['is_active'] = $user_info['is_active'];
  1161. $arr[] = $data;
  1162. }
  1163. $this->success('success', $arr);
  1164. }
  1165. //查询对方用户收费价格和我的余额
  1166. public function otheruserinfo(){
  1167. $user_id = input('user_id', 0, 'intval');
  1168. if (!$user_id) {
  1169. $this->error('您的网络开小差啦~');
  1170. }
  1171. $field = [
  1172. 'chat_price','voice_price','video_price','gender','is_kefu', 'open_match_audio', 'open_match_video'
  1173. ];
  1174. $info = Db::name('user')->field($field)->where('id',$user_id)->find();
  1175. if ($info['is_kefu'] != 1 && $this->auth->is_kefu != 1) { //不是客服
  1176. if ($info['gender'] == $this->auth->gender || $info['gender'] == -1 || $this->auth->gender == -1) {
  1177. $this->error('性别存在问题');
  1178. }
  1179. }
  1180. if ($this->auth->gender == 0) { //收费价格按照女号
  1181. $info['chat_price'] = $this->auth->chat_price;
  1182. $info['voice_price'] = $this->auth->voice_price;
  1183. $info['video_price'] = $this->auth->video_price;
  1184. $info['open_match_audio'] = $this->auth->open_match_audio;
  1185. $info['open_match_video'] = $this->auth->open_match_video;
  1186. }
  1187. $info['chat_price_vip'] = $info['chat_price']; //vip价格
  1188. $info['voice_price_vip'] = $info['voice_price'];
  1189. $info['video_price_vip'] = $info['video_price'];
  1190. $info['is_vip'] = 0; //是否vip: 0否 1是
  1191. //获取聊天折扣
  1192. $vip_chat_discount = config('site.vip_chat_discount');
  1193. if ($vip_chat_discount >= 0 && $vip_chat_discount <= 10) {
  1194. $info['vip_chat_discount'] = $vip_chat_discount;
  1195. } else {
  1196. $info['vip_chat_discount'] = 10;
  1197. }
  1198. //获取语音视频折扣
  1199. $vip_video_discount = config('site.vip_video_discount');
  1200. if ($vip_video_discount >= 0 && $vip_video_discount <= 10) {
  1201. $info['vip_voice_discount'] = config('site.vip_video_discount'); //语音
  1202. $info['vip_video_discount'] = config('site.vip_video_discount'); //视频
  1203. } else {
  1204. $info['vip_voice_discount'] = 10;
  1205. $info['vip_video_discount'] = 10;
  1206. }
  1207. //获取聊天折扣
  1208. $info['chat_price_vip'] = ceil($info['chat_price'] * $info['vip_chat_discount'] / 10);
  1209. //获取语音折扣
  1210. $info['voice_price_vip'] = ceil($info['voice_price'] * $info['vip_voice_discount'] / 10);
  1211. //获取视频折扣
  1212. $info['video_price_vip'] = ceil($info['video_price'] * $info['vip_video_discount'] / 10);
  1213. if ($this->auth->gender == 0) { //查询男号余额和vip
  1214. $wallet_info = Db::name('user_wallet')->where(['user_id' => $user_id])->find();
  1215. } else {
  1216. $wallet_info = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  1217. }
  1218. if ($wallet_info['vip_endtime'] >= time()) {
  1219. $info['is_vip'] = 1;
  1220. }
  1221. $info['gold'] = $wallet_info['gold'];
  1222. $info['plat_scale'] = config('site.gift_plat_scale'); //平台抽成百分比
  1223. $info['money_to_gold'] = config('site.money_to_gold'); //一元人民币兑换金币数量
  1224. $this->success('success',$info);
  1225. }
  1226. //亲密度等级信息
  1227. public function intimacylevel() {
  1228. $user_id = input('user_id', 0, 'intval'); //对方id
  1229. if (!$user_id) {
  1230. $this->error('参数缺失');
  1231. }
  1232. if ($this->auth->gender == 0) { //女用户
  1233. $where['uid'] = $user_id;
  1234. $where['other_uid'] = $this->auth->id;
  1235. } else { //男用户
  1236. $where['uid'] = $this->auth->id;
  1237. $where['other_uid'] = $user_id;
  1238. }
  1239. $level = 0; //当前等级
  1240. $level_name = ''; //当前等级名称
  1241. $qinmi_sum = 0; //当前亲密度
  1242. $next_level_diff = 0; //距下一等级亲密度差值
  1243. //亲密度等级列表
  1244. $list = Db::name('intimacy_level')->field('name,level')->order('value')->select();
  1245. //当前亲密度信息
  1246. $user_intimacy_info = Db::name('user_intimacy')->where($where)->find();
  1247. if ($user_intimacy_info) {
  1248. //当前亲密度
  1249. $qinmi_sum = $user_intimacy_info['value'];
  1250. if ($list) {
  1251. //当前等级信息
  1252. $level_info = Db::name('intimacy_level')->where(['value' => ['elt', $user_intimacy_info['value']]])->order('id desc')->find();
  1253. if ($level_info) {
  1254. $level = $level_info['level'];
  1255. $level_name = $level_info['name'];
  1256. }
  1257. //下一等级信息
  1258. $next_level_info = Db::name('intimacy_level')->where(['value' => ['gt', $user_intimacy_info['value']]])->order('value')->find();
  1259. if ($next_level_info) {
  1260. $next_level_diff = $next_level_info['value'] - $user_intimacy_info['value'];
  1261. }
  1262. }
  1263. } else {
  1264. $next_level_diff = Db::name('intimacy_level')->order('value')->value('value');
  1265. }
  1266. if ($list) {
  1267. foreach ($list as &$v) {
  1268. if ($v['level'] < $level) {
  1269. $v['is_unlock'] = 1; //当前等级是否解锁: 1已解锁 2当前等级 3未解锁
  1270. } elseif ($v['level'] == $level) {
  1271. $v['is_unlock'] = 2;
  1272. } else {
  1273. $v['is_unlock'] = 3;
  1274. }
  1275. }
  1276. }
  1277. $data['level'] = $level; //当前等级
  1278. $data['level_name'] = $level_name; //当前等级名称
  1279. $data['qinmi_sum'] = $qinmi_sum; //当前亲密度
  1280. $data['next_level_diff'] = $next_level_diff; //距下一等级亲密度差值
  1281. $data['level_list'] = $list; //等级列表
  1282. $this->success('亲密度等级信息', $data);
  1283. }
  1284. //语音视频随聊
  1285. public function getrandomuser() {
  1286. $type = input('type', 0, 'intval'); //类型:1语音 2视频
  1287. if (!in_array($type, [1, 2, 3])) {
  1288. $this->error('您的网络开小差了');
  1289. }
  1290. $size = input('size',0,'intval');// 是否查 10 条
  1291. $size = $size > 0 ? 1 : 0;
  1292. //给出备选用户
  1293. $map = [
  1294. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  1295. 'is_active' => 1, //在线的
  1296. 'status' =>1, //未封禁用户
  1297. 'is_kefu' => 0, //不是客服
  1298. ];
  1299. if ($this->auth->gender == 1) {
  1300. $map['real_status|idcard_status'] = 1; //真人认证或实名认证
  1301. }
  1302. if ($type == 1) {
  1303. $map['open_match_audio'] = 1;
  1304. } elseif ($type == 2) {
  1305. $map['open_match_video'] = 1;
  1306. }
  1307. $key = md5(json_encode($map));// 根据搜索条件做Key
  1308. $lists = Cache::remember("getrandomuser_{$key}_{$size}",function () use ($map,$size){
  1309. $query = Db::name('user')
  1310. ->field('id,nickname,username,avatar')
  1311. ->where($map)
  1312. ->order('is_active desc, active_time desc');
  1313. if ($size == 1){
  1314. $query->limit(10);
  1315. }else{
  1316. $query->page($this->page,100);
  1317. }
  1318. $lists = $query->select();
  1319. $lists = list_domain_image($lists,['avatar,audio_bio']);
  1320. $mt_user_greet_content = Db::name('user_greet_content');
  1321. foreach ($lists as &$v) {
  1322. $info = $mt_user_greet_content->where(['user_id' => $v['id'], 'is_default' => 1])->find();
  1323. if ($info) {
  1324. if ($info['type'] == 0) {
  1325. $v['greet_content'] = $info['content'];
  1326. } elseif ($info['type'] == 1) {
  1327. $v['greet_content'] = '[语音]';
  1328. } elseif ($info['type'] == 2) {
  1329. $v['greet_content'] = '[图片]';
  1330. }
  1331. } else {
  1332. $v['greet_content'] = '';
  1333. }
  1334. }
  1335. return $lists;
  1336. },2);
  1337. $count = Db::name('user')->where(['is_active' => 1])->count('id');
  1338. $return_data['count'] = config('site.randomuser_num') > 0 ? config('site.randomuser_num') + $count : $count;
  1339. $return_data['list'] = $lists;
  1340. $this->success('success',$return_data);
  1341. }
  1342. //查询免费体验视频通话用户信息
  1343. public function getfreevideouserinfo() {
  1344. $to_user_id = input('to_user_id', 0, 'intval');
  1345. $to_user_info = Db::name('user')->field('id,gender,nickname,avatar,birthday,height,bio')->where(['id' => $to_user_id, 'is_kefu' => 0])->find();
  1346. if(!$to_user_info){
  1347. $this->error('不存在的用户');
  1348. }
  1349. if ($to_user_info['gender'] == $this->auth->gender) {
  1350. $this->error('同性不能聊天~');
  1351. }
  1352. $to_user_info['desc'] = '';
  1353. $age = birthtime_to_age($to_user_info['birthday']);
  1354. if ($age > 0) {
  1355. $to_user_info['desc'] = $age . '岁';
  1356. } else {
  1357. $to_user_info['desc'] = '18岁';
  1358. }
  1359. if ($to_user_info['height']) {
  1360. $to_user_info['desc'] .= ' | ' . $to_user_info['height'];
  1361. }
  1362. //查询是否免费过
  1363. $to_user_info['free_count'] = 1;//Db::name('user_match_video_log_free')->where(['user_id' => $this->auth->id])->count('id');
  1364. $this->success('信息', $to_user_info);
  1365. }
  1366. //免费体验视频通话1分钟
  1367. public function videochat_onemin_free(){
  1368. $this->success('success');
  1369. if ($this->auth->is_kefu == 1) { //我是客服或者对方是客服
  1370. $this->success('success');
  1371. }
  1372. //检查是否免费过
  1373. $count = Db::name('user_match_video_log_free')->where(['user_id' => $this->auth->id])->count('id');
  1374. if ($count) {
  1375. $this->error('已经免费体验过');
  1376. }
  1377. //检测用户
  1378. $to_user_id = input_post('to_user_id');
  1379. $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();
  1380. if(!$to_user_info){
  1381. $this->error('不存在的用户');
  1382. }
  1383. if ($to_user_info['is_kefu'] == 1) { //我是客服或者对方是客服
  1384. $this->success('success');
  1385. }
  1386. if ($to_user_info['gender'] == $this->auth->gender) {
  1387. $this->error('同性不能聊天~');
  1388. }
  1389. //记录日志
  1390. $data = [
  1391. 'user_id' => $this->auth->id,
  1392. 'price' => 0,
  1393. 'createtime' => time(),
  1394. 'to_user_id' => $to_user_id,
  1395. 'money' => 0,
  1396. ];
  1397. $log_id = Db::name('user_match_video_log_free')->insertGetId($data);
  1398. if(!$log_id){
  1399. $this->error('扣费失败');
  1400. }
  1401. $this->success('success');
  1402. }
  1403. //男性非会员解锁聊天
  1404. public function unlockchat() {
  1405. // 接口防并发
  1406. if (!$this->apiLimit(1, 1)) {
  1407. $this->error(__('Operation frequently'));
  1408. }
  1409. //男性非会员是否已开通聊天
  1410. if ($this->auth->gender == 0) {
  1411. $this->error('不需要解锁');
  1412. }
  1413. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  1414. if ($vip_endtime > time()) {
  1415. $this->error('不需要解锁');
  1416. }
  1417. $user_id = input('user_id', 0, 'intval');//对方id
  1418. if (!$user_id) {
  1419. $this->error();
  1420. }
  1421. // 不可以守护给自己
  1422. if($this->auth->id == $user_id) {
  1423. $this->error("不可以解锁自己");
  1424. }
  1425. //查询是否解锁过
  1426. $user_chat_unlock = Db::name('user_chat_unlock')->where(['user_id' => $this->auth->id, 'user_to_id' => $user_id])->count('id');
  1427. if ($user_chat_unlock) {
  1428. $this->error('已经解锁过了');
  1429. }
  1430. $giftvalue = config('site.unlock_chat_gold');
  1431. if ($giftvalue <= 0) {
  1432. $this->error('解锁金额异常~');
  1433. }
  1434. //被解锁人信息
  1435. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  1436. if (!$touserinfo) {
  1437. $this->error("不存在的用户");
  1438. }
  1439. // 判断当前用户余额
  1440. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  1441. if($user_gold < $giftvalue) {
  1442. $this->error("您的金币余额不足");
  1443. }
  1444. Db::startTrans();
  1445. //添加解锁记录表
  1446. $data = [
  1447. 'user_id' => $this->auth->id,
  1448. 'user_to_id' => $user_id,
  1449. 'price' => $giftvalue,
  1450. 'createtime' => time(),
  1451. ];
  1452. $log_id = Db::name('user_chat_unlock')->insertGetId($data);
  1453. if(!$log_id){
  1454. Db::rollback();
  1455. $this->error('解锁失败');
  1456. }
  1457. if($giftvalue > 0){
  1458. // 扣除当前用户余额
  1459. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,81,'开通守护','user_chat_unlock',$log_id);
  1460. if($wallet_rs['status'] === false){
  1461. Db::rollback();
  1462. $this->error($wallet_rs['msg']);
  1463. }
  1464. // 添加守护用户余额
  1465. $money_to_gold = config('site.money_to_gold');
  1466. $gift_plat_scale = config('site.gift_plat_scale');
  1467. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  1468. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  1469. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,82,'被守护收益','user_chat_unlock',$log_id,2);
  1470. if($wallet_rs['status'] === false){
  1471. Db::rollback();
  1472. $this->error($wallet_rs['msg']);
  1473. }
  1474. //增加赠送用户上级余额
  1475. if ($touserinfo['intro_uid']) {
  1476. //获取返利比率
  1477. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  1478. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  1479. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  1480. //上级获得金额
  1481. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  1482. if ($intro_uid_money > 0) {
  1483. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人解锁聊天获赠奖励','gift_user_typing',$log_id);
  1484. if($intro_result['status']===false)
  1485. {
  1486. Db::rollback();
  1487. $this->error($intro_result['msg']);
  1488. }
  1489. }
  1490. }
  1491. }
  1492. }
  1493. //tag任务守护金币
  1494. //搭讪奖励
  1495. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  1496. // if($task_rs === false){
  1497. // Db::rollback();
  1498. // $this->error('完成任务守护奖励失败');
  1499. // }
  1500. Db::commit();
  1501. $this->success('解锁成功');
  1502. }
  1503. }