User.php 71 KB

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