1
0

SysAdmin.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * MiniUser: Administrator
  5. * Date: 2018/12/8
  6. * Time: 21:38
  7. */
  8. namespace app\common\model;
  9. use think\exception\ErrorException;
  10. use think\Model;
  11. /**
  12. * Class username 渠道、运营、系统
  13. * @package app\admin\model
  14. */
  15. class SysAdmin extends Base
  16. {
  17. protected $pk = 'sid';
  18. protected $insert = ['username', 'status' => 1];
  19. public $editfield = array(
  20. array('name'=>'sid','type'=>'hidden'),
  21. array('name'=>'username','title'=>'用户名','type'=>'readonly','help'=>''),
  22. array('name'=>'nickname','title'=>'昵称','type'=>'text','help'=>''),
  23. // array('name'=>'avator','title'=>'头像','type'=>'picture','help'=>''),
  24. array('name'=>'password','title'=>'密码','type'=>'password','help'=>'为空时则不修改'),
  25. array('name'=>'email','title'=>'邮箱','type'=>'text','help'=>'用户邮箱,用于找回密码等安全操作'),
  26. array('name'=>'mobile','title'=>'手机号码','type'=>'text','help'=>''),
  27. array('name'=>'status','title'=>'状态','type'=>'select','option'=>array('1'=>'启用','0'=>'禁用'),'help'=>''),
  28. );
  29. public $addfield = array(
  30. array('name'=>'username','title'=>'用户名','type'=>'text','help'=>'用户名会作为默认的昵称'),
  31. array('name'=>'password','title'=>'密码','type'=>'password','help'=>'用户密码不能少于6位'),
  32. array('name'=>'repassword','title'=>'确认密码','type'=>'password','help'=>'确认密码'),
  33. array('name'=>'email','title'=>'邮箱','type'=>'text','help'=>'用户邮箱,用于找回密码等安全操作'),
  34. array('name'=>'mobile','title'=>'手机号码','type'=>'text','help'=>'用户邮箱,用于找回密码等安全操作'),
  35. );
  36. public $useredit = array(
  37. array('name'=>'id','type'=>'hidden'),
  38. array('name'=>'nickname','title'=>'昵称','type'=>'text','help'=>''),
  39. // array('name'=>'avator','title'=>'头像','type'=>'picture','help'=>''),
  40. array('name'=>'email','title'=>'邮箱','type'=>'text','help'=>'用户邮箱,用于找回密码等安全操作'),
  41. array('name'=>'mobile','title'=>'手机号码','type'=>'text','help'=>'用户邮箱,用于找回密码等安全操作'),
  42. );
  43. protected function getIdAttr($value, $data){
  44. return $data['sid'];
  45. }
  46. protected function setIdAttr($value, $data){
  47. return $data['id'];
  48. }
  49. public function groupId()
  50. {
  51. return $this->hasOne('AuthGroupAccess','uid','sid');
  52. }
  53. /**
  54. * username查询器
  55. * @param Model $query
  56. * @param $value
  57. * @param int $type 0:完全匹配 1:前匹配 2:后匹配 3:模糊匹配
  58. */
  59. public function searchUsernameAttr(Model $query, $value, $type = 0)
  60. {
  61. switch ($type) {
  62. case 0:
  63. $query->where('username', '=', $value);
  64. break;
  65. case 1:
  66. $query->where('username', 'like', $value . '%');
  67. break;
  68. case 2:
  69. $query->where('username', 'like', '%' . $value);
  70. break;
  71. case 3:
  72. $query->where('username', 'like', '%' . $value . '%');
  73. break;
  74. default:
  75. $query->where('username', '=', $value);
  76. }
  77. }
  78. public function searchCreateTimestampAttr(Model $query, $value)
  79. {
  80. $query->whereBetweenTime('create_timestamp', $value[0], $value[1]);
  81. }
  82. /**
  83. * 获取账户列表
  84. */
  85. public function getUserList($arrayMap, $order)
  86. {
  87. try {
  88. return $this->where($arrayMap)->order($order)->find();
  89. } catch (\think\Exception $e) {
  90. trace('数据库操作失败:' . $e->getMessage(), 'error');
  91. return -1;
  92. }
  93. }
  94. /**
  95. * 获取账户信息
  96. */
  97. private function getUserInfo($sid)
  98. {
  99. try {
  100. return $this->get($sid);
  101. } catch (\think\Exception $e) {
  102. trace('数据库操作失败:' . $e->getMessage(), 'error');
  103. return -1;
  104. }
  105. }
  106. /**
  107. * 账户登录
  108. * @param string $username 账号
  109. * @param string $password 密码
  110. * @return bool|mixed 用户Id | false
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public function login($username = '', $password = '')
  116. {
  117. if (!$username) {
  118. return false;
  119. }
  120. $user = $this->where([
  121. 'username' => $username,
  122. ])->find();
  123. if (isset($user->sid) && $user->sid && $user->status && md5($password . $user->salt) === $user->password) {
  124. $user->save([
  125. 'login' => array('inc', 'login', 1),
  126. ],['sid' => $user->sid]);
  127. /* 记录登录SESSION和COOKIES */
  128. $auth = array(
  129. 'sid' => $user->sid,
  130. 'nickname' => $user->nickname,
  131. 'avator' => $user->avator,
  132. );
  133. session('user_auth', $auth);
  134. session('user_auth_sign', data_auth_sign($auth));
  135. /* 记录登录SESSION和COOKIES */
  136. return $user->sid;
  137. } else {
  138. return false;
  139. }
  140. }
  141. /**
  142. * 账户注册
  143. * @param $username **账号
  144. * @param $password *密码
  145. * @param $repassword *确认密码
  146. * @param $type *账号类型
  147. * @param $name *账户名称
  148. * @return bool 用户id | false
  149. */
  150. public function register($data)
  151. {
  152. try {
  153. $data['salt'] = rand_string(6);
  154. !isset($data['nickname']) && ($data['nickname'] = $data['username']);
  155. !isset($data['avator']) && ($data['avator'] = config('siteinfo.avator'));
  156. $this->save($data);
  157. return $this->id;
  158. }catch (\think\Exception $e){
  159. trace('数据库操作失败:' . $e->getMessage(), 'error');
  160. return false;
  161. }
  162. }
  163. /**
  164. * 账户登出
  165. */
  166. public function logout()
  167. {
  168. session('user_auth', null);
  169. session('user_auth_sign', null);
  170. }
  171. /**
  172. * 修改账户信息
  173. * @param $data
  174. * @param bool $ischangepwd
  175. * @return bool
  176. */
  177. public function editInfo($data, $ischangepwd = false)
  178. {
  179. if ($data['sid']) {
  180. if (!$ischangepwd || ($ischangepwd && $data['password'] == '')) {
  181. unset($data['salt']);
  182. unset($data['password']);
  183. } else {
  184. $data['salt'] = rand_string(6);
  185. }
  186. try {
  187. $this->save($data,['sid' => $data['sid']]);
  188. return true;
  189. }catch (\think\Exception $e){
  190. trace('数据库操作失败:' . $e->getMessage(), 'error');
  191. return false;
  192. }
  193. } else {
  194. return false;
  195. }
  196. }
  197. /**
  198. * 修改账户密码
  199. * @param $data
  200. * @param bool $is_reset
  201. * @return bool
  202. */
  203. public function editPassword($data, $is_reset = false)
  204. {
  205. $sid = $is_reset ? $data['sid'] : session('user_auth.sid');
  206. if (!$is_reset && !($checkPass = $this->checkPassword($sid, $data['oldpassword']))) {
  207. return false;
  208. }else if($is_reset){
  209. $data['password'] = '123456';
  210. }
  211. $data['salt'] = rand_string(6);
  212. return $this->save($data, array('sid' => $sid));
  213. }
  214. /**
  215. * 验证账户密码
  216. * @param $sid
  217. * @param $password
  218. * @return bool
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. * @throws \think\exception\DbException
  222. */
  223. protected function checkPassword($sid, $password)
  224. {
  225. if (!$sid || !$password) {
  226. $this->error = '原始用户sid和密码不能为空';
  227. return false;
  228. }
  229. try{
  230. $this->get($sid);
  231. if (md5($password . $this->salt) === $this->password) {
  232. return true;
  233. } else {
  234. $this->error = '原始密码错误!';
  235. return false;
  236. }
  237. }catch (\think\Exception $e){
  238. trace('数据库操作失败:' . $e->getMessage(), 'error');
  239. return false;
  240. }
  241. }
  242. }