User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\system;
  13. use think\facade\Session;
  14. use app\model\BaseModel;
  15. /**
  16. * 管理员模型
  17. */
  18. class User extends BaseModel
  19. {
  20. /*******************************************************************用户 编辑查询 start*****************************************************/
  21. /**
  22. * 添加用户
  23. * @param $data
  24. */
  25. public function addUser($data){
  26. $site_id = isset($data['site_id']) ? $data['site_id'] : '';
  27. $app_module = isset($data['app_module']) ? $data['app_module'] : '';
  28. $member_id = isset($data['member_id']) ? $data['member_id'] : 0;
  29. if ($site_id === '') {
  30. return $this->error('', 'REQUEST_SITE_ID');
  31. }
  32. if ($app_module === '') {
  33. return $this->error('', 'REQUEST_APP_MODULE');
  34. }
  35. //判断 用户名 是否存在
  36. $user_info = model('user')->getInfo([ ['username', "=", $data["username"]], ["app_module", "=", $data["app_module"]]]);
  37. if (!empty($user_info)) {
  38. if(data_md5($data["password"]) == $user_info['password']){
  39. return $this->success(2);
  40. }else{
  41. return $this->error('', '账号已存在');
  42. }
  43. }
  44. if($member_id > 0){
  45. $temp_condition = array(
  46. "app_module" => $data["app_module"],
  47. "member_id" => $member_id
  48. );
  49. $temp_count = model('user')->getCount($temp_condition, 'uid');
  50. if ($temp_count > 0) {
  51. return $this->error('', 'USERNAME_EXISTED');
  52. }
  53. }
  54. $group_id = isset($data['group_id']) ? $data['group_id'] : 0;
  55. if($group_id > 0){
  56. $group_model = new Group();
  57. $group_info_result = $group_model->getGroupInfo([["group_id", "=", $group_id],["site_id", "=", $site_id],["app_module", "=", $app_module]], "group_name");
  58. $group_info = $group_info_result["data"];
  59. $data["group_name"] = $group_info["group_name"];
  60. }
  61. $data["password"] = data_md5($data["password"]);
  62. $data["create_time"] = time();
  63. $result = model("user")->add($data);
  64. if ($result === false) {
  65. return $this->error('', 'UNKNOW_ERROR');
  66. }
  67. return $this->success($result);
  68. }
  69. /**
  70. * 编辑用户
  71. * @param $data
  72. * @param $condition
  73. */
  74. public function editUser($data, $condition){
  75. $check_condition = array_column($condition, 2, 0);
  76. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  77. $app_module = isset($check_condition['app_module']) ? $check_condition['app_module'] : '';
  78. if ($site_id === '') {
  79. return $this->error('', 'REQUEST_SITE_ID');
  80. }
  81. if ($app_module === '') {
  82. return $this->error('', 'REQUEST_APP_MODULE');
  83. }
  84. $group_id = isset($data['group_id']) ? $data['group_id'] : 0;
  85. if($group_id > 0){
  86. $group_model = new Group();
  87. $group_info_result = $group_model->getGroupInfo([["group_id", "=", $group_id],["site_id", "=", $site_id],["app_module", "=", $app_module]], "group_name");
  88. $group_info = $group_info_result["data"];
  89. $data["group_name"] = $group_info["group_name"];
  90. }
  91. $res = model("user")->update($data, $condition);
  92. if ($res === false) {
  93. return $this->error('', 'UNKNOW_ERROR');
  94. }
  95. return $this->success($res);
  96. }
  97. /**
  98. * 编辑用户状态
  99. * @param $status
  100. * @param $condition
  101. */
  102. public function modifyUserStatus($status, $condition){
  103. $check_condition = array_column($condition, 2, 0);
  104. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  105. $app_module = isset($check_condition['app_module']) ? $check_condition['app_module'] : '';
  106. if ($site_id === '') {
  107. return $this->error('', 'REQUEST_SITE_ID');
  108. }
  109. if ($app_module === '') {
  110. return $this->error('', 'REQUEST_APP_MODULE');
  111. }
  112. $data = array(
  113. "status" => $status,
  114. "update_time" => time()
  115. );
  116. $res = model('user')->update($data, $condition);
  117. if ($res === false) {
  118. return $this->error('', 'UNKNOW_ERROR');
  119. }
  120. return $this->success($res);
  121. }
  122. /**
  123. * 重置密码
  124. * @param $password
  125. * @param $condition
  126. * @return array|\multitype
  127. */
  128. public function modifyUserPassword($password, $condition)
  129. {
  130. $res = model('user')->update(['password' => data_md5($password)], $condition);
  131. if ($res === false) {
  132. return $this->error('', 'RESULT_ERROR');
  133. }
  134. return $this->success($res);
  135. }
  136. /**
  137. * 系统用户修改密码
  138. *
  139. * @param unknown $uid
  140. * @param unknown $old_password
  141. * @param unknown $new_password
  142. */
  143. public function modifyAdminUserPassword($uid, $old_password, $new_password)
  144. {
  145. $condition = array(
  146. 'uid' => $uid,
  147. 'password' => data_md5($old_password)
  148. );
  149. $res = model('user')->getInfo($condition, "uid");
  150. if (!empty($res['uid'])) {
  151. $data = array(
  152. 'password' => data_md5($new_password)
  153. );
  154. $res = model('user')->update($data, [
  155. 'uid' => $uid
  156. ]);
  157. return $this->success($res,'SUCCESS');
  158. } else{
  159. return $this->error('', 'PASSWORD_ERROR');
  160. }
  161. }
  162. /**
  163. * 删除用户
  164. * @param array $condition
  165. * @return multitype:string mixed
  166. */
  167. public function deleteUser($condition)
  168. {
  169. $check_condition = array_column($condition, 2, 0);
  170. $app_module = isset($check_condition['app_module']) ? $check_condition['app_module'] : '';
  171. if ($app_module === '') {
  172. return $this->error('', 'REQUEST_APP_MODULE');
  173. }
  174. $res = model('user')->delete($condition);
  175. if ($res === false) {
  176. return $this->error('', 'UNKNOW_ERROR');
  177. }
  178. return $this->success($res);
  179. }
  180. /**
  181. * 获取用户信息
  182. * @param $condition
  183. * @param string $field
  184. * @return \multitype
  185. */
  186. public function getUserInfo($condition, $field = "uid, app_module, site_id, group_id, username, member_id, create_time, update_time, status, login_time, login_ip"){
  187. $info = model('user')->getInfo($condition, $field);
  188. return $this->success($info);
  189. }
  190. /**
  191. * 获取用户列表
  192. * @param array $condition
  193. * @param string $field
  194. * @param string $order
  195. * @param string $limit
  196. * @return multitype:string mixed
  197. */
  198. public function getUserList($condition = [], $field = 'uid, app_module, site_id, group_id, username, member_id, create_time, update_time, status, login_time, login_ip, is_admin, group_name', $order = '', $limit = null)
  199. {
  200. $list = model('user')->getList($condition, $field, $order, '', '', '', $limit);
  201. return $this->success($list);
  202. }
  203. /**
  204. * 获取会员分页列表
  205. * @param array $condition
  206. * @param number $page
  207. * @param string $page_size
  208. * @param string $order
  209. * @param string $field
  210. * @return multitype:string mixed
  211. */
  212. public function getUserPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'uid, app_module, site_id, group_id, username, member_id, create_time, update_time, status, login_time, login_ip, is_admin, group_name')
  213. {
  214. $list = model('user')->pageList($condition, $field, $order, $page, $page_size);
  215. return $this->success($list);
  216. }
  217. /**
  218. * 获取站点用户分页列表
  219. * @param unknown $condition
  220. * @param number $page
  221. * @param string $page_size
  222. * @param string $order
  223. */
  224. public function getSiteUserPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '')
  225. {
  226. $field = ' nu.uid, nu.app_module, nu.app_group,
  227. nu.is_admin, nu.site_id, nu.group_id, nu.group_name, nu.username, nu.member_id, nu.create_time,
  228. nu.update_time, nu.status, nu.login_time, nu.login_ip, ns.site_name, ns.is_own, ns.level_name, ns.category_name';
  229. $alias = 'nu';
  230. $join = [
  231. [
  232. 'shop ns',
  233. 'nu.site_id = ns.site_id',
  234. 'left'
  235. ],
  236. ];
  237. $list = model("user")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  238. return $this->success($list);
  239. }
  240. /**
  241. * 检测权限
  242. * @param unknown $app_module
  243. * @param unknown $group_info
  244. * @param unknown $url
  245. */
  246. public function checkAuth($url, $app_module, $group_info)
  247. {
  248. if ($group_info['is_system'] == 1) {
  249. return true;
  250. }
  251. $menu_model = new Menu();
  252. $menu_info = $menu_model->getMenuInfoByUrl($url, $app_module);
  253. if (!empty($menu_info['data'])) {
  254. //权限组
  255. if (empty($group_info)) {
  256. return false;
  257. }
  258. if (strpos(',' . $group_info['array'] . ',', ',' . $menu_info['data']['name'] . ',') !== false) {
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. } else {
  264. return true;
  265. }
  266. }
  267. /**
  268. * 回去会员总数
  269. * @return mixed
  270. */
  271. public function getMemberTotalCount()
  272. {
  273. $res = model('member')->getCount();
  274. return $this->success($res);
  275. }
  276. /*******************************************************************用户 编辑查询 end*****************************************************/
  277. /*******************************************************************用户注册登录 start*****************************************************/
  278. /**
  279. * 用户登录
  280. * @param unknown $mobile
  281. * @param unknown $password
  282. */
  283. public function login($username, $password, $app_module)
  284. {
  285. $time = time();
  286. // 验证参数 预留
  287. $user_info = model('user')->getInfo([['username', "=", $username], ["app_module", "=", $app_module]]);
  288. if (empty($user_info)) {
  289. return $this->error('', 'USER_LOGIN_ERROR');
  290. } else if (1==1) {
  291. return $this->error([], 'PASSWORD_ERROR');
  292. } else if ($user_info['status'] !== 1) {
  293. return $this->error([], 'USER_IS_LOCKED');
  294. }
  295. // data_md5($password) !== $user_info['password']
  296. // 记录登录SESSION
  297. $auth = array(
  298. 'uid' => $user_info['uid'],
  299. 'username' => $user_info['username'],
  300. 'member_id' => $user_info['member_id'],
  301. 'create_time' => $user_info['create_time'],
  302. 'status' => $user_info['status'],
  303. 'group_id' => $user_info["group_id"],
  304. 'site_id' => $user_info["site_id"],
  305. 'app_group' => $user_info["app_group"],
  306. 'login_time' => $time,
  307. 'login_ip' => request()->ip()
  308. );
  309. //更新登录记录
  310. $data = [
  311. 'login_time' => time(),
  312. 'login_ip' => request()->ip(),
  313. ];
  314. model('user')->update($data, [ ['uid', "=", $user_info['uid']]]);
  315. //填写日志
  316. Session::set($app_module."."."uid", $user_info['uid']);
  317. Session::set($app_module."."."user_info", $auth);
  318. $this->addUserLog($user_info['uid'], $user_info['username'], $user_info['site_id'], "用户登录", []);//添加日志
  319. return $this->success();
  320. }
  321. /**
  322. * 模拟登录
  323. * @param unknown $mobile
  324. */
  325. public function simulatedLogin($username, $app_module)
  326. {
  327. // 验证参数 预留
  328. $user_info = model('user')->getInfo([['username', "=", $username], ["app_module", "=", $app_module]]);
  329. if (empty($user_info)) {
  330. return $this->error('', 'USER_LOGIN_ERROR');
  331. }else if ($user_info['status'] !== 1) {
  332. return $this->error([], 'USER_IS_LOCKED');
  333. }
  334. // 记录登录SESSION
  335. $auth = array(
  336. 'uid' => $user_info['uid'],
  337. 'username' => $user_info['username'],
  338. 'member_id' => $user_info['member_id'],
  339. 'create_time' => $user_info['create_time'],
  340. 'status' => $user_info['status'],
  341. 'group_id' => $user_info["group_id"],
  342. 'site_id' => $user_info["site_id"],
  343. 'app_group' => $user_info["app_group"],
  344. );
  345. //填写日志
  346. Session::set($app_module."."."uid", $user_info['uid']);
  347. Session::set($app_module."."."user_info", $auth);
  348. $this->addUserLog($user_info['uid'], $user_info['username'], $user_info['site_id'], "用户登录", []);//添加日志
  349. return $this->success();
  350. }
  351. /**
  352. * 获取当前登录uid
  353. * @param unknown $app_module
  354. */
  355. public function uid($app_module)
  356. {
  357. return Session::get($app_module."."."uid");
  358. }
  359. /**
  360. * 获取当前登录管理员信息
  361. * @param unknown $app_module
  362. */
  363. public function userInfo($app_module)
  364. {
  365. return Session::get($app_module."."."user_info");
  366. }
  367. /**
  368. * 清除登录信息
  369. */
  370. public function clearLogin($app_module){
  371. Session::delete($app_module);
  372. }
  373. /*******************************************************************用户注册登录 end*****************************************************/
  374. /*******************************************************************用户日志 start*****************************************************/
  375. /**
  376. * 添加用户日志
  377. * @param $data
  378. */
  379. public function addUserLog($uid, $username, $site_id, $action_name, $data = []){
  380. $url = request()->parseUrl();
  381. $ip = request()->ip();
  382. $log = array(
  383. "uid" => $uid,
  384. "username" => $username,
  385. "site_id" => $site_id,
  386. "url" => $url,
  387. "ip" =>$ip,
  388. "data" => json_encode($data),
  389. "action_name" => $action_name,
  390. "create_time" =>time(),
  391. );
  392. $res = model("user_log")->add($log);
  393. if ($res === false) {
  394. return $this->error('', 'UNKNOW_ERROR');
  395. }
  396. return $this->success($res);
  397. }
  398. /**
  399. * 删除用户日志
  400. */
  401. public function deleteUserLog($condition){
  402. $check_condition = array_column($condition, 2, 0);
  403. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  404. if ($site_id === '') {
  405. return $this->error('', 'REQUEST_SITE_ID');
  406. }
  407. $res = model("user_log")->delete($condition);
  408. if ($res === false) {
  409. return $this->error('', 'UNKNOW_ERROR');
  410. }
  411. return $this->success($res);
  412. }
  413. /**
  414. * 获用户员日志分页列表
  415. *
  416. * @param array $condition
  417. * @param number $page
  418. * @param string $page_size
  419. * @param string $order
  420. * @param string $field
  421. * @return multitype:string mixed
  422. */
  423. public function getUserLogPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'username, site_id, url, id, uid, data, ip, action_name, create_time')
  424. {
  425. $list = model('user_log')->pageList($condition, $field, $order, $page, $page_size);
  426. return $this->success($list);
  427. }
  428. /*******************************************************************用户日志 end*****************************************************/
  429. }