From aed59ed8cc9fedab20cc1a50b0f7fbc0a929d18e Mon Sep 17 00:00:00 2001 From: Wayne Date: Wed, 5 Jul 2023 00:11:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A0=90=E8=A3=BD=E5=8D=A1?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/adminapi/controller/v1/Card.php | 44 +-- app/api/controller/v1/Auth.php | 8 +- app/appapi/controller/v1/Auth.php | 35 ++- app/appapi/controller/v1/Card.php | 15 +- app/appapi/controller/v1/User.php | 428 ++++++++++++++++------------ app/common.php | 35 ++- app/common/Activation.php | 39 +++ app/common/lib/Aes.php | 1 - 8 files changed, 376 insertions(+), 229 deletions(-) create mode 100644 app/common/Activation.php diff --git a/app/adminapi/controller/v1/Card.php b/app/adminapi/controller/v1/Card.php index a8a38b8..2633091 100644 --- a/app/adminapi/controller/v1/Card.php +++ b/app/adminapi/controller/v1/Card.php @@ -10,39 +10,34 @@ use app\common\lib\Aes; class Card extends ApiController { public function addPrecard(){ - $req=input(); - //取得prefix - $agent = Db::name('agent') - ->where('id',$req['agent_id']) - ->find(); + $req=input('post.'); $aes = new Aes([]); for($i=0;$iencrypt('user_id='.$user_id.'&verify_code='.$verify_code)); // $nfcUrl = genQrCode(getUrl().'/card/?params='.$params,$user_id,'nfc'); $data[]=[ - // 'user_id'=>$user_id, - 'agent_id'=>$req['agent_id'], - 'try_days'=>7, + 'agent_id'=>0, + 'name'=>$req['name'], + 'days'=>$req['days'], + 'serial_no' => '', 'verify_code'=>'', 'expire_time'=>$req['expire_time'], 'status'=>0, - ]; - + ]; } try{ Db::name('precard') ->insertAll($data); }catch(\Exception $e){ - print_r($e); - return $this->error('新增失敗'); + return $this->error($e->getMessage()); } return $this->success('新增成功'); @@ -80,7 +75,6 @@ class Card extends ApiController // $aes = new Aes([]); // $nfc_url = 'user_id='.$val['user_id'].'&verify_code='.$val['verify_code']; // $result[$key]['params'] = getUrl().'/card/'.urlencode($aes->encrypt($nfc_url)); - $result[$key]['agent_name'] = Db::name('agent')->where('id',$val['agent_id'])->value('name'); $result[$key]['expire'] = date('Y-m-d',$val['expire_time']); switch($val['status']){ case 0: @@ -98,9 +92,9 @@ class Card extends ApiController } $aes = new Aes([]); - $params = urlencode($aes->encrypt('verify_code='.$val['verify_code'])); + $params = urlencode($aes->encrypt('sno='.$val['serial_no'])); $result[$key]['nfcurl']= getUrl().'/card/?params='.$params; - // $nfcUrl = genQrCode('https://'.$_SERVER['HTTP_HOST'].'/card/?params='.$params,$data['user_id'],'nfc'); + // $result[$key]['qrcode'] = genQrCode($result[$key]['nfcurl'],'nfc'); } $rtn=[ @@ -139,18 +133,20 @@ class Card extends ApiController public function updateVerifyCode(){ $id=input('id'); - $verify_code=strtoupper(input('code')); + $serial_no=strtoupper(input('code')); + //TODO: $is_user = Db::name('user') - ->where('uniqid',$verify_code) + ->where('uniqid',$serial_no) ->count(); if($is_user){ return $this->error('卡片已綁定會員'); } + $is_precard = Db::name('precard') - ->where('verify_code',$verify_code) + ->where('serial_no',$serial_no) ->count(); if($is_precard){ @@ -160,9 +156,13 @@ class Card extends ApiController try{ $result=Db::name('precard') ->where('id',$id) - ->update(['verify_code'=>$verify_code,'status'=>1]); - - return $this->success('設定成功'); + ->update(['serial_no'=>$serial_no,'status'=>1]); + + $aes = new Aes([]); + $params = urlencode($aes->encrypt('sno='.$serial_no)); + $nfcurl= $_SERVER['HTTP_HOST'].'/card/?params='.$params; + + return $this->success($nfcurl); }catch(\Exception $e){ return $this->error('系統錯誤'); } diff --git a/app/api/controller/v1/Auth.php b/app/api/controller/v1/Auth.php index 11b09a7..d476f76 100644 --- a/app/api/controller/v1/Auth.php +++ b/app/api/controller/v1/Auth.php @@ -7,12 +7,16 @@ use app\common\lib\Aes; class Auth extends ApiController { function test(){ - echo genSerialNo(); + $token='32UQBG%2BcpJ2AnvBDCR9gEix6a2ysc1Ro5pXg5%2Fvaang%3D'; + $aes = new Aes([]); + print_r($aes->descrypt($token)); + // parse_str($aes->descrypt($token),$params); + // print_r($params); } function testAes(){ $aes = new Aes([]); - echo urlencode($aes->encrypt('user_id=tg123467890&verify_code=1111')); + echo ($aes->encrypt('sno=1234567890')); } function testDec(){ diff --git a/app/appapi/controller/v1/Auth.php b/app/appapi/controller/v1/Auth.php index 5531e59..2c78a35 100644 --- a/app/appapi/controller/v1/Auth.php +++ b/app/appapi/controller/v1/Auth.php @@ -25,7 +25,6 @@ class Auth extends ApiController $id_token = input('token'); $line_id = input('line_id'); - // $profile = (new Parser())->parse($id_token); // print_r($token->getClaim('name')); @@ -346,7 +345,37 @@ class Auth extends ApiController return $this->Success($avatar); } - public function test(){ - Vcard::genVcf('mc63de2a162b218'); + public function verifyActivationToken(){ + $token = input('token'); + + if(!$token){ + return $this->error('參數錯誤'); + } + + $aes = new Aes([]); + parse_str($aes->descrypt($token),$params); + + if(!isset($params['sno'])){ + return $this->error('參數錯誤'); + } + + $precard = Db::name('precard') + ->where('serial_no',$params['sno']) + ->find(); + + if(!$precard){ + return $this->error('查無預開卡'); + } + //檢查到期日 + if($precard['expire_time']!=0 && $precard['expire_time']error('預開卡已過期'); + } + + //檢查是否已經開卡 + if($precard['status']!=1){ + return $this->error('預開卡已使用或無效'); + } + + return $this->success('驗證成功'); } } diff --git a/app/appapi/controller/v1/Card.php b/app/appapi/controller/v1/Card.php index 6dacd09..eb818dd 100644 --- a/app/appapi/controller/v1/Card.php +++ b/app/appapi/controller/v1/Card.php @@ -29,20 +29,19 @@ class Card extends ApiController $aes = new Aes([]); parse_str($aes->descrypt($token),$params); - if(!isset($params['verify_code'])){ + if(!isset($params['sno'])){ if(!isset($params['user_id'])){ return $this->error('參數錯誤'); } $user_id = $params['user_id']; }else{ - if(strlen($params['verify_code'])>0){ - $user_id = getUseridByCuid(strtoupper($params['verify_code'])); + if(strlen($params['sno'])>0){ + $user_id = getUseridByCuid(strtoupper($params['sno'])); }else{ return $this->error('參數錯誤'); } } - - + if($user_id){ Db::name('user') ->where('user_id',$user_id) @@ -52,14 +51,14 @@ class Card extends ApiController return $this->success($user_id); } + //檢查是否為預開卡會員 $is_precard=Db::name('precard') - ->where('verify_code',strtoupper($params['verify_code'])) + ->where('serial_no',strtoupper($params['sno'])) ->count(); - if($is_precard){ - return $this->success(['verify'=>strtoupper($params['verify_code'])],201); + return $this->success(['sno'=>strtoupper($params['sno'])],201); } return $this->error('請求錯誤'); diff --git a/app/appapi/controller/v1/User.php b/app/appapi/controller/v1/User.php index 50b6089..0028a28 100644 --- a/app/appapi/controller/v1/User.php +++ b/app/appapi/controller/v1/User.php @@ -1,4 +1,5 @@ field('id,user_id,address,avatar,phone,email,url,facebook,ig,youtube,mark,uniqid,overdue_time,level,nc_func,agent_id,real_name,nfc_addon,company') - ->where('user_id',$this->uid) - ->find(); + /* + * 取得用戶資料 + */ + public function getUserInfo() + { + //檢查是否有serail_no + $slashToken = urldecode(input('slashToken')); + + $precardData = ''; + + if (!empty($slashToken)) { + + $rtn = \app\common\Activation::checkSlashtoken($slashToken); + + if ($rtn['code'] > 0) { + return $this->error($rtn['msg'], $rtn['code']); + } + $precardData = $rtn['data']; + } + + $user = Db::name('user') + // ->field('id,user_id,address,avatar,phone,email,url,facebook,ig,youtube,mark,uniqid,overdue_time,level,nc_func,agent_id,real_name,nfc_addon,company') + ->where('user_id', $this->uid) + ->find(); //使用者不存在,至SSO Server取得,並加入會員資料表 - if(!$user){ + if (!$user) { $user_data = [ 'user_id' => $this->uid ]; - $sso = Sso::getUserInfo($user_data); - if(!$sso['code']==200){ + + $sso = Sso::getUserInfo($user_data); + if (!$sso['code'] == 200) { return $this->error('get sso user info error!!!'); - } + } $sso_data = $sso['data']; - try{ + try { $sso_data['level'] = 2; $sso_data['status'] = 1; - $sso_data['overdue_time'] = strtotime(date('Y-m-d',time() + (60 * 60 * 24 * 7))); + $sso_data['overdue_time'] = strtotime(date('Y-m-d', time() + (60 * 60 * 24 * 7))); $sso_data['cus_card'] = ''; $sso_data['create_time'] = date('Y-m-d H:i:s'); Db::name('user') - ->insert($sso_data); - - $user=Db::name('user') - ->where('user_id',$this->uid) - ->find(); - + ->insert($sso_data); + + unset($sso_data); - }catch(\Exception $e){ + } catch (\Exception $e) { return $this->error($e->getMessage()); - } + } } + //更新預開卡狀態 + if($precardData){ + //更新卡號,會員編號連結 + Db::name('user_sno') + ->insert([ + 'user_id'=>$this->uid, + 'serial_no'=>$precardData['serial_no'], + ]); + + //更新用戶資料 + if($precardData['days']>0){ + Db::name('user') + ->where('user_id', $this->uid) + ->update( + [ + 'level' => 1, + 'overdue_time' => Db::raw('overdue_time + '.(60 * 60 * 24 * $precardData['days'])) + ] + ); + } + + //更新預開卡狀態 + Db::name('precard') + ->where('serial_no', $precardData['serial_no']) + ->update([ + 'status'=>2, + 'user_id'=>$this->uid + ]); + } + + $user = Db::name('user') + ->where('user_id', $this->uid) + ->find(); + $aes = new Aes([]); - if(strlen(trim($user['uniqid']))>0){ - $params = urlencode($aes->encrypt('verify_code='.$user['uniqid'])); - }else{ - $params = urlencode($aes->encrypt('user_id='.$user['user_id'])); + if (strlen(trim($user['uniqid'])) > 0) { + $params = urlencode($aes->encrypt('verify_code=' . $user['uniqid'])); + } else { + $params = urlencode($aes->encrypt('user_id=' . $user['user_id'])); } - $user['nfcurl'] = getUrl().'/card/?params='.$params; + $user['nfcurl'] = getUrl() . '/card/?params=' . $params; // $user['level_name'] = Db::name('user_level')->where('agent_id',$user['agent_id'])->where('level_id',$user['level'])->value('name'); // $user['overdue_time'] = date('Y-m-d H:i:s',$user['overdue_time']); - if(time()>$user['overdue_time']){ + if (time() > $user['overdue_time']) { //更新用戶level - Db::name('user')->where('user_id',$this->uid)->update(['level'=>0]); + Db::name('user')->where('user_id', $this->uid)->update(['level' => 0]); $user['level'] = 0; } - switch($user['level']){ + switch ($user['level']) { case 0: - $user['level_name']='未付費用戶'; + $user['level_name'] = '未付費用戶'; break; case 1: - $user['level_name']='付費用戶'; + $user['level_name'] = '付費用戶'; break; case 2: - $user['level_name']='試用用戶'; + $user['level_name'] = '試用用戶'; break; default: break; } - $user['nc_func'] = explode(',',$user['nc_func']); + $user['nc_func'] = explode(',', $user['nc_func']); - $user['agent_prefix'] = Db::name('agent')->where('id',$user['agent_id'])->value('prefix'); + $user['agent_prefix'] = Db::name('agent')->where('id', $user['agent_id'])->value('prefix'); return $this->Success($user); - } + } - public function getUserCompany(){ - if(!$this->uid){ + public function getUserCompany() + { + if (!$this->uid) { $this->error('用戶ID錯誤'); } - + $result = Db::name('user_company') - ->where('user_id',$this->uid) - ->select(); + ->where('user_id', $this->uid) + ->select(); return $this->success($result); } - public function addUserCompany(){ - if(!$this->uid){ + public function addUserCompany() + { + if (!$this->uid) { $this->error('用戶ID錯誤'); } @@ -108,108 +162,107 @@ class User extends ApiController unset($ucData['uid']); unset($ucData['action']); - try{ - if(isset($ucData['is_default']) && $ucData['is_default']){ + try { + if (isset($ucData['is_default']) && $ucData['is_default']) { Db::name('user_company') - ->where('user_id',$this->uid) - ->update(['is_default'=>0]); + ->where('user_id', $this->uid) + ->update(['is_default' => 0]); Db::name('user') - ->where('user_id',$this->uid) - ->update([ - 'company' => isset($ucData['uc_name'])?$ucData['uc_name']:'', - 'title' => isset($ucData['uc_title'])?$ucData['uc_title']:'', - 'tel' => isset($ucData['uc_tel'])?$ucData['uc_tel']:'', - 'address' => isset($ucData['uc_address'])?$ucData['uc_address']:'', - 'url' => isset($ucData['uc_url'])?$ucData['uc_url']:'', - ]); - + ->where('user_id', $this->uid) + ->update([ + 'company' => isset($ucData['uc_name']) ? $ucData['uc_name'] : '', + 'title' => isset($ucData['uc_title']) ? $ucData['uc_title'] : '', + 'tel' => isset($ucData['uc_tel']) ? $ucData['uc_tel'] : '', + 'address' => isset($ucData['uc_address']) ? $ucData['uc_address'] : '', + 'url' => isset($ucData['uc_url']) ? $ucData['uc_url'] : '', + ]); } Db::name('user_company') - ->insert($ucData); + ->insert($ucData); $result = Db::name('user_company') - ->where('user_id',$this->uid) - ->select(); + ->where('user_id', $this->uid) + ->select(); return $this->success($result); - - }catch(\Exception $e){ + } catch (\Exception $e) { print_r($e); return $this->error('操作錯誤'); } } - public function setUCDefault(){ - if(!$this->uid){ + public function setUCDefault() + { + if (!$this->uid) { $this->error('用戶ID錯誤'); } - try{ + try { Db::name('user_company') - ->where('user_id',$this->uid) - ->update(['is_default'=>0]); + ->where('user_id', $this->uid) + ->update(['is_default' => 0]); Db::name('user_company') - ->where('id',input('id')) - ->update(['is_default'=>1]); + ->where('id', input('id')) + ->update(['is_default' => 1]); $res = Db::name('user_company') - ->where('id',input('id')) - ->find(); + ->where('id', input('id')) + ->find(); Db::name('user') - ->where('user_id',$this->uid) - ->update([ - 'company' => $res['uc_name'], - 'title' => $res['uc_title'], - 'tel' => $res['uc_tel'], - 'address' => $res['uc_address'], - 'url' => $res['uc_url'], - ]); + ->where('user_id', $this->uid) + ->update([ + 'company' => $res['uc_name'], + 'title' => $res['uc_title'], + 'tel' => $res['uc_tel'], + 'address' => $res['uc_address'], + 'url' => $res['uc_url'], + ]); //更新用戶資料 $result = Db::name('user_company') - ->where('user_id',$this->uid) + ->where('user_id', $this->uid) ->select(); return $this->success($result); - - }catch(\Exception $e){ + } catch (\Exception $e) { print_r($e); return $this->error('操作錯誤'); } } - public function updateUserCompany(){ - if(!$this->uid){ + public function updateUserCompany() + { + if (!$this->uid) { $this->error('用戶ID錯誤'); } $ucData = input('post.'); unset($ucData['action']); unset($ucData['uid']); unset($ucData['id']); - try{ + try { - if(isset($ucData['is_default']) && $ucData['is_default']){ + if (isset($ucData['is_default']) && $ucData['is_default']) { Db::name('user_company') - ->where('user_id',$this->uid) - ->update(['is_default'=>0]); + ->where('user_id', $this->uid) + ->update(['is_default' => 0]); Db::name('user') - ->where('user_id',$this->uid) - ->update([ - 'company' => $ucData['uc_name'], - 'title' => $ucData['uc_title'], - 'tel' => $ucData['uc_tel'], - 'address' => $ucData['uc_address'], - 'url' => $ucData['uc_url'], - ]); + ->where('user_id', $this->uid) + ->update([ + 'company' => $ucData['uc_name'], + 'title' => $ucData['uc_title'], + 'tel' => $ucData['uc_tel'], + 'address' => $ucData['uc_address'], + 'url' => $ucData['uc_url'], + ]); } - + Db::name('user_company') - ->where('id',input('id')) + ->where('id', input('id')) ->update($ucData); // $res = Db::name('user_company') @@ -218,82 +271,81 @@ class User extends ApiController //更新用戶資料 $result = Db::name('user_company') - ->where('user_id',$this->uid) + ->where('user_id', $this->uid) ->select(); return $this->success($result); - - }catch(\Exception $e){ + } catch (\Exception $e) { print_r($e); return $this->error('操作錯誤'); } } - public function deleteUserCompany(){ - if(!$this->uid){ + public function deleteUserCompany() + { + if (!$this->uid) { $this->error('用戶ID錯誤'); } - try{ + try { Db::name('user_company') - ->where('id',input('id')) - ->delete(); + ->where('id', input('id')) + ->delete(); //更新用戶資料 $result = Db::name('user_company') - ->where('user_id',$this->uid) + ->where('user_id', $this->uid) ->select(); - if(count($result)==0){ + if (count($result) == 0) { Db::name('user') - ->where('user_id',$this->uid) - ->update([ - 'company' => '', - 'title' => '', - 'tel' => '', - 'address' => '', - 'url' => '', - ]); - + ->where('user_id', $this->uid) + ->update([ + 'company' => '', + 'title' => '', + 'tel' => '', + 'address' => '', + 'url' => '', + ]); } return $this->success($result); - - }catch(\Exception $e){ + } catch (\Exception $e) { return $this->error('操作錯誤'); } - - } - public function setUserLevel(){ - $result=Db::name('user') - ->where('user_id',$this->uid) - ->update(['level'=>input('level')]); + public function setUserLevel() + { + $result = Db::name('user') + ->where('user_id', $this->uid) + ->update(['level' => input('level')]); + - return $this->Success($result); - } - - public function setUserTpl(){ - try{ - $result=Db::name('user') - ->where('user_id',$this->uid) - ->update(['nc_template'=>input('tpl')]); - }catch(\Excenption $e){ + } + + public function setUserTpl() + { + try { + $result = Db::name('user') + ->where('user_id', $this->uid) + ->update(['nc_template' => input('tpl')]); + } catch (\Excenption $e) { return $this->Error('更新失敗'); } - + return $this->Success($result); - } + } - public function uploadAvatar(){ + public function uploadAvatar() + { - $files = request()->file('file'); - $savename = \think\facade\Filesystem::disk('public')->putFile( input('user_id'), $files); + $files = request()->file('file'); + $savename = \think\facade\Filesystem::disk('public')->putFile(input('user_id'), $files); + + $avatar = getUrl() . '/storage/' . $savename; - $avatar = getUrl().'/storage/'.$savename; - // Db::name('user') // ->where('user_id',input('user_id')) // ->update(['avatar'=>$avatar]); @@ -301,101 +353,103 @@ class User extends ApiController return $this->Success($avatar); } - public function updateSendCount(){ + public function updateSendCount() + { $user_id = input('userid'); - + Db::name('user') - ->where('user_id',input('userid')) - ->exp('send_count', 'send_count+1') - ->update(); + ->where('user_id', input('userid')) + ->exp('send_count', 'send_count+1') + ->update(); // ->inc('send_count',1); return $this->Success('更新成功'); } - public function addFavorite(){ - try{ + public function addFavorite() + { + try { $is_exist = Db::name('user_favorite') - ->where('user_id',$this->uid) - ->where('uf_user_id',input('userid')) - ->count(); + ->where('user_id', $this->uid) + ->where('uf_user_id', input('userid')) + ->count(); - if(!$is_exist){ + if (!$is_exist) { Db::name('user_favorite') - ->insert(['user_id'=>$this->uid,'uf_user_id'=>input('userid')]); + ->insert(['user_id' => $this->uid, 'uf_user_id' => input('userid')]); } return $this->success('操作成功'); - }catch(\Exception $e){ + } catch (\Exception $e) { return $this->error('操作失敗'); } } - public function delFavorite(){ - try{ + public function delFavorite() + { + try { Db::name('user_favorite') - ->where('id',input('id')) - ->delete(); + ->where('id', input('id')) + ->delete(); return $this->success('操作成功'); - }catch(\Exception $e){ + } catch (\Exception $e) { return $this->error('操作失敗'); } } - public function uploadConnections(){ - try{ + public function uploadConnections() + { + try { Db::name('user') - ->where('user_id',$this->uid) - ->update(['connections'=>input('uc')]); + ->where('user_id', $this->uid) + ->update(['connections' => input('uc')]); return $this->success('操作成功'); - }catch(\Exception $e){ + } catch (\Exception $e) { print_r($e); return $this->error('操作失敗'); } - } + } - public function getFavorite(){ - try{ + public function getFavorite() + { + try { $uf = Db::name('user_favorite') - ->alias('uf') - ->leftjoin('user u','uf.uf_user_id = u.user_id') - ->field('uf.id as ufid,u.*') - ->where('uf.user_id',$this->uid) - ->select() - ->toArray(); - -// print_r(Db::getLastSql()); -// print_r($uf); -// return; + ->alias('uf') + ->leftjoin('user u', 'uf.uf_user_id = u.user_id') + ->field('uf.id as ufid,u.*') + ->where('uf.user_id', $this->uid) + ->select() + ->toArray(); + $aes = new Aes([]); - foreach($uf as $key=>$val){ + foreach ($uf as $key => $val) { - if(strlen($val['uniqid'])>0){ - $params = urlencode($aes->encrypt('verify_code='.$val['uniqid'])); - }else{ - $params = urlencode($aes->encrypt('user_id='.$val['user_id'])); + if (strlen($val['uniqid']) > 0) { + $params = urlencode($aes->encrypt('verify_code=' . $val['uniqid'])); + } else { + $params = urlencode($aes->encrypt('user_id=' . $val['user_id'])); } - - $uf[$key]['nfcurl'] = getUrl().'/card/?params='.$params; - } + $uf[$key]['nfcurl'] = getUrl() . '/card/?params=' . $params; + } return $this->success($uf); - }catch(\Exception $e){ + } catch (\Exception $e) { return $this->error('操作失敗'); } } - public function getConnections(){ - try{ + public function getConnections() + { + try { $result = Db::name('user') - ->where('user_id',$this->uid) - ->value('connections'); + ->where('user_id', $this->uid) + ->value('connections'); return $this->success($result); - }catch(\Exception $e){ + } catch (\Exception $e) { return $this->error('操作失敗'); } } diff --git a/app/common.php b/app/common.php index 44d56c0..016665f 100644 --- a/app/common.php +++ b/app/common.php @@ -31,8 +31,8 @@ function getIdByLid($lid){ } function getUseridByCuid($cuid){ - $id =Db::name('user') - ->where('uniqid',$cuid) + $id =Db::name('user_sno') + ->where('serial_no',$cuid) ->value('user_id'); if(!$id){ @@ -70,8 +70,32 @@ function genUniqid($prefix='mc'){ * * @return array */ -function genSerialNo(){ +function genSerialNo($len=8){ + $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $serialNumber = ''; + $max = strlen($characters) - 1; + + for ($i = 0; $i < $len; $i++) { + $randomIndex = random_int(0, $max); + $serialNumber .= $characters[$randomIndex]; + } + + //檢查是否存在DB,若存在則重新產生 + $result = Db::name('precard') + ->where('serial_no',$serialNumber) + ->count(); + + if($result){ + $serialNumber = genSerialNo($len); + } + + return $serialNumber; +} + +function genSNo($len=8){ + //產生$len長度序號 $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $rand = $code[rand(0,25)] .strtoupper(dechex(date('m'))) .date('d').substr(time(),-5) @@ -83,15 +107,14 @@ function genSerialNo(){ $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $d = '', $f = 0; - $f < 8; + $f < $len; $g = ord( $a[ $f ] ), $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ], $f++ ); - return $d; + return $d; } - function encodeRefer($userId) { $sourceString = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ'; diff --git a/app/common/Activation.php b/app/common/Activation.php new file mode 100644 index 0000000..a152fe9 --- /dev/null +++ b/app/common/Activation.php @@ -0,0 +1,39 @@ +descrypt($slashtoken), $params); + + if (!isset($params['sno'])) { + return ['code' => 404, 'msg' => '預開卡不存在']; + } + + $precard = Db::name('precard') + ->where('serial_no', $params['sno']) + ->find(); + + if (!$precard) { + return ['code' => 404, 'msg' => '預開卡不存在']; + } + //檢查到期日 + if ($precard['expire_time'] != 0 && $precard['expire_time'] < time()) { + return ['code' => 401, 'msg' => '預開卡已過期']; + } + + //檢查是否已經開卡 + if ($precard['status'] != 1) { + return ['code'=>500,'msg'=>'預開卡已使用或無效']; + } + + return ['code'=>0,'data'=>$precard]; + } +} diff --git a/app/common/lib/Aes.php b/app/common/lib/Aes.php index 5bee2be..2cecf8b 100644 --- a/app/common/lib/Aes.php +++ b/app/common/lib/Aes.php @@ -12,7 +12,6 @@ class Aes{ */ public function __construct($config){ foreach($config as $k=>$v){ - $this->$k = $v; } }