User.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Validate;
  8. use think\Db;
  9. use think\Cache;
  10. use Redis;
  11. use AlibabaCloud\Client\AlibabaCloud;
  12. use AlibabaCloud\Client\Exception\ClientException;
  13. use AlibabaCloud\Client\Exception\ServerException;
  14. /**
  15. * 会员接口
  16. */
  17. class User extends Api
  18. {
  19. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','onLogin'];
  20. protected $noNeedRight = '*';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $this->success('', ['welcome' => $this->auth->nickname]);
  31. }
  32. /**
  33. * 会员登录
  34. *
  35. * @param string $account 账号
  36. * @param string $password 密码
  37. */
  38. public function login()
  39. {
  40. $account = $this->request->request('account');
  41. $password = $this->request->request('password');
  42. if (!$account || !$password) {
  43. $this->error(__('Invalid parameters'));
  44. }
  45. $ret = $this->auth->login($account, $password);
  46. if ($ret) {
  47. $data = ['userinfo' => $this->auth->getUserinfo()];
  48. $this->success(__('Logged in successful'), $data);
  49. } else {
  50. $this->error($this->auth->getError());
  51. }
  52. }
  53. /**
  54. * 手机验证码登录
  55. *
  56. * @param string $mobile 手机号
  57. * @param string $captcha 验证码
  58. */
  59. public function mobilelogin()
  60. {
  61. $mobile = $this->request->request('mobile');
  62. $captcha = $this->request->request('captcha');
  63. if (!$mobile || !$captcha) {
  64. $this->error(__('Invalid parameters'));
  65. }
  66. if (!Validate::regex($mobile, "^1\d{10}$")) {
  67. $this->error(__('Mobile is incorrect'));
  68. }
  69. if (!Sms::check($mobile, $captcha, 'login')) {
  70. $this->error(__('Captcha is incorrect'));
  71. }
  72. $user = \app\common\model\User::getByMobile($mobile);
  73. if ($user) {
  74. if ($user->status != 'normal') {
  75. $this->error(__('Account is locked'));
  76. }
  77. //如果已经有账号则直接登录
  78. $ret = $this->auth->direct($user->id);
  79. } else {
  80. $ret = $this->auth->register($mobile, Random::alnum(), $mobile, []);
  81. }
  82. if ($ret) {
  83. Sms::flush($mobile, 'login');
  84. $data = ['userinfo' => $this->auth->getUserinfo()];
  85. $this->success(__('Logged in successful'), $data);
  86. } else {
  87. $this->error($this->auth->getError());
  88. }
  89. }
  90. /**
  91. * 注册会员
  92. *
  93. * @param string $username 用户名
  94. * @param string $password 密码
  95. * @param string $email 邮箱
  96. * @param string $mobile 手机号
  97. * @param string $code 验证码
  98. */
  99. public function register()
  100. {
  101. $username = $this->request->request('username');
  102. $password = $this->request->request('password');
  103. $email = $this->request->request('email');
  104. $mobile = $this->request->request('mobile');
  105. $code = $this->request->request('code');
  106. if (!$username || !$password) {
  107. $this->error(__('Invalid parameters'));
  108. }
  109. if ($email && !Validate::is($email, "email")) {
  110. $this->error(__('Email is incorrect'));
  111. }
  112. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  113. $this->error(__('Mobile is incorrect'));
  114. }
  115. $ret = Sms::check($mobile, $code, 'register');
  116. if (!$ret) {
  117. $this->error(__('Captcha is incorrect'));
  118. }
  119. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  120. if ($ret) {
  121. $data = ['userinfo' => $this->auth->getUserinfo()];
  122. $this->success(__('Sign up successful'), $data);
  123. } else {
  124. $this->error($this->auth->getError());
  125. }
  126. }
  127. /**
  128. * 退出登录
  129. */
  130. public function logout()
  131. {
  132. $this->auth->logout();
  133. $this->success(__('Logout successful'));
  134. }
  135. /**
  136. * 修改邮箱
  137. *
  138. * @param string $email 邮箱
  139. * @param string $captcha 验证码
  140. */
  141. public function changeemail()
  142. {
  143. $user = $this->auth->getUser();
  144. $email = $this->request->post('email');
  145. $captcha = $this->request->request('captcha');
  146. if (!$email || !$captcha) {
  147. $this->error(__('Invalid parameters'));
  148. }
  149. if (!Validate::is($email, "email")) {
  150. $this->error(__('Email is incorrect'));
  151. }
  152. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  153. $this->error(__('Email already exists'));
  154. }
  155. $result = Ems::check($email, $captcha, 'changeemail');
  156. if (!$result) {
  157. $this->error(__('Captcha is incorrect'));
  158. }
  159. $verification = $user->verification;
  160. $verification->email = 1;
  161. $user->verification = $verification;
  162. $user->email = $email;
  163. $user->save();
  164. Ems::flush($email, 'changeemail');
  165. $this->success();
  166. }
  167. /**
  168. * 修改手机号
  169. *
  170. * @param string $mobile 手机号
  171. * @param string $captcha 验证码
  172. */
  173. public function changemobile()
  174. {
  175. $user = $this->auth->getUser();
  176. $mobile = $this->request->request('mobile');
  177. $captcha = $this->request->request('captcha');
  178. if (!$mobile || !$captcha) {
  179. $this->error(__('Invalid parameters'));
  180. }
  181. if (!Validate::regex($mobile, "^1\d{10}$")) {
  182. $this->error(__('Mobile is incorrect'));
  183. }
  184. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  185. $this->error(__('Mobile already exists'));
  186. }
  187. $result = Sms::check($mobile, $captcha, 'changemobile');
  188. if (!$result) {
  189. $this->error(__('Captcha is incorrect'));
  190. }
  191. $verification = $user->verification;
  192. $verification->mobile = 1;
  193. $user->verification = $verification;
  194. $user->mobile = $mobile;
  195. $user->save();
  196. Sms::flush($mobile, 'changemobile');
  197. $this->success();
  198. }
  199. /**
  200. * 第三方登录
  201. *
  202. * @param string $platform 平台名称
  203. * @param string $code Code码
  204. */
  205. public function third()
  206. {
  207. $url = url('user/index');
  208. $platform = $this->request->request("platform");
  209. $code = $this->request->request("code");
  210. $config = get_addon_config('third');
  211. if (!$config || !isset($config[$platform])) {
  212. $this->error(__('Invalid parameters'));
  213. }
  214. $app = new \addons\third\library\Application($config);
  215. //通过code换access_token和绑定会员
  216. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  217. if ($result) {
  218. $loginret = \addons\third\library\Service::connect($platform, $result);
  219. if ($loginret) {
  220. $data = [
  221. 'userinfo' => $this->auth->getUserinfo(),
  222. 'thirdinfo' => $result
  223. ];
  224. $this->success(__('Logged in successful'), $data);
  225. }
  226. }
  227. $this->error(__('Operation failed'), $url);
  228. }
  229. /**
  230. * 重置密码
  231. *
  232. * @param string $mobile 手机号
  233. * @param string $newpassword 新密码
  234. * @param string $captcha 验证码
  235. */
  236. public function resetpwd()
  237. {
  238. $type = $this->request->request("type");
  239. $mobile = $this->request->request("mobile");
  240. $email = $this->request->request("email");
  241. $newpassword = $this->request->request("newpassword");
  242. $captcha = $this->request->request("captcha");
  243. if (!$newpassword || !$captcha) {
  244. $this->error(__('Invalid parameters'));
  245. }
  246. if ($type == 'mobile') {
  247. if (!Validate::regex($mobile, "^1\d{10}$")) {
  248. $this->error(__('Mobile is incorrect'));
  249. }
  250. $user = \app\common\model\User::getByMobile($mobile);
  251. if (!$user) {
  252. $this->error(__('User not found'));
  253. }
  254. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  255. if (!$ret) {
  256. $this->error(__('Captcha is incorrect'));
  257. }
  258. Sms::flush($mobile, 'resetpwd');
  259. } else {
  260. if (!Validate::is($email, "email")) {
  261. $this->error(__('Email is incorrect'));
  262. }
  263. $user = \app\common\model\User::getByEmail($email);
  264. if (!$user) {
  265. $this->error(__('User not found'));
  266. }
  267. $ret = Ems::check($email, $captcha, 'resetpwd');
  268. if (!$ret) {
  269. $this->error(__('Captcha is incorrect'));
  270. }
  271. Ems::flush($email, 'resetpwd');
  272. }
  273. //模拟一次登录
  274. $this->auth->direct($user->id);
  275. $ret = $this->auth->changepwd($newpassword, '', true);
  276. if ($ret) {
  277. $this->success(__('Reset password successful'));
  278. } else {
  279. $this->error($this->auth->getError());
  280. }
  281. }
  282. /**
  283. * 运营商一键登录
  284. */
  285. public function onLogin() {
  286. $token = $this->request->param('token');// 易盾返回的token
  287. // 判断登录token是否有效
  288. if (!$token) {
  289. //如果token为空就返回
  290. $this->error('token不能为空,请重试');
  291. } else {
  292. //调用getPhone方法并且将token传给getPhone
  293. $res = $this->getPhone($token);
  294. //如果返回的状态为1说明是注册过的用户
  295. if ($res['state'] == 1) {
  296. $phone = $res['phone'];
  297. // 用户登录逻辑 === 开始
  298. $userModel = new \app\common\model\User();
  299. $auth = \app\common\library\Auth::instance();
  300. $userInfo = $userModel->where(["mobile"=>$phone])->find();
  301. // 判断用户是否已经存在
  302. if($userInfo) { // 登录
  303. $user = \app\common\model\User::get($userInfo["id"]);
  304. if (!$user) {
  305. $this->error("网络错误!请稍后重试");
  306. }
  307. $user->save(["logintime"=>time()]);
  308. $res_login = $auth->direct($user->id);
  309. } else { // 注册
  310. // 先随机一个用户名,随后再变更为u+数字id
  311. $username = Random::alnum(20);
  312. $password = Random::alnum(6);
  313. // 获取默认头像和昵称
  314. $nickname = array_column(\app\admin\model\website\Nickname::select(),'content');
  315. $avatar = array_column(\app\admin\model\website\Avatar::select(),'content');
  316. $extend = [
  317. 'nickname'=>$nickname[rand(0,count($nickname)-1)],
  318. 'avatar'=>$avatar[rand(0,count($avatar)-1)],
  319. "mobile"=>$phone
  320. ];
  321. Db::startTrans();
  322. try {
  323. // 默认注册一个会员
  324. $result = $auth->register($username, $password, "", $extend);
  325. if (!$result) {
  326. return false;
  327. }
  328. $user = $auth->getUser();
  329. $fields = ['username' => 'u' . $user->id];
  330. // 更新会员资料
  331. $user = \app\common\model\User::get($user->id);
  332. $user->save($fields);
  333. Db::commit();
  334. } catch (PDOException $e) {
  335. Db::rollback();
  336. $auth->logout();
  337. return false;
  338. }
  339. // 写入登录Cookies和Token
  340. $res_login = $auth->direct($user->id);
  341. }
  342. $userInfo = $auth->getUserinfo();
  343. if($res_login) {
  344. $this->success("登录成功!",['userinfo' => $userInfo]);
  345. } else {
  346. $this->error("登录失败!");
  347. }
  348. // 用户登录逻辑 === 结束
  349. } else {
  350. //如果没有注册过就返回注册状态
  351. $this->error($res['msg']);
  352. }
  353. }
  354. }
  355. /*
  356. * 根据token换取手机号码
  357. */
  358. public function getPhone($token) {
  359. $config = config('onLogin');
  360. AlibabaCloud::accessKeyClient($config['phone_access_key'], $config['phone_access_secret'])
  361. ->regionId('cn-hangzhou')
  362. ->asDefaultClient();
  363. try {
  364. $result = AlibabaCloud::rpc()
  365. ->product('Dypnsapi')
  366. ->scheme('https')// https | http
  367. ->version('2017-05-25')
  368. ->action('GetMobile')
  369. ->method('POST')
  370. ->host('dypnsapi.aliyuncs.com')
  371. ->options([
  372. 'query' => [
  373. 'RegionId' => "cn-hangzhou",
  374. 'AccessToken' => $token
  375. ],
  376. ])
  377. ->request();
  378. // 将返回的结果转化为数组
  379. $result = $result->toArray();
  380. //判断当前数组不为空
  381. if (isset($result['GetMobileResultDTO']['Mobile'])) {
  382. // token不为空返回手机号码
  383. $phone = $result['GetMobileResultDTO']['Mobile'];
  384. $res = [
  385. 'state' => 1,
  386. 'phone' => $phone
  387. ];
  388. return $res;
  389. } else {
  390. //如果token为空
  391. $res = [
  392. 'state' => 0,
  393. 'msg' => 'token无效'
  394. ];
  395. return $res;
  396. }
  397. } catch (ClientException $e) {//有异常就抛出异常
  398. // 客户端错误
  399. $res = [
  400. 'state' => 101,
  401. 'msg' => '注册失败'
  402. ];
  403. return $res;
  404. } catch (ServerException $e) {
  405. // 服务端错误
  406. $res = [
  407. 'state' => 101,
  408. 'msg' => '注册失败'
  409. ];
  410. return $res;
  411. }
  412. }
  413. /**
  414. * 修改会员个人信息
  415. * 头像,昵称,性别,
  416. */
  417. public function userAvatar()
  418. {
  419. $user = $this->auth->getUser();
  420. $gender = $this->request->request('gender'); // 性别:1=男,-1=女
  421. $nickname_auth = $this->request->request('nickname');
  422. $avatar_auth = $this->request->request('avatar');
  423. if (!$gender && !$nickname_auth && !$avatar_auth) $this->error('参数为空!');
  424. // 随机获取昵称和头像
  425. if(!$user->nickname && !$nickname_auth) {
  426. $nicknameList = \app\admin\model\website\Nickname::select();//得到总条数
  427. $nicknameArr = [];
  428. if($nicknameList) foreach($nicknameList as $k => $v) {
  429. $nicknameArr[] = $v['content'];
  430. }
  431. $user->nickname = $nicknameArr[array_rand($nicknameArr,1)];
  432. }
  433. if(!$user->avatar && !$avatar_auth) {
  434. $avatarList = \app\admin\model\website\Avatar::select();//得到总条数
  435. $avatarArr = [];
  436. if($avatarList) foreach($avatarList as $k => $v) {
  437. $avatarArr[] = $v['content'];
  438. }
  439. $user->avatar = $avatarArr[array_rand($avatarArr,1)];
  440. }
  441. Db::startTrans();
  442. try {
  443. $res1 = true;
  444. if ($nickname_auth) {
  445. if($nickname_auth == $user->nickname) {
  446. $this->error(__('与原昵称相同无需修改!'));
  447. }
  448. $user->nickname_auth = $nickname_auth;
  449. // 添加昵称修改申请表
  450. if(\app\common\model\NicknameAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("昵称已在审核中!请勿重复申请");
  451. $data = [];
  452. $data['user_id'] = $this->auth->id;
  453. $data['nickname'] = $nickname_auth;
  454. $data['old_nickname'] = $user->nickname;
  455. $data['createtime'] = time();
  456. $res1 = \app\common\model\NicknameAuth::insert($data);
  457. }
  458. if($avatar_auth) {
  459. $user->avatar_auth = $avatar_auth;
  460. // 添加头像修改申请表
  461. if(\app\common\model\AvatarAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("头像已在审核中!请勿重复申请");
  462. $data = [];
  463. $data['user_id'] = $this->auth->id;
  464. $data['avatar'] = $avatar_auth;
  465. $data['old_avatar'] = $user->avatar;
  466. $data['createtime'] = time();
  467. $res1 = \app\common\model\AvatarAuth::insert($data);
  468. }
  469. $gender && $user->gender = $gender;
  470. $res2 = $user->save();
  471. if($res1 && $res2 !== false) {
  472. Db::commit();
  473. delUserInfo($this->auth->id);
  474. $this->success("修改成功!昵称审核中");
  475. }
  476. } catch (PDOException $e) {
  477. Db::rollback();
  478. $this->error("修改失败!");
  479. }
  480. }
  481. /**
  482. * 修改会员个人信息
  483. * 城市,年龄,收入
  484. */
  485. public function userCity() {
  486. $user = $this->auth->getUser();
  487. $province = $this->request->request('province'); // 省
  488. $city = $this->request->request('city'); // 市
  489. $district = $this->request->request('district'); // 区
  490. $birthday = $this->request->request('birthday');
  491. $age = $this->request->request('age');
  492. $constellation = $this->request->request('constellation'); // 星座
  493. $income = $this->request->request('income');
  494. if ((!$province || !$city || !$district) && $age < 3 && !$income) $this->error('年龄太小了哦!');
  495. $province && $user->province = $province;
  496. $city && $user->city = $city;
  497. $district && $user->district = $district;
  498. $province && $user->province_name = \app\common\model\Area::getNameFromId($province);
  499. $city && $user->city_name = \app\common\model\Area::getNameFromId($city);
  500. $district && $user->district_name = \app\common\model\Area::getNameFromId($district);
  501. $age >= 3 && $user->age = $age;
  502. $constellation && $user->constellation = $constellation;
  503. $birthday && $user->birthday = $birthday;
  504. $income && $user->income = $income;
  505. $user->save();
  506. delUserInfo($this->auth->id);
  507. $this->success("修改成功!");
  508. }
  509. /**
  510. * 修改会员个人信息
  511. * 期望对象
  512. */
  513. public function userExpect() {
  514. $user = $this->auth->getUser();
  515. $expect = $this->request->request('expect'); // 期望对象,格式:1,2,3
  516. if (!$expect) $this->error('参数为空!');
  517. $user->expect_ids = $expect;
  518. $user->save();
  519. delUserInfo($user->id);
  520. $this->success("修改成功!");
  521. }
  522. /**
  523. * 修改会员个人信息
  524. * 最后登录的经纬度
  525. */
  526. public function userLnglat() {
  527. $user = $this->auth->getUser();
  528. $lng = $this->request->request('lng'); // 经度
  529. $lat = $this->request->request('lat'); // 纬度
  530. if (!$lng || !$lat) $this->error('参数缺失!');
  531. $user->lng = $lng;
  532. $user->lat = $lat;
  533. $user->save();
  534. $this->success("修改成功!");
  535. }
  536. /**
  537. * 修改会员个人信息
  538. * 环信注册id
  539. */
  540. public function userEmcid() {
  541. $user = $this->auth->getUser();
  542. $emcid = $this->request->request('emcid'); // 环信注册ID
  543. $user->emcid = $emcid;
  544. $user->save();
  545. $this->success("修改成功!");
  546. }
  547. /**
  548. * 修改会员个人信息
  549. * 爱好,职业,微信,交友宣言
  550. */
  551. public function userhoppy() {
  552. $user = $this->auth->getUser();
  553. $hobby_ids = $this->request->request('hobby_ids'); // 爱好
  554. $profession = $this->request->request('profession'); // 职业(传汉字即可)
  555. $wechat = $this->request->request('wechat'); // 微信号
  556. $declaration = $this->request->request('declaration'); // 交友宣言
  557. if (!$hobby_ids && !$profession && !$wechat && !$declaration) $this->error('参数为空!');
  558. Db::startTrans();
  559. try {
  560. $hobby_ids && $user->hobby_ids = $hobby_ids;
  561. $profession && $user->profession = $profession;
  562. if($wechat) {
  563. if($user->wechat_time + 30*86400 > time()) {
  564. $this->error('微信号每月最多修改一次哦!');
  565. }
  566. $user->wechat_auth = $wechat;
  567. // 添加微信号修改申请表
  568. if(\app\common\model\WechatAuth::where(["status"=>0,"user_id"=>$this->auth->id])->find()) $this->error("微信号已在审核中!请勿重复申请");
  569. $data = [];
  570. $data['user_id'] = $this->auth->id;
  571. $data['wechat'] = $wechat;
  572. $data['old_wechat'] = $user->wechat;
  573. $data['createtime'] = time();
  574. $res1 = \app\common\model\WechatAuth::insert($data);
  575. $user->wechat_time = time();
  576. } else {
  577. $res1 = true;
  578. }
  579. $declaration && $user->declaration = $declaration;
  580. $res2 = $user->save();
  581. if($res1 && $res2) {
  582. Db::commit();
  583. delUserInfo($this->auth->id);
  584. if($wechat) {
  585. $this->success("微信号修改申请已提交,请耐心等待审核!");
  586. } else {
  587. $this->success("修改成功!");
  588. }
  589. }
  590. } catch (PDOException $e) {
  591. Db::rollback();
  592. $this->error("修改失败!");
  593. }
  594. }
  595. /**
  596. * 实名认证
  597. */
  598. public function authApply() {
  599. $realname = $this->request->request('realname'); // 真实姓名
  600. $idcard = $this->request->request('idcard'); // 身份证号
  601. $zimage = $this->request->request('zimage'); // 身份证正面照
  602. $fimage = $this->request->request('fimage'); // 身份证反面照
  603. if (!$zimage || !$fimage) {
  604. $this->error(__('Invalid parameters'));
  605. }
  606. $userauthModel = new \app\common\model\UserAuth();
  607. $data = [];
  608. $data["user_id"] = $this->auth->id;
  609. if($userauthModel->where($data)->where(['status'=>['in',[0,1]]])->find()) $this->error('您已经申请过了,请勿重复操作!');
  610. $data["idcard"] = $idcard;
  611. $data["realname"] = $realname;
  612. $zimage && $data["zimage"] = $zimage;
  613. $fimage && $data["fimage"] = $fimage;
  614. $data["status"] = 0;
  615. $data["updatetime"] = time();
  616. $data["createtime"] = time();
  617. $res = $userauthModel->insertGetId($data);
  618. \app\common\model\User::update(['is_auth'=>1],["id"=>$this->auth->id]);
  619. if($res) {
  620. $this->success("实名认证申请提交成功,请耐心等待审核");
  621. } else {
  622. $this->error("网络错误,请稍后重试");
  623. }
  624. }
  625. /**
  626. * 加入黑名单
  627. */
  628. public function addBlacklist() {
  629. $black_user_id = $this->request->request('black_user_id'); // 黑名单用户ID
  630. if (!$black_user_id) {
  631. $this->error(__('Invalid parameters'));
  632. }
  633. $user_id = $this->auth->id;
  634. if($user_id == $black_user_id) {
  635. $this->error(__('为何拉黑自己呢?'));
  636. }
  637. $userblacklistModel = new \app\common\model\UserBlacklist();
  638. $data = [];
  639. $data["user_id"] = $user_id;
  640. $data["black_user_id"] = $black_user_id;
  641. if($userblacklistModel->where($data)->find()) $this->error(__('已在黑名单!'));
  642. $data["createtime"] = time();
  643. $res = $userblacklistModel->insertGetId($data);
  644. if($res) {
  645. $this->success("加入成功!");
  646. } else {
  647. $this->error("网络错误,请稍后重试");
  648. }
  649. }
  650. /**
  651. * 获取黑名单用户
  652. */
  653. public function getBlacklist() {
  654. $page = $this->request->request('page',1); // 分页
  655. $pageNum = $this->request->request('pageNum',10); // 分页
  656. // 分页搜索构建
  657. $pageStart = ($page-1)*$pageNum;
  658. $userblacklistModel = new \app\common\model\UserBlacklist();// ->limit($pageStart,$pageNum)
  659. $where = [];
  660. $where["a.user_id"] = $this->auth->id;
  661. $list = $userblacklistModel->alias("a")
  662. ->field("a.id,a.black_user_id,u.avatar,u.nickname,u.age,u.gender,u.constellation,u.hobby_ids,u.profession")
  663. ->join("hx_user u","u.id = a.black_user_id")
  664. ->where($where)
  665. ->limit($pageStart,$pageNum)
  666. ->select();
  667. if($list) {
  668. foreach($list as $k => $v) {
  669. $list[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
  670. }
  671. $this->success("获取成功!",$list);
  672. } else {
  673. $this->success("数据为空",[]);
  674. }
  675. }
  676. /**
  677. * 移除用户黑名单
  678. */
  679. public function removeUserBlack() {
  680. $id = $this->request->request('id'); // 黑名单ID
  681. if (!$id) {
  682. $this->error(__('Invalid parameters'));
  683. }
  684. $userblacklistModel = new \app\common\model\UserBlacklist();
  685. $where = [];
  686. $where["id"] = $id;
  687. $info = $userblacklistModel->where($where)->find();
  688. if($info['user_id'] != $this->auth->id) $this->error("无权限!");
  689. $res = $userblacklistModel->where($where)->delete();
  690. if($res) {
  691. $this->success("移除成功!",$res);
  692. } else {
  693. $this->error("网络错误,请稍后重试!");
  694. }
  695. }
  696. /**
  697. * 举报用户
  698. */
  699. public function addReport() {
  700. $ruser_id = $this->request->request('ruser_id'); // 被举报用户ID
  701. $content = $this->request->request('content'); // 举报内容
  702. $type_id = $this->request->request('type_id'); // 举报类型
  703. $image = $this->request->request('image'); // 图片描述(多个用半角逗号隔开)
  704. if (!$ruser_id) {
  705. $this->error(__('Invalid parameters'));
  706. }
  707. $userreportModel = new \app\common\model\UserReport();
  708. $data = [];
  709. $data["user_id"] = $this->auth->id;
  710. $data["ruser_id"] = $ruser_id;
  711. $data["type_id"] = $type_id;
  712. $data["content"] = $content;
  713. $data["image"] = $image;
  714. $data["createtime"] = time();
  715. $res = $userreportModel->insertGetId($data);
  716. if($res) {
  717. $this->success("举报成功!");
  718. } else {
  719. $this->error("网络错误,请稍后重试");
  720. }
  721. }
  722. /**
  723. * 剩余特权次数
  724. * @return int|mixed
  725. */
  726. public function getFateCount() {
  727. $fate_count = \app\common\model\User::getViewCount($this->auth->id);
  728. $this->success("获取成功!",$fate_count);
  729. }
  730. /**
  731. * 添加有眼缘
  732. */
  733. public function addFate() {
  734. $fate_user_id = $this->request->request('fate_user_id'); // 被眼缘用户ID
  735. if (!$fate_user_id) {
  736. $this->error(__('Invalid parameters'));
  737. }
  738. $user_id = $this->auth->id;
  739. if($fate_user_id == $user_id) {
  740. $this->error("不需要添加自己为有眼缘哦!");
  741. }
  742. $user = \app\common\model\User::get($user_id);
  743. // 查看当前用户剩余次数
  744. $view_count = \app\common\model\User::getViewCount($user_id);
  745. if($view_count <= 0) {
  746. $this->error(__('可查看次数不够了哦!',[],100));
  747. } else {
  748. Db::startTrans();
  749. try {
  750. $user->view_count = $user->view_count - 1;
  751. $res1 = $user->save();
  752. // 添加眼缘记录
  753. $data = [];
  754. $data['user_id'] = $user_id;
  755. $data['fate_user_id'] = $fate_user_id;
  756. if(\app\common\model\UserFate::where($data)->find()) {
  757. $this->error("已经添加眼缘啦!");
  758. }
  759. $data['createtime'] = time();
  760. $res2 = \app\common\model\UserFate::insert($data);
  761. // 添加返利
  762. if($user->is_goddess == 1) {
  763. $memo = '被查看有眼缘获得收益!';
  764. $profit = config('site.fate') * config('site.goddessProfitRate') * 0.01;
  765. } else {
  766. $memo = '被查看有眼缘获得收益!';
  767. $profit = config('site.fate') * config('site.userProfitRate') * 0.01;
  768. }
  769. if($profit >= 0.01 && $fate_user_id > 0) {
  770. $res3 = \app\common\model\User::profit($profit,$fate_user_id,$memo);
  771. } else {
  772. $res3 = true;
  773. }
  774. if($res1 && $res2 && $res3) {
  775. Db::commit();
  776. $fate_user_info = \app\common\model\User::where(['id'=>$fate_user_id])->find();
  777. $title = '眼缘提醒!';
  778. $content = $fate_user_info->nickname.': 等你很久了,终于来了。希望你可以眼缘这里找到有趣的灵魂。无论白天还是深夜,无论快乐还是寂寞,始终有人陪你~';
  779. \app\common\model\SysMsg::sendSysMsg($fate_user_id,6,$title,$content);
  780. $this->success("眼缘添加成功!");
  781. }
  782. } catch (PDOException $e) {
  783. Db::rollback();
  784. $this->error("添加失败!");
  785. }
  786. }
  787. }
  788. /**
  789. * 获取有眼缘列表
  790. */
  791. public function getFate() {
  792. $page = $this->request->request('page',1); // 分页
  793. $pageNum = $this->request->request('pageNum',10); // 分页
  794. // 分页搜索构建
  795. $pageStart = ($page-1)*$pageNum;
  796. $user_id = $this->auth->id;
  797. $where = [];
  798. $where['a.user_id'] = $user_id;
  799. $res = \app\common\model\UserFate::alias("a")
  800. ->field("a.id,u.id as user_id,u.avatar,u.nickname,u.age,u.constellation,u.hobby_ids,u.profession,u.wechat")
  801. ->join("hx_user u","u.id = a.fate_user_id")
  802. ->where($where)
  803. ->order("a.createtime",'desc')
  804. ->limit($pageStart,$pageNum)
  805. ->select();
  806. if($res) foreach($res as $k => $v) {
  807. $res[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
  808. }
  809. $this->success("获取成功!",$res);
  810. }
  811. /**
  812. * 获取用户个人信息
  813. */
  814. public function getUserInfo() {
  815. $user_id = $this->request->request('user_id',0); // 用户ID
  816. if(!$user_id) {
  817. $this->error('参数缺失!');
  818. }
  819. // // redis
  820. // $redis = new Redis();
  821. // $redisconfig = config("redis");
  822. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  823. // $userInfo = decodeArray($redis->hGetAll('userInfo_'.$user_id));
  824. //
  825. // if(!$userInfo){
  826. // 获取用户信息
  827. $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';
  828. $userInfo = \app\common\model\User::field($field)->where(['id'=>$user_id])->find();
  829. $userInfo['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($userInfo['hobby_ids']);
  830. // 获取是否有眼缘
  831. $userInfo['is_fate'] = \app\common\model\User::getIsView($user_id,$this->auth->id);
  832. // 获取地区
  833. $userInfo['address'] = \app\common\model\Eyemargin::getDistanceTxt($userInfo['lng'],$userInfo['lat'],$this->auth->lng,$this->auth->lat,$userInfo['city_name'],$userInfo['district_name']);
  834. // 微信号
  835. if(!$userInfo['wechat']) {
  836. $userInfo['wechat'] = '暂未设置微信号!';
  837. } elseif(!$userInfo['is_fate']) {
  838. $userInfo['wechat'] = '******';
  839. }
  840. // 获取已有标签以及数量
  841. $userInfo['tagUser'] = \app\common\model\TagUser::alias('a')
  842. ->field('a.id,t.name,a.number')
  843. ->join('hx_tag t','t.id = a.tag_id','left')
  844. ->where(['a.user_id'=>$user_id])
  845. ->select();
  846. // $userInfo = $userInfo->toArray();
  847. // $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
  848. // }
  849. $this->success("获取成功!",$userInfo);
  850. }
  851. /**
  852. * 获取我的个人信息
  853. */
  854. public function getMyInfo() {
  855. $user_id = $this->auth->id;
  856. // redis
  857. // $redis = new Redis();
  858. // $redisconfig = config("redis");
  859. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  860. // $userInfo = decodeArray($redis->hGetAll('userInfo_'.$user_id));
  861. //
  862. // if(!$userInfo){
  863. // 获取用户信息
  864. $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,money,wechat,wechat_auth,pre_user_id';
  865. $userInfo = \app\common\model\User::field($field)->where(['id'=>$user_id])->find();
  866. $userInfo['vip_duetime'] = $userInfo['vip_duetime']?date('Y-m-d',$userInfo['vip_duetime']):"";
  867. if($userInfo['pre_user_id']>0) {
  868. $userInfo['pre_invite_no'] = \app\common\model\User::where(['id'=>$userInfo['pre_user_id']])->value("invite_no");
  869. } else {
  870. $userInfo['pre_invite_no'] = "";
  871. }
  872. // 获取我喜欢的统计
  873. $userInfo['ilike_count'] = \app\common\model\UserLike::where(['fans_id'=>$user_id])->count();
  874. $userInfo['likeme_count'] = \app\common\model\UserLike::where(['user_id'=>$user_id])->count();
  875. $userInfo['fate_count'] = \app\common\model\UserFate::where(['user_id'=>$user_id])->count();
  876. $userInfo['money_count'] = $userInfo['money'];
  877. $userInfo['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($userInfo['hobby_ids']);
  878. $userInfo['expect_ids'] = $userInfo['expect_ids']?explode(",",$userInfo['expect_ids']):[];
  879. $userInfo['nickname_auth_stauts'] = \app\common\model\NicknameAuth::getAuthStatus($userInfo['id'],$userInfo['nickname_auth']);
  880. $userInfo['avatar_auth_stauts'] = \app\common\model\AvatarAuth::getAuthStatus($userInfo['id'],$userInfo['avatar_auth']);
  881. $userInfo['wechat_auth_stauts'] = \app\common\model\WechatAuth::getAuthStatus($userInfo['id'],$userInfo['wechat_auth']);
  882. // $userInfo = $userInfo->toArray();
  883. // $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
  884. // }
  885. $this->success("获取成功!",$userInfo);
  886. }
  887. /**
  888. * 获取动态/我的动态
  889. * @throws \think\db\exception\DataNotFoundException
  890. * @throws \think\db\exception\ModelNotFoundException
  891. * @throws \think\exception\DbException
  892. */
  893. public function getUserEyemagin() {
  894. $user_id = $this->request->request('user_id',0); // 用户ID
  895. $page = $this->request->request('page',1); // 分页
  896. $pageNum = $this->request->request('pageNum',10); // 分页
  897. // 分页搜索构建
  898. $pageStart = ($page-1)*$pageNum;
  899. $where = [];
  900. if($user_id > 0) {
  901. $where['a.user_id'] = $user_id;
  902. $where['a.status'] = 1;
  903. } else {
  904. $user_id = $this->auth->id;
  905. $where['a.user_id'] = $user_id;
  906. $where['a.status'] = ['in',[0,1]];
  907. }
  908. $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,
  909. u.age,u.constellation,u.hobby_ids,u.profession,u.declaration,u.lng,u.lat";
  910. $list = \app\common\model\Eyemargin::alias("a")
  911. ->field($field)
  912. ->join("user u","a.user_id = u.id")
  913. ->where($where)
  914. ->limit($pageStart,$pageNum)
  915. ->order("a.createtime desc")
  916. ->select();
  917. if($list) foreach($list as $k => $v) {
  918. // 计算距离
  919. $list[$k]['distance'] = \app\common\model\Eyemargin::getDistance($v['lng'],$v['lat'],$this->auth->lng,$this->auth->lat);
  920. $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']);
  921. $list[$k]['right_info'] = \app\common\model\Eyemargin::getIsView($v['user_id'],$user_id);
  922. $v['cover'] || $list[$k]['cover'] = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].'/assets/img/video_cover.jpeg';
  923. $v['hobby_ids'] || $list[$k]['hobby_ids'] = [];
  924. $v['profession'] || $list[$k]['profession'] = '';
  925. $v['music'] || $list[$k]['music'] = '';
  926. $v['video'] || $list[$k]['video'] = '';
  927. }
  928. $this->success("获取成功!",$list);
  929. }
  930. /**
  931. * 获取我的个人基本信息
  932. */
  933. public function getMyBaseInfo() {
  934. $user_id = $this->auth->id;
  935. // redis
  936. // $redis = new Redis();
  937. // $redisconfig = config("redis");
  938. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  939. // $userInfo = decodeArray($redis->hGetAll('userInfo_'.$user_id));
  940. // if(!$userInfo){
  941. // 获取用户信息
  942. $field = 'id,avatar,nickname,gender,age,city_name,district_name,constellation,hobby_ids,profession,declaration,wechat,income';
  943. $userInfo = \app\common\model\User::field($field)->where(['id'=>$user_id])->find();
  944. $userInfo['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($userInfo['hobby_ids']);
  945. // 获取已有标签以及数量
  946. $userInfo['tagUser'] = \app\common\model\TagUser::alias('a')
  947. ->field('a.id,t.name,a.number')
  948. ->join('hx_tag t','t.id = a.tag_id','left')
  949. ->where(['a.user_id'=>$user_id])
  950. ->select();
  951. $userInfo = $userInfo->toArray();
  952. // $redis->hMSet('userInfo_'.$user_id,encodeArray($userInfo));
  953. // }
  954. $this->success("获取成功!",$userInfo);
  955. }
  956. /**
  957. * 为用户添加标签
  958. */
  959. public function setUserTag() {
  960. $tag_id = $this->request->request('tag_id',0); // 标签ID
  961. $fate_user_id = $this->request->request('fate_user_id',0); // 有眼缘用户ID
  962. if(!$tag_id || !$fate_user_id) {
  963. $this->error('参数缺失!');
  964. }
  965. if(!\app\common\model\Tag::where(['id'=>$tag_id])->find()) {
  966. $this->error('标签不存在!');
  967. }
  968. $user_id = $this->auth->id;
  969. $is_fate = \app\common\model\User::getIsView($fate_user_id,$user_id);
  970. if(!$is_fate) $this->error('您需要先获取微信号才能添加标签');
  971. $tag_user_log = \app\common\model\TagUserLog::where(['user_id'=>$user_id,'fate_user_id'=>$fate_user_id,'tag_id'=>$tag_id])->find();
  972. if($tag_user_log) {
  973. $this->error('您已经为ta添加过此标签了');
  974. }
  975. Db::startTrans();
  976. try {
  977. // 添加记录
  978. $data = [];
  979. $data['user_id'] = $user_id;
  980. $data['fate_user_id'] = $fate_user_id;
  981. $data['tag_id'] = $tag_id;
  982. $data['createtime'] = time();
  983. $res1 = \app\common\model\TagUserLog::insert($data);
  984. // 修改标签数量
  985. $where = [];
  986. $where['user_id'] = $fate_user_id;
  987. $where['tag_id'] = $tag_id;
  988. $tag_user = \app\common\model\TagUser::where($where)->find();
  989. if($tag_user) {
  990. $tag_user->number = $tag_user->number + 1;
  991. $res2 = $tag_user->save();
  992. } else {
  993. $data = [];
  994. $data['user_id'] = $fate_user_id;
  995. $data['tag_id'] = $tag_id;
  996. $data['number'] = 1;
  997. $data['createtime'] = time();
  998. $res2 = \app\common\model\TagUser::insert($data);
  999. }
  1000. if($res1 && $res2) {
  1001. Db::commit();
  1002. delUserInfo($fate_user_id);
  1003. $this->success("标签添加成功!");
  1004. }
  1005. } catch (PDOException $e) {
  1006. Db::rollback();
  1007. $this->error("修改失败!");
  1008. }
  1009. }
  1010. /**
  1011. * 绑定用户
  1012. */
  1013. public function bindUser() {
  1014. $invite_no = $this->request->request('invite_no'); // 邀请码
  1015. if(!$invite_no) {
  1016. $this->error("请输入邀请码!");
  1017. }
  1018. $user_id = $this->auth->id;
  1019. // 查询邀请码用户信息
  1020. $inviteUserInfo = \app\common\model\User::where(["invite_no"=>$invite_no])->find();
  1021. if(!$inviteUserInfo) $this->error("查询不到该邀请码用户信息!");
  1022. // 判断是否已经绑定过
  1023. $my_pre_user_id = \app\common\model\User::where(["id"=>$user_id])->value("pre_user_id");
  1024. if($my_pre_user_id > 0) {
  1025. $this->error(__('您已绑定过,不可重复绑定!'));
  1026. }
  1027. if($user_id == $inviteUserInfo->id) {
  1028. $this->error(__('不能绑定自己哦?'));
  1029. }
  1030. // 判断当前用户是否实名认证
  1031. $userAuthInfo = \app\common\model\UserAuth::userIsAuth($this->auth->id);
  1032. if($userAuthInfo['status'] == 0) $this->error($userAuthInfo['msg']);
  1033. $res = \app\common\model\User::update(["pre_user_id"=>$inviteUserInfo->id,'invite_time'=>time()],["id"=>$user_id]);
  1034. if($res) {
  1035. $this->success("恭喜,绑定成功!");
  1036. } else {
  1037. $this->error("网络繁忙,请稍后重试!");
  1038. }
  1039. }
  1040. /**
  1041. * 添加银行卡
  1042. */
  1043. public function addBank() {
  1044. $user_name = $this->request->request('user_name'); //真实姓名
  1045. $bank_name= $this->request->request('bank_name'); //银行名称
  1046. $bank_no = $this->request->request('bank_no'); //银行卡号
  1047. if (!$user_name || !$bank_name || !$bank_no) {
  1048. $this->error(__('Invalid parameters'));
  1049. }
  1050. $bankModel = new \app\common\model\UserBank();
  1051. $where = [];
  1052. $where["user_id"] = $this->auth->id;
  1053. $where["bank_no"] = $bank_no;
  1054. $bankInfo = $bankModel->where($where)->find();
  1055. if($bankInfo) {
  1056. $this->error('该银行卡已经添加过了!');
  1057. }
  1058. $data = [];
  1059. $data["user_id"] = $this->auth->id;
  1060. $data["user_name"] = $user_name;
  1061. $data["bank_name"] = $bank_name;
  1062. $data["bank_no"] = $bank_no;
  1063. $data["createtime"] = time();
  1064. $id = $bankModel->insertGetId($data);
  1065. if($id > 0) {
  1066. $this->success('添加成功!');
  1067. } else {
  1068. $this->error('添加失败!');
  1069. }
  1070. }
  1071. /**
  1072. * 获取银行卡信息
  1073. */
  1074. public function bankInfo() {
  1075. $bankModel = new \app\common\model\UserBank();
  1076. $where = [];
  1077. $where["user_id"] = $this->auth->id;
  1078. $bankInfo = $bankModel->where($where)->find();
  1079. $bankInfo['bank_no'] && $bankInfo['bank_no'] = substr_replace($bankInfo['bank_no'],'********','0','8');
  1080. $this->success('获取成功!',$bankInfo);
  1081. }
  1082. /**
  1083. * 删除银行卡
  1084. */
  1085. public function bankDel() {
  1086. $bankModel = new \app\common\model\UserBank();
  1087. $where = [];
  1088. $where["user_id"] = $this->auth->id;
  1089. $bankInfo = $bankModel->where($where)->delete();
  1090. if($bankInfo) {
  1091. $this->success('删除成功!');
  1092. } else {
  1093. $this->error('删除失败!');
  1094. }
  1095. }
  1096. /**
  1097. * 获取会员开通配置信息
  1098. */
  1099. public function getVipConfig() {
  1100. $res = [];
  1101. $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();
  1102. $res['user_info']['vip_duetime'] = $res['user_info']['vip_duetime']?date('Y-m-d',$res['user_info']['vip_duetime']):"";
  1103. $res['vip_config'] = \app\admin\model\vip\Config::order("weight","desc")->select();
  1104. $this->success("获取成功!",$res);
  1105. }
  1106. /**
  1107. * 设置首页推荐
  1108. */
  1109. public function setEyemaginToMain() {
  1110. $fate_id = $this->request->request('fate_id'); //动态ID
  1111. if (!$fate_id) {
  1112. $this->error(__('Invalid parameters'));
  1113. }
  1114. $user_id = $this->auth->id;
  1115. $fateInfo = \app\common\model\Eyemargin::get($fate_id);
  1116. if($fateInfo->user_id != $user_id) $this->error('抱歉,您无权限操作!');
  1117. // 判断动态是否在审核中
  1118. if($fateInfo->status != 1) $this->error('当前动态状态不允许设置为推荐!');
  1119. Db::startTrans();
  1120. try {
  1121. // 先取消掉所有的推荐
  1122. $res1 = \app\common\model\Eyemargin::update(['is_main'=>0],['user_id'=>$user_id]);
  1123. $res2 = \app\common\model\Eyemargin::update(['is_main'=>1],['id'=>$fate_id]);
  1124. if($res1 && $res2) {
  1125. Db::commit();
  1126. $this->success("设置成功!");
  1127. }
  1128. } catch (PDOException $e) {
  1129. Db::rollback();
  1130. $this->error("设置失败!");
  1131. }
  1132. }
  1133. /**
  1134. * 删除动态
  1135. */
  1136. public function delEyemagin() {
  1137. $fate_id = $this->request->request('fate_id'); //动态ID
  1138. if (!$fate_id) {
  1139. $this->error(__('Invalid parameters'));
  1140. }
  1141. $user_id = $this->auth->id;
  1142. $fateInfo = \app\common\model\Eyemargin::get($fate_id);
  1143. if($fateInfo->user_id != $user_id) $this->error('抱歉,您无权限操作!');
  1144. $res = \app\common\model\Eyemargin::where(['id'=>$fate_id])->delete();
  1145. if($res) {
  1146. $this->success("删除成功!");
  1147. } else {
  1148. $this->error("删除失败!");
  1149. }
  1150. }
  1151. /**
  1152. * 获取第一条系统消息
  1153. */
  1154. public function getFirstSysMsg() {
  1155. $user_id = $this->auth->id;
  1156. $res = [];
  1157. $res['msg_content'] = \app\common\model\SysMsg::where(['user_id'=>$user_id])->order('createtime','desc')->value('title');
  1158. $res['msg_count'] = \app\common\model\SysMsg::where(['user_id'=>$user_id,'is_read'=>0])->count();
  1159. $this->success("获取成功!",$res);
  1160. }
  1161. /**
  1162. * 获取系统消息列表
  1163. */
  1164. public function getSysMsg() {
  1165. $page = $this->request->request('page',1); // 分页
  1166. $pageNum = $this->request->request('pageNum',10); // 分页
  1167. // 分页搜索构建
  1168. $pageStart = ($page-1)*$pageNum;
  1169. $user_id = $this->auth->id;
  1170. $sysMsgList = \app\common\model\SysMsg::where(['user_id'=>$user_id])->order('createtime','desc')->limit($pageStart,$pageNum)->select();
  1171. if($sysMsgList) {
  1172. // 标记所有消息已读1
  1173. \app\common\model\SysMsg::update(['is_read'=>1],['user_id'=>$user_id]);
  1174. }
  1175. $this->success("获取成功!",$sysMsgList);
  1176. }
  1177. /**
  1178. * 获取实名认证信息
  1179. */
  1180. public function getAuthInfo() {
  1181. $user_id = $this->auth->id;
  1182. // 判断当前用户是否实名认证
  1183. $userAuthInfo = \app\common\model\UserAuth::where(["user_id"=>$user_id])->find();
  1184. $res = [];
  1185. $res['status'] = 2;
  1186. $res['msg'] = "已实名!";
  1187. $res['data'] = $userAuthInfo;
  1188. if($userAuthInfo) {
  1189. if($userAuthInfo->status == 0) {
  1190. $res['status'] = 1;
  1191. $res['msg'] = "审核中!";
  1192. } elseif($userAuthInfo->status == 2) {
  1193. $res['status'] = -1;
  1194. $res['msg'] = "审核未通过!";
  1195. }
  1196. } else {
  1197. $res['status'] = 0;
  1198. $res['msg'] = "请先申请实名认证!";
  1199. $res['data'] = [];
  1200. }
  1201. $res['recharge_auth'] = \app\common\model\User::where(['id'=>$user_id])->value("recharge_auth");
  1202. $this->success("获取成功!",$res);
  1203. }
  1204. }