User.php 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\DBFS\DBFS;
  4. use app\common\controller\Api;
  5. use app\common\library\Ems;
  6. use app\common\library\Sms;
  7. use fast\Random;
  8. use think\Validate;
  9. use think\Db;
  10. use think\Cache;
  11. use Redis;
  12. use AlibabaCloud\Client\AlibabaCloud;
  13. use AlibabaCloud\Client\Exception\ClientException;
  14. use AlibabaCloud\Client\Exception\ServerException;
  15. /**
  16. * 会员接口
  17. */
  18. class User extends Api
  19. {
  20. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','onLogin'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 会员登录
  35. *
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = $this->request->request('account');
  42. $password = $this->request->request('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = ['userinfo' => $this->auth->getUserinfo()];
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @param string $mobile 手机号
  58. * @param string $captcha 验证码
  59. */
  60. public function mobilelogin()
  61. {
  62. $mobile = $this->request->request('mobile');
  63. $captcha = $this->request->request('captcha');
  64. if (!$mobile || !$captcha) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::regex($mobile, "^1\d{10}$")) {
  68. $this->error(__('Mobile is incorrect'));
  69. }
  70. if (!Sms::check($mobile, $captcha, 'login')) {
  71. $this->error(__('Captcha is incorrect'));
  72. }
  73. $user = \app\common\model\User::getByMobile($mobile);
  74. if ($user) {
  75. if ($user->status != 'normal') {
  76. $this->error(__('Account is locked'));
  77. }
  78. //如果已经有账号则直接登录
  79. $ret = $this->auth->direct($user->id);
  80. } else {
  81. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, []);
  82. }
  83. if ($ret) {
  84. Sms::flush($mobile, 'login');
  85. $data = ['userinfo' => $this->auth->getUserinfo()];
  86. $this->success(__('Logged in successful'), $data);
  87. } else {
  88. $this->error($this->auth->getError());
  89. }
  90. }
  91. /**
  92. * 注册会员
  93. *
  94. * @param string $username 用户名
  95. * @param string $password 密码
  96. * @param string $email 邮箱
  97. * @param string $mobile 手机号
  98. * @param string $code 验证码
  99. */
  100. public function register()
  101. {
  102. $username = $this->request->request('username');
  103. $password = $this->request->request('password');
  104. $email = $this->request->request('email');
  105. $mobile = $this->request->request('mobile');
  106. $code = $this->request->request('code');
  107. if (!$username || !$password) {
  108. $this->error(__('Invalid parameters'));
  109. }
  110. if ($email && !Validate::is($email, "email")) {
  111. $this->error(__('Email is incorrect'));
  112. }
  113. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  114. $this->error(__('Mobile is incorrect'));
  115. }
  116. $ret = Sms::check($mobile, $code, 'register');
  117. if (!$ret) {
  118. $this->error(__('Captcha is incorrect'));
  119. }
  120. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  121. if ($ret) {
  122. $data = ['userinfo' => $this->auth->getUserinfo()];
  123. $this->success(__('Sign up successful'), $data);
  124. } else {
  125. $this->error($this->auth->getError());
  126. }
  127. }
  128. /**
  129. * 退出登录
  130. */
  131. public function logout()
  132. {
  133. $this->auth->logout();
  134. $this->success(__('Logout successful'));
  135. }
  136. /**
  137. * 修改邮箱
  138. *
  139. * @param string $email 邮箱
  140. * @param string $captcha 验证码
  141. */
  142. public function changeemail()
  143. {
  144. $user = $this->auth->getUser();
  145. $email = $this->request->post('email');
  146. $captcha = $this->request->request('captcha');
  147. if (!$email || !$captcha) {
  148. $this->error(__('Invalid parameters'));
  149. }
  150. if (!Validate::is($email, "email")) {
  151. $this->error(__('Email is incorrect'));
  152. }
  153. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  154. $this->error(__('Email already exists'));
  155. }
  156. $result = Ems::check($email, $captcha, 'changeemail');
  157. if (!$result) {
  158. $this->error(__('Captcha is incorrect'));
  159. }
  160. $verification = $user->verification;
  161. $verification->email = 1;
  162. $user->verification = $verification;
  163. $user->email = $email;
  164. $user->save();
  165. Ems::flush($email, 'changeemail');
  166. $this->success();
  167. }
  168. /**
  169. * 修改手机号
  170. *
  171. * @param string $mobile 手机号
  172. * @param string $captcha 验证码
  173. */
  174. public function changemobile()
  175. {
  176. $user = $this->auth->getUser();
  177. $mobile = $this->request->request('mobile');
  178. $captcha = $this->request->request('captcha');
  179. if (!$mobile || !$captcha) {
  180. $this->error(__('Invalid parameters'));
  181. }
  182. if (!Validate::regex($mobile, "^1\d{10}$")) {
  183. $this->error(__('Mobile is incorrect'));
  184. }
  185. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  186. $this->error(__('Mobile already exists'));
  187. }
  188. $result = Sms::check($mobile, $captcha, 'changemobile');
  189. if (!$result) {
  190. $this->error(__('Captcha is incorrect'));
  191. }
  192. $verification = $user->verification;
  193. $verification->mobile = 1;
  194. $user->verification = $verification;
  195. $user->mobile = $mobile;
  196. $user->save();
  197. Sms::flush($mobile, 'changemobile');
  198. $this->success();
  199. }
  200. /**
  201. * 第三方登录
  202. *
  203. * @param string $platform 平台名称
  204. * @param string $code Code码
  205. */
  206. public function third()
  207. {
  208. $url = url('user/index');
  209. $platform = $this->request->request("platform");
  210. $code = $this->request->request("code");
  211. $config = get_addon_config('third');
  212. if (!$config || !isset($config[$platform])) {
  213. $this->error(__('Invalid parameters'));
  214. }
  215. $app = new \addons\third\library\Application($config);
  216. //通过code换access_token和绑定会员
  217. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  218. if ($result) {
  219. $loginret = \addons\third\library\Service::connect($platform, $result);
  220. if ($loginret) {
  221. $data = [
  222. 'userinfo' => $this->auth->getUserinfo(),
  223. 'thirdinfo' => $result
  224. ];
  225. $this->success(__('Logged in successful'), $data);
  226. }
  227. }
  228. $this->error(__('Operation failed'), $url);
  229. }
  230. /**
  231. * 重置密码
  232. *
  233. * @param string $mobile 手机号
  234. * @param string $newpassword 新密码
  235. * @param string $captcha 验证码
  236. */
  237. public function resetpwd()
  238. {
  239. $type = $this->request->request("type");
  240. $mobile = $this->request->request("mobile");
  241. $email = $this->request->request("email");
  242. $newpassword = $this->request->request("newpassword");
  243. $captcha = $this->request->request("captcha");
  244. if (!$newpassword || !$captcha) {
  245. $this->error(__('Invalid parameters'));
  246. }
  247. if ($type == 'mobile') {
  248. if (!Validate::regex($mobile, "^1\d{10}$")) {
  249. $this->error(__('Mobile is incorrect'));
  250. }
  251. $user = \app\common\model\User::getByMobile($mobile);
  252. if (!$user) {
  253. $this->error(__('User not found'));
  254. }
  255. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  256. if (!$ret) {
  257. $this->error(__('Captcha is incorrect'));
  258. }
  259. Sms::flush($mobile, 'resetpwd');
  260. } else {
  261. if (!Validate::is($email, "email")) {
  262. $this->error(__('Email is incorrect'));
  263. }
  264. $user = \app\common\model\User::getByEmail($email);
  265. if (!$user) {
  266. $this->error(__('User not found'));
  267. }
  268. $ret = Ems::check($email, $captcha, 'resetpwd');
  269. if (!$ret) {
  270. $this->error(__('Captcha is incorrect'));
  271. }
  272. Ems::flush($email, 'resetpwd');
  273. }
  274. //模拟一次登录
  275. $this->auth->direct($user->id);
  276. $ret = $this->auth->changepwd($newpassword, '', true);
  277. if ($ret) {
  278. $this->success(__('Reset password successful'));
  279. } else {
  280. $this->error($this->auth->getError());
  281. }
  282. }
  283. /**
  284. * 运营商一键登录
  285. */
  286. public function onLogin() {
  287. $token = $this->request->param('token');// 易盾返回的token
  288. // 判断登录token是否有效
  289. if (!$token) {
  290. //如果token为空就返回
  291. $this->error('token不能为空,请重试');
  292. } else {
  293. //调用getPhone方法并且将token传给getPhone
  294. $res = $this->getPhone($token);
  295. //如果返回的状态为1说明是注册过的用户
  296. if ($res['state'] == 1) {
  297. $phone = $res['phone'];
  298. // 用户登录逻辑 === 开始
  299. $userModel = new \app\common\model\User();
  300. $auth = \app\common\library\Auth::instance();
  301. $userInfo = $userModel->where(["mobile"=>$phone])->find();
  302. // 判断用户是否已经存在
  303. if($userInfo) { // 登录
  304. $user = \app\common\model\User::get($userInfo["id"]);
  305. if (!$user) {
  306. $this->error("网络错误!请稍后重试");
  307. }
  308. $user->save(["logintime"=>time()]);
  309. $res_login = $auth->direct($user->id);
  310. } else { // 注册
  311. // 先随机一个用户名,随后再变更为u+数字id
  312. $username = Random::alnum(20);
  313. $password = Random::alnum(6);
  314. // 获取默认头像和昵称
  315. $nickname = array_column(\app\admin\model\website\Nickname::select(),'content');
  316. $avatar = array_column(\app\admin\model\website\Avatar::select(),'content');
  317. $extend = [
  318. 'nickname'=>$nickname[rand(0,count($nickname)-1)],
  319. 'avatar'=>$avatar[rand(0,count($avatar)-1)],
  320. "mobile"=>$phone
  321. ];
  322. Db::startTrans();
  323. try {
  324. // 默认注册一个会员
  325. $result = $auth->register($username, $password, "", $extend);
  326. if (!$result) {
  327. return false;
  328. }
  329. $user = $auth->getUser();
  330. $fields = ['username' => 'u' . $user->id];
  331. // 更新会员资料
  332. $user = \app\common\model\User::get($user->id);
  333. $user->save($fields);
  334. Db::commit();
  335. } catch (PDOException $e) {
  336. Db::rollback();
  337. $auth->logout();
  338. return false;
  339. }
  340. // 写入登录Cookies和Token
  341. $res_login = $auth->direct($user->id);
  342. }
  343. $userInfo = $auth->getUserinfo();
  344. if($res_login) {
  345. $this->success("登录成功!",['userinfo' => $userInfo]);
  346. } else {
  347. $this->error("登录失败!");
  348. }
  349. // 用户登录逻辑 === 结束
  350. } else {
  351. //如果没有注册过就返回注册状态
  352. $this->error($res['msg']);
  353. }
  354. }
  355. }
  356. /*
  357. * 根据token换取手机号码
  358. */
  359. public function getPhone($token) {
  360. $config = config('onLogin');
  361. AlibabaCloud::accessKeyClient($config['phone_access_key'], $config['phone_access_secret'])
  362. ->regionId('cn-hangzhou')
  363. ->asDefaultClient();
  364. try {
  365. $result = AlibabaCloud::rpc()
  366. ->product('Dypnsapi')
  367. ->scheme('https')// https | http
  368. ->version('2017-05-25')
  369. ->action('GetMobile')
  370. ->method('POST')
  371. ->host('dypnsapi.aliyuncs.com')
  372. ->options([
  373. 'query' => [
  374. 'RegionId' => "cn-hangzhou",
  375. 'AccessToken' => $token
  376. ],
  377. ])
  378. ->request();
  379. // 将返回的结果转化为数组
  380. $result = $result->toArray();
  381. //判断当前数组不为空
  382. if (isset($result['GetMobileResultDTO']['Mobile'])) {
  383. // token不为空返回手机号码
  384. $phone = $result['GetMobileResultDTO']['Mobile'];
  385. $res = [
  386. 'state' => 1,
  387. 'phone' => $phone
  388. ];
  389. return $res;
  390. } else {
  391. //如果token为空
  392. $res = [
  393. 'state' => 0,
  394. 'msg' => 'token无效'
  395. ];
  396. return $res;
  397. }
  398. } catch (ClientException $e) {//有异常就抛出异常
  399. // 客户端错误
  400. $res = [
  401. 'state' => 101,
  402. 'msg' => '注册失败'
  403. ];
  404. return $res;
  405. } catch (ServerException $e) {
  406. // 服务端错误
  407. $res = [
  408. 'state' => 101,
  409. 'msg' => '注册失败'
  410. ];
  411. return $res;
  412. }
  413. }
  414. /**
  415. * 修改会员个人信息
  416. * 头像,昵称,性别,
  417. */
  418. public function userAvatar()
  419. {
  420. $user = $this->auth->getUser();
  421. $gender = $this->request->request('gender'); // 性别:1=男,0=女
  422. $nickname_auth = $this->request->request('nickname');
  423. $avatar_auth = $this->request->request('avatar');
  424. if (!in_array($gender, [0, 1]) && !$nickname_auth && !$avatar_auth) $this->error('参数为空!');
  425. // 随机获取昵称和头像
  426. if(!$user->nickname && !$nickname_auth) {
  427. $nicknameList = \app\admin\model\website\Nickname::select();//得到总条数
  428. $nicknameArr = [];
  429. if($nicknameList) foreach($nicknameList as $k => $v) {
  430. $nicknameArr[] = $v['content'];
  431. }
  432. $user->nickname = $nicknameArr[array_rand($nicknameArr,1)];
  433. }
  434. if(!$user->avatar && !$avatar_auth) {
  435. $avatarList = \app\admin\model\website\Avatar::select();//得到总条数
  436. $avatarArr = [];
  437. if($avatarList) foreach($avatarList as $k => $v) {
  438. $avatarArr[] = $v['content'];
  439. }
  440. $user->avatar = $avatarArr[array_rand($avatarArr,1)];
  441. }
  442. Db::startTrans();
  443. try {
  444. $res1 = true;
  445. if ($nickname_auth && $nickname_auth != $user->nickname) {
  446. // if($nickname_auth == $user->nickname) {
  447. // $this->error(__('与原昵称相同无需修改!'));
  448. // }
  449. $user->nickname_auth = $nickname_auth;
  450. // 添加昵称修改申请表
  451. if(\app\common\model\NicknameAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("昵称已在审核中!请勿重复申请");
  452. $data = [];
  453. $data['user_id'] = $this->auth->id;
  454. $data['nickname'] = $nickname_auth;
  455. $data['old_nickname'] = $user->nickname;
  456. $data['createtime'] = time();
  457. $res1 = \app\common\model\NicknameAuth::insert($data);
  458. }
  459. if($avatar_auth) {
  460. $user->avatar_auth = $avatar_auth;
  461. // 添加头像修改申请表
  462. if(\app\common\model\AvatarAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("头像已在审核中!请勿重复申请");
  463. $data = [];
  464. $data['user_id'] = $this->auth->id;
  465. $data['avatar'] = $avatar_auth;
  466. $data['old_avatar'] = $user->avatar;
  467. $data['createtime'] = time();
  468. $res1 = \app\common\model\AvatarAuth::insert($data);
  469. }
  470. in_array($gender, [0, 1]) && $user->gender = $gender;
  471. $res2 = $user->save();
  472. if($res1 && $res2 !== false) {
  473. Db::commit();
  474. delUserInfo($this->auth->id);
  475. $this->success("修改成功!昵称审核中");
  476. }
  477. } catch (PDOException $e) {
  478. Db::rollback();
  479. $this->error("修改失败!");
  480. }
  481. }
  482. /**
  483. * 修改会员个人信息
  484. * 城市,年龄,收入
  485. */
  486. public function userCity() {
  487. $user = $this->auth->getUser();
  488. $province = $this->request->request('province'); // 省
  489. $city = $this->request->request('city'); // 市
  490. $district = $this->request->request('district'); // 区
  491. $birthday = $this->request->request('birthday');
  492. $age = $this->request->request('age');
  493. $constellation = $this->request->request('constellation'); // 星座
  494. $income = $this->request->request('income');
  495. if ((!$province || !$city || !$district) && $age < 3 && !$income) $this->error('年龄太小了哦!');
  496. if ($province && $city && $district) {
  497. $province = mb_substr($province, 0, 2);
  498. $province_info = Db::name('area')->where(['name' => ['like', $province . '%'], 'pid' => 0])->find();
  499. $user->province = $province_info['id'];
  500. $user->province_name = $province_info['name'];
  501. $city = mb_substr($city, 0, 2);
  502. $city_info = Db::name('area')->where(['name' => ['like', $city . '%'], 'pid' => $province_info['id']])->find();
  503. $user->city = $city_info['id'];
  504. $user->city_name = $city_info['name'];
  505. $district = mb_substr($district, 0, 2);
  506. $district_info = Db::name('area')->where(['name' => ['like', $district . '%'], 'pid' => $city_info['id']])->find();
  507. $user->district = $district_info['id'];
  508. $user->district_name = $district_info['name'];
  509. }
  510. // $province && $user->province = $province;
  511. // $city && $user->city = $city;
  512. // $district && $user->district = $district;
  513. // $province && $user->province_name = \app\common\model\Area::getNameFromId($province);
  514. // $city && $user->city_name = \app\common\model\Area::getNameFromId($city);
  515. // $district && $user->district_name = \app\common\model\Area::getNameFromId($district);
  516. $age >= 3 && $user->age = $age;
  517. $constellation && $user->constellation = $constellation;
  518. $birthday && $user->birthday = $birthday;
  519. $income && $user->income = $income;
  520. $user->save();
  521. delUserInfo($this->auth->id);
  522. $this->success("修改成功!");
  523. }
  524. /**
  525. * 修改会员个人信息
  526. * 期望对象
  527. */
  528. public function userExpect() {
  529. $user = $this->auth->getUser();
  530. $expect = $this->request->request('expect'); // 期望对象,格式:1,2,3
  531. if (!$expect) $this->error('参数为空!');
  532. $user->expect_ids = $expect;
  533. $user->save();
  534. delUserInfo($user->id);
  535. $this->success("修改成功!");
  536. }
  537. /**
  538. * 修改会员个人信息
  539. * 最后登录的经纬度
  540. */
  541. public function userLnglat() {
  542. $user = $this->auth->getUser();
  543. $lng = $this->request->request('lng'); // 经度
  544. $lat = $this->request->request('lat'); // 纬度
  545. if (!$lng || !$lat) $this->error('参数缺失!');
  546. $user->lng = $lng;
  547. $user->lat = $lat;
  548. $user->save();
  549. $this->success("修改成功!");
  550. }
  551. /**
  552. * 修改会员个人信息
  553. * 环信注册id
  554. */
  555. public function userEmcid() {
  556. $user = $this->auth->getUser();
  557. $emcid = $this->request->request('emcid'); // 环信注册ID
  558. $user->emcid = $emcid;
  559. $user->save();
  560. $this->success("修改成功!");
  561. }
  562. /**
  563. * 修改会员个人信息
  564. * 爱好,职业,微信,交友宣言
  565. */
  566. public function userhoppy() {
  567. $user = $this->auth->getUser();
  568. $hobby_ids = $this->request->request('hobby_ids'); // 爱好
  569. $profession = $this->request->request('profession'); // 职业(传汉字即可)
  570. $wechat = $this->request->request('wechat'); // 微信号
  571. $declaration = $this->request->request('declaration'); // 交友宣言
  572. if (!$hobby_ids && !$profession && !$wechat && !$declaration) $this->error('参数为空!');
  573. Db::startTrans();
  574. try {
  575. $hobby_ids && $user->hobby_ids = $hobby_ids;
  576. $profession && $user->profession = $profession;
  577. if($wechat) {
  578. if($user->wechat_time + 30*86400 > time()) {
  579. $this->error('微信号每月最多修改一次哦!');
  580. }
  581. $user->wechat_auth = $wechat;
  582. // 添加微信号修改申请表
  583. if(\app\common\model\WechatAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("微信号已在审核中!请勿重复申请");
  584. $data = [];
  585. $data['user_id'] = $this->auth->id;
  586. $data['wechat'] = $wechat;
  587. $data['old_wechat'] = $user->wechat;
  588. $data['createtime'] = time();
  589. $res1 = \app\common\model\WechatAuth::insert($data);
  590. $user->wechat_time = time();
  591. } else {
  592. $res1 = true;
  593. }
  594. if($declaration) {
  595. if (iconv_strlen($declaration, 'utf-8') > 64) {
  596. $this->error('交友宣言最多64位哦!');
  597. }
  598. $user->declaration_auth = $declaration;
  599. // 添加交友宣言修改申请表
  600. if(\app\common\model\DeclarationAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("交友宣言已在审核中!请勿重复申请");
  601. $data = [];
  602. $data['user_id'] = $this->auth->id;
  603. $data['declaration'] = $declaration;
  604. $data['old_declaration'] = $user->declaration;
  605. $data['createtime'] = time();
  606. $res3 = \app\common\model\DeclarationAuth::insert($data);
  607. } else {
  608. $res3 = true;
  609. }
  610. // $declaration && $user->declaration = $declaration;
  611. $res2 = $user->save();
  612. if($res1 && $res2 && $res3) {
  613. Db::commit();
  614. delUserInfo($this->auth->id);
  615. if($wechat) {
  616. $this->success("微信号修改申请已提交,请耐心等待审核!");
  617. } elseif ($declaration) {
  618. $this->success("交友宣言修改申请已提交,请耐心等待审核!");
  619. } else {
  620. $this->success("修改成功!");
  621. }
  622. }
  623. } catch (PDOException $e) {
  624. Db::rollback();
  625. $this->error("修改失败!");
  626. }
  627. }
  628. /**
  629. * 实名认证
  630. */
  631. public function authApply() {
  632. $realname = $this->request->request('realname'); // 真实姓名
  633. $idcard = $this->request->request('idcard'); // 身份证号
  634. $zimage = $this->request->request('zimage'); // 身份证正面照
  635. $fimage = $this->request->request('fimage'); // 身份证反面照
  636. if (!$zimage || !$fimage) {
  637. $this->error(__('Invalid parameters'));
  638. }
  639. $userauthModel = new \app\common\model\UserAuth();
  640. $data = [];
  641. $data["user_id"] = $this->auth->id;
  642. if($userauthModel->where($data)->where(['status'=>['in',[0,1]]])->find()) $this->error('您已经申请过了,请勿重复操作!');
  643. $data["idcard"] = $idcard;
  644. $data["realname"] = $realname;
  645. $zimage && $data["zimage"] = $zimage;
  646. $fimage && $data["fimage"] = $fimage;
  647. $data["status"] = 0;
  648. $data["updatetime"] = time();
  649. $data["createtime"] = time();
  650. $res = $userauthModel->insertGetId($data);
  651. \app\common\model\User::update(['is_auth'=>1],["id"=>$this->auth->id]);
  652. if($res) {
  653. $this->success("实名认证申请提交成功,请耐心等待审核");
  654. } else {
  655. $this->error("网络错误,请稍后重试");
  656. }
  657. }
  658. /**
  659. * 加入黑名单
  660. */
  661. public function addBlacklist() {
  662. $black_user_id = $this->request->request('black_user_id'); // 黑名单用户ID
  663. if (!$black_user_id) {
  664. $this->error(__('Invalid parameters'));
  665. }
  666. $user_id = $this->auth->id;
  667. if($user_id == $black_user_id) {
  668. $this->error(__('为何拉黑自己呢?'));
  669. }
  670. $userblacklistModel = new \app\common\model\UserBlacklist();
  671. $data = [];
  672. $data["user_id"] = $user_id;
  673. $data["black_user_id"] = $black_user_id;
  674. if($userblacklistModel->where($data)->find()) $this->error(__('已在黑名单!'));
  675. $data["createtime"] = time();
  676. $res = $userblacklistModel->insertGetId($data);
  677. if($res) {
  678. $this->success("加入成功!");
  679. } else {
  680. $this->error("网络错误,请稍后重试");
  681. }
  682. }
  683. /**
  684. * 获取黑名单用户
  685. */
  686. public function getBlacklist() {
  687. $page = $this->request->request('page',1); // 分页
  688. $pageNum = $this->request->request('pageNum',10); // 分页
  689. // 分页搜索构建
  690. $pageStart = ($page-1)*$pageNum;
  691. $userblacklistModel = new \app\common\model\UserBlacklist();// ->limit($pageStart,$pageNum)
  692. $where = [];
  693. $where["a.user_id"] = $this->auth->id;
  694. $list = $userblacklistModel->alias("a")
  695. ->field("a.id,a.black_user_id,u.avatar,u.nickname,u.age,u.gender,u.constellation,u.hobby_ids,u.profession")
  696. ->join("hx_user u","u.id = a.black_user_id")
  697. ->where($where)
  698. ->limit($pageStart,$pageNum)
  699. ->select();
  700. if($list) {
  701. foreach($list as $k => $v) {
  702. $list[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
  703. }
  704. $this->success("获取成功!",$list);
  705. } else {
  706. $this->success("数据为空",[]);
  707. }
  708. }
  709. /**
  710. * 移除用户黑名单
  711. */
  712. public function removeUserBlack() {
  713. $id = $this->request->request('id'); // 黑名单ID
  714. if (!$id) {
  715. $this->error(__('Invalid parameters'));
  716. }
  717. $userblacklistModel = new \app\common\model\UserBlacklist();
  718. $where = [];
  719. $where["id"] = $id;
  720. $info = $userblacklistModel->where($where)->find();
  721. if($info['user_id'] != $this->auth->id) $this->error("无权限!");
  722. $res = $userblacklistModel->where($where)->delete();
  723. if($res) {
  724. $this->success("移除成功!",$res);
  725. } else {
  726. $this->error("网络错误,请稍后重试!");
  727. }
  728. }
  729. /**
  730. * 举报用户
  731. */
  732. public function addReport() {
  733. $ruser_id = $this->request->request('ruser_id'); // 被举报用户ID
  734. $content = $this->request->request('content'); // 举报内容
  735. $type_id = $this->request->request('type_id'); // 举报类型
  736. $image = $this->request->request('image'); // 图片描述(多个用半角逗号隔开)
  737. if (!$ruser_id) {
  738. $this->error(__('Invalid parameters'));
  739. }
  740. $userreportModel = new \app\common\model\UserReport();
  741. $data = [];
  742. $data["user_id"] = $this->auth->id;
  743. $data["ruser_id"] = $ruser_id;
  744. $data["type_id"] = $type_id;
  745. $data["content"] = $content;
  746. $data["image"] = $image;
  747. $data["createtime"] = time();
  748. $res = $userreportModel->insertGetId($data);
  749. if($res) {
  750. $this->success("举报成功!");
  751. } else {
  752. $this->error("网络错误,请稍后重试");
  753. }
  754. }
  755. /**
  756. * 剩余特权次数
  757. * @return int|mixed
  758. */
  759. public function getFateCount() {
  760. $fate_count = \app\common\model\User::getViewCount($this->auth->id);
  761. $this->success("获取成功!",$fate_count);
  762. }
  763. /**
  764. * 添加有眼缘
  765. */
  766. public function addFate() {
  767. $fate_user_id = $this->request->request('fate_user_id'); // 被眼缘用户ID
  768. if (!$fate_user_id) {
  769. $this->error(__('Invalid parameters'));
  770. }
  771. $user_id = $this->auth->id;
  772. if($fate_user_id == $user_id) {
  773. $this->error("不需要添加自己为有眼缘哦!");
  774. }
  775. $fate_user = Db::name('user')->find($fate_user_id);
  776. if (!$fate_user) {
  777. $this->error('用户不存在');
  778. }
  779. $data = [];
  780. $data['user_id'] = $user_id;
  781. $data['fate_user_id'] = $fate_user_id;
  782. $user_fate = \app\common\model\UserFate::where($data)->find();
  783. if($user_fate && $user_fate['createtime'] >= time() - 86400) {
  784. $this->error("已经添加眼缘啦!");
  785. }
  786. $user = \app\common\model\User::get($user_id);
  787. // 查看当前用户剩余次数
  788. $view_count = \app\common\model\User::getViewCount($user_id);
  789. if($view_count <= 0) {
  790. $this->error(__('可查看次数不够了哦!'),[],100);
  791. } else {
  792. Db::startTrans();
  793. try {
  794. $user->view_count = $view_count - 1;
  795. $res1 = $user->save();
  796. // 添加眼缘记录
  797. if ($user_fate) {
  798. $res2 = Db::name('user_fate')->where($data)->setField('createtime', time());
  799. } else {
  800. $data['createtime'] = time();
  801. $res2 = \app\common\model\UserFate::insert($data);
  802. }
  803. // 添加返利
  804. if ($fate_user['gender'] == 0) {
  805. //女的返钱
  806. if ($user->is_goddess == 1) {
  807. $memo = '被查看有眼缘获得收益!';
  808. $profit = config('site.fate') * config('site.goddessProfitRate') * 0.01;
  809. } else {
  810. $memo = '被查看有眼缘获得收益!';
  811. $profit = config('site.fate') * config('site.userProfitRate') * 0.01;
  812. }
  813. if ($profit >= 0.01 && $fate_user_id > 0) {
  814. $res3 = \app\common\model\User::profit($profit, $fate_user_id, $memo);
  815. //给上级返利
  816. $fate_user_info = \app\common\model\User::where(['id' => $fate_user_id])->find();
  817. $profit_last = $profit * config('site.SuperioruserProfitRate') * 0.01;
  818. if ($profit_last >= 0.01 && $fate_user_info['pre_user_id'] > 0) {
  819. $res4 = \app\common\model\User::profit($profit_last, $fate_user_info['pre_user_id'], $fate_user_info['nickname'] . '被查看有眼缘获得收益');
  820. } else {
  821. $res4 = true;
  822. }
  823. } else {
  824. $res3 = true;
  825. $res4 = true;
  826. }
  827. } else {
  828. //男的返钻石
  829. $faterebate = (int)config('site.faterebate'); //男士被查看有眼缘可获得钻石数量
  830. if ($faterebate > 0) {
  831. //修改被查看用户钻石余额
  832. $diamond = $fate_user['diamond'] + $faterebate;
  833. $res3 = Db::name('user')->where(['id' => $fate_user_id, 'diamond' => $fate_user['diamond']])->setField('diamond', $diamond);
  834. // 添加钻石明细
  835. $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $fate_user['id']])->order('id', 'desc')->find();
  836. if (!$diamond_log && $fate_user['diamond'] > 0) {
  837. Db::rollback();
  838. $this->error('钻石余额异常');
  839. }
  840. if ($diamond_log && $diamond_log['after'] != $fate_user['diamond']) {
  841. Db::rollback();
  842. $this->error('钻石余额异常');
  843. }
  844. $_data['user_id'] = $fate_user_id;
  845. $_data['diamond'] = $faterebate;
  846. $_data['before'] = $fate_user['diamond'];
  847. $_data['after'] = $diamond;
  848. $_data['memo'] = '被查看有眼缘获得收益';
  849. $_data['createtime'] = time();
  850. $res4 = Db::name('user_diamond_log')->insertGetId($_data);
  851. } else {
  852. $res3 = true;
  853. $res4 = true;
  854. }
  855. }
  856. if($res1 && $res2 && $res3 && $res4) {
  857. Db::commit();
  858. $fate_user_info = \app\common\model\User::where(['id'=>$fate_user_id])->find();
  859. $title = '眼缘提醒!';
  860. $content = $this->auth->nickname.': 等你很久了,终于来了。希望你可以眼缘这里找到有趣的灵魂。无论白天还是深夜,无论快乐还是寂寞,始终有人陪你~';
  861. \app\common\model\SysMsg::sendSysMsg($fate_user_id,6,$title,$content);
  862. $this->success("眼缘添加成功!");
  863. }
  864. } catch (PDOException $e) {
  865. Db::rollback();
  866. $this->error("添加失败!");
  867. }
  868. }
  869. }
  870. /**
  871. * 获取有眼缘列表
  872. */
  873. public function getFate() {
  874. $page = $this->request->request('page',1); // 分页
  875. $pageNum = $this->request->request('pageNum',10); // 分页
  876. // 分页搜索构建
  877. $pageStart = ($page-1)*$pageNum;
  878. $user_id = $this->auth->id;
  879. $where = [];
  880. $where['a.user_id'] = $user_id;
  881. $where['a.createtime'] = ['egt', time() - 86400];
  882. $res = \app\common\model\UserFate::alias("a")
  883. ->field("a.id,u.id as user_id,u.avatar,u.nickname,u.age,u.constellation,u.hobby_ids,u.profession,u.wechat,u.copy_mobile,u.mobile,u.gender")
  884. ->join("hx_user u","u.id = a.fate_user_id")
  885. ->where($where)
  886. ->order("a.createtime",'desc')
  887. ->limit($pageStart,$pageNum)
  888. ->select();
  889. if($res) {
  890. $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
  891. wordwrap(config('public_key'), 64, PHP_EOL, true) .
  892. PHP_EOL."-----END PUBLIC KEY-----";
  893. foreach($res as $k => &$v) {
  894. if ($v['wechat']) {
  895. $wechat = "";
  896. openssl_public_encrypt($v['wechat'], $wechat, $public_key);
  897. $v['wechat'] = base64_encode($wechat);
  898. } else {
  899. $v['wechat'] = '';
  900. }
  901. $mobile = "";
  902. // openssl_private_encrypt($data['mobile'], $mobile, $private_key); // 使用私钥加密数据
  903. openssl_public_encrypt($v['mobile'], $mobile, $public_key);
  904. $v['mobile'] = base64_encode($mobile);
  905. $res[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
  906. }
  907. }
  908. $this->success("获取成功!",$res);
  909. }
  910. /**
  911. * 获取用户个人信息
  912. */
  913. public function getUserInfo() {
  914. $user_id = $this->request->request('user_id',0); // 用户ID
  915. if(!$user_id) {
  916. $this->error('参数缺失!');
  917. }
  918. // // redis
  919. // $redis = new Redis();
  920. // $redisconfig = config("redis");
  921. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  922. // $userInfo = decodeArray($redis->hGetAll('userInfo_'.$user_id));
  923. //
  924. // if(!$userInfo){
  925. // 获取用户信息
  926. $field = 'id,avatar,nickname,is_goddess,is_auth,recharge_auth,vipStatus(vip_duetime) as is_vip,age,lng,lat,city_name,district_name,constellation,hobby_ids,profession,declaration,wechat,income,copy_mobile,mobile,is_auth_person,gender';
  927. $userInfo = \app\common\model\User::field($field)->where(['id'=>$user_id])->find();
  928. $userInfo['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($userInfo['hobby_ids']);
  929. // 获取是否有眼缘
  930. $userInfo['is_fate'] = \app\common\model\User::getIsView($user_id,$this->auth->id);
  931. // 获取地区
  932. $userInfo['address'] = \app\common\model\Eyemargin::getDistanceTxt($userInfo['lng'],$userInfo['lat'],$this->auth->lng,$this->auth->lat,$userInfo['city_name'],$userInfo['district_name']);
  933. // 微信号
  934. if(!$userInfo['wechat']) {
  935. $userInfo['wechat'] = '暂未设置微信号!';
  936. } elseif(!$userInfo['is_fate']) {
  937. $userInfo['wechat'] = '******';
  938. }
  939. //查询在线状态和活跃状态
  940. $user_info = Db::name('user_info')->where(['user_id' => $user_id])->find();
  941. $asktime = explode(',', $user_info['asktime']);
  942. if ($user_info && $asktime && time() - $asktime[count($asktime) - 1] <= 3600) {
  943. $userInfo['is_online'] = 1; //0离线 1在线
  944. } else {
  945. $userInfo['is_online'] = 0; //0离线 1在线
  946. }
  947. if ($user_info && count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
  948. $userInfo['is_active'] = 1; //活跃
  949. } else {
  950. $userInfo['is_active'] = 0; //不活跃
  951. }
  952. // 获取已有标签以及数量
  953. $userInfo['tagUser'] = \app\common\model\TagUser::alias('a')
  954. ->field('a.id,t.name,a.number')
  955. ->join('hx_tag t','t.id = a.tag_id','left')
  956. ->where(['a.user_id'=>$user_id])
  957. ->select();
  958. // $userInfo = $userInfo->toArray();
  959. // $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
  960. // }
  961. $this->success("获取成功!",$userInfo);
  962. }
  963. /**
  964. * 获取我的个人信息
  965. */
  966. public function getMyInfo() {
  967. $user_id = $this->auth->id;
  968. // redis
  969. // $redis = new Redis();
  970. // $redisconfig = config("redis");
  971. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  972. // $userInfo = decodeArray($redis->hGetAll('userInfo_'.$user_id));
  973. //
  974. // if(!$userInfo){
  975. // 获取用户信息
  976. $field = 'id,avatar,avatar_auth,gender,nickname,nickname_auth,is_goddess,is_auth,vipStatus(vip_duetime) as is_vip,vip_duetime,age,city_name,district_name,constellation,hobby_ids,expect_ids,profession,declaration,declaration_auth,money,wechat,wechat_auth,pre_user_id,copy_mobile,mobile,is_auth_person,diamond,offline_notice';
  977. $userInfo = \app\common\model\User::field($field)->where(['id'=>$user_id])->find();
  978. $userInfo['vip_duetime'] = $userInfo['vip_duetime']?date('Y-m-d',$userInfo['vip_duetime']):"";
  979. if($userInfo['pre_user_id']>0) {
  980. $userInfo['pre_invite_no'] = \app\common\model\User::where(['id'=>$userInfo['pre_user_id']])->value("invite_no");
  981. } else {
  982. $userInfo['pre_invite_no'] = "";
  983. }
  984. // 获取我喜欢的统计
  985. $userInfo['ilike_count'] = \app\common\model\UserLike::where(['fans_id'=>$user_id])->count();
  986. $userInfo['likeme_count'] = \app\common\model\UserLike::where(['user_id'=>$user_id])->count();
  987. $userInfo['fate_count'] = \app\common\model\UserFate::where(['user_id'=>$user_id])->count();
  988. $userInfo['money_count'] = $userInfo['money'];
  989. $userInfo['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($userInfo['hobby_ids']);
  990. $userInfo['expect_ids'] = $userInfo['expect_ids']?explode(",",$userInfo['expect_ids']):[];
  991. $userInfo['nickname_auth_stauts'] = \app\common\model\NicknameAuth::getAuthStatus($userInfo['id'],$userInfo['nickname_auth']);
  992. $userInfo['avatar_auth_stauts'] = \app\common\model\AvatarAuth::getAuthStatus($userInfo['id'],$userInfo['avatar_auth']);
  993. $userInfo['wechat_auth_stauts'] = \app\common\model\WechatAuth::getAuthStatus($userInfo['id'],$userInfo['wechat_auth']);
  994. $userInfo['declaration_auth_stauts'] = $userInfo['declaration_auth'] !== '' ? \app\common\model\DeclarationAuth::getAuthStatus($userInfo['id'],$userInfo['declaration_auth']) : 1;
  995. //查询在线状态和活跃状态
  996. $user_info = Db::name('user_info')->where(['user_id' => $user_id])->find();
  997. $asktime = explode(',', $user_info['asktime']);
  998. $userInfo['is_online'] = time() - $asktime[count($asktime) - 1] > 3600 ? 0 : 1; //0离线 1在线
  999. if (count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
  1000. $userInfo['is_active'] = 1; //活跃
  1001. } else {
  1002. $userInfo['is_active'] = 0; //不活跃
  1003. }
  1004. $userInfo['avatar_auth'] = $userInfo['avatar_auth'] ? $userInfo['avatar_auth'] : $userInfo['avatar'];
  1005. // $userInfo = $userInfo->toArray();
  1006. // $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
  1007. // }
  1008. $this->success("获取成功!",$userInfo);
  1009. }
  1010. /**
  1011. * 获取动态/我的动态
  1012. * @throws \think\db\exception\DataNotFoundException
  1013. * @throws \think\db\exception\ModelNotFoundException
  1014. * @throws \think\exception\DbException
  1015. */
  1016. public function getUserEyemagin() {
  1017. $user_id = $this->request->request('user_id',0); // 用户ID
  1018. $page = $this->request->request('page',1); // 分页
  1019. $pageNum = $this->request->request('pageNum',10); // 分页
  1020. // 分页搜索构建
  1021. $pageStart = ($page-1)*$pageNum;
  1022. $where = [];
  1023. if($user_id > 0) {
  1024. $where['a.user_id'] = $user_id;
  1025. $where['a.status'] = 1;
  1026. } else {
  1027. $user_id = $this->auth->id;
  1028. $where['a.user_id'] = $user_id;
  1029. $where['a.status'] = ['in',[0,1]];
  1030. }
  1031. $field = "a.*,u.avatar,u.city_name,u.district_name,u.nickname,u.is_goddess,u.is_auth,vipStatus(u.vip_duetime) as is_vip,
  1032. u.age,u.constellation,u.hobby_ids,u.profession,u.declaration,u.lng,u.lat";
  1033. $list = \app\common\model\Eyemargin::alias("a")
  1034. ->field($field)
  1035. ->join("user u","a.user_id = u.id")
  1036. ->where($where)
  1037. ->limit($pageStart,$pageNum)
  1038. ->order("a.createtime desc")
  1039. ->select();
  1040. if($list) foreach($list as $k => $v) {
  1041. // 计算距离
  1042. $list[$k]['distance'] = (int)\app\common\model\Eyemargin::getDistance($v['lng'],$v['lat'],$this->auth->lng,$this->auth->lat);
  1043. $list[$k]['distance_txt'] = \app\common\model\Eyemargin::getDistanceTxt($v['lng'],$v['lat'],$this->auth->lng,$this->auth->lat,$v['city_name'],$v['district_name']);
  1044. $list[$k]['right_info'] = \app\common\model\Eyemargin::getIsView($v['user_id'],$user_id);
  1045. $v['cover'] || $list[$k]['cover'] = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].'/assets/img/video_cover.jpeg';
  1046. $v['hobby_ids'] || $list[$k]['hobby_ids'] = '';
  1047. $v['profession'] || $list[$k]['profession'] = '';
  1048. $v['music'] || $list[$k]['music'] = '';
  1049. $v['video'] || $list[$k]['video'] = '';
  1050. if ($v['eye_type'] == 2) {
  1051. $album = explode(',', $v['album']);
  1052. foreach ($album as &$vv) {
  1053. $vv = $vv . config('oss.img_watermark');
  1054. }
  1055. $list[$k]['album'] = join('|', $album);
  1056. }
  1057. }
  1058. $this->success("获取成功!",$list);
  1059. }
  1060. /**
  1061. * 获取我的个人基本信息
  1062. */
  1063. public function getMyBaseInfo() {
  1064. $user_id = $this->auth->id;
  1065. // redis
  1066. // $redis = new Redis();
  1067. // $redisconfig = config("redis");
  1068. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1069. // $userInfo = decodeArray($redis->hGetAll('userInfo_'.$user_id));
  1070. // if(!$userInfo){
  1071. // 获取用户信息
  1072. $field = 'id,avatar,nickname,gender,age,city_name,district_name,constellation,hobby_ids,profession,declaration,wechat,income';
  1073. $userInfo = \app\common\model\User::field($field)->where(['id'=>$user_id])->find();
  1074. $userInfo['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($userInfo['hobby_ids']);
  1075. // 获取已有标签以及数量
  1076. $userInfo['tagUser'] = \app\common\model\TagUser::alias('a')
  1077. ->field('a.id,t.name,a.number')
  1078. ->join('hx_tag t','t.id = a.tag_id','left')
  1079. ->where(['a.user_id'=>$user_id])
  1080. ->select();
  1081. $userInfo = $userInfo->toArray();
  1082. // $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
  1083. // }
  1084. $this->success("获取成功!",$userInfo);
  1085. }
  1086. /**
  1087. * 为用户添加标签
  1088. */
  1089. public function setUserTag() {
  1090. $tag_id = $this->request->request('tag_id',0); // 标签ID
  1091. $fate_user_id = $this->request->request('fate_user_id',0); // 有眼缘用户ID
  1092. if(!$tag_id || !$fate_user_id) {
  1093. $this->error('参数缺失!');
  1094. }
  1095. if(!\app\common\model\Tag::where(['id'=>$tag_id])->find()) {
  1096. $this->error('标签不存在!');
  1097. }
  1098. $user_id = $this->auth->id;
  1099. $is_fate = \app\common\model\User::getIsView($fate_user_id,$user_id);
  1100. if(!$is_fate) $this->error('您需要先获取微信号才能添加标签');
  1101. $tag_user_log = \app\common\model\TagUserLog::where(['user_id'=>$user_id,'fate_user_id'=>$fate_user_id,'tag_id'=>$tag_id])->find();
  1102. if($tag_user_log) {
  1103. $this->error('您已经为ta添加过此标签了');
  1104. }
  1105. Db::startTrans();
  1106. try {
  1107. // 添加记录
  1108. $data = [];
  1109. $data['user_id'] = $user_id;
  1110. $data['fate_user_id'] = $fate_user_id;
  1111. $data['tag_id'] = $tag_id;
  1112. $data['createtime'] = time();
  1113. $res1 = \app\common\model\TagUserLog::insert($data);
  1114. // 修改标签数量
  1115. $where = [];
  1116. $where['user_id'] = $fate_user_id;
  1117. $where['tag_id'] = $tag_id;
  1118. $tag_user = \app\common\model\TagUser::where($where)->find();
  1119. if($tag_user) {
  1120. $tag_user->number = $tag_user->number + 1;
  1121. $res2 = $tag_user->save();
  1122. } else {
  1123. $data = [];
  1124. $data['user_id'] = $fate_user_id;
  1125. $data['tag_id'] = $tag_id;
  1126. $data['number'] = 1;
  1127. $data['createtime'] = time();
  1128. $res2 = \app\common\model\TagUser::insert($data);
  1129. }
  1130. if($res1 && $res2) {
  1131. Db::commit();
  1132. delUserInfo($fate_user_id);
  1133. $this->success("标签添加成功!");
  1134. }
  1135. } catch (PDOException $e) {
  1136. Db::rollback();
  1137. $this->error("修改失败!");
  1138. }
  1139. }
  1140. /**
  1141. * 绑定用户
  1142. */
  1143. public function bindUser() {
  1144. $invite_no = $this->request->request('invite_no'); // 邀请码
  1145. if(!$invite_no) {
  1146. $this->error("请输入邀请码!");
  1147. }
  1148. $user_id = $this->auth->id;
  1149. // 查询邀请码用户信息
  1150. $inviteUserInfo = \app\common\model\User::where(["invite_no"=>$invite_no])->find();
  1151. if(!$inviteUserInfo) $this->error("查询不到该邀请码用户信息!");
  1152. // 判断是否已经绑定过
  1153. $my_pre_user_id = \app\common\model\User::where(["id"=>$user_id])->value("pre_user_id");
  1154. if($my_pre_user_id > 0) {
  1155. $this->error(__('您已绑定过,不可重复绑定!'));
  1156. }
  1157. if($user_id == $inviteUserInfo->id) {
  1158. $this->error(__('不能绑定自己哦?'));
  1159. }
  1160. // 判断当前用户是否实名认证
  1161. $userAuthInfo = \app\common\model\UserAuth::userIsAuth($this->auth->id);
  1162. if($userAuthInfo['status'] == 0) $this->error($userAuthInfo['msg']);
  1163. $res = \app\common\model\User::update(["pre_user_id"=>$inviteUserInfo->id,'invite_time'=>time()],["id"=>$user_id]);
  1164. if($res) {
  1165. $this->success("恭喜,绑定成功!");
  1166. } else {
  1167. $this->error("网络繁忙,请稍后重试!");
  1168. }
  1169. }
  1170. /**
  1171. * 添加银行卡
  1172. */
  1173. public function addBank() {
  1174. $user_name = $this->request->request('user_name'); //真实姓名
  1175. $bank_name= $this->request->request('bank_name'); //银行名称
  1176. $bank_no = $this->request->request('bank_no'); //银行卡号
  1177. if (!$user_name || !$bank_name || !$bank_no) {
  1178. $this->error(__('Invalid parameters'));
  1179. }
  1180. $bankModel = new \app\common\model\UserBank();
  1181. $where = [];
  1182. $where["user_id"] = $this->auth->id;
  1183. $where["bank_no"] = $bank_no;
  1184. $bankInfo = $bankModel->where($where)->find();
  1185. if($bankInfo) {
  1186. $this->error('该银行卡已经添加过了!');
  1187. }
  1188. $data = [];
  1189. $data["user_id"] = $this->auth->id;
  1190. $data["user_name"] = $user_name;
  1191. $data["bank_name"] = $bank_name;
  1192. $data["bank_no"] = $bank_no;
  1193. $data["createtime"] = time();
  1194. $id = $bankModel->insertGetId($data);
  1195. if($id > 0) {
  1196. $this->success('添加成功!');
  1197. } else {
  1198. $this->error('添加失败!');
  1199. }
  1200. }
  1201. /**
  1202. * 获取银行卡信息
  1203. */
  1204. public function bankInfo() {
  1205. $bankModel = new \app\common\model\UserBank();
  1206. $where = [];
  1207. $where["user_id"] = $this->auth->id;
  1208. $bankInfo = $bankModel->where($where)->find();
  1209. $bankInfo['bank_no'] && $bankInfo['bank_no'] = substr_replace($bankInfo['bank_no'],'********','0','8');
  1210. $this->success('获取成功!',$bankInfo);
  1211. }
  1212. /**
  1213. * 删除银行卡
  1214. */
  1215. public function bankDel() {
  1216. $bankModel = new \app\common\model\UserBank();
  1217. $where = [];
  1218. $where["user_id"] = $this->auth->id;
  1219. $bankInfo = $bankModel->where($where)->delete();
  1220. if($bankInfo) {
  1221. $this->success('删除成功!');
  1222. } else {
  1223. $this->error('删除失败!');
  1224. }
  1225. }
  1226. /**
  1227. * 获取会员开通配置信息
  1228. */
  1229. public function getVipConfig() {
  1230. $res = [];
  1231. $res['user_info'] = \app\common\model\User::field('id,nickname,avatar,vipStatus(vip_duetime) as is_vip,vip_duetime')->where(['id'=>$this->auth->id])->find();
  1232. $res['user_info']['vip_duetime'] = $res['user_info']['vip_duetime']?date('Y-m-d',$res['user_info']['vip_duetime']):"";
  1233. $res['vip_config'] = \app\admin\model\vip\Config::order("weight","desc")->select();
  1234. $this->success("获取成功!",$res);
  1235. }
  1236. /**
  1237. * 设置首页推荐
  1238. */
  1239. public function setEyemaginToMain() {
  1240. $fate_id = $this->request->request('fate_id'); //动态ID
  1241. if (!$fate_id) {
  1242. $this->error(__('Invalid parameters'));
  1243. }
  1244. $user_id = $this->auth->id;
  1245. $fateInfo = \app\common\model\Eyemargin::get($fate_id);
  1246. if($fateInfo->user_id != $user_id) $this->error('抱歉,您无权限操作!');
  1247. // 判断动态是否在审核中
  1248. if($fateInfo->status != 1) $this->error('当前动态状态不允许设置为推荐!');
  1249. Db::startTrans();
  1250. try {
  1251. // 先取消掉所有的推荐
  1252. $res1 = \app\common\model\Eyemargin::update(['is_main'=>0],['user_id'=>$user_id]);
  1253. $res2 = \app\common\model\Eyemargin::update(['is_main'=>1],['id'=>$fate_id]);
  1254. if($res1 && $res2) {
  1255. Db::commit();
  1256. $this->success("设置成功!");
  1257. }
  1258. } catch (PDOException $e) {
  1259. Db::rollback();
  1260. $this->error("设置失败!");
  1261. }
  1262. }
  1263. /**
  1264. * 删除动态
  1265. */
  1266. public function delEyemagin() {
  1267. $fate_id = $this->request->request('fate_id'); //动态ID
  1268. if (!$fate_id) {
  1269. $this->error(__('Invalid parameters'));
  1270. }
  1271. $user_id = $this->auth->id;
  1272. $fateInfo = \app\common\model\Eyemargin::get($fate_id);
  1273. if($fateInfo->user_id != $user_id) $this->error('抱歉,您无权限操作!');
  1274. $res = \app\common\model\Eyemargin::where(['id'=>$fate_id])->delete();
  1275. if($res) {
  1276. $this->success("删除成功!");
  1277. } else {
  1278. $this->error("删除失败!");
  1279. }
  1280. }
  1281. /**
  1282. * 获取第一条系统消息
  1283. */
  1284. public function getFirstSysMsg() {
  1285. $user_id = $this->auth->id;
  1286. $res = [];
  1287. $res['msg_content'] = \app\common\model\SysMsg::where(['user_id'=>$user_id])->order('createtime','desc')->value('title');
  1288. $res['msg_count'] = \app\common\model\SysMsg::where(['user_id'=>$user_id,'is_read'=>0])->count();
  1289. $this->success("获取成功!",$res);
  1290. }
  1291. /**
  1292. * 获取系统消息列表
  1293. */
  1294. public function getSysMsg() {
  1295. $page = $this->request->request('page',1); // 分页
  1296. $pageNum = $this->request->request('pageNum',10); // 分页
  1297. // 分页搜索构建
  1298. $pageStart = ($page-1)*$pageNum;
  1299. $user_id = $this->auth->id;
  1300. $sysMsgList = \app\common\model\SysMsg::where(['user_id'=>$user_id])->order('createtime','desc')->limit($pageStart,$pageNum)->select();
  1301. if($sysMsgList) {
  1302. // 标记所有消息已读1
  1303. \app\common\model\SysMsg::update(['is_read'=>1],['user_id'=>$user_id]);
  1304. }
  1305. $this->success("获取成功!",$sysMsgList);
  1306. }
  1307. /**
  1308. * 获取实名认证信息
  1309. */
  1310. public function getAuthInfo() {
  1311. $user_id = $this->auth->id;
  1312. // 判断当前用户是否实名认证
  1313. $userAuthInfo = \app\common\model\UserAuth::where(["user_id"=>$user_id])->find();
  1314. $res = [];
  1315. $res['status'] = 2;
  1316. $res['msg'] = "已实名!";
  1317. $res['data'] = $userAuthInfo;
  1318. if($userAuthInfo) {
  1319. if($userAuthInfo->status == 0) {
  1320. $res['status'] = 1;
  1321. $res['msg'] = "审核中!";
  1322. } elseif($userAuthInfo->status == 2) {
  1323. $res['status'] = -1;
  1324. $res['msg'] = "审核未通过!";
  1325. }
  1326. } else {
  1327. $res['status'] = 0;
  1328. $res['msg'] = "请先申请实名认证!";
  1329. $res['data'] = [];
  1330. }
  1331. $res['recharge_auth'] = \app\common\model\User::where(['id'=>$user_id])->value("recharge_auth");
  1332. $this->success("获取成功!",$res);
  1333. }
  1334. //设置复制手机号状态
  1335. public function setcopymobile() {
  1336. $status = $this->auth->copy_mobile == 1 ? 2 : 1;
  1337. $rs = Db::name('user')->where(['id' => $this->auth->id, 'copy_mobile' => $this->auth->copy_mobile])->setField('copy_mobile', $status);
  1338. if (!$rs) {
  1339. $this->error('设置失败');
  1340. }
  1341. $this->success('设置成功', $status);
  1342. }
  1343. //收集收集信息
  1344. public function collectmobileinfo() {
  1345. $system = input('system', '', 'trim'); //系统
  1346. $mobilemodel = input('mobilemodel', '', 'trim'); //手机型号
  1347. $mobilebrand = input('mobilebrand', '', 'trim'); //手机品牌
  1348. if (!$system || !$mobilemodel || !$mobilebrand) {
  1349. $this->error('参数缺失');
  1350. }
  1351. if (iconv_strlen($system, 'utf-8') > 255) {
  1352. $this->error('超出限制');
  1353. }
  1354. if (iconv_strlen($mobilemodel, 'utf-8') > 255) {
  1355. $this->error('超出限制');
  1356. }
  1357. if (iconv_strlen($mobilebrand, 'utf-8') > 255) {
  1358. $this->error('超出限制');
  1359. }
  1360. $data['system'] = $system;
  1361. $data['mobilemodel'] = $mobilemodel;
  1362. $data['mobilebrand'] = $mobilebrand;
  1363. $info = Db::name('user_info')->where(['user_id' => $this->auth->id])->find();
  1364. if (!$info) {
  1365. $data['user_id'] = $this->auth->id;
  1366. $rs = Db::name('user_info')->insertGetId($data);
  1367. } else {
  1368. $rs = Db::name('user_info')->where(['user_id' => $this->auth->id])->setField($data);
  1369. }
  1370. if ($rs === false) {
  1371. $this->error('收集失败');
  1372. }
  1373. $this->success('收集成功');
  1374. }
  1375. //查询真人认证动作
  1376. public function authpersontag() {
  1377. $name = Db::name('user_auth_person_tag')->orderRaw("rand()")->limit(1)->value('name');
  1378. if (!$name) {
  1379. $this->error('认证动作缺失,请联系管理员');
  1380. }
  1381. $this->success('查询成功', $name);
  1382. }
  1383. //真人认证
  1384. public function personauth() {
  1385. $info = Db::name('user_auth_person')->where(['user_id' => $this->auth->id])->find();
  1386. if ($info && $info['status'] == 0) {
  1387. $this->error('您已经申请过了,请勿重复操作!');
  1388. }
  1389. if ($info && $info['status'] == 1) {
  1390. $this->error('您已通过审核!');
  1391. }
  1392. $actname = input('actname', '', 'trim'); // 动作名称
  1393. $videofile = input('videofile', '', 'trim'); // 视频
  1394. if (!$videofile || !$actname) {
  1395. $this->error(__('Invalid parameters'));
  1396. }
  1397. if (iconv_strlen($actname, 'utf-8') > 128) {
  1398. $this->error('超出限制');
  1399. }
  1400. if (iconv_strlen($videofile, 'utf-8') > 255) {
  1401. $this->error('超出限制');
  1402. }
  1403. $data = [];
  1404. $data["actname"] = $actname;
  1405. $data["videofile"] = $videofile;
  1406. $data["status"] = 0;
  1407. $data["updatetime"] = time();
  1408. //开启事务
  1409. Db::startTrans();
  1410. if (!$info) { //未认证
  1411. $data["user_id"] = $this->auth->id;
  1412. $data["createtime"] = time();
  1413. $res = Db::name('user_auth_person')->insertGetId($data);
  1414. } else { //认证被拒绝过
  1415. $res = Db::name('user_auth_person')->where(['id' => $info['id'], 'user_id' => $this->auth->id])->setField($data);
  1416. }
  1417. if (!$res) {
  1418. Db::rollback();
  1419. $this->error('提交失败');
  1420. }
  1421. $rt = Db::name('user')->where(['id' => $this->auth->id, 'is_auth_person' => $this->auth->is_auth_person])->setField('is_auth_person', 1);
  1422. if ($rt === false) {
  1423. Db::rollback();
  1424. $this->error('提交失败');
  1425. }
  1426. Db::commit();
  1427. $this->success("真人认证申请提交成功,请耐心等待审核");
  1428. }
  1429. //钻石列表
  1430. public function getdiamond() {
  1431. $list = Db::name('diamond')->order('weigh', 'desc')->select();
  1432. $this->success('钻石', $list);
  1433. }
  1434. //钻石支付有眼缘
  1435. public function diamondpayfate() {
  1436. $fate_user_id = $this->request->request('fate_user_id'); // 被眼缘用户ID
  1437. if (!$fate_user_id) {
  1438. $this->error(__('Invalid parameters'));
  1439. }
  1440. $user_id = $this->auth->id;
  1441. if($fate_user_id == $user_id) {
  1442. $this->error("不需要添加自己为有眼缘哦!");
  1443. }
  1444. $fate_user = Db::name('user')->find($fate_user_id);
  1445. if (!$fate_user) {
  1446. $this->error('用户不存在');
  1447. }
  1448. $data = [];
  1449. $data['user_id'] = $user_id;
  1450. $data['fate_user_id'] = $fate_user_id;
  1451. $user_fate = \app\common\model\UserFate::where($data)->find();
  1452. if($user_fate && $user_fate['createtime'] >= time() - 86400) {
  1453. $this->error("已经添加眼缘啦!");
  1454. }
  1455. $user = \app\common\model\User::get($user_id);
  1456. // 查看当前用户钻石余额是否充足
  1457. $diamondpayfate = (int)config('site.diamondpayfate');
  1458. if ($diamondpayfate <= 0) {
  1459. $this->error('钻石配置错误,请联系管理员');
  1460. }
  1461. if($user['diamond'] < $diamondpayfate) {
  1462. $this->error(__('钻石余额不足,请先充值!'), [],100);
  1463. } else {
  1464. Db::startTrans();
  1465. try {
  1466. //修改用户钻石余额
  1467. $diamond = $user['diamond'] - $diamondpayfate;
  1468. $res1 = Db::name('user')->where(['id' => $user_id, 'diamond' => $user['diamond']])->setField('diamond', $diamond);
  1469. // 添加钻石明细
  1470. $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $user['id']])->order('id', 'desc')->find();
  1471. if (!$diamond_log && $user['diamond'] > 0) {
  1472. Db::rollback();
  1473. $this->error('钻石余额异常');
  1474. }
  1475. if ($diamond_log && $diamond_log['after'] != $user['diamond']) {
  1476. Db::rollback();
  1477. $this->error('钻石余额异常');
  1478. }
  1479. $_data['user_id'] = $user['id'];
  1480. $_data['diamond'] = -$diamondpayfate;
  1481. $_data['before'] = $user['diamond'];
  1482. $_data['after'] = $diamond;
  1483. $_data['memo'] = '查看有眼缘';
  1484. $_data['createtime'] = time();
  1485. $res5 = Db::name('user_diamond_log')->insertGetId($_data);
  1486. // 添加眼缘记录
  1487. if ($user_fate) {
  1488. $res2 = Db::name('user_fate')->where($data)->setField('createtime', time());
  1489. } else {
  1490. $data['createtime'] = time();
  1491. $res2 = \app\common\model\UserFate::insert($data);
  1492. }
  1493. // 添加返利
  1494. if ($fate_user['gender'] == 0) {
  1495. //女的返钱
  1496. if ($user->is_goddess == 1) {
  1497. $memo = '被查看有眼缘获得收益!';
  1498. $profit = config('site.fate') * config('site.goddessProfitRate') * 0.01;
  1499. } else {
  1500. $memo = '被查看有眼缘获得收益!';
  1501. $profit = config('site.fate') * config('site.userProfitRate') * 0.01;
  1502. }
  1503. if ($profit >= 0.01 && $fate_user_id > 0) {
  1504. $res3 = \app\common\model\User::profit($profit, $fate_user_id, $memo);
  1505. //给上级返利
  1506. $fate_user_info = \app\common\model\User::where(['id' => $fate_user_id])->find();
  1507. $profit_last = $profit * config('site.SuperioruserProfitRate') * 0.01;
  1508. if ($profit_last >= 0.01 && $fate_user_info['pre_user_id'] > 0) {
  1509. $res4 = \app\common\model\User::profit($profit_last, $fate_user_info['pre_user_id'], $fate_user_info['nickname'] . '被查看有眼缘获得收益');
  1510. } else {
  1511. $res4 = true;
  1512. }
  1513. } else {
  1514. $res3 = true;
  1515. $res4 = true;
  1516. }
  1517. } else {
  1518. //男的返钻石
  1519. $faterebate = (int)config('site.faterebate'); //男士被查看有眼缘可获得钻石数量
  1520. if ($faterebate > 0) {
  1521. //修改被查看用户钻石余额
  1522. $diamond = $fate_user['diamond'] + $faterebate;
  1523. $res3 = Db::name('user')->where(['id' => $fate_user_id, 'diamond' => $fate_user['diamond']])->setField('diamond', $diamond);
  1524. // 添加钻石明细
  1525. $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $fate_user['id']])->order('id', 'desc')->find();
  1526. if (!$diamond_log && $fate_user['diamond'] > 0) {
  1527. Db::rollback();
  1528. $this->error('钻石余额异常');
  1529. }
  1530. if ($diamond_log && $diamond_log['after'] != $fate_user['diamond']) {
  1531. Db::rollback();
  1532. $this->error('钻石余额异常');
  1533. }
  1534. $_data['user_id'] = $fate_user_id;
  1535. $_data['diamond'] = $faterebate;
  1536. $_data['before'] = $fate_user['diamond'];
  1537. $_data['after'] = $diamond;
  1538. $_data['memo'] = '被查看有眼缘获得收益';
  1539. $_data['createtime'] = time();
  1540. $res4 = Db::name('user_diamond_log')->insertGetId($_data);
  1541. } else {
  1542. $res3 = true;
  1543. $res4 = true;
  1544. }
  1545. }
  1546. if($res1 && $res2 && $res3 && $res4 && $res5) {
  1547. Db::commit();
  1548. $fate_user_info = \app\common\model\User::where(['id'=>$fate_user_id])->find();
  1549. $title = '眼缘提醒!';
  1550. $content = $this->auth->nickname.': 等你很久了,终于来了。希望你可以眼缘这里找到有趣的灵魂。无论白天还是深夜,无论快乐还是寂寞,始终有人陪你~';
  1551. \app\common\model\SysMsg::sendSysMsg($fate_user_id,6,$title,$content);
  1552. $this->success("眼缘添加成功!");
  1553. }
  1554. } catch (PDOException $e) {
  1555. Db::rollback();
  1556. $this->error("添加失败!");
  1557. }
  1558. }
  1559. }
  1560. //设置线下通知状态
  1561. public function setofflinenotice() {
  1562. $status = $this->auth->offline_notice == 1 ? 2 : 1;
  1563. $rs = Db::name('user')->where(['id' => $this->auth->id, 'offline_notice' => $this->auth->offline_notice])->setField('offline_notice', $status);
  1564. if (!$rs) {
  1565. $this->error('设置失败');
  1566. }
  1567. $this->success('设置成功', $status);
  1568. }
  1569. //查询是否可以聊天
  1570. public function getchatstatus() {
  1571. if ($this->auth->gender == 0) {
  1572. $this->success('可以聊天', '1');
  1573. }
  1574. $other_user_id = input('other_user_id', 0, 'intval');
  1575. if (!$other_user_id) {
  1576. $this->error('参数缺失');
  1577. }
  1578. //查询是否是客服
  1579. $count = Db::name('kefu')->where(['user_id' => $other_user_id])->count('id');
  1580. if ($count) {
  1581. $this->success('可以聊天', '1');
  1582. }
  1583. if ($this->auth->id == $other_user_id) {
  1584. $this->error('这是您自己');
  1585. }
  1586. $info = Db::name('user')->find($other_user_id);
  1587. if (!$info) {
  1588. $this->error('用户不存在');
  1589. }
  1590. //查询是否有眼缘
  1591. $fate_count = Db::name('user_fate')->where(['user_id' => $this->auth->id, 'fate_user_id' => $other_user_id, 'createtime' => ['egt', time() - 86400]])->count('id');
  1592. if ($fate_count) {
  1593. $this->success('可以聊天', '1');
  1594. }
  1595. //查询是否支付过
  1596. $pay_count = Db::name('user_chat_pay')->where(['user_id' => $this->auth->id, 'chat_user_id' => $other_user_id])->count('id');
  1597. if ($pay_count) {
  1598. $this->success('可以聊天', '1');
  1599. }
  1600. $this->success('请先支付', '0');
  1601. }
  1602. //男士支付聊天钻石
  1603. public function paychat() {
  1604. $other_user_id = input('other_user_id', 0, 'intval');
  1605. if (!$other_user_id) {
  1606. $this->error('参数缺失');
  1607. }
  1608. if ($this->auth->id == $other_user_id) {
  1609. $this->error('这是您自己');
  1610. }
  1611. $info = Db::name('user')->find($other_user_id);
  1612. if (!$info) {
  1613. $this->error('用户不存在');
  1614. }
  1615. //查询是否支付过
  1616. $pay_count = Db::name('user_chat_pay')->where(['user_id' => $this->auth->id, 'chat_user_id' => $other_user_id])->count('id');
  1617. if ($pay_count) {
  1618. $this->error('已经支付过');
  1619. }
  1620. $manpaychat = (int)config('site.manpaychat'); //男士被查看有眼缘(未添加对方时)回复聊天支付钻石数量
  1621. if ($manpaychat <= 0) {
  1622. $this->error('支付数量异常,请联系管理员');
  1623. }
  1624. if ($this->auth->diamond < $manpaychat) {
  1625. $this->error(__('钻石余额不足,请先充值!'), [],100);
  1626. }
  1627. //开启事务
  1628. Db::startTrans();
  1629. //修改用户钻石余额
  1630. $user = Db::name('user')->find($this->auth->id);
  1631. $diamond = $user['diamond'] - $manpaychat;
  1632. $res1 = Db::name('user')->where(['id' => $user['id'], 'diamond' => $user['diamond']])->setField('diamond', $diamond);
  1633. // 添加钻石明细
  1634. $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $user['id']])->order('id', 'desc')->find();
  1635. if (!$diamond_log && $user['diamond'] > 0) {
  1636. Db::rollback();
  1637. $this->error('钻石余额异常');
  1638. }
  1639. if ($diamond_log && $diamond_log['after'] != $user['diamond']) {
  1640. Db::rollback();
  1641. $this->error('钻石余额异常');
  1642. }
  1643. $_data['user_id'] = $user['id'];
  1644. $_data['diamond'] = -$manpaychat;
  1645. $_data['before'] = $user['diamond'];
  1646. $_data['after'] = $diamond;
  1647. $_data['memo'] = '支付聊天';
  1648. $_data['createtime'] = time();
  1649. $res2 = Db::name('user_diamond_log')->insertGetId($_data);
  1650. //添加支付记录
  1651. $chat_pay['user_id'] = $user['id'];
  1652. $chat_pay['chat_user_id'] = $other_user_id;
  1653. $chat_pay['createtime'] = time();
  1654. $res3 = Db::name('user_chat_pay')->insertGetId($chat_pay);
  1655. if ($res1 && $res2 && $res3) {
  1656. Db::commit();
  1657. $this->success('支付成功');
  1658. }
  1659. Db::rollback();
  1660. $this->error('支付失败');
  1661. }
  1662. }