心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年3月13日
  7. * 首页模型类
  8. */
  9. namespace app\admin\model;
  10. use core\basic\Model;
  11. class IndexModel extends Model
  12. {
  13. // 检查用户账号密码
  14. public function login($where)
  15. {
  16. // 执行登录
  17. $result = parent::table('ay_user')->field('id,ucode,username,realname')
  18. ->where($where)
  19. ->where('status=1')
  20. ->find();
  21. if ($result) { // 登录成功
  22. $this->updateLogin($where); // 执行更新登录记录
  23. $menus = $this->getUserMenu($result->ucode); // 用户菜单
  24. $result->menus = get_tree($menus, 0, 'mcode', 'pcode'); // 用户菜单树
  25. $result->rcodes = $this->getUserRcode($result->ucode); // 用户角色
  26. $result->levels = $this->getUserLevel($result->ucode); // 用户权限
  27. $acodes = $this->getUserAcode($result->ucode); // 获取用户区域
  28. $areas = $this->getAreas();
  29. $first_acode = $areas[0]->acode;
  30. if (in_array($first_acode, $acodes)) {
  31. array_unshift($acodes, $first_acode);
  32. $acodes = array_unique($acodes);
  33. }
  34. $result->acodes = $acodes;
  35. $result->area_map = get_mapping($areas, 'name', 'acode'); // 区域代码名称映射表
  36. $result->area_tree = $this->getUserAreaTree($areas, 0, 'acode', 'pcode', 'son', $result->acodes); // 当前用户的区域树
  37. return $result;
  38. } else {
  39. return false;
  40. }
  41. }
  42. // 用户登录成功后更新登录信息
  43. private function updateLogin($where)
  44. {
  45. $data = array(
  46. 'last_login_ip' => ip2long(get_user_ip()),
  47. 'login_count' => '+=1'
  48. );
  49. return parent::table('ay_user')->where($where)
  50. ->autoTime()
  51. ->update($data);
  52. }
  53. // 获取用户菜单列表
  54. public function getUserMenu($ucode)
  55. {
  56. if ($ucode == '10001') { // 管理员获所有区域
  57. $field = array(
  58. 'id',
  59. 'mcode',
  60. 'pcode',
  61. 'name',
  62. 'url',
  63. 'shortcut',
  64. 'ico',
  65. 'sorting',
  66. 'status'
  67. );
  68. $order = array(
  69. 'sorting',
  70. 'pcode',
  71. 'id'
  72. );
  73. $result = parent::table('ay_menu')->distinct()
  74. ->field($field)
  75. ->order($order)
  76. ->select();
  77. } else {
  78. $table = array(
  79. 'ay_user',
  80. 'ay_user_role',
  81. 'ay_role',
  82. 'ay_role_level',
  83. 'ay_menu'
  84. );
  85. $field = array(
  86. 'ay_menu.id',
  87. 'ay_menu.mcode',
  88. 'ay_menu.pcode',
  89. 'ay_menu.name',
  90. 'ay_menu.url',
  91. 'ay_menu.shortcut',
  92. 'ay_menu.ico',
  93. 'ay_menu.sorting',
  94. 'ay_menu.status'
  95. );
  96. $where = array(
  97. "ay_user.ucode='$ucode'",
  98. "ay_user.ucode=ay_user_role.ucode",
  99. "ay_role.rcode=ay_user_role.rcode",
  100. "ay_role.rcode=ay_role_level.rcode",
  101. "ay_menu.url=ay_role_level.level"
  102. );
  103. $order = array(
  104. 'ay_menu.sorting',
  105. 'ay_menu.pcode',
  106. 'ay_menu.id'
  107. );
  108. $result = parent::table($table)->distinct()
  109. ->field($field)
  110. ->where($where)
  111. ->order($order)
  112. ->select();
  113. }
  114. return $result;
  115. }
  116. // 获取用户的角色代码
  117. public function getUserRcode($ucode)
  118. {
  119. return parent::table('ay_user_role')->where("ucode='$ucode'")->column('rcode');
  120. }
  121. // 获取用户权限列表
  122. public function getUserLevel($ucode)
  123. {
  124. $table = array(
  125. 'ay_user',
  126. 'ay_user_role',
  127. 'ay_role',
  128. 'ay_role_level'
  129. );
  130. $where = array(
  131. "ay_user.ucode='$ucode'",
  132. "ay_user.ucode=ay_user_role.ucode",
  133. "ay_role.rcode=ay_user_role.rcode",
  134. "ay_role.rcode=ay_role_level.rcode"
  135. );
  136. return parent::table($table)->distinct()
  137. ->where($where)
  138. ->column('ay_role_level.level');
  139. }
  140. // 获取用户可管理的区域代码
  141. public function getUserAcode($ucode)
  142. {
  143. if ($ucode == '10001') { // 管理员获所有区域
  144. $result = parent::table('ay_area')->distinct()->column('acode');
  145. } else {
  146. $table = array(
  147. 'ay_user',
  148. 'ay_user_role',
  149. 'ay_role',
  150. 'ay_role_area',
  151. 'ay_area'
  152. );
  153. $where = array(
  154. "ay_user.ucode = '$ucode'",
  155. "ay_user.ucode=ay_user_role.ucode",
  156. "ay_role.rcode=ay_user_role.rcode",
  157. "ay_role.rcode=ay_role_area.rcode",
  158. "ay_area.acode=ay_role_area.acode"
  159. );
  160. $result = parent::table($table)->distinct()
  161. ->where($where)
  162. ->column('ay_area.acode');
  163. }
  164. return $result;
  165. }
  166. // 获取区域列表
  167. public function getAreas()
  168. {
  169. $result = parent::table('ay_area')->field('pcode,acode,name,is_default')
  170. ->order('is_default DESC,pcode,acode')
  171. ->select();
  172. return $result;
  173. }
  174. // 生成授权的区域树
  175. public function getUserAreaTree($data, $tid, $idField, $pidField, $sonName = 'son', $valid = array())
  176. {
  177. $tree = array();
  178. foreach ($data as $key => $value) {
  179. if ($value->$pidField == "$tid") { // 父亲找到儿子
  180. $value->$sonName = $this->getUserAreaTree($data, $value->$idField, $idField, $pidField, $sonName, $valid);
  181. if ($value->$sonName) {
  182. $tree[] = $value;
  183. } elseif (in_array($value->$idField, $valid)) {
  184. $tree[] = $value;
  185. }
  186. }
  187. }
  188. return $tree;
  189. }
  190. // 检查当前用户密码
  191. public function checkUserPwd($password)
  192. {
  193. return parent::table('ay_user')->field('id')
  194. ->where("id=" . session('id') . " AND password='$password'")
  195. ->find();
  196. }
  197. // 修改当前用户信息
  198. public function modUserInfo($data)
  199. {
  200. return parent::table('ay_user')->where("id=" . session('id'))->update($data);
  201. }
  202. // 获取用户信息
  203. public function getUserInfo($ucode)
  204. {
  205. $result = parent::table('ay_user')->field('id,ucode,username,realname,last_login_ip,update_time,login_count')
  206. ->where("ucode='$ucode'")
  207. ->find();
  208. return $result;
  209. }
  210. }