verify_sign(); // 接收参数 $code = request('code',''); // 授权结果 $result = Mini::getUserPhone($code); // 如果所需字段不存在 if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>'授权失败','data'=>['error'=>$result['error']]]); // 如果所需字段不存在 if( empty($result['purePhoneNumber']) ) return json_send(['code'=>'error','msg'=>'未获取到手机号','data'=>['error'=>'未获取到手机号']]); // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号) $phone = $result['purePhoneNumber']; // 查询用户 $custom = $Custom->getOneByPhone($phone); // 如果用户状态被拉黑,不允许登录 if( !empty($custom['status']) ) return json_send(['code'=>'error','msg'=>'禁用账号','data'=>['error'=>'禁用账号']]); // 如果没有ID if( empty($custom['uid']) ) { // 注册账号 $custom['uid'] = $Custom->add(['phone'=>$phone,'username'=>hide_phone($phone)]); // 注册失败 if( empty($custom['uid']) ) return json_send(['code'=>'error','msg'=>'注册失败,请重试','data'=>['error'=>'注册失败,请重试']]); } // 进行登录 $token = $Custom->createLoginAuthcode($custom['uid'],time()); // 返回结果 return json_send(['code'=>'success','msg'=>'登录成功','data'=>$token]); } }