Login.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\UsersModel;
  4. use app\common\controller\Api;
  5. use fast\Random;
  6. use think\Cache;
  7. use think\Controller;
  8. use think\Db;
  9. use think\Loader;
  10. /**
  11. * 登录接口
  12. */
  13. class Login extends Controller
  14. {
  15. protected $noNeedLogin = '*';
  16. protected $noNeedRight = '*';
  17. /**
  18. * 手机号登录
  19. * @ApiMethod (POST)
  20. * @param string $user_tel 账号
  21. * @param string $user_pwd 密码
  22. */
  23. public function Login()
  24. {
  25. $data = $this->request->post();
  26. $rules = [
  27. 'user_tel' => 'require|max:11|number|min:11',
  28. 'user_pwd' => 'require|max:25|min:6',
  29. ];
  30. $msg = [
  31. 'user_tel.require' => '手机号不能为空',
  32. 'user_tel.max' => '手机号长度不正确',
  33. 'user_tel.min' => '手机号长度不正确',
  34. 'user_tel.number' => '手机号必须为数字',
  35. 'user_pwd.require' => '密码不能为空',
  36. 'user_pwd.max' => '密码长度过长',
  37. 'user_pwd.min' => '密码长度不足',
  38. ];
  39. if (isset($data['token'])) { //先删除token
  40. $delete = \app\common\library\Token::delete($data['token']);
  41. }
  42. if (isset($data['user_tel']) && isset($data['user_pwd'])) {
  43. $validata = $this->validate($data, $rules, $msg); //验证数据规则
  44. if (is_string($validata)) {
  45. return json(['code' => 100, 'msg' => '', 'data' => $validata]);
  46. }
  47. $valdatatel = UsersModel::where('user_tel', $data['user_tel'])->find(); //判断手机号是否存在
  48. if ($valdatatel) {
  49. $data['user_pwd'] = sha1(md5($data['user_pwd'])); //加密验证密码
  50. $where = array(
  51. 'user_tel' => $data['user_tel'],
  52. 'user_pwd' => $data['user_pwd'],
  53. );
  54. $validatapwd = Db::name('users')->where($data)->field('user_id')->find(); //判断密码是否正确
  55. if ($valdatatel['status_switch'] == 0) {
  56. return json(['code' => 100, 'msg' => '此账号已被禁用', 'data' => []]);
  57. }
  58. if ($validatapwd) {
  59. $validatapwd['token'] = self::settoken($validatapwd['user_id']);
  60. return json(['code' => 200, 'msg' => '登陆成功,欢迎回来哦', 'data' => $validatapwd]);
  61. } else {
  62. return json(['code' => 100, 'msg' => '密码错误', 'data' => []]);
  63. }
  64. } else {
  65. return json(['code' => 100, 'msg' => '手机号不存在', 'data' => []]);
  66. }
  67. }
  68. }
  69. /**
  70. * 微信登录
  71. * @ApiMethod (POST)
  72. * @param string $user_nickname 昵称
  73. * @param string $user_avatar 头像
  74. * @param string $user_unionid unionID
  75. */
  76. public function wechatLogin()
  77. {
  78. $params = $this->request->post();
  79. $rules = [
  80. 'user_nickname' => 'require',
  81. 'user_avatar' => 'require',
  82. 'user_unionid' => 'require',
  83. ];
  84. $msg = [
  85. 'user_nickname.require' => '昵称未获取',
  86. 'user_avatar.require' => '头像未获取',
  87. 'user_unionid.require' => '开放id未获取',
  88. ];
  89. $validata = $this->validate($params, $rules, $msg);
  90. if (is_string($validata)) {
  91. return json(['code' => 100, 'msg' => '', 'data' => $validata]);
  92. }
  93. $validataopenid = UsersModel::where('user_unionid', $params['user_unionid'])->field('user_id,user_tel')->find(); //判断QQ或者微信登录的openid是否正确
  94. if ($validataopenid) {
  95. if ($validataopenid['user_tel'] == "") {
  96. return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $validataopenid]);
  97. }
  98. $validataopenid['token'] = self::settoken($validataopenid['user_id']);
  99. return json(['code' => 200, 'msg' => '登陆成功,欢迎回来哦', 'data' => $validataopenid]);
  100. } else {
  101. return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $params]);
  102. // $data = array(
  103. // 'user_nickname' => $params['user_nickname'],
  104. // 'user_avatar' => $params['user_avatar'],
  105. // 'create_time' => date('Y-m-d H:i:s', time()),
  106. // 'user_unionid' => $params['user_unionid'],
  107. // 'type' => 1,
  108. // );
  109. // $userId['user_id'] = Db::name('users')->insertGetId($data);
  110. // if ($userId) {
  111. // return json(['code' => 10, 'msg' => '请去绑定手机号', 'data' => []]);
  112. // } else {
  113. // return json(['code' => 100, 'msg' => '登录失败', 'data' => []]);
  114. // }
  115. }
  116. }
  117. /**
  118. * QQ登录
  119. * @ApiMethod (POST)
  120. * @param string $user_nickname 昵称
  121. * @param string $user_avatar 头像
  122. * @param string $user_unionid unionID
  123. */
  124. public function qqLogin()
  125. {
  126. $params = $this->request->post();
  127. $rules = [
  128. 'user_nickname' => 'require',
  129. 'user_avatar' => 'require',
  130. 'user_unionid' => 'require',
  131. ];
  132. $msg = [
  133. 'user_nickname.require' => '昵称未获取',
  134. 'user_avatar.require' => '头像未获取',
  135. 'user_unionid.require' => '开放id未获取',
  136. ];
  137. $validata = $this->validate($params, $rules, $msg);
  138. if (is_string($validata)) {
  139. return json(['code' => 100, 'msg' => '', 'data' => $validata]);
  140. }
  141. $validataopenid = UsersModel::where('user_unionid', $params['user_unionid'])->field('user_id,user_tel')->find(); //判断QQ或者微信登录的openid是否正确
  142. $validataopenid['token'] = self::settoken($validataopenid['user_id']);
  143. if ($validataopenid) {
  144. if ($validataopenid['user_tel'] == "") {
  145. return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $validataopenid]);
  146. }
  147. return json(['code' => 200, 'msg' => '登陆成功,欢迎回来哦', 'data' => $validataopenid]);
  148. } else {
  149. return json(['code' => 10, 'msg' => '请先绑定手机号', 'data' => $params]);
  150. // $data = array(
  151. // 'user_nickname' => $params['user_nickname'],
  152. // 'user_avatar' => $params['user_avatar'],
  153. // 'create_time' => $params('Y-m-d H:i:s', time()),
  154. // 'user_tjtel' => $params['user_tjtel'],
  155. // 'user_unionid' => $params['user_openid'],
  156. // 'type' => 2,
  157. // );
  158. // $userId['user_id'] = Db::name('users')->insertGetId($params);
  159. // if ($userId) {
  160. //
  161. // return json(['code' => 10, 'msg' => '请先绑定手机号','data' => []]);
  162. // } else {
  163. // return json(['code' => 100, 'msg' => '登录失败','data' => []]);
  164. // }
  165. }
  166. }
  167. /**
  168. * 添加登录手机号登录
  169. * @ApiMethod (POST)
  170. * @param string $user_tel 用户手机号
  171. * @param string $code 验证码
  172. * @param string $user_tjtel 用户手机号
  173. * @param string $user_nickname 昵称
  174. * @param string $user_avatar 头像
  175. * @param string $user_unionid unionID
  176. * @param string $type 0手机号1微信2QQ
  177. */
  178. public function addUserTel()
  179. {
  180. $params = $this->request->post();
  181. $rules = [
  182. // 'user_id' => 'require|number',
  183. 'code' => 'require|number',
  184. 'user_tel' => 'require|number',
  185. 'user_tjtel' => 'require|number',
  186. //'token' => 'require',
  187. ];
  188. $msg = [
  189. //'user_id.require' => '网络错误1',
  190. 'code .require' => '网络错误2',
  191. 'user_tel.require' => '手机号不能为空',
  192. 'user_tjtel.require' => '推荐人手机号不能为空',
  193. //'token.require' => '网络错误3',
  194. // 'user_id.number' => '网络错误4',
  195. 'code.number' => '网络错误5',
  196. 'user_tel.number' => '网络错误6',
  197. 'user_tjtel.number' => '网络错误7',
  198. ];
  199. $validate = $this->validate($params, $rules, $msg);
  200. if (is_string($validate)) {
  201. return json(['code' => 100, 'msg' => $validate, 'data' => []]);
  202. }
  203. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  204. if (!preg_match($check, $params['user_tel'])) {
  205. return json(['code' => 100, 'msg' => '手机号不合法', 'data' => []]);
  206. }
  207. //检查数据库是否有此推荐人手机号
  208. $user_tjtel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
  209. if (!$user_tjtel) {
  210. return json(['code' => 100, 'msg' => '无此推荐人', 'data' => []]);
  211. }
  212. //判断验证码是否正确
  213. if (!Cache::get($params['code'])) {
  214. return json(['code' => 100, 'msg' => '验证码不正确', 'data' => []]);
  215. }
  216. //Cache::rm($params['code']);
  217. // 判断是否有此手机号
  218. $user_tel = UsersModel::where('user_tel', $params['user_tel'])->find();
  219. if ($user_tel) {
  220. $upddata = array(
  221. 'user_tjtel' => $params['user_tjtel'],
  222. 'type' => $params['type'],
  223. 'create_time' => date('Y-m-d H:i:s', time()),
  224. 'user_unionid' => $params['user_unionid'],
  225. 'user_nickname' => $params['user_nickname'],
  226. 'user_avatar' => $params['user_avatar'],
  227. );
  228. $addUserMember = Db::name('Users')->where('user_tel', $params['user_tjtel'])->setInc('user_member_count', 1); // 给上机的下级成员总数加1
  229. $updUsertelAndwechat = Db::name('users')->where('user_id', $user_tel['user_id'])->update($upddata);
  230. if ($updUsertelAndwechat) {
  231. $token = self::settoken($user_tel['user_id']);
  232. $res = array(
  233. "user_id" => $user_tel['user_id'],
  234. "token" => $token,
  235. );
  236. return json(['code' => 200, 'msg' => '绑定成功', 'data' => $res]);
  237. } else {
  238. return json(['code' => 100, 'msg' => '绑定失败', 'data' => []]);
  239. }
  240. }
  241. $data = array(
  242. 'user_tel' => $params['user_tel'],
  243. 'user_tjtel' => $params['user_tjtel'],
  244. 'type' => $params['type'],
  245. 'create_time' => date('Y-m-d H:i:s', time()),
  246. 'user_unionid' => $params['user_unionid'],
  247. 'user_nickname' => $params['user_nickname'],
  248. 'user_avatar' => $params['user_avatar'],
  249. );
  250. $adduser = Db::name('users')->insertGetId($data);
  251. if ($adduser) {
  252. $token = self::settoken($adduser);
  253. $res = array(
  254. "user_id" => $adduser,
  255. "token" => $token,
  256. );
  257. return json(['code' => 200, 'msg' => '绑定成功', 'data' => $res]);
  258. } else {
  259. return json(['code' => 100, 'msg' => '绑定失败', 'data' => []]);
  260. }
  261. }
  262. /**
  263. * 验证手机号是否已存在
  264. * @ApiMethod (POST)
  265. * 可以通过@ApiInternal忽略请求的方法
  266. * @ApiInternal
  267. */
  268. public function validatatel($tel)
  269. {
  270. $tel = UsersModel::where('user_tel', $tel)->find();
  271. $num = count($tel);
  272. if ($num > 0) {
  273. return json(['code' => 100, 'msg' => '手机号已存在', 'data' => []]);
  274. }
  275. }
  276. /**
  277. * 服务协议
  278. * @ApiMethod (POST)
  279. */
  280. public function agreement()
  281. {
  282. $data = Db::name('agreement')->where('type', 0)->find();
  283. if ($data) {
  284. return json(['code' => 200, 'msg' => '绑定成功', 'data' => $data]);
  285. } else {
  286. return json(['code' => 100, 'msg' => '网络错误', 'data' => []]);
  287. }
  288. }
  289. /**
  290. * 生成token
  291. *
  292. * 可以通过@ApiInternal忽略请求的方法
  293. * @ApiInternal
  294. */
  295. public function settoken($user_id)
  296. {
  297. $token = Random::uuid();
  298. \app\common\library\Token::set($token, $user_id, 0);
  299. return $tokenInfo = \app\common\library\Token::get($token);
  300. }
  301. }