User.php 62 KB

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