User.php 74 KB

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