MyController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\love\controller;
  12. use app\common\Constant;
  13. use app\common\Fun;
  14. use app\love\model\LotteryPrizeModel;
  15. use app\love\model\QuestionModel;
  16. use app\love\model\UserFavoriteModel;
  17. use app\love\model\UserFriendModel;
  18. use app\love\model\UserGiftModel;
  19. use app\love\model\UserInviteModel;
  20. use app\love\model\UserMarryModel;
  21. use app\love\model\UserMatingModel;
  22. use app\love\model\UserMessageModel;
  23. use app\love\model\UserModel;
  24. use app\love\model\UserOutlookModel;
  25. use app\love\model\UserSelectLogModel;
  26. use app\love\model\UserSelectModel;
  27. use app\love\model\UserVisitModel;
  28. use think\Db;
  29. class MyController extends LoveBaseController
  30. {
  31. private $base_arr = ['nation', 'company', 'job', 'hobby', 'smoke', 'drink'];
  32. /**
  33. * 我的
  34. */
  35. public function index()
  36. {
  37. //用户信息
  38. $user_id = cmf_get_current_user_id();
  39. $this->assign('user_id', $user_id);
  40. $user = UserModel::get($user_id);
  41. unset($user['user_pass']);
  42. $this->assign('user', json_encode($user));
  43. //完善资料
  44. $is_perfect = 'false';
  45. foreach ($this->base_arr as $v) {
  46. if (empty($user[$v])) {
  47. $is_perfect = 'true';
  48. break;
  49. }
  50. }
  51. $this->assign('is_perfect', $is_perfect);
  52. //择偶
  53. // $mating = UserMatingModel::get(['user_id' => cmf_get_current_user_id()]);
  54. $is_mating = 'false';
  55. /*foreach ($this->mating_arr as $v) {
  56. if (empty($mating[$v])) {
  57. $is_mating = 'true';
  58. break;
  59. }
  60. }*/
  61. $this->assign('is_mating', $is_mating);
  62. //未读消息数
  63. $unread_num = UserFriendModel::where('user_id', $user_id)->sum('unread_num');
  64. $invite_num = UserInviteModel::where('to_id', $user_id)
  65. ->where('status', 1)
  66. ->count();
  67. $this->assign('unread_num', $unread_num + $invite_num);
  68. //我的访客
  69. $visit_where = [
  70. ['to_id', '=', $user_id],
  71. ['status', '=', 2],
  72. ];
  73. $visit_count = UserVisitModel::where($visit_where)->count();
  74. $this->assign('visit_count', $visit_count ?: '');
  75. //收到的礼物
  76. $gift_count = UserGiftModel::where($visit_where)->count();
  77. $this->assign('gift_count', $gift_count ?: '');
  78. //收到的选择
  79. $select_count = UserSelectModel::where([
  80. ['uid', '=', $user_id],
  81. ['status', '=', 2],
  82. ])->count();
  83. $this->assign('select_count', $select_count ?: '');
  84. //是否互选
  85. $select_log = 'false';
  86. if ($this->user['sex'] == 1 && $this->user['use_ticket'] == 2) {
  87. $log_check = UserSelectLogModel::where('user_id1', $this->user['id'])->find();
  88. if (!empty($log_check)) {
  89. $select_log = 'true';
  90. }
  91. }
  92. $this->assign('select_log', $select_log);
  93. return $this->fetch();
  94. }
  95. /**
  96. * 审核
  97. */
  98. public function check()
  99. {
  100. //用户信息
  101. $user = UserModel::get(cmf_get_current_user_id());
  102. unset($user['user_pass']);
  103. $this->assign('user', json_encode($user));
  104. return $this->fetch();
  105. }
  106. /**
  107. * 个人资料
  108. */
  109. public function profile()
  110. {
  111. //用户信息
  112. $user = UserModel::get(cmf_get_current_user_id());
  113. $user['birthday'] = date('Y-m-d', $user['birthday']);
  114. unset($user['user_pass']);
  115. $this->assign('user', json_encode($user));
  116. $data = [
  117. 'marry' => Constant::MARRY,
  118. 'high' => Constant::HIGH,
  119. 'weight' => Constant::WEIGHT,
  120. 'education' => Constant::EDUCATION,
  121. 'income' => Constant::INCOME,
  122. 'nation' => Constant::NATION,
  123. 'smoke' => Constant::SMOKE,
  124. 'drink' => Constant::DRINK,
  125. 'family' => Constant::FAMILY,
  126. 'tinyint' => Constant::TINYINT,
  127. ];
  128. foreach ($data as &$v) {
  129. $v = json_encode($v);
  130. unset($v);
  131. }
  132. return $this->fetch('', $data);
  133. }
  134. /**
  135. * 个人资料提交
  136. */
  137. public function profilePost()
  138. {
  139. $param = $this->request->post();
  140. $param['user_nickname'] = $param['realname'];
  141. $param['birthday'] = strtotime($param['birthday']);
  142. UserModel::update($param, ['id' => cmf_get_current_user_id()]);
  143. $this->success('操作成功');
  144. }
  145. /**
  146. * 择偶要求
  147. */
  148. public function cond()
  149. {
  150. $mating = UserMatingModel::get(['user_id' => cmf_get_current_user_id()]);
  151. $this->assign('mating', $mating);
  152. $education = Constant::EDUCATION;
  153. $education[] = '无要求';
  154. $data = [
  155. 'min_age' => Constant::MIN_AGE,
  156. 'max_age' => Constant::MAX_AGE,
  157. 'min_high' => Constant::MIN_HIGH,
  158. 'max_high' => Constant::MAX_HIGH,
  159. 'min_weight' => Constant::MIN_WEIGHT,
  160. 'max_weight' => Constant::MAX_WEIGHT,
  161. 'native' => Constant::NATIVE,
  162. 'education' => $education,
  163. 'income' => Constant::COND_INCOME,
  164. 'smoke' => Constant::SMOKE,
  165. 'drink' => Constant::DRINK,
  166. 'tinyint' => Constant::COND_TINYINT,
  167. ];
  168. foreach ($data as &$v) {
  169. $v = json_encode($v);
  170. unset($v);
  171. }
  172. return $this->fetch('', $data);
  173. }
  174. /**
  175. * 择偶要求提交
  176. */
  177. public function condPost()
  178. {
  179. $param = $this->request->post();
  180. UserMatingModel::update($param, ['user_id' => cmf_get_current_user_id()]);
  181. $this->success('操作成功');
  182. }
  183. /**
  184. * 自我介绍
  185. */
  186. public function intro()
  187. {
  188. $user = UserModel::field('main_image,main_image_thumb,signature')->where('id', cmf_get_current_user_id())->find();
  189. $this->assign('user', json_encode($user));
  190. return $this->fetch();
  191. }
  192. /**
  193. * 自我介绍提交
  194. */
  195. public function introPost()
  196. {
  197. $param = $this->request->post();
  198. $param['avatar'] = $param['main_image_thumb'];
  199. UserModel::update($param, ['id' => cmf_get_current_user_id()], ['avatar', 'main_image', 'main_image_thumb', 'signature']);
  200. $this->success('操作成功');
  201. }
  202. /**
  203. * 相册
  204. */
  205. public function photo()
  206. {
  207. $user = UserModel::get(cmf_get_current_user_id());
  208. if (empty($user['more'])) {
  209. $user['more'] = [];
  210. }
  211. $more = array_filter($user['more']);
  212. $this->assign('more', json_encode($more));
  213. return $this->fetch();
  214. }
  215. /**
  216. * 相册提交
  217. */
  218. public function photoPost()
  219. {
  220. $param = $this->request->post();
  221. if (empty($param)) {
  222. $param = ['more' => ''];
  223. }
  224. UserModel::update($param, ['id' => cmf_get_current_user_id()], 'more');
  225. $this->success('操作成功');
  226. }
  227. /**
  228. * 我的访客
  229. */
  230. public function visitme()
  231. {
  232. //清除未读
  233. UserVisitModel::update(['status' => 1], ['to_id' => cmf_get_current_user_id(), 'status' => 2]);
  234. $list = UserVisitModel::with('to_user')
  235. ->where('from_id', '=', cmf_get_current_user_id())
  236. ->limit(10)
  237. ->order('last_visit_time desc')
  238. ->select();
  239. foreach ($list as $v) {
  240. $v['user'] = $v['to_user'];
  241. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  242. $v['last_visit_time'] = date('m-d H:i', $v['last_visit_time']);
  243. }
  244. $this->assign('list1', $list);
  245. $list = UserVisitModel::with('from_user')
  246. ->where('to_id', '=', cmf_get_current_user_id())
  247. ->limit(10)
  248. ->order('last_visit_time desc')
  249. ->select();
  250. foreach ($list as $v) {
  251. $v['user'] = $v['from_user'];
  252. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  253. $v['last_visit_time'] = date('m-d H:i', $v['last_visit_time']);
  254. }
  255. $this->assign('list2', $list);
  256. return $this->fetch();
  257. }
  258. /**
  259. * 我的访客列表
  260. */
  261. public function visitmeList()
  262. {
  263. $param = $this->request->post();
  264. $user_id = cmf_get_current_user_id();
  265. $where = [];
  266. $with = 'to_user';
  267. if ($param['status'] == 0) {
  268. $where[] = ['from_id', '=', $user_id];
  269. } elseif ($param['status'] == 1) {
  270. $where[] = ['to_id', '=', $user_id];
  271. $with = 'from_user';
  272. }
  273. $list = UserVisitModel::with($with)
  274. ->where($where)
  275. ->page($param['page'])
  276. ->limit(10)
  277. ->order('last_visit_time desc')
  278. ->select();
  279. foreach ($list as $v) {
  280. if ($param['status'] == 0) {
  281. $v['user'] = $v['to_user'];
  282. } elseif ($param['status'] == 1) {
  283. $v['user'] = $v['from_user'];
  284. }
  285. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  286. $v['last_visit_time'] = date('m-d H:i', $v['last_visit_time']);
  287. }
  288. $this->result($list, 1);
  289. }
  290. /**
  291. * 我的收藏
  292. */
  293. public function star()
  294. {
  295. $list = UserFavoriteModel::with('to_user')
  296. ->where('user_id', cmf_get_current_user_id())
  297. ->limit(10)
  298. ->order('create_time desc')
  299. ->select();
  300. foreach ($list as $v) {
  301. $v['to_user']['age'] = empty($v['to_user']['birthday']) ? 0 : date('Y') - date('Y', $v['to_user']['birthday']);
  302. $v['create_time'] = date('m-d H:i', $v['create_time']);
  303. }
  304. $this->assign('list', $list);
  305. return $this->fetch();
  306. }
  307. /**
  308. * 我的收藏列表
  309. */
  310. public function starList()
  311. {
  312. $param = $this->request->post();
  313. $list = UserFavoriteModel::with('to_user')
  314. ->where('user_id', cmf_get_current_user_id())
  315. ->page($param['page'])
  316. ->limit(10)
  317. ->order('create_time desc')
  318. ->select();
  319. foreach ($list as $v) {
  320. $v['to_user']['age'] = empty($v['to_user']['birthday']) ? 0 : date('Y') - date('Y', $v['to_user']['birthday']);
  321. $v['create_time'] = date('m-d H:i', $v['create_time']);
  322. }
  323. $this->result($list, 1);
  324. }
  325. /**
  326. * 我的互选
  327. */
  328. public function select()
  329. {
  330. //清除未读
  331. $user_id = cmf_get_current_user_id();
  332. UserSelectModel::update(['status' => 1], ['uid' => $user_id, 'status' => 2]);
  333. $select = UserSelectModel::where('user_id|uid', '=', $user_id)
  334. ->where('is_confirm', 1)
  335. ->find();
  336. //奖品
  337. $has_prize = LotteryPrizeModel::all();
  338. $this->assign('has_prize', $has_prize->isEmpty() ? 'false' : 'true');
  339. if (!empty($select)) {
  340. //已选择
  341. if ($select['user_id'] == $user_id) {
  342. $uid = $select['uid'];
  343. } else {
  344. $uid = $select['user_id'];
  345. }
  346. $user = UserModel::get($uid);
  347. $user['age'] = Fun::getAgeByBirth($user['birthday']);
  348. $this->assign('user', $user);
  349. $this->assign('list1', '[]');
  350. $this->assign('list2', '[]');
  351. $this->assign('is_select', 'true');
  352. } else {
  353. //未选择
  354. $this->assign('user', '[]');
  355. $list = UserSelectModel::with('to_user')
  356. ->where('uid', '=', cmf_get_current_user_id())
  357. ->limit(10)
  358. ->order('create_time desc')
  359. ->select();
  360. foreach ($list as $v) {
  361. $v['user'] = $v['to_user'];
  362. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  363. $v['create_time'] = date('m-d H:i', $v['create_time']);
  364. }
  365. $this->assign('list1', $list);
  366. $list = UserSelectModel::with('from_user')
  367. ->where('user_id', '=', cmf_get_current_user_id())
  368. ->limit(10)
  369. ->order('create_time desc')
  370. ->select();
  371. foreach ($list as $v) {
  372. $v['user'] = $v['from_user'];
  373. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  374. $v['create_time'] = date('m-d H:i', $v['create_time']);
  375. }
  376. $this->assign('list2', $list);
  377. $this->assign('is_select', 'false');
  378. }
  379. return $this->fetch();
  380. }
  381. /**
  382. * 我的访客列表
  383. */
  384. public function selectList()
  385. {
  386. $param = $this->request->post();
  387. $user_id = cmf_get_current_user_id();
  388. $where = [];
  389. $with = 'to_user';
  390. if ($param['status'] == 0) {
  391. $where[] = ['user_id', '=', $user_id];
  392. } elseif ($param['status'] == 1) {
  393. $where[] = ['uid', '=', $user_id];
  394. $with = 'from_user';
  395. }
  396. $list = UserSelectModel::with($with)
  397. ->where($where)
  398. ->page($param['page'])
  399. ->limit(10)
  400. ->order('create_time desc')
  401. ->select();
  402. foreach ($list as $v) {
  403. if ($param['status'] == 0) {
  404. $v['user'] = $v['to_user'];
  405. } elseif ($param['status'] == 1) {
  406. $v['user'] = $v['from_user'];
  407. }
  408. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  409. $v['create_time'] = date('m-d H:i', $v['create_time']);
  410. }
  411. $this->result($list, 1);
  412. }
  413. /**
  414. * 取消选择
  415. */
  416. public function selectCancel()
  417. {
  418. $id = $this->request->post('id');
  419. $user_id = cmf_get_current_user_id();
  420. $user = UserSelectModel::get(['user_id' => $user_id, 'uid' => $id]);
  421. //20230423增加两个userid对调的查询,因为双方都有权利取消
  422. if (empty($user)) {
  423. $user = UserSelectModel::get(['user_id' => $id, 'uid' => $user_id]);
  424. }
  425. if (!empty($user)) {
  426. UserSelectLogModel::where('user_id1|user_id2', $id)->update(['delete_time' => time()]);
  427. $user->delete();
  428. }
  429. $this->success('操作成功');
  430. }
  431. /**
  432. * 确认选择
  433. */
  434. public function selectConfirm()
  435. {
  436. $id = $this->request->post('id');
  437. $user_id = cmf_get_current_user_id();
  438. if ($user_id == $id) {
  439. $this->error('不可以选择自己!');
  440. }
  441. $user = UserSelectModel::get(['user_id' => $id, 'uid' => $user_id]);
  442. $check = UserSelectLogModel::where('user_id1|user_id2', $id)->find();
  443. if (!empty($check)) {
  444. $this->error('对方已被选择!');
  445. }
  446. if (empty($user)) {
  447. $this->error('该数据不存在!');
  448. }
  449. $user->is_confirm = 1;
  450. $user->confirm_time = time();
  451. $user->save();
  452. //增加互选记录
  453. $log = ['confirm_time' => time()];
  454. if ($this->user['sex'] == 1) {
  455. $log['user_id1'] = $user_id;
  456. $log['user_id2'] = $id;
  457. } else {
  458. $log['user_id1'] = $id;
  459. $log['user_id2'] = $user_id;
  460. }
  461. UserSelectLogModel::create($log);
  462. //查看是否为好友
  463. $friend = UserFriendModel::where([
  464. ['user_id', '=', $user_id],
  465. ['friend_id', '=', $id],
  466. ])->find();
  467. if (empty($friend)) {
  468. //查看是否发起过聊天申请
  469. $apply = UserInviteModel::where(function ($query) use ($user_id, $id) {
  470. $query->where([
  471. ['from_id', '=', $user_id],
  472. ['to_id', '=', $id],
  473. ]);
  474. })->whereOr(function ($query) use ($user_id, $id) {
  475. $query->where([
  476. ['to_id', '=', $user_id],
  477. ['from_id', '=', $id],
  478. ]);
  479. })->find();
  480. if (!empty($apply)) {
  481. $apply->status = 2;
  482. }
  483. //添加好友
  484. UserFriendModel::create([
  485. 'user_id' => $id,
  486. 'friend_id' => $user_id,
  487. 'last_msg_time' => time(),
  488. ]);
  489. UserFriendModel::create([
  490. 'user_id' => $user_id,
  491. 'friend_id' => $id,
  492. 'last_msg_time' => time(),
  493. ]);
  494. }
  495. //增加互选次数
  496. Db::name('config')->where('id', 1)->setInc('select_num');
  497. //发送提示
  498. $form_id = $this->user->sex = 1 ? $user_id : $id;
  499. $to_id = $this->user->sex = 1 ? $id : $user_id;
  500. $time = time();
  501. UserMessageModel::create([
  502. 'from_id' => $form_id,
  503. 'to_id' => $to_id,
  504. 'message' => '【系统消息】系统赠送了两张咖啡券,约个时间一起去喝咖啡吧。',
  505. 'create_time' => $time,
  506. ]);
  507. //增加最后聊天消息
  508. UserFriendModel::update(['last_msg' => '我:【系统消息】系统赠送了两张咖啡券,约个时间一起去喝咖啡吧。', 'last_msg_time' => $time], ['user_id' => $form_id, 'friend_id' => $to_id]);
  509. $friend = UserFriendModel::get(['user_id' => $to_id, 'friend_id' => $form_id]);
  510. $friend->last_msg = '对方:【系统消息】系统赠送了两张咖啡券,约个时间一起去喝咖啡吧。';
  511. $friend->last_msg_time = $time;
  512. $friend->unread_num++;
  513. $friend->save();
  514. $other = UserModel::get($id);
  515. $this->success('操作成功', '', ['mobile' => $other['mobile']]);
  516. }
  517. /**
  518. * 结婚
  519. */
  520. public function marry()
  521. {
  522. $user_id = cmf_get_current_user_id();
  523. $log = UserSelectLogModel::where('user_id1|user_id2', $user_id)->find();
  524. if (empty($log)) {
  525. $this->error('请先确认关系后再结婚!');
  526. }
  527. $id = $log['user_id1'] == $user_id ? $log['user_id2'] : $log['user_id1'];
  528. $marry = UserMarryModel::where(function ($query) use ($user_id, $id) {
  529. $query->where([
  530. ['user_id1', '=', $user_id],
  531. ['user_id2', '=', $id],
  532. ]);
  533. })->whereOr(function ($query) use ($user_id, $id) {
  534. $query->where([
  535. ['user_id2', '=', $user_id],
  536. ['user_id1', '=', $id],
  537. ]);
  538. })->find();
  539. //20230423增加两个userid对调的查询,因为双方都有权利取消
  540. if (empty($marry)) {
  541. UserMarryModel::create(['user_id1' => $user_id, 'user_id2' => $id, 'create_time' => time()]);
  542. UserModel::where('id', 'in', [$id, $user_id])->update(['is_marry' => 1]);
  543. }
  544. //增加结婚次数
  545. Db::name('config')->where('id', 1)->setInc('marry_num');
  546. $this->success('操作成功');
  547. }
  548. /**
  549. * 收到的礼物
  550. */
  551. public function gift()
  552. {
  553. //清除未读
  554. UserGiftModel::update(['status' => 1], ['to_id' => cmf_get_current_user_id(), 'status' => 2]);
  555. $list = UserGiftModel::with('gift,from_user')
  556. ->where('to_id', '=', cmf_get_current_user_id())
  557. ->limit(10)
  558. ->order('create_time desc')
  559. ->select();
  560. foreach ($list as $v) {
  561. $v['user'] = $v['from_user'];
  562. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  563. $v['create_time'] = date('m-d H:i', $v['create_time']);
  564. }
  565. $this->assign('list1', $list);
  566. $list = UserGiftModel::with('gift,to_user')
  567. ->where('from_id', '=', cmf_get_current_user_id())
  568. ->limit(10)
  569. ->order('create_time desc')
  570. ->select();
  571. foreach ($list as $v) {
  572. $v['user'] = $v['to_user'];
  573. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  574. $v['create_time'] = date('m-d H:i', $v['create_time']);
  575. }
  576. $this->assign('list2', $list);
  577. return $this->fetch();
  578. }
  579. /**
  580. * 收到的礼物列表
  581. */
  582. public function giftList()
  583. {
  584. $param = $this->request->post();
  585. $user_id = cmf_get_current_user_id();
  586. $where = [];
  587. $with = 'gift,from_user';
  588. if ($param['status'] == 0) {
  589. $where[] = ['to_id', '=', $user_id];
  590. } elseif ($param['status'] == 1) {
  591. $where[] = ['from_id', '=', $user_id];
  592. $with = 'gift,to_user';
  593. }
  594. $list = UserGiftModel::with($with)
  595. ->where($where)
  596. ->page($param['page'])
  597. ->limit(10)
  598. ->order('create_time desc')
  599. ->select();
  600. foreach ($list as $v) {
  601. if ($param['status'] == 0) {
  602. $v['user'] = $v['from_user'];
  603. } elseif ($param['status'] == 1) {
  604. $v['user'] = $v['to_user'];
  605. }
  606. $v['user']['age'] = empty($v['user']['birthday']) ? 0 : date('Y') - date('Y', $v['user']['birthday']);
  607. $v['create_time'] = date('m-d H:i', $v['create_time']);
  608. }
  609. $this->result($list, 1);
  610. }
  611. /**
  612. * 图片上传
  613. */
  614. public function imageUpload()
  615. {
  616. $file = $this->request->post('file');
  617. $ext = pathinfo($this->request->post('name'))['extension'];
  618. if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
  619. $this->error('文件后缀必须为jpg,jpeg,png');
  620. }
  621. if ($file) {
  622. //创建目录
  623. $upload_dir = WEB_ROOT . 'upload' . '/' . 'image';
  624. if (!is_dir($upload_dir)) {
  625. @mkdir($upload_dir);
  626. }
  627. $upload_dir = $upload_dir . '/' . date('Ymd');
  628. if (!is_dir($upload_dir)) {
  629. @mkdir($upload_dir);
  630. }
  631. //保存文件
  632. $file_name = $this->_file_name($ext);
  633. $file_path = $upload_dir . '/' . $file_name;
  634. $is_upload = $this->_base64_image_content($file, $file_path);
  635. if (!$is_upload) {
  636. $this->error('上传失败,请重新上传');
  637. }
  638. //缩略图
  639. $thumb_file = $this->_file_name($ext);
  640. $image = \think\Image::open($file_path);
  641. $image->thumb(220, 270)->save($upload_dir . '/' . $thumb_file);
  642. $main_image = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $file_name);
  643. $main_image_thumb = cmf_get_user_avatar_url('image' . '/' . date('Ymd') . '/' . $thumb_file);
  644. $this->result([
  645. 'main_image' => cmf_get_user_avatar_url($main_image),
  646. 'main_image_thumb' => cmf_get_user_avatar_url($main_image_thumb),
  647. ], 1);
  648. } else {
  649. $this->error('请上传文件');
  650. }
  651. }
  652. /**
  653. * 用户头像上传
  654. */
  655. public function avatarUpload()
  656. {
  657. $file = $this->request->post('file');
  658. $ext = pathinfo($this->request->post('name'))['extension'];
  659. if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
  660. $this->error('文件后缀必须为jpg,jpeg,png');
  661. }
  662. if ($file) {
  663. //创建目录
  664. $upload_dir = WEB_ROOT . 'upload' . '/' . 'avatar';
  665. if (!is_dir($upload_dir)) {
  666. @mkdir($upload_dir);
  667. }
  668. $upload_dir = $upload_dir . '/' . date('Ymd');
  669. if (!is_dir($upload_dir)) {
  670. @mkdir($upload_dir);
  671. }
  672. //保存文件
  673. $file_name = $this->_file_name($ext);
  674. $is_upload = $this->_base64_image_content($file, $upload_dir . '/' . $file_name);
  675. if (!$is_upload) {
  676. $this->error('上传失败,请重新上传');
  677. }
  678. //更新头像
  679. $avatar = cmf_get_user_avatar_url('avatar' . '/' . date('Ymd') . '/' . $file_name);
  680. UserModel::update(['avatar' => $avatar], ['id' => cmf_get_current_user_id()]);
  681. $this->result(cmf_get_user_avatar_url($avatar), 1);
  682. } else {
  683. $this->error('请上传文件');
  684. }
  685. }
  686. /**
  687. * 用户相册上传
  688. */
  689. public function photoUpload()
  690. {
  691. $file = $this->request->post('file');
  692. $ext = pathinfo($this->request->post('name'))['extension'];
  693. if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
  694. $this->error('文件后缀必须为jpg,jpeg,png');
  695. }
  696. if ($file) {
  697. //创建目录
  698. $upload_dir = WEB_ROOT . 'upload' . '/' . 'photo';
  699. if (!is_dir($upload_dir)) {
  700. @mkdir($upload_dir);
  701. }
  702. $upload_dir = $upload_dir . '/' . date('Ymd');
  703. if (!is_dir($upload_dir)) {
  704. @mkdir($upload_dir);
  705. }
  706. //保存文件
  707. $file_name = $this->_file_name($ext);
  708. $is_upload = $this->_base64_image_content($file, $upload_dir . '/' . $file_name);
  709. if (!$is_upload) {
  710. $this->error('上传失败,请重新上传');
  711. }
  712. $photo = cmf_get_user_avatar_url('photo' . '/' . date('Ymd') . '/' . $file_name);
  713. $this->result(cmf_get_user_avatar_url($photo), 1);
  714. } else {
  715. $this->error('请上传文件');
  716. }
  717. }
  718. /**
  719. * 咖啡券
  720. */
  721. public function ticket()
  722. {
  723. //是否互选
  724. if ($this->user['sex'] == 2) {
  725. $this->error('暂无咖啡券', url('index'));
  726. }
  727. $log_check = UserSelectLogModel::where('user_id1', $this->user['id'])->find();
  728. if (empty($log_check)) {
  729. $this->error('暂无咖啡券', url('index'));
  730. }
  731. return $this->fetch();
  732. }
  733. public function useTicket()
  734. {
  735. $this->user->use_ticket = 1;
  736. $this->user->save();
  737. $this->success('核销成功!');
  738. }
  739. public function outlook()
  740. {
  741. $question = QuestionModel::all();
  742. $question_list = [];
  743. foreach ($question as $k => $v) {
  744. $question_list[$k]['title'] = $v['title'];
  745. $question_list[$k]['option'] = [$v['option1'], $v['option2'], $v['option3'], $v['option4']];
  746. }
  747. $this->assign('question', $question_list);
  748. $this->assign('option_title', ['A', 'B', 'C', 'D']);
  749. return $this->fetch();
  750. }
  751. public function postOutlook()
  752. {
  753. $data = $this->request->post();
  754. $user_id = $this->user['id'];
  755. $outlook = UserOutlookModel::where('user_id', $user_id)->find();
  756. if (empty($outlook)) {
  757. $data['user_id'] = $user_id;
  758. $data['create_time'] = time();
  759. UserOutlookModel::create($data);
  760. } else {
  761. foreach ($data as $k => $v) {
  762. $outlook->$k = $v;
  763. }
  764. $outlook->create_time = time();
  765. $outlook->save();
  766. }
  767. $this->success();
  768. }
  769. private function _file_name($ext)
  770. {
  771. //生成随机文件名
  772. //定义一个包含大小写字母数字的字符串
  773. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  774. //把字符串分割成数组
  775. $newchars = str_split($chars);
  776. //打乱数组
  777. shuffle($newchars);
  778. //从数组中随机取出15个字符
  779. $chars_key = array_rand($newchars, 15);
  780. //把取出的字符重新组成字符串
  781. $fnstr = '';
  782. for ($i = 0; $i < 15; $i++) {
  783. $fnstr .= $newchars[$chars_key[$i]];
  784. }
  785. //输出文件名并做MD5加密
  786. return md5($fnstr . microtime(true) * 1000) . '.' . $ext;
  787. }
  788. private function _base64_image_content($base64_image_content, $file_path)
  789. {
  790. //匹配出图片的格式
  791. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  792. if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  793. return true;
  794. } else {
  795. return false;
  796. }
  797. } else {
  798. return false;
  799. }
  800. }
  801. }