UserController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\web\controller;
  12. use service\UserService;
  13. use think\Db;
  14. use app\common\plugin\Jwt;
  15. //ar类
  16. class UserController extends MainController
  17. {
  18. public $pages = 20;
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. // echo '这是永固构造';
  23. }
  24. public function userInfo()
  25. {
  26. if (empty($this->userid))
  27. $this->api_return_json(array('msg' => '登录过期'), 0);
  28. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  29. if (empty($user))
  30. $this->api_return_json(array('msg' => '用户数据异常'));
  31. $user = json_decode($user, true);
  32. $userInfo = array();
  33. foreach ($user as $val) {
  34. if ($this->userid == $val['user_id']) {
  35. $userInfo = $val;
  36. break;
  37. }
  38. }
  39. if (empty($userInfo))
  40. $this->api_return_json(array('msg' => '用户信息异常'));
  41. $tem = array('dianliu0' => 0.1, 'dianliu1' => 10, 'wendu0' => 50, 'wendu1' => 100);
  42. if (!file_exists(CMF_ROOT . 'public/userConfig.json'))
  43. file_put_contents(CMF_ROOT . 'public/userConfig.json', json_encode($tem), LOCK_EX);
  44. $userConfig = file_get_contents(CMF_ROOT . 'public/userConfig.json');
  45. $userConfig = empty($userConfig) ? array() : json_decode($userConfig, true);
  46. $this->api_return_json(array('userInfo' => $userInfo, 'userConfig' => $userConfig), 1);
  47. }
  48. public function editUser()
  49. {
  50. if (empty($this->userid))
  51. $this->api_return_json(array('msg' => '登录过期'), 0);
  52. $phone = $this->request->param('phone', '');
  53. $nickname = $this->request->param('nickname', '');
  54. if (empty($nickname))
  55. $this->api_return_json(array('msg' => '昵称不能为空'));
  56. if (!isset($phone) || empty($phone) || !preg_match('/^1[345789]\d{9}$/', $phone)) {
  57. $this->api_return_json(array('msg' => '手机号不合法'));
  58. }
  59. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  60. if (empty($user))
  61. $this->api_return_json(array('msg' => '用户数据异常'));
  62. $user = json_decode($user, true);
  63. foreach ($user as &$val) {
  64. if ($this->userid == $val['user_id']) {
  65. $val['phone'] = $phone;
  66. $val['nickname'] = $nickname;
  67. break;
  68. }
  69. }
  70. file_put_contents(CMF_ROOT . 'public/user.json', json_encode($user), LOCK_EX);
  71. $this->api_return_json(array('msg' => '修改成功'), 1);
  72. }
  73. public function uploadAvatar()
  74. {
  75. if (empty($this->userid))
  76. $this->api_return_json(array('msg' => '登录过期'), 0);
  77. $type = $this->request->param('type');
  78. $file = $this->request->file('file');
  79. $info = $file->validate(array('ext' => 'jpg,png'))->move(CMF_ROOT . 'public/upload' . DIRECTORY_SEPARATOR . 'image'); //上传后的文件名
  80. if ($info) {
  81. $upame = $info->getSaveName(); //数据库存储文件名
  82. $path = 'image/' . $upame;
  83. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  84. if (empty($user))
  85. $this->api_return_json(array('msg' => '用户数据异常'));
  86. $user = json_decode($user, true);
  87. foreach ($user as &$val) {
  88. if ($this->userid == $val['user_id']) {
  89. if ($type == 0)
  90. $val['avatar'] = $path;
  91. else
  92. $val['cover'] = $path;
  93. break;
  94. }
  95. }
  96. file_put_contents(CMF_ROOT . 'public/user.json', json_encode($user), LOCK_EX);
  97. $this->api_return_json(array('path' => $path), 1);
  98. } else {
  99. $this->api_return_json(array('msg' => $file->getError()));
  100. }
  101. }
  102. public function login()
  103. {
  104. $username = $this->request->param('username');
  105. $password = $this->request->param('password');
  106. if (empty($username))
  107. $this->api_return_json(array('msg' => '用户不能为空'));
  108. if (empty($password))
  109. $this->api_return_json(array('msg' => '密码不能为空'));
  110. if (!file_exists(CMF_ROOT . 'public/user.json'))
  111. file_put_contents(CMF_ROOT . 'public/user.json', '', LOCK_EX);
  112. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  113. if (empty($user)) {
  114. //注册
  115. $this->api_return_json(array('msg' => '用户不存在'));
  116. $id = 1;
  117. $userData = array(array('user_id' => $id, 'username' => $username, 'password' => $password, 'phone' => '', 'avatar' => '', 'cover' => '', 'nickname' => '', 'add_time' => time(), 'update_time' => time()));
  118. file_put_contents(CMF_ROOT . 'public/user.json', json_encode($userData), LOCK_EX);
  119. } else {
  120. $user = json_decode($user, true);
  121. $jsonUserName = array_column($user, 'username');
  122. if (in_array($username, $jsonUserName)) {
  123. //登录
  124. foreach ($user as &$val) {
  125. if ($username == $val['username']) {
  126. if ($password != $val['password'])
  127. $this->api_return_json(array('msg' => '密码错误'));
  128. $id = $val['user_id'];
  129. }
  130. }
  131. } else {
  132. //注册追加
  133. $this->api_return_json(array('msg' => '用户不存在'));
  134. $id = max(array_column($user, 'user_id')) + 1;
  135. $userData = array('user_id' => $id, 'username' => $username, 'password' => $password, 'phone' => '', 'avatar' => '', 'cover' => '', 'nickname' => '', 'add_time' => time(), 'update_time' => time());
  136. array_push($user, $userData);
  137. file_put_contents(CMF_ROOT . 'public/user.json', json_encode($user), LOCK_EX);
  138. }
  139. }
  140. $token = JWT::getToken($id, $this->major);
  141. $this->api_return_json(array('msg' => '登录成功', 'sid' => $token), 1);
  142. }
  143. public function register()
  144. {
  145. if (empty($this->userid))
  146. $this->api_return_json(array('msg' => '登录过期'), 0);
  147. if ($this->userid != 1)
  148. $this->api_return_json(array('msg' => '您没权限操作'), 0);
  149. $username = $this->request->param('username');
  150. $password = $this->request->param('password');
  151. if (empty($username))
  152. $this->api_return_json(array('msg' => '用户不能为空'));
  153. if (empty($password))
  154. $this->api_return_json(array('msg' => '密码不能为空'));
  155. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  156. $user = json_decode($user, true);
  157. $jsonUserName = array_column($user, 'username');
  158. if (in_array($username, $jsonUserName)) {
  159. //登录
  160. $this->api_return_json(array('msg' => '账号已存在'));
  161. }
  162. $id = max(array_column($user, 'user_id')) + 1;
  163. $userData = array('user_id' => $id, 'username' => $username, 'password' => $password, 'phone' => '', 'avatar' => '', 'cover' => '', 'nickname' => '', 'add_time' => time(), 'update_time' => time());
  164. array_push($user, $userData);
  165. file_put_contents(CMF_ROOT . 'public/user.json', json_encode($user), LOCK_EX);
  166. $this->api_return_json(array('msg' => '账号注册成功'), 1);
  167. }
  168. public function forgetSecret()
  169. {
  170. if (empty($this->userid))
  171. $this->api_return_json(array('msg' => '登录过期'), 0);
  172. $ori_password = $this->request->param('ori_password');
  173. $new_password = $this->request->param('new_password');
  174. $re_password = $this->request->param('re_password');
  175. if ($new_password == "" || strlen($new_password) < 6) {
  176. $this->api_return_json(array('msg' => '请输入不少于6位数的新密码'));
  177. } else if ($re_password == "") {
  178. $this->api_return_json(array('msg' => '请再次输入新密码'));
  179. } else if ($new_password != $re_password) {
  180. $this->api_return_json(array('msg' => '两次密码不一致'));
  181. } else if ($ori_password == "")
  182. $this->api_return_json(array('msg' => '旧密码不能为空'));
  183. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  184. if (empty($user))
  185. $this->api_return_json(array('msg' => '用户数据异常'));
  186. $user = json_decode($user, true);
  187. foreach ($user as &$val) {
  188. if ($this->userid == $val['user_id']) {
  189. if ($val['password'] != $ori_password)
  190. $this->api_return_json(array('msg' => '旧密码不正确'));
  191. $val['password'] = $new_password;
  192. break;
  193. }
  194. }
  195. file_put_contents(CMF_ROOT . 'public/user.json', json_encode($user), LOCK_EX);
  196. $this->api_return_json(array('msg' => '密码修改成功'), 1);
  197. }
  198. public function userConfig()
  199. {
  200. $k = $this->request->param('key');
  201. $v = $this->request->param('value');
  202. if (empty($k) || !is_numeric($v))
  203. $this->api_return_json(array('msg' => '参数有误'), 0);
  204. if (empty($this->userid))
  205. $this->api_return_json(array('msg' => '登录过期'), 0);
  206. if ($this->userid != 1)
  207. $this->api_return_json(array('msg' => '您没有权限操作'), 0);
  208. $tem = array('dianliu0' => 0.1, 'dianliu1' => 10, 'wendu0' => 50, 'wendu1' => 100);
  209. if (!file_exists(CMF_ROOT . 'public/userConfig.json') || empty(file_get_contents(CMF_ROOT . 'public/userConfig.json')))
  210. file_put_contents(CMF_ROOT . 'public/userConfig.json', json_encode($tem), LOCK_EX);
  211. $userConfig = file_get_contents(CMF_ROOT . 'public/userConfig.json');
  212. $userConfig = empty($userConfig) ? array() : json_decode($userConfig, true);
  213. if (array_key_exists($k, $userConfig)) {
  214. $userConfig[$k] = $v;
  215. file_put_contents(CMF_ROOT . 'public/userConfig.json', json_encode($userConfig), LOCK_EX);
  216. } else
  217. $this->api_return_json(array('msg' => '修改失败'), 0);
  218. $this->api_return_json(array('msg' => '修改成功'), 1);
  219. }
  220. //关注
  221. public function collect()
  222. {
  223. $ID = $this->request->param('ID');
  224. if (!intval($this->userid)) {
  225. $this->api_return_json(array('msg' => '请重新登录'), 0);
  226. }
  227. $data = array();
  228. if (!intval($ID))
  229. $this->api_return_json(array('msg' => '数据获取失败'), 0);
  230. $data[$this->userid] = array($ID);
  231. if (!is_file(CMF_ROOT . 'public/collect.json')) {
  232. file_put_contents(CMF_ROOT . 'public/collect.json', json_encode($data), LOCK_EX);
  233. }
  234. $i = 0;
  235. $res = file_get_contents(CMF_ROOT . 'public/collect.json');
  236. if ($res) {
  237. $userli = json_decode($res, true);
  238. if (is_array($userli)) {
  239. if (isset($userli[$this->userid])) {
  240. $re = array_search($ID, $userli[$this->userid]);
  241. if ($re !== false) {
  242. unset($userli[$this->userid][$re]);
  243. $i = 1;
  244. $data = $userli;
  245. } else {
  246. array_unshift($userli[$this->userid], $ID);
  247. $data = $userli;
  248. }
  249. } else {
  250. $userli[$this->userid] = array($ID);
  251. $data = $userli;
  252. }
  253. }
  254. }
  255. file_put_contents(CMF_ROOT . 'public/collect.json', json_encode($data), LOCK_EX);
  256. $this->api_return_json(array('msg' => $i ? '关注' : '取消关注'), 1);
  257. }
  258. /*
  259. *
  260. * @name 关注
  261. *
  262. * */
  263. public function follow()
  264. {
  265. $PID = $this->request->param('PRIMARYADDR');
  266. $DID = $this->request->param('DEVICEID');
  267. if (!intval($this->userid)) {
  268. $this->api_return_json(array('msg' => '请重新登录'), 0);
  269. }
  270. $data = array();
  271. if (!intval($DID) || !intval($PID))
  272. $this->api_return_json(array('msg' => '数据获取失败'), 0);
  273. $data['UID'] = $this->userid;
  274. $data['PID'] = $PID;
  275. $data['DID'] = $DID;
  276. $i = 0;
  277. //查询当前用户是否关注 是 取关 否 关注
  278. $res = Db::table('follow')->where($data)->find();
  279. if ($res) {
  280. $i = 1;
  281. Db::table('follow')->where($data)->delete();
  282. } else {
  283. Db::table('follow')->insertGetId($data);
  284. }
  285. $this->api_return_json(array('msg' => $i ? '关注' : '已关注'), 1);
  286. }
  287. //取关
  288. public function quitCollect()
  289. {
  290. $id = $this->request->param('id');
  291. $collect = empty(file_get_contents(CMF_ROOT . 'public/collect.json')) ? array() : json_decode(file_get_contents(CMF_ROOT . 'public/collect.json'), true);
  292. foreach ($collect as $key => $val) {
  293. if ($val['id'] == $id)
  294. array_splice($collect, $key, 1);
  295. }
  296. file_put_contents(CMF_ROOT . 'public/collect.json', json_encode($collect), LOCK_EX);
  297. $this->api_return_json(array('msg' => '取关成功'), 1);
  298. }
  299. public function home()
  300. {
  301. $user = file_get_contents(CMF_ROOT . 'public/user.json');
  302. if (empty($user))
  303. $this->api_return_json(array('msg' => '用户数据异常'));
  304. $user = json_decode($user, true);
  305. $userInfo = array();
  306. foreach ($user as $val) {
  307. if (1 == $val['user_id']) {
  308. $userInfo = $val;
  309. break;
  310. }
  311. }
  312. $userInfo['map'] = !empty($userInfo['cover']) ? $userInfo['cover'] : 'map.jpg';
  313. $this->api_return_json(array('home' => $userInfo), 1);
  314. }
  315. //我的消息
  316. public function myMessage()
  317. {
  318. $page = $this->request->param('page');
  319. if (!isset($this->userid) || empty($this->userid)) {
  320. $this->api_return_json(array('msg' => '登录过期'));
  321. }
  322. if (!isset($page) || empty($page)) {
  323. $page = 1;
  324. }
  325. $where = array('c.user_id' => $this->userid, 'c.is_delete' => 0);
  326. $products = Db::name('user_message')
  327. ->field('c.add_time,c.id,c.type,m.desc,m.url,c.user_id,c.content,c.siteurl,u.user_message_id')
  328. ->alias("c")
  329. ->join('message m', 'm.id=c.message_id', 'left')
  330. ->join('user_message_read u', 'u.user_message_id=c.id', 'left')
  331. ->where('(c.user_id=0 or c.user_id=' . $this->userid . ') and c.is_delete=0')
  332. //->group('c.message_id')
  333. ->order('c.add_time desc')
  334. ->limit(($page - 1) * $this->pages, $this->pages)
  335. ->select()
  336. ->each(function ($v) {
  337. $v['add_time'] = $this->getChatTimeStr($v['add_time']);
  338. if ($v['type'] == 1) {
  339. $v['url'] = $v['siteurl'];
  340. $v['desc'] = $v['content'];
  341. }
  342. if (empty($v['user_message_id']))
  343. $v['is_read'] = 0;
  344. else
  345. $v['is_read'] = 1;
  346. return $v;
  347. })
  348. ->toArray();
  349. //未读总数
  350. $count = Db::name('user_message')
  351. ->alias("c")
  352. ->field('user_message_id')
  353. ->join('user_message_read u', 'u.user_message_id=c.id', 'left')
  354. ->where('(c.user_id=0 or c.user_id=' . $this->userid . ') and c.is_delete=0')
  355. //->group('c.message_id')
  356. ->select()
  357. ->toArray();
  358. $i = 0;
  359. foreach ($count as $val) {
  360. if ($val['user_message_id'] == null)
  361. $i++;
  362. }
  363. $this->api_return_json(array('myMessage' => $products, 'count' => $i, 'pages' => $this->pages), 1);
  364. }
  365. //消息是否已读
  366. public function isRead()
  367. {
  368. $id = $this->request->param('id');
  369. if (!isset($id) || empty($id))
  370. $this->api_return_json(array('msg' => '消息id异常'));
  371. if (!isset($this->userid) || empty($this->userid))
  372. $this->api_return_json(array('msg' => '登录过期'));
  373. $data['user_id'] = $this->userid;
  374. $data['user_message_id'] = $id;
  375. $data['add_time'] = time();
  376. Db::name('user_message_read')->insert($data);
  377. $this->api_return_json(array('msg' => '阅读成功'), 1);
  378. }
  379. //删除消息
  380. public function deleteMessage()
  381. {
  382. $id = $this->request->param('id');
  383. if (!isset($id) || empty($id))
  384. $this->api_return_json(array('msg' => '消息id异常'));
  385. Db::name('user_message')->where(array('id' => $id))->update(array('is_delete' => 1));
  386. $this->api_return_json(array('msg' => '删除成功'), 1);
  387. }
  388. //更新用户信息
  389. public function updateUser()
  390. {
  391. //微信小程序
  392. $avatar = $this->request->param('avatar', '');
  393. $wx_nickname = $this->request->param('nickName', '');
  394. $msg = '未修改';
  395. if (!empty($avatar)) {
  396. $update_date['avatar'] = $avatar;
  397. $update_date['wx_nickname'] = $wx_nickname;
  398. db('wx_user')->where('user_id', $this->userid)->update($update_date);
  399. $msg = '已修改';
  400. }
  401. $this->api_return_json(array('msg' => $msg), 1);
  402. }
  403. //上传
  404. public function upload()
  405. {
  406. $file = $this->request->file('file');
  407. $info = $file->validate(array('ext' => 'jpg,png'))->move(CMF_ROOT . 'public/upload' . DIRECTORY_SEPARATOR . 'portal'); //上传后的文件名
  408. if ($info) {
  409. $upame = $info->getSaveName(); //数据库存储文件名
  410. $path = 'portal/' . $upame;
  411. $this->api_return_json(array('path' => $path), 1);
  412. } else {
  413. $this->api_return_json(array('msg' => $file->getError()));
  414. }
  415. }
  416. public function deleteFile()
  417. {
  418. $image_url = $this->request->param('file');
  419. if (!isset($image_url) || empty($image_url)) {
  420. $this->api_return_json(array('msg' => '资源错误'));
  421. }
  422. if (file_exists(CMF_ROOT . 'public/upload/' . $image_url)) {
  423. unlink(CMF_ROOT . 'public/upload/' . $image_url);
  424. $this->api_return_json(array('msg' => '删除成功'), 1);
  425. } else {
  426. $this->api_return_json(array('msg' => '文件不存在删除失败'));
  427. }
  428. }
  429. /**
  430. * 微信展示时间的方法
  431. * @param $addTime
  432. * @return string
  433. */
  434. public static function getChatTimeStr($addTime)
  435. {
  436. $nowTime = time();
  437. if ($addTime > $nowTime) {
  438. return "";
  439. }
  440. //返回的时间
  441. $timeStr = "";
  442. //获取当前时间
  443. $addTime = explode(',', date('Y,m,d,w,a,h,i,Y', $addTime));//年,月,日,星期,上下午,时,分
  444. $nowTime = explode(',', date('Y,m,d,w,a,h,i,Y', $nowTime));
  445. $dayPerMonthAddTime = self::getDayPerMonth($addTime[0]);
  446. $week = array(0 => "星期日", 1 => "星期一", 2 => "星期二", 3 => "星期三", 4 => "星期四", 5 => "星期五", 6 => "星期六");
  447. //如果时间差小于一天的,显示(上午 时间) / (下午 时间)
  448. if ($addTime[0] == $nowTime[0] && $addTime[1] == $nowTime[1] && $addTime[2] == $nowTime[2]) {
  449. $timeStr .= $addTime[5] . ":" . $addTime[6];
  450. } else if (($addTime[0] == $nowTime[0] && $addTime[1] == $nowTime[1] && $addTime[2] == $nowTime[2] - 1)
  451. || ($addTime[0] == $nowTime[0] && $nowTime[1] - $addTime[1] == 1 && $dayPerMonthAddTime[(int)$addTime[1]] == $addTime[2] && $nowTime[2] == 1)
  452. || ($nowTime[0] - $addTime[0] == 1 && $addTime[1] == 12 && $addTime[2] == 31 && $nowTime[1] == 1 && $nowTime[2] == 1)) { //如果时间差在昨天,三种情况(同一月份内跨一天、月末跨越到月初、年末跨越到年初)显示格式:昨天 时:分 上午/下午
  453. $timeStr .= "昨天 " . $addTime[5] . ":" . $addTime[6] . " ";
  454. } else if (($addTime[0] == $nowTime[0] && $addTime[1] == $nowTime[1] && $nowTime[2] - $addTime[2] < 7)
  455. || ($addTime[0] == $nowTime[0] && $nowTime[1] - $addTime[1] == 1 && $dayPerMonthAddTime[(int)$addTime[1]] - $addTime[2] + $nowTime[2] < 7
  456. || ($nowTime[0] - $addTime[0] == 1 && $addTime[1] == 12 && $nowTime[1] == 1 && 31 - $addTime[2] + $nowTime[2] < 7))) { //如果时间差在一个星期之内的,也是三种情况,显示格式:星期 时:分 上午/下午
  457. $timeStr .= $week[$addTime[3]] . " " . $addTime[5] . ":" . $addTime[6];
  458. } else { //显示格式:月/日/年 时:分 上午/下午
  459. $timeStr .= $addTime[7] . "-" . $addTime[1] . "-" . $addTime[2] . " " . $addTime[5] . ":" . $addTime[6];
  460. }
  461. if ($addTime[4] == "am") {
  462. $timeStr .= " 上午";
  463. } else if ($addTime[4] == "pm") {
  464. $timeStr .= " 下午";
  465. }
  466. return $timeStr;
  467. }
  468. //根据年份获取每个月份的总天数和每年最后一个月的天数
  469. public static function getDayPerMonth($year)
  470. {
  471. $arr = array(
  472. 1 => 31,
  473. 3 => 31,
  474. 4 => 30,
  475. 5 => 31,
  476. 6 => 30,
  477. 7 => 31,
  478. 8 => 31,
  479. 9 => 30,
  480. 10 => 31,
  481. 11 => 30,
  482. 12 => 31
  483. );
  484. //闰年
  485. if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
  486. $arr[2] = 29;
  487. } else {
  488. $arr[2] = 28;
  489. }
  490. return $arr;
  491. }
  492. /*************** 校验身份证 start *******************/
  493. function checkIdCard($idc)
  494. {
  495. if (empty($idc)) {
  496. return false;
  497. }
  498. $idcard = $idc;
  499. $City = array(11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁", 22 => "吉林", 23 => "黑龙江", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽", 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北", 43 => "湖南", 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州", 53 => "云南", 54 => "西藏", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏", 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外");
  500. $iSum = 0;
  501. $idCardLength = strlen($idcard);
  502. //长度验证
  503. if (!preg_match('/^\d{17}(\d|x)$/i', $idcard) and !preg_match('/^\d{15}$/i', $idcard)) {
  504. return false;
  505. }
  506. //地区验证
  507. if (!array_key_exists(intval(substr($idcard, 0, 2)), $City)) {
  508. return false;
  509. }
  510. // 15位身份证验证生日,转换为18位
  511. if ($idCardLength == 15) {
  512. $sBirthday = '19' . substr($idcard, 6, 2) . '-' . substr($idcard, 8, 2) . '-' . substr($idcard, 10, 2);
  513. // $d = new DateTime($sBirthday);
  514. // $dd = $d->format('Y-m-d');
  515. // if($sBirthday != $dd)
  516. if ($sBirthday != $sBirthday) {
  517. return false;
  518. }
  519. $idcard = substr($idcard, 0, 6) . "19" . substr($idcard, 6, 9);//15to18
  520. $Bit18 = $this->getVerifyBit($idcard);//算出第18位校验码
  521. $idcard = $idcard . $Bit18;
  522. }
  523. // 判断是否大于2078年,小于1900年
  524. $year = substr($idcard, 6, 4);
  525. if ($year < 1900 || $year > 2078) {
  526. return false;
  527. }
  528. //18位身份证处理
  529. $sBirthday = substr($idcard, 6, 4) . '-' . substr($idcard, 10, 2) . '-' . substr($idcard, 12, 2);
  530. // var_dump($sBirthday);
  531. // $d = new DateTime($sBirthday);
  532. // $dd = $d->format('Y-m-d');
  533. // echo $dd;
  534. // die();
  535. // if($sBirthday != $dd)
  536. if ($sBirthday != $sBirthday) {
  537. return false;
  538. }
  539. //身份证编码规范验证
  540. $idcard_base = substr($idcard, 0, 17);
  541. if (strtoupper(substr($idcard, 17, 1)) != $this->getVerifyBit($idcard_base)) {
  542. return false;
  543. }
  544. return true;
  545. }
  546. // 计算身份证校验码,根据国家标准GB 11643-1999
  547. function getVerifyBit($idcard_base)
  548. {
  549. if (strlen($idcard_base) != 17) {
  550. return false;
  551. }
  552. //加权因子
  553. $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  554. //校验码对应值
  555. $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  556. $checksum = 0;
  557. for ($i = 0; $i < strlen($idcard_base); $i++) {
  558. $checksum += substr($idcard_base, $i, 1) * $factor[$i];
  559. }
  560. $mod = $checksum % 11;
  561. $verify_number = $verify_number_list[$mod];
  562. return $verify_number;
  563. }
  564. //全读
  565. public function isAllRead()
  566. {
  567. if (!isset($this->userid) || empty($this->userid))
  568. $this->api_return_json(array('msg' => '登录过期'));
  569. $result = Db::name('user_message')
  570. ->field('id')
  571. ->where('(user_id=0 or user_id=' . $this->userid . ') and is_delete=0')
  572. ->select()
  573. ->toArray();
  574. $datas = array();
  575. foreach ($result as $val) {
  576. $res = Db::name('user_message_read')->where(array('user_message_id' => $val['id'], 'user_id' => $this->userid))->find();
  577. if ($res)
  578. continue;
  579. $data['user_id'] = $this->userid;
  580. $data['user_message_id'] = $val['id'];
  581. $data['add_time'] = time();
  582. $datas[] = $data;
  583. }
  584. Db::name('user_message_read')->insertAll($datas);
  585. $this->api_return_json(array('msg' => '全部阅读成功'), 1);
  586. }
  587. /**
  588. * @name 获取城市
  589. */
  590. public function city()
  591. {
  592. $where[] = ['delete_time', '=', 0];
  593. $name = $this->request->get('name');
  594. // $page=$this->request->get('page',1);
  595. $where[] = ['name', 'like', '%苏州%'];
  596. $index = City::where($where)->field('id,name')->order(['list_order' => 'asc', 'create_time' => 'desc'])->select();
  597. $keyword = Keyword::where([['delete_time', '=', 0]])->field('name')->order(['list_order' => 'desc', 'create_time' => 'desc'])->select();
  598. $this->api_return_json(['city' => $index, 'pages' => $this->pages, 'keyword' => $keyword], 1);
  599. }
  600. /**
  601. * @name 分类category
  602. */
  603. public function category()
  604. {
  605. $where[] = ['delete_time', '=', 0];
  606. $city_id = $this->request->get('city_id', '', 'intval');
  607. if (empty($city_id)) $this->api_return_json(['msg' => '请选择城市']);
  608. $city = City::where([['delete_time', '=', 0], ['id', '=', $city_id]])->find();
  609. if (empty($city)) $this->api_return_json(['msg' => '该城市不存在']);
  610. $categories = $city->category()->field('id,name,desc,icon,table_name,detail')->order(['list_order' => 'asc', 'create_time' => 'desc'])->select()->each(function ($v) {
  611. unset($v['pivot']);
  612. return $v;
  613. });
  614. $this->api_return_json(['category' => $categories], 1);
  615. }
  616. public function setLocation()
  617. {//$this->redis->flushAll();
  618. $this->api_return_json(['msg' => 'ok'], 1);
  619. $page = $this->request->get('page', 1);
  620. if (!$this->request->isGet()) {
  621. return $this->api_return_json(['msg' => '请求方式不对']);
  622. }
  623. if (!isset($this->userid) || empty($this->userid)) {
  624. return $this->api_return_json(['msg' => '请先登录']);
  625. }
  626. $data = $this->request->get();
  627. // if (!isset($data['lat']) || empty($data['lat'])) {
  628. // $this->api_return_json(['msg' => '请传入纬度']);
  629. // }
  630. // if (!isset($data['long']) || empty($data['long'])) {
  631. // $this->api_return_json(['msg' => '请传入经度']);
  632. // }
  633. if (!isset($data['city']) || empty($data['city'])) {
  634. $this->api_return_json(['msg' => '请输入城市名']);
  635. }
  636. //$citys = City::where('delete_time', 0)->field('id,name')->limit(($page - 1), $this->pages)->orderRaw('',["name <>".$data['city']])->select();
  637. $citys = Db::query('select id,name from rbi_city where delete_time=0 order by field(name,"' . $data['city'] . '") desc,list_order desc');
  638. $this->api_return_json(compact('citys'), 1);
  639. }
  640. /**
  641. * @name
  642. */
  643. public function gf()
  644. {
  645. $name = $this->request->post('name');
  646. $city_id = $this->request->post('city_id');
  647. if (empty($name)) $this->api_return_json(['msg' => '名称不能为空']);
  648. if (empty($city_id)) $this->api_return_json(['msg' => '请选择城市']);
  649. $GarbageForm = GarbageForm::where('name', $name)->where('city_id', $city_id)->where('delete_time', 0)->find();
  650. if (!empty($GarbageForm)) {
  651. $this->api_return_json(['msg' => '该名称已提交']);
  652. }
  653. $search = Db::field('name,list_order,create_time,category_id')
  654. ->table('rbi_recycled')
  655. ->alias('r')
  656. ->unionAll([
  657. 'SELECT name,list_order,create_time,category_id FROM rbi_harmful where ' . 'delete_time=0 and ' . '(city_id=' . $city_id . ' or city_id=0) and name like "%' . $name . '%"',
  658. 'SELECT name,list_order,create_time,category_id FROM rbi_perishable_refuse where ' . 'delete_time=0 and ' . '(city_id=' . $city_id . ' or city_id=0) and name like "%' . $name . '%"',
  659. 'SELECT name,list_order,create_time,category_id FROM rbi_kitchen_waste where ' . 'delete_time=0 and ' . '(city_id=' . $city_id . ' or city_id=0)and name like "%' . $name . '%"',
  660. 'SELECT name,list_order,create_time,category_id FROM rbi_wet_refuse where ' . 'delete_time=0 and ' . '(city_id=' . $city_id . ' or city_id=0) and name like "%' . $name . '%"',
  661. 'SELECT name,list_order,create_time,category_id FROM rbi_other where ' . 'delete_time=0 and ' . '(city_id=' . $city_id . ' or city_id=0) and name like "%' . $name . '%"',
  662. ])->where(function ($query) use ($city_id) {
  663. $query->where('r.city_id', 0)->whereOr('r.city_id', '=', $city_id);
  664. })->where('name', $name)->find();
  665. if (!empty($search)) {
  666. $this->api_return_json(['msg' => '该名称已提交']);
  667. }
  668. $result = GarbageFormService::store(compact('name', 'city_id'));
  669. if ($result['code'] != 0) $this->api_return_json(['msg' => "提交失败"]);
  670. $this->api_return_json(['msg' => "提交成功"], 1);
  671. }
  672. /**
  673. * @name 详情
  674. */
  675. public function info()
  676. {
  677. $data = $this->request->get();
  678. if (!$this->request->isGet()) {
  679. return $this->api_return_json(['msg' => '请求方式不对']);
  680. }
  681. if (!isset($this->userid) || empty($this->userid)) {
  682. return $this->api_return_json(['msg' => '请先登录']);
  683. }
  684. $user = UserService::getOne([['user_id', '=', $this->userid]]);
  685. $member = $user->member;
  686. $this->api_return_json(compact('member'), 1);
  687. }
  688. /**
  689. * @name 详情
  690. */
  691. public function store_info()
  692. {
  693. $data = $this->request->post();
  694. $id = isset($data['id']) && empty($data['id']) ? 0 : $data['id'];
  695. if (!$this->request->isPost()) {
  696. return $this->api_return_json(['msg' => '请求方式不对']);
  697. }
  698. if (!isset($this->userid) || empty($this->userid)) {
  699. return $this->api_return_json(['msg' => '请先登录']);
  700. }
  701. $user = UserService::getOne([['user_id', '=', $this->userid]]);
  702. $member = $user->member;
  703. if (empty($member)) {
  704. $result = HouseMemberService::store($data);
  705. } else {
  706. $result = HouseMemberService::store($data, $id);
  707. }
  708. if ($result['code'] == -1) {
  709. $return = ['msg' => $data['message']];
  710. } elseif ($result['code'] == 0) {
  711. $return = ['msg' => '操作失败'];
  712. } else {
  713. $return = ['msg' => "操作成功"];
  714. }
  715. $this->api_return_json($return, $result['code'] == 1 ? 1 : 0);
  716. }
  717. }