UserController.php 35 KB

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