123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Ems;
- use app\common\library\Sms;
- use fast\Random;
- use think\Config;
- use think\Validate;
- use app\common\library\Token;
- use think\Db;
- use app\common\model\UserDeviceInfo;
- use onlogin\onlogin;
- class User extends Api
- {
- protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'onlogin'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- if (!Config::get('fastadmin.usercenter')) {
- $this->error(__('User center already closed'));
- }
- }
-
- public function index()
- {
- $this->success('', ['welcome' => $this->auth->nickname]);
- }
-
- public function login()
- {
- $account = input('account');
- $password = input('password');
- if (!$account || !$password) {
- $this->error(__('Invalid parameters'));
- }
- $ret = $this->auth->login($account, $password);
- if ($ret) {
- $data = $this->userInfo('return');
- $this->success(__('Logged in successful'), $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
-
- public function mobilelogin()
- {
- $mobile = input('mobile');
- $captcha = input('captcha');
- if (!$mobile || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
- $this->error(__('Captcha is incorrect'));
- }
- $user = \app\common\model\User::getByMobile($mobile);
- if ($user) {
- if ($user->status == -1) {
- $this->error('账户已注销');
- }
- if ($user->status != 1) {
- $this->error(__('Account is locked'));
- }
-
- $ret = $this->auth->direct($user->id);
- } else {
- $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
- }
- if ($ret) {
- Sms::flush($mobile, 'mobilelogin');
- $data = $this->userInfo('return');
- $this->success(__('Logged in successful'), $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
-
-
-
- public function wechatlogin(){
- $nickname = input('nickname','');
- $avatar = input('avatar','');
- $gender = input('gender',1);
- $wechat_openid = input('wechat_openid','');
- if (!$wechat_openid) {
- $this->error(__('Invalid parameters'));
- }
- if($gender != 1){
- $gender = 0;
- }
- $user = \app\common\model\User::getByOpenid($wechat_openid);
- if ($user) {
- if ($user->status != 1) {
- $this->error(__('Account is locked'));
- }
-
- $ret = $this->auth->direct($user->id);
- } else {
- if (!$nickname || !$avatar) {
- $this->error(__('Invalid parameters'));
- }
- $reg_data = ['nickname'=>$nickname,'avatar'=>$avatar,'gender'=>$gender];
- $ret = $this->auth->openid_register($wechat_openid,$reg_data);
- }
- if ($ret) {
- $data = $this->userInfo('return');
- $this->success(__('Logged in successful'), $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
-
- public function onLogin()
- {
- $accessToken = input('accessToken');
- $token = input('tokenT');
- if (!$accessToken || !$token) {
- $this->error("参数获取失败!");
- }
- $params = array(
-
- "accessToken" => $accessToken,
-
- "token" => $token
- );
-
- $configInfo = config("onLogin");
- $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
- $onret = $onlogin->check($params);
- if ($onret["code"] == 200) {
- $mobile = $onret["data"]["phone"];
- if (empty($mobile)) {
-
- $this->error("取号登录失败,请用验证码方式登录!");
- } else {
-
-
- $user = \app\common\model\User::getByMobile($mobile);
- if ($user) {
- if ($user->status != 1) {
- $this->error(__('Account is locked'));
- }
-
- $ret = $this->auth->direct($user->id);
- $is_register = 0;
- } else {
- $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
- $is_register = 1;
- }
-
-
- if ($ret) {
- $this->success(__('Logged in successful'), $this->auth->getUserinfo());
- } else {
- $this->error($this->auth->getError());
- }
-
- }
- } else {
- $this->error("登录失败,请用验证码方式登录!");
- }
- }
-
- public function userInfo($type = 1){
- $info = $this->auth->getUserinfo();
- if($type == 'return'){
- return $info;
- }
- $this->success(__('success'),$info);
- }
-
- public function userintroinfo(){
- $intro_num = Db::name('user')->where('intro_uid',$this->auth->id)->count();
- $money_sum = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63])->sum('change_value');
- $user_list = Db::name('user')->field('id,avatar,nickname,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
- $rs = [
- 'intro_num' => $intro_num,
- 'money_sum' => $money_sum,
- 'user_list' => $user_list,
- ];
- $this->success('success',$rs);
- }
- public function testhaibao(){
- $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
- dump($haibao);
- }
-
- public function haibao($player_id,$data){
-
- $params = [
- 'text' => config('site.domain_name').'/index/index/appdownload',
- 'size' => 90,
- 'logo' => false,
- 'label' => false,
- 'padding' => 0,
- ];
- $qrCode = \addons\qrcode\library\Service::qrcode($params);
- $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
- mk_dir($qrcode_path);
- $download_qrcode = $qrcode_path.'/download.png';
- $qrCode->writeFile($download_qrcode);
-
- $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
- return $haibao;
- }
- public function createhaibao($download_qrcode,$player_id,$sub_data){
-
- $background = config('site.domain_name').'/assets/img/haibao.png';
-
- $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
- mk_dir($new_path);
- $wap_file_name = $new_path .'wap_player_'. $player_id . '.png';
-
- $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
-
- $image = new \addons\poster\library\Image2();
- $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
- return '/'.$wap_file_name;
- }
-
- public function apply_real_confirm(){
- $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
- if(!$avatar){
- $this->error('请上传真人头像');
- }
- Db::startTrans();
- $data = [
- 'avatar' => $avatar,
- 'real_status' => 1,
- ];
- $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
- if($rs === false){
- Db::rollback();
- $this->error('认证失败');
- }
-
-
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
-
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
-
- $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
- if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
- $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
- if($task_rs['status'] === false){
- Db::rollback();
- $this->error($task_rs['msg']);
- }
- }
-
- $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
- Db::commit();
- $this->success();
- }
-
- public function idcard_confirm_info(){
- $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
- $this->success('success',$check);
- }
-
- public function apply_idcard_confirm(){
- $truename = input('truename','');
- $idcard = input('idcard','');
-
- $alipay_account = input('alipay_account','');
- if(empty($truename) || empty($idcard) || empty($alipay_account)){
- $this->error('实名认证信息必填');
- }
- if($this->auth->idcard_status == 1){
- $this->error('您已经完成实名认证');
- }
- if($this->auth->idcard_status == 0){
- $this->error('您已经提交实名认证,请等待审核');
- }
- Db::startTrans();
- $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
- if(!empty($check)){
- if($check['status'] == 0){
- Db::rollback();
- $this->error('您已经提交实名认证,请等待审核');
- }
- if($check['status'] == 1){
- Db::rollback();
- $this->error('您已经完成实名认证');
- }
- }
- $data = [
- 'user_id' => $this->auth->id,
- 'truename' => $truename,
- 'idcard' => $idcard,
-
- 'alipay_account' => $alipay_account,
- 'status' => 0,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
-
- $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
- if(!empty($check)){
- $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
- }else{
- $rs = Db::name('user_idconfirm')->insertGetId($data);
- }
- if(!$rs || !$update_rs){
- Db::rollback();
- $this->error('提交失败');
- }
- Db::commit();
- $this->success('提交成功,请等待审核');
- }
-
- public function logout()
- {
- if (!$this->request->isPost()) {
- $this->error(__('Invalid parameters'));
- }
-
- $tenIm = new Tenim();
- $tenIm->loginoutim($this->auth->id);
- $this->auth->logout();
- $this->success(__('Logout successful'));
- }
-
- public function profile()
- {
- $field_array = ['nickname','introcode','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo'];
- $data = [];
- foreach($field_array as $key => $field){
- if(!input('?'.$field)){
- continue;
- }
- $newone = input($field);
- if($field == 'avatar'){
- $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
- }
- if($field == 'photo_images'){
- $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
- }
- $data[$field] = $newone;
- }
- if(isset($data['birthday'])){
- $data['birthday'] = strtotime($data['birthday']);
- }
- if(isset($data['avatar'])){
- $data['real_status'] = -1;
- }
- if(isset($data['hobby_ids'])){
- $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
- }
- if(isset($data['tag_ids'])){
- $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
- }
- if(isset($data['introcode'])){
- $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
- if(!$intro_user){
- $this->error('不存在的邀请人');
- }
- unset($data['introcode']);
- $data['intro_uid'] = $intro_user;
- }
-
- if(empty($data)){
- $this->error('没有任何改变');
- }
- Db::startTrans();
- $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
- if($update_rs === false){
- Db::rollback();
- $this->error('修改资料失败');
- }
-
-
- if(isset($data['avatar'])){
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
- }
-
- if(isset($data['audio_bio'])){
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
- }
-
- if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
- }
-
- $user_info = Db::name('user')->where('id',$this->auth->id)->find();
- if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
- $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
- if($task_rs === false){
- Db::rollback();
- $this->error('完成任务赠送奖励失败');
- }
- }
- Db::commit();
- $this->success();
- }
- public function set_status_switch(){
- }
-
- public function change_longlat(){
- $longitude = input_post('longitude');
- $latitude = input_post('latitude');
- $cityname = input_post('cityname');
- if(empty($longitude) || empty($latitude) || empty($cityname)){
- $this->error();
- }
- $data = [
- 'longitude' => $longitude,
- 'latitude' => $latitude,
- 'cityname' => $cityname,
- ];
- Db::name('user')->where('id',$this->auth->id)->update($data);
- $this->success();
- }
-
-
-
- public function changemobile()
- {
- $user = $this->auth->getUser();
- $oldcaptcha = $this->request->request('oldcaptcha');
- $mobile = $this->request->request('mobile');
- $captcha = $this->request->request('captcha');
- if (!$oldcaptcha || !$mobile || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- if($user->mobile == $mobile){
- $this->error('新手机号不能与旧手机号相同');
- }
- if (\app\common\model\User::where('mobile', $mobile)->find()) {
- $this->error(__('Mobile already exist'));
- }
- $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
- if (!$result) {
- $this->error(__('Captcha is incorrect'));
- }
- $result = Sms::check($mobile, $captcha, 'changemobile');
- if (!$result) {
- $this->error(__('Captcha is incorrect'));
- }
- $verification = $user->verification;
- $verification->mobile = 1;
- $user->verification = $verification;
- $user->mobile = $mobile;
- $user->save();
- Sms::flush($user->mobile, 'changemobile');
- Sms::flush($mobile, 'changemobile');
- $this->success();
- }
-
- public function bindmobile()
- {
- $user = $this->auth->getUser();
- $mobile = $this->request->request('mobile');
- $captcha = $this->request->request('captcha');
- if(!empty($this->auth->mobile)){
- $this->error('已经绑定了手机号');
- }
- if (!$mobile || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- if (\app\common\model\User::where('mobile', $mobile)->find()) {
- $this->error('该手机号已被其他用户绑定');
- }
- $result = Sms::check($mobile, $captcha, 'changemobile');
- if (!$result) {
- $this->error(__('Captcha is incorrect'));
- }
- $verification = $user->verification;
- $verification->mobile = 1;
- $user->verification = $verification;
- $user->mobile = $mobile;
- $user->save();
- Sms::flush($mobile, 'changemobile');
- $this->success('success',$this->userInfo('return'));
- }
-
- public function bindopenid()
- {
- $user = $this->auth->getUser();
- $wechat_openid = $this->request->request('wechat_openid');
- if(!empty($this->auth->wechat_openid)){
- $this->error('已经绑定了微信号');
- }
- if (!$wechat_openid) {
- $this->error(__('Invalid parameters'));
- }
- if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
- $this->error('该微信号已被其他用户绑定');
- }
- $user->wechat_openid = $wechat_openid;
- $user->save();
- $this->success('success',$this->userInfo('return'));
- }
-
-
-
- public function resetpwd()
- {
-
- $type = 'mobile';
- $mobile = input("mobile");
- $email = input("email");
- $newpassword = input("newpassword");
- $captcha = input("captcha");
- if (!$newpassword || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if ($type == 'mobile') {
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- $user = \app\common\model\User::getByMobile($mobile);
- if (!$user) {
- $this->error(__('User not found'));
- }
- $ret = Sms::check($mobile, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- Sms::flush($mobile, 'resetpwd');
- } else {
- if (!Validate::is($email, "email")) {
- $this->error(__('Email is incorrect'));
- }
- $user = \app\common\model\User::getByEmail($email);
- if (!$user) {
- $this->error(__('User not found'));
- }
- $ret = Ems::check($email, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- Ems::flush($email, 'resetpwd');
- }
-
- $this->auth->direct($user->id);
- $ret = $this->auth->changepwd($newpassword, '', true);
- if ($ret) {
- $this->success(__('Reset password successful'));
- } else {
- $this->error($this->auth->getError());
- }
- }
-
- public function changepwd(){
- $newpassword = input('newpassword');
- $oldpassword = input('oldpassword','');
- if (!$newpassword) {
- $this->error(__('Invalid parameters'));
- }
- if($this->auth->password && empty($oldpassword)){
- $this->error('原密码必填');
- }
- if(empty($this->auth->password)){
- $ret = $this->auth->changepwd($newpassword, '', true);
- }else{
- $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
- }
- if ($ret) {
- $this->success(__('Reset password successful'));
- } else {
- $this->error($this->auth->getError());
- }
- }
-
- public function changeDeviceIp()
- {
-
- if (!$this->apiLimit(1, 5000)) {
- return ;
- }
- $user = $this->auth->getUser();
- $ip = request()->ip();
- $deviceId = $this->request->request('device_id','');
- $phoneModel = $this->request->request('phone_model','');
- $brand = $this->request->request('brand','');
- $apiVersion = $this->request->request('api_version','');
- $deviceOs = $this->request->request('device_os','');
- if ($ip !== $user->loginip){
- $update = [];
- $update['id'] = $user->id;
- $update['loginip'] = $ip;
- \app\common\model\User::update($update);
- }
- $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
- if (empty($userDeviceInfo)){
- $userDeviceInfo = new UserDeviceInfo();
- $userDeviceInfo->user_id = $user->u_id;
- }
- $userDeviceInfo->device_os = $deviceOs;
- $userDeviceInfo->device_id = $deviceId;
- $userDeviceInfo->phone_model = $phoneModel;
- $userDeviceInfo->brand = $brand;
- $userDeviceInfo->api_version = $apiVersion;
- $userDeviceInfo->save();
-
- }
-
- public function cancleUser(){
- if (!$this->request->isPost()) {
- $this->error(__('Invalid parameters'));
- }
-
- $tenIm = new Tenim();
- $tenIm->loginoutim($this->auth->id);
- Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
- $this->auth->logout();
- $this->success('注销成功');
- }
- }
|