User.php 72 KB

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