User.php 72 KB

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