Login.php 4.0 KB

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