LoginController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\index\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Users;
  5. use app\model\Member;
  6. use app\model\UuidRelation;
  7. use app\model\Openid;
  8. use app\model\MessageSms;
  9. use app\model\Technical;
  10. use app\model\Operatingcity;
  11. use app\model\Store;
  12. class LoginController extends Base
  13. {
  14. //用户登录
  15. public function index()
  16. {
  17. $data = input('post.');
  18. $ptype = input('post.ptype', '', 'serach_in');
  19. if ($data['username'] == 'undefined') {
  20. $data['username'] = '';
  21. }
  22. if ($data['password'] == 'undefined') {
  23. $data['password'] = '';
  24. }
  25. if ($data['userphone'] == 'undefined') {
  26. $data['userphone'] = '';
  27. }
  28. if ($data['captcha'] == 'undefined') {
  29. $data['captcha'] = '';
  30. }
  31. if (empty($data['username'])) {
  32. $data['username'] = $data['tel'];
  33. }
  34. $data['username'] = trim($data['username']);
  35. if (!empty($data['userphone'])) {
  36. if (empty($data['captcha'])) {
  37. throw new ValidateException('请输入验证');
  38. }
  39. $res = $this->checkphonecaptcha($data);
  40. } else {
  41. if (empty($data['username'])) {
  42. throw new ValidateException('请输入用户名');
  43. }
  44. if (empty($data['password'])) {
  45. throw new ValidateException('请输入密码');
  46. }
  47. $res = $this->checkLogin($data);
  48. }
  49. if ($res) {
  50. $this->userInfo['uuid'] = $res['uuid'];
  51. $this->setAppToken($this->userInfo, $this->getAppToken());
  52. UuidRelation::create(['weid' => weid(), 'ptype' => $ptype, 'uuid' => $res['uuid'], 'uid' => UID()]);
  53. return $this->json(['msg' => '登录成功', 'data' => $res]);
  54. Users::where('id', $res['id'])->update(['login_time' => time(), 'login_ip' => client_ip()]);
  55. }
  56. }
  57. //获取小程序手机号
  58. public function getphonenumber()
  59. {
  60. $code = input('post.code', '', 'serach_in');
  61. $phonedata = \app\samos\wechat\MiniProgram::getphonenumber($code);
  62. $data = Member::bindphonenumber($phonedata->phoneNumber);
  63. $phone = $this->logintype($phonedata->phoneNumber);
  64. if ($phone && $phone['status'] == 1) {
  65. $data['is_reg'] = 0;
  66. } else {
  67. $data['is_reg'] = 1;
  68. }
  69. return $this->json(['data' => $data]);
  70. }
  71. //验证登录
  72. private function checkLogin($data)
  73. {
  74. $where['username'] = $data['username'];
  75. $field = 'id,weid,uuid,uid,sid,title,username,password,salt,create_time,status';
  76. $info = Users::field($field)->where($where)->find();
  77. if (empty($info)) {
  78. $uuidarray = $this->logintype($data['username']);
  79. if ($uuidarray['uuid']) {
  80. $info = Users::field($field)->where('uuid', $uuidarray['uuid'])->find();
  81. }
  82. }
  83. if ($info) {
  84. $info = $info->toArray();
  85. if ($info['password'] === pass_hash($data['password'], $info["salt"])) {
  86. unset($info['password']);
  87. unset($info["salt"]);
  88. if (empty($info['uuid'])) {
  89. $info['uuid'] = uniqid(rand(1, 10000));
  90. Users::where('id', $info['id'])->update(['uuid' => $info['uuid']]);
  91. }
  92. if (!($info['status'])) {
  93. throw new ValidateException("该账户被禁用");
  94. }
  95. } else {
  96. throw new ValidateException("用户名或者密码不正确");
  97. }
  98. } else {
  99. throw new ValidateException("用户名不存在");
  100. }
  101. return $info;
  102. }
  103. //验证登录
  104. private function checkphonecaptcha($data)
  105. {
  106. if ($data['captcha'] == $this->userInfo['captcha']) {
  107. $info = Member::bindphonenumber($data['userphone']);
  108. $phone = $this->logintype($data['userphone']);
  109. if ($phone && $phone['status'] == 1) {
  110. $info['is_reg'] = 0;
  111. } else {
  112. $info['is_reg'] = 1;
  113. }
  114. $from = input('get.from', '', 'serach_in');
  115. if ($from != 'wxapp' && $from != 'mp') {
  116. $this->userInfo['openid'] = $data['userphone'];
  117. }
  118. } else {
  119. throw new ValidateException("验证码不正确" . $this->getAppToken() . 'dssssss');
  120. }
  121. return $info;
  122. }
  123. //手机短信验证码
  124. public function captcha()
  125. {
  126. $phone = input('post.phone', '', 'serach_in');
  127. if (!empty($phone)) {
  128. $this->userInfo['captcha'] = rand(1111, 9999);
  129. $data = MessageSms::send_sms([
  130. 'phone' => $phone,
  131. 'param' => json_encode([
  132. 'code' => $this->userInfo['captcha']
  133. ])
  134. ]);
  135. $this->setAppToken($this->userInfo, $this->getAppToken());
  136. }
  137. return $this->json(['msg' => '手机验证码发送成功', 'data' => $data]);
  138. }
  139. //用户退出登录
  140. public function logout()
  141. {
  142. $ptype = input('post.ptype', '', 'serach_in');
  143. if ($ptype) {
  144. UuidRelation::where(['weid' => weid(), 'ptype' => $ptype, 'uid' => UID()])->delete();
  145. $this->userInfo[$ptype] = '';
  146. $this->setAppToken($this->userInfo, $this->getAppToken());
  147. }
  148. return $this->json(['msg' => '退出成功']);
  149. }
  150. //手机登录
  151. public function logintype($tel)
  152. {
  153. $ptype = input('post.ptype', '', 'serach_in');
  154. if ($ptype == 'technical') {
  155. $res = Technical::where('tel', $tel)->find();
  156. if ($res) {
  157. $res = $res->toArray();
  158. }
  159. }
  160. if ($ptype == 'operatingcity') {
  161. $res = Operatingcity::where('tel', $tel)->find();
  162. if ($res) {
  163. $res = $res->toArray();
  164. }
  165. }
  166. if ($ptype == 'store') {
  167. $res = Store::where('tel', $tel)->find();
  168. if ($res) {
  169. $res = $res->toArray();
  170. }
  171. }
  172. if ($ptype == 'member') {
  173. $res['status'] = 1;
  174. }
  175. return $res;
  176. }
  177. }