Login.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-04-30
  10. // +—————————————————————————————————————————————————————————————————————
  11. session_start();
  12. class Api_Login extends PhalApi_Api {
  13. public function getRules() {
  14. return array(
  15. 'userLogin' => array(
  16. 'user_login' => array('name' => 'user_login', 'type' => 'string', 'desc' => '账号'),
  17. 'code' => array('name' => 'code', 'type' => 'string', 'require' => true, 'desc' => '验证码'),
  18. 'source' => array('name' => 'source', 'type' => 'string', 'desc' => '注册来源android/ios'),
  19. 'mobileid' => array('name' => 'mobileid', 'type' => 'string', 'desc' => '手机设备号'),
  20. ),
  21. 'getLoginCode' => array(
  22. 'country_code' => array('name' => 'country_code', 'type' => 'int','default'=>'86', 'require' => true, 'desc' => '国家代号'),
  23. 'mobile' => array('name' => 'mobile', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '手机号'),
  24. 'time' => array('name' => 'time', 'type' => 'string', 'desc' => '时间戳'),
  25. 'sign' => array('name' => 'sign', 'type' => 'string', 'default'=>'', 'desc' => '签名'),
  26. ),
  27. 'getCountrys'=>array(
  28. 'field' => array('name' => 'field', 'type' => 'string', 'default'=>'', 'desc' => '搜索json串'),
  29. ),
  30. );
  31. }
  32. /**
  33. * 会员登录
  34. * @desc 用于用户登陆
  35. * @return int code 操作码,0表示成功
  36. * @return array info 用户信息
  37. * @return string info[0].id 用户ID
  38. * @return string info[0].user_nicename 昵称
  39. * @return string info[0].avatar 头像
  40. * @return string info[0].avatar_thumb 头像缩略图
  41. * @return string info[0].sex 性别
  42. * @return string info[0].signature 签名
  43. * @return string info[0].coin 用户余额
  44. * @return string info[0].login_type 注册类型
  45. * @return string info[0].province 省份
  46. * @return string info[0].city 城市
  47. * @return string info[0].birthday 生日
  48. * @return string info[0].token 用户Token
  49. * @return string msg 提示信息
  50. */
  51. public function userLogin() {
  52. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  53. $user_login=checkNull($this->user_login);
  54. $code=checkNull($this->code);
  55. $source=checkNull($this->source);
  56. $mobileid=checkNull($this->mobileid);
  57. if(!$user_login){
  58. $rs['code']=1001;
  59. $rs['msg']='请填写手机号';
  60. return $rs;
  61. }
  62. $ismobile=checkMobile($user_login);
  63. if(!$ismobile){
  64. $rs['code']=1001;
  65. $rs['msg']='请输入正确的手机号';
  66. return $rs;
  67. }
  68. if($code==''){
  69. $rs['code'] = 1001;
  70. $rs['msg'] = '请填写验证码';
  71. return $rs;
  72. }
  73. if($mobileid==''){
  74. $rs['code'] = 1001;
  75. $rs['msg'] = '缺少设备码';
  76. return $rs;
  77. }
  78. if(!$_SESSION['login_mobile']){
  79. $rs['code'] = 1001;
  80. $rs['msg'] = '请获取验证码';
  81. return $rs;
  82. }
  83. if($user_login!=$_SESSION['login_mobile']){
  84. $rs['code'] = 1001;
  85. $rs['msg'] = '手机号码错误';
  86. return $rs;
  87. }
  88. if($code!=$_SESSION['login_mobile_code']){
  89. $rs['code'] = 1001;
  90. $rs['msg'] = '验证码错误';
  91. return $rs;
  92. }
  93. $domain = new Domain_Login();
  94. $info = $domain->userLogin($user_login,$source,$mobileid);
  95. if($info==1001){
  96. $rs['code'] = 1001;
  97. $rs['msg'] = '同一设备同一IP下注册账号过多';
  98. return $rs;
  99. }
  100. if($info==1002){
  101. $rs['code'] = 1002;
  102. $rs['msg'] = '该账号已被禁用';
  103. return $rs;
  104. }
  105. if($info==1003){
  106. $rs['code'] = 1003;
  107. $rs['msg'] = '该账号已注销';
  108. return $rs;
  109. }
  110. if($info==1004){
  111. $rs['code'] = 1004;
  112. $rs['msg'] = '管理员账号无法登陆';
  113. return $rs;
  114. }
  115. $rs['info'][0] = $info;
  116. return $rs;
  117. }
  118. /**
  119. * 获取登录短信验证码
  120. * @desc 用于登录获取短信验证码
  121. * @return int code 操作码,0表示成功,2发送失败
  122. * @return array info
  123. * @return string msg 提示信息
  124. */
  125. public function getLoginCode() {
  126. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  127. $country_code = checkNull($this->country_code);
  128. $mobile = checkNull($this->mobile);
  129. $time=checkNull($this->time);
  130. $sign=checkNull($this->sign);
  131. if(!$mobile){
  132. $rs['code']=1001;
  133. $rs['msg']='请填写手机号';
  134. return $rs;
  135. }
  136. $configpri=getConfigPri();
  137. $code_switch=$configpri['code_switch'];
  138. $aly_sendcode_type=$configpri['aly_sendcode_type'];
  139. if($aly_sendcode_type==1){ //国内验证码
  140. if($country_code!=86){
  141. $rs['code']=1001;
  142. $rs['msg']='平台只允许选择中国大陆';
  143. return $rs;
  144. }
  145. $ismobile=checkMobile($mobile);
  146. if(!$ismobile){
  147. $rs['code']=1001;
  148. $rs['msg']='请输入正确的手机号';
  149. return $rs;
  150. }
  151. }else if($aly_sendcode_type==2){ //海外/港澳台 验证码
  152. if($country_code==86){
  153. $rs['code']=1001;
  154. $rs['msg']='平台只允许选择除中国大陆外的国家/地区';
  155. return $rs;
  156. }
  157. }
  158. $checkdata=array(
  159. 'mobile'=>$mobile,
  160. 'time'=>$time,
  161. );
  162. $issign=checkSign($checkdata,$sign);
  163. if(!$issign){
  164. $rs['code']=1001;
  165. $rs['msg']='签名错误';
  166. return $rs;
  167. }
  168. //验证手机号是否被禁用
  169. $status=checkMoblieCanCode($mobile);
  170. if($status==0){
  171. $rs['code']=1001;
  172. $rs['msg']='该账号已被禁用';
  173. return $rs;
  174. }
  175. if($_SESSION['country_code']==$country_code && $_SESSION['login_mobile']==$mobile && $_SESSION['login_mobile_expiretime']> time() ){
  176. $rs['code']=1002;
  177. $rs['msg']='验证码5分钟有效,请勿多次发送';
  178. return $rs;
  179. }
  180. $limit = ip_limit();
  181. if( $limit == 1){
  182. $rs['code']=1003;
  183. $rs['msg']='您已当日发送次数过多';
  184. return $rs;
  185. }
  186. $mobile_code = random(6,1);
  187. /* 发送验证码 */
  188. $result=sendCode($country_code,$mobile,$mobile_code);
  189. if($result['code']===0){
  190. $_SESSION['country_code'] = $country_code;
  191. $_SESSION['login_mobile'] = $mobile;
  192. $_SESSION['login_mobile_code'] = $mobile_code;
  193. $_SESSION['login_mobile_expiretime'] = time() +60*5;
  194. }else if($result['code']==667){
  195. $_SESSION['country_code'] = $country_code;
  196. $_SESSION['login_mobile'] = $mobile;
  197. $_SESSION['login_mobile_code'] = $result['msg'];
  198. $_SESSION['login_mobile_expiretime'] = time() +60*5;
  199. $rs['code']=$result['code'];
  200. $rs['msg']='验证码为:'.$result['msg'];
  201. return $rs;
  202. }else{
  203. $rs['code']=1002;
  204. $rs['msg']=$result['msg'];
  205. return $rs;
  206. }
  207. $rs['msg']="发送成功";
  208. return $rs;
  209. }
  210. /**
  211. * 获取国家列表
  212. * @desc 用于获取国家列表
  213. * string field 搜索内容
  214. * @return int code 操作码,0表示成功
  215. * @return array info
  216. * @return string name 国家中文名称
  217. * @return string name_name 国家英文名称
  218. * @return string tel 国家区号
  219. * @return string msg 提示信息
  220. */
  221. public function getCountrys() {
  222. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  223. $field=checkNull($this->field);
  224. $key='getCountrys';
  225. $info=getcaches($key);
  226. if(!$info){
  227. $country=API_ROOT.'/../data/config/country.json';
  228. // 从文件中读取数据到PHP变量
  229. $json_string = file_get_contents($country);
  230. // 用参数true把JSON字符串强制转成PHP数组
  231. $data = json_decode($json_string, true);
  232. $info=$data['country']; //国家
  233. setcaches($key,$info);
  234. }
  235. if($field){
  236. $rs['info']=$this->country_searchs($field,$info);
  237. return $rs;
  238. }
  239. $rs['info']=$info;
  240. return $rs;
  241. }
  242. private function country_searchs($field,$data) {
  243. $arr=array();
  244. foreach($data as $k => $v){
  245. $lists=$v['lists'];
  246. foreach ($lists as $k => $v) {
  247. if(strstr($v['name'], $field) !== false){//英文搜索替换为:$v['name_en']
  248. array_push($arr, $v);
  249. }
  250. }
  251. }
  252. return $arr;
  253. }
  254. }