MyController.php 28 KB

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