Login.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\DataUser;
  4. use app\data\service\MessageService;
  5. use app\data\service\UserAdminService;
  6. use think\admin\Controller;
  7. /**
  8. * 用户登录注册接口
  9. * Class Login
  10. * @package app\data\controller\api
  11. */
  12. class Login extends Controller
  13. {
  14. /**
  15. * 接口认证类型
  16. * @var string
  17. */
  18. private $type;
  19. /**
  20. * 控制器初始化
  21. */
  22. protected function initialize()
  23. {
  24. // 接收接口类型
  25. $this->type = $this->request->request('api');
  26. $this->type = $this->type ?: $this->request->header('api-name');
  27. $this->type = $this->type ?: $this->request->header('api-type');
  28. $this->type = $this->type ?: UserAdminService::API_TYPE_WAP;
  29. if (empty(UserAdminService::TYPES[$this->type])) {
  30. $this->error("接口支付[{$this->type}]未定义规则!");
  31. }
  32. }
  33. /**
  34. * 用户登录接口
  35. * @throws \think\admin\Exception
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function in()
  41. {
  42. $data = $this->_vali([
  43. 'phone.mobile' => '手机号码格式错误!',
  44. 'phone.require' => '手机号码不能为空!',
  45. 'password.require' => '登录密码不能为空!',
  46. ]);
  47. $map = ['deleted' => 0, 'phone' => $data['phone']];
  48. $user = DataUser::mk()->where($map)->find();
  49. if (empty($user)) $this->error('该手机号还没有注册哦!');
  50. if (empty($user['status'])) $this->error('该用户账号状态异常!');
  51. if (md5($data['password']) === $user['password']) {
  52. $this->success('手机登录成功!', UserAdminService::instance()->set($map, [], $this->type, true));
  53. } else {
  54. $this->error('账号登录失败,请稍候再试!');
  55. }
  56. }
  57. /**
  58. * 用户统一注册入口
  59. * @throws \think\admin\Exception
  60. * @throws \think\db\exception\DbException
  61. */
  62. public function register()
  63. {
  64. $data = $this->_vali([
  65. 'region_province.default' => '',
  66. 'region_city.default' => '',
  67. 'region_area.default' => '',
  68. 'username.default' => '',
  69. 'phone.mobile' => '手机号码格式错误!',
  70. 'phone.require' => '手机号码不能为空!',
  71. // 'verify.require' => '验证码不能为空!',
  72. 'password.require' => '登录密码不能为空!',
  73. ]);
  74. // if (MessageService::instance()->checkVerifyCode($data['verify'], $data['phone'])) {
  75. // @验证码验证能完
  76. // } else {
  77. // $this->error('验证失败!');
  78. // }
  79. $map = ['phone' => $data['phone'], 'deleted' => 0];
  80. if (DataUser::mk()->where($map)->count() > 0) {
  81. $this->error('手机号已注册,请使用其它手机号!');
  82. }
  83. $data['password'] = md5($data['password']);
  84. $user = UserAdminService::instance()->set($map, $data, $this->type, true);
  85. empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
  86. }
  87. /**
  88. * 发送短信验证码
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function sendsms()
  94. {
  95. $data = $this->_vali([
  96. 'phone.mobile' => '手机号格式错误!',
  97. 'phone.require' => '手机号不能为空!',
  98. 'secure.require' => '安全码不能为空!',
  99. ]);
  100. if ($data['secure'] !== sysconf('zt.secure_code')) $this->error('接口安全码错误!');
  101. [$state, $message, $data] = MessageService::instance()->sendVerifyCode($data['phone']);
  102. $state ? $this->success($message, $data) : $this->error($message, $data);
  103. }
  104. }