Forgetpwd.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\UsersModel;
  4. use app\common\controller\Api;
  5. use think\Cache;
  6. use think\Controller;
  7. use think\Db;
  8. use think\exception\HttpResponseException;
  9. use think\Request;
  10. use think\Response;
  11. /**
  12. * 忘记密码
  13. */
  14. class Forgetpwd extends Controller
  15. {
  16. protected $noNeedRight = '*';
  17. protected $noNeedLogin = '*';
  18. /**
  19. * 忘记密码
  20. * @ApiMethod (POST)
  21. * @param string $user_tel 用户手机号
  22. * @param string $user-pwd 密码
  23. * @param string $code 验证码
  24. */
  25. public function forgetPwd()
  26. {
  27. $params = $this->request->post();
  28. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  29. if (!preg_match($check, $params['user_tel'])) {
  30. return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
  31. }
  32. $is_tel = UsersModel::where('user_tel',$params['user_tel'])->find();
  33. if (!$is_tel) {
  34. return json(['code' => 100, 'msg' => '密码相同','data' => []]);
  35. }
  36. if (!Cache::get($params['code'])) {
  37. return json(['code' => 100, 'msg' => '验证码错误','data' => []]);
  38. }
  39. if (!isset($params['user_pwd']) || !isset($params['code'])) {
  40. return json(['code' => 100, 'msg' => '密码相同','data' => []]);
  41. }
  42. $params['user_pwd'] = sha1(md5($params['user_pwd']));
  43. //验证密码是否已存在
  44. $is_pwd = Db::name('users')->where('user_tel',$params['user_tel'])->where('user_pwd', $params['user_pwd'])->find();
  45. if ($is_pwd) {
  46. return json(['code' => 100, 'msg' => '密码相同','data' => []]);
  47. }
  48. $updPwd = Db::name('users')->where('user_tel',$params['user_tel'])->update(['user_pwd' => $params['user_pwd']]);
  49. if ($updPwd) {
  50. Cache::rm($params['code']);
  51. return json(['code' => 200, 'msg' => '修改成功','data' => []]);
  52. } else {
  53. return json(['code' => 100, 'msg' => '修改失败','data' => []]);
  54. }
  55. }
  56. }