Index.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\index\controller;
  15. use library\Controller;
  16. use think\Db;
  17. /**
  18. * 应用入口
  19. * Class Index
  20. * @package app\index\controller
  21. */
  22. class Index extends Controller
  23. {
  24. /**
  25. * 入口跳转链接
  26. */
  27. public function index()
  28. {
  29. $this->redirect('@admin/login');
  30. }
  31. /**
  32. * 注册
  33. *
  34. * username 用户姓名
  35. * mobile 手机号
  36. * pwd 密码
  37. * level 0业主1德高会员2分销会员
  38. */
  39. public function register()
  40. {
  41. $data = $this->request->post();
  42. if (!isset($data['username']) || empty($data['username'])) return $this->error('请输入姓名');
  43. if (!isset($data['mobile']) || empty($data['mobile'])) return $this->error('请输入手机号');
  44. if (!isset($data['pwd']) || empty($data['pwd'])) return $this->error('请输入密码');
  45. if(strlen($data['username'])<3 || strlen($data['username'])>12) return $this->error('姓名长度为3-12位');
  46. if(!preg_match("/^1[34578]\d{9}$/", $data['mobile'])) return $this->error('手机号格式不正确');
  47. if(strlen($data['pwd'])<6 || strlen($data['pwd'])>18) return $this->error('密码长度为6-18位');
  48. $data['pwd'] = md5(md5($data['pwd']));
  49. // 检测该手机号是否注册过
  50. $isReg = Db::name('user')->where('mobile',$data['mobile'])->where('level',$data['level'])->find();
  51. if ($isReg) return $this->error('该手机号已被注册');
  52. $register = Db::name('user')->insert($data);
  53. if ($register) {
  54. return $this->success('注册成功');
  55. } else {
  56. return $this->error('注册失败');
  57. }
  58. }
  59. /**
  60. * 登录
  61. * mobile 手机号
  62. * pwd 密码
  63. * level 0业主1德高会员2分销会员
  64. */
  65. public function login()
  66. {
  67. $data = $this->request->post();
  68. if (!isset($data['mobile']) || empty($data['mobile'])) return $this->error('请输入手机号');
  69. if(!preg_match("/^1[34578]\d{9}$/", $data['mobile'])) return $this->error('手机号格式不正确');
  70. if (!isset($data['pwd']) || empty($data['pwd'])) return $this->error('请输入密码');
  71. if(strlen($data['pwd'])<6 || strlen($data['pwd'])>18) return $this->error('密码长度为6-18位');
  72. $isMobile = Db::name('user')->where('mobile',$data['mobile'])->where('level',$data['level'])->find();
  73. if ($isMobile) {
  74. $data['pwd'] = md5(md5($data['pwd']));
  75. $login = Db::name('user')->where($data)->find();
  76. if ($login) {
  77. $token = md5(md5(time()));
  78. $userTokenData = [
  79. 'uid' => $login['id'],
  80. 'token' => $token,
  81. 'create_time' => time(),
  82. ];
  83. Db::name('user_token')->where('uid',$login['id'])->delete();
  84. $saveToken = Db::name('user_token')->insert($userTokenData);
  85. $login['token'] = $token;
  86. unset($login['pwd']);
  87. return $this->success('登录成功',$login);
  88. } else {
  89. return $this->error('密码错误');
  90. }
  91. } else {
  92. return $this->error('暂未找到该账号');
  93. }
  94. }
  95. /**
  96. * 上传图片
  97. */
  98. public function upload()
  99. {
  100. $folder = 'images';
  101. $files = request()->file();
  102. if (empty($files) || $files == null) {
  103. $this->error('请上传文件');
  104. }
  105. $imags = [];
  106. $errors = [];
  107. $root = $this->app->getRootPath();
  108. foreach ($files as $file) {
  109. if ($folder) { //保存目录
  110. // 移动到框架应用根目录/public/uploads/ 目录下
  111. $info = $file->move($root.'/public/upload/'. $folder);
  112. if ($info) {
  113. // 成功上传后 获取上传信息
  114. //echo $info->getFilename();
  115. // $path = '/uploads/editor/' . $info->getSaveName();
  116. $path = $folder . '/' . $info->getSaveName();
  117. $fileName = str_replace('\\', '/', $path);
  118. array_push($imags, '/' . 'uploads/'.$fileName);
  119. } else {
  120. array_push($errors, $file->getError()); // 上传失败获取错误信息
  121. }
  122. }
  123. }
  124. if (!$errors) {
  125. $msg['code'] = 1;
  126. $msg['data'] = $imags;
  127. return json($msg);
  128. } else {
  129. $msg['code'] = 0;
  130. $msg['data'] = $imags;
  131. $msg['msg'] = "上传出错";
  132. return json($msg);
  133. }
  134. }
  135. }