增加預製卡功能

main
Wayne 3 years ago
parent 2eb24248d7
commit aed59ed8cc

@ -10,39 +10,34 @@ use app\common\lib\Aes;
class Card extends ApiController class Card extends ApiController
{ {
public function addPrecard(){ public function addPrecard(){
$req=input(); $req=input('post.');
//取得prefix
$agent = Db::name('agent')
->where('id',$req['agent_id'])
->find();
$aes = new Aes([]); $aes = new Aes([]);
for($i=0;$i<input('number');$i++){ for($i=0;$i<input('number');$i++){
// $user_id=genUniqid($agent['prefix']); // $user_id=genUniqid($agent['prefix']);
// $verify_code = genSerialNo(); // $serial_no = genSerialNo(10);
// $params = urlencode($aes->encrypt('user_id='.$user_id.'&verify_code='.$verify_code)); // $params = urlencode($aes->encrypt('user_id='.$user_id.'&verify_code='.$verify_code));
// $nfcUrl = genQrCode(getUrl().'/card/?params='.$params,$user_id,'nfc'); // $nfcUrl = genQrCode(getUrl().'/card/?params='.$params,$user_id,'nfc');
$data[]=[ $data[]=[
// 'user_id'=>$user_id, 'agent_id'=>0,
'agent_id'=>$req['agent_id'], 'name'=>$req['name'],
'try_days'=>7, 'days'=>$req['days'],
'serial_no' => '',
'verify_code'=>'', 'verify_code'=>'',
'expire_time'=>$req['expire_time'], 'expire_time'=>$req['expire_time'],
'status'=>0, 'status'=>0,
]; ];
} }
try{ try{
Db::name('precard') Db::name('precard')
->insertAll($data); ->insertAll($data);
}catch(\Exception $e){ }catch(\Exception $e){
print_r($e); return $this->error($e->getMessage());
return $this->error('新增失敗');
} }
return $this->success('新增成功'); return $this->success('新增成功');
@ -80,7 +75,6 @@ class Card extends ApiController
// $aes = new Aes([]); // $aes = new Aes([]);
// $nfc_url = 'user_id='.$val['user_id'].'&verify_code='.$val['verify_code']; // $nfc_url = 'user_id='.$val['user_id'].'&verify_code='.$val['verify_code'];
// $result[$key]['params'] = getUrl().'/card/'.urlencode($aes->encrypt($nfc_url)); // $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']); $result[$key]['expire'] = date('Y-m-d',$val['expire_time']);
switch($val['status']){ switch($val['status']){
case 0: case 0:
@ -98,9 +92,9 @@ class Card extends ApiController
} }
$aes = new Aes([]); $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; $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=[ $rtn=[
@ -139,18 +133,20 @@ class Card extends ApiController
public function updateVerifyCode(){ public function updateVerifyCode(){
$id=input('id'); $id=input('id');
$verify_code=strtoupper(input('code')); $serial_no=strtoupper(input('code'));
//TODO:
$is_user = Db::name('user') $is_user = Db::name('user')
->where('uniqid',$verify_code) ->where('uniqid',$serial_no)
->count(); ->count();
if($is_user){ if($is_user){
return $this->error('卡片已綁定會員'); return $this->error('卡片已綁定會員');
} }
$is_precard = Db::name('precard') $is_precard = Db::name('precard')
->where('verify_code',$verify_code) ->where('serial_no',$serial_no)
->count(); ->count();
if($is_precard){ if($is_precard){
@ -160,9 +156,13 @@ class Card extends ApiController
try{ try{
$result=Db::name('precard') $result=Db::name('precard')
->where('id',$id) ->where('id',$id)
->update(['verify_code'=>$verify_code,'status'=>1]); ->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('設定成功'); return $this->success($nfcurl);
}catch(\Exception $e){ }catch(\Exception $e){
return $this->error('系統錯誤'); return $this->error('系統錯誤');
} }

@ -7,12 +7,16 @@ use app\common\lib\Aes;
class Auth extends ApiController class Auth extends ApiController
{ {
function test(){ 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(){ function testAes(){
$aes = new Aes([]); $aes = new Aes([]);
echo urlencode($aes->encrypt('user_id=tg123467890&verify_code=1111')); echo ($aes->encrypt('sno=1234567890'));
} }
function testDec(){ function testDec(){

@ -25,7 +25,6 @@ class Auth extends ApiController
$id_token = input('token'); $id_token = input('token');
$line_id = input('line_id'); $line_id = input('line_id');
// $profile = (new Parser())->parse($id_token); // $profile = (new Parser())->parse($id_token);
// print_r($token->getClaim('name')); // print_r($token->getClaim('name'));
@ -346,7 +345,37 @@ class Auth extends ApiController
return $this->Success($avatar); return $this->Success($avatar);
} }
public function test(){ public function verifyActivationToken(){
Vcard::genVcf('mc63de2a162b218'); $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']<time()){
return $this->error('預開卡已過期');
}
//檢查是否已經開卡
if($precard['status']!=1){
return $this->error('預開卡已使用或無效');
}
return $this->success('驗證成功');
} }
} }

@ -29,20 +29,19 @@ class Card extends ApiController
$aes = new Aes([]); $aes = new Aes([]);
parse_str($aes->descrypt($token),$params); parse_str($aes->descrypt($token),$params);
if(!isset($params['verify_code'])){ if(!isset($params['sno'])){
if(!isset($params['user_id'])){ if(!isset($params['user_id'])){
return $this->error('參數錯誤'); return $this->error('參數錯誤');
} }
$user_id = $params['user_id']; $user_id = $params['user_id'];
}else{ }else{
if(strlen($params['verify_code'])>0){ if(strlen($params['sno'])>0){
$user_id = getUseridByCuid(strtoupper($params['verify_code'])); $user_id = getUseridByCuid(strtoupper($params['sno']));
}else{ }else{
return $this->error('參數錯誤'); return $this->error('參數錯誤');
} }
} }
if($user_id){ if($user_id){
Db::name('user') Db::name('user')
->where('user_id',$user_id) ->where('user_id',$user_id)
@ -52,14 +51,14 @@ class Card extends ApiController
return $this->success($user_id); return $this->success($user_id);
} }
//檢查是否為預開卡會員 //檢查是否為預開卡會員
$is_precard=Db::name('precard') $is_precard=Db::name('precard')
->where('verify_code',strtoupper($params['verify_code'])) ->where('serial_no',strtoupper($params['sno']))
->count(); ->count();
if($is_precard){ if($is_precard){
return $this->success(['verify'=>strtoupper($params['verify_code'])],201); return $this->success(['sno'=>strtoupper($params['sno'])],201);
} }
return $this->error('請求錯誤'); return $this->error('請求錯誤');

@ -1,4 +1,5 @@
<?php <?php
namespace app\appapi\controller\v1; namespace app\appapi\controller\v1;
use app\appapi\ApiController; use app\appapi\ApiController;
@ -10,7 +11,26 @@ use app\service\Sso;
class User extends ApiController class User extends ApiController
{ {
public function getUserInfo(){ /*
* 取得用戶資料
*/
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') $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') // ->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) ->where('user_id', $this->uid)
@ -21,6 +41,7 @@ class User extends ApiController
$user_data = [ $user_data = [
'user_id' => $this->uid 'user_id' => $this->uid
]; ];
$sso = Sso::getUserInfo($user_data); $sso = Sso::getUserInfo($user_data);
if (!$sso['code'] == 200) { if (!$sso['code'] == 200) {
return $this->error('get sso user info error!!!'); return $this->error('get sso user info error!!!');
@ -37,9 +58,6 @@ class User extends ApiController
Db::name('user') Db::name('user')
->insert($sso_data); ->insert($sso_data);
$user=Db::name('user')
->where('user_id',$this->uid)
->find();
unset($sso_data); unset($sso_data);
} catch (\Exception $e) { } catch (\Exception $e) {
@ -47,6 +65,40 @@ class User extends ApiController
} }
} }
//更新預開卡狀態
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([]); $aes = new Aes([]);
if (strlen(trim($user['uniqid'])) > 0) { if (strlen(trim($user['uniqid'])) > 0) {
$params = urlencode($aes->encrypt('verify_code=' . $user['uniqid'])); $params = urlencode($aes->encrypt('verify_code=' . $user['uniqid']));
@ -86,7 +138,8 @@ class User extends ApiController
return $this->Success($user); return $this->Success($user);
} }
public function getUserCompany(){ public function getUserCompany()
{
if (!$this->uid) { if (!$this->uid) {
$this->error('用戶ID錯誤'); $this->error('用戶ID錯誤');
} }
@ -98,7 +151,8 @@ class User extends ApiController
return $this->success($result); return $this->success($result);
} }
public function addUserCompany(){ public function addUserCompany()
{
if (!$this->uid) { if (!$this->uid) {
$this->error('用戶ID錯誤'); $this->error('用戶ID錯誤');
} }
@ -124,7 +178,6 @@ class User extends ApiController
'address' => isset($ucData['uc_address']) ? $ucData['uc_address'] : '', 'address' => isset($ucData['uc_address']) ? $ucData['uc_address'] : '',
'url' => isset($ucData['uc_url']) ? $ucData['uc_url'] : '', 'url' => isset($ucData['uc_url']) ? $ucData['uc_url'] : '',
]); ]);
} }
Db::name('user_company') Db::name('user_company')
->insert($ucData); ->insert($ucData);
@ -134,14 +187,14 @@ class User extends ApiController
->select(); ->select();
return $this->success($result); return $this->success($result);
} catch (\Exception $e) { } catch (\Exception $e) {
print_r($e); print_r($e);
return $this->error('操作錯誤'); return $this->error('操作錯誤');
} }
} }
public function setUCDefault(){ public function setUCDefault()
{
if (!$this->uid) { if (!$this->uid) {
$this->error('用戶ID錯誤'); $this->error('用戶ID錯誤');
} }
@ -175,14 +228,14 @@ class User extends ApiController
->select(); ->select();
return $this->success($result); return $this->success($result);
} catch (\Exception $e) { } catch (\Exception $e) {
print_r($e); print_r($e);
return $this->error('操作錯誤'); return $this->error('操作錯誤');
} }
} }
public function updateUserCompany(){ public function updateUserCompany()
{
if (!$this->uid) { if (!$this->uid) {
$this->error('用戶ID錯誤'); $this->error('用戶ID錯誤');
} }
@ -222,14 +275,14 @@ class User extends ApiController
->select(); ->select();
return $this->success($result); return $this->success($result);
} catch (\Exception $e) { } catch (\Exception $e) {
print_r($e); print_r($e);
return $this->error('操作錯誤'); return $this->error('操作錯誤');
} }
} }
public function deleteUserCompany(){ public function deleteUserCompany()
{
if (!$this->uid) { if (!$this->uid) {
$this->error('用戶ID錯誤'); $this->error('用戶ID錯誤');
} }
@ -254,18 +307,15 @@ class User extends ApiController
'address' => '', 'address' => '',
'url' => '', 'url' => '',
]); ]);
} }
return $this->success($result); return $this->success($result);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->error('操作錯誤'); return $this->error('操作錯誤');
} }
} }
public function setUserLevel(){ public function setUserLevel()
{
$result = Db::name('user') $result = Db::name('user')
->where('user_id', $this->uid) ->where('user_id', $this->uid)
->update(['level' => input('level')]); ->update(['level' => input('level')]);
@ -274,7 +324,8 @@ class User extends ApiController
return $this->Success($result); return $this->Success($result);
} }
public function setUserTpl(){ public function setUserTpl()
{
try { try {
$result = Db::name('user') $result = Db::name('user')
->where('user_id', $this->uid) ->where('user_id', $this->uid)
@ -286,7 +337,8 @@ class User extends ApiController
return $this->Success($result); return $this->Success($result);
} }
public function uploadAvatar(){ public function uploadAvatar()
{
$files = request()->file('file'); $files = request()->file('file');
$savename = \think\facade\Filesystem::disk('public')->putFile(input('user_id'), $files); $savename = \think\facade\Filesystem::disk('public')->putFile(input('user_id'), $files);
@ -301,7 +353,8 @@ class User extends ApiController
return $this->Success($avatar); return $this->Success($avatar);
} }
public function updateSendCount(){ public function updateSendCount()
{
$user_id = input('userid'); $user_id = input('userid');
Db::name('user') Db::name('user')
@ -313,7 +366,8 @@ class User extends ApiController
return $this->Success('更新成功'); return $this->Success('更新成功');
} }
public function addFavorite(){ public function addFavorite()
{
try { try {
$is_exist = Db::name('user_favorite') $is_exist = Db::name('user_favorite')
->where('user_id', $this->uid) ->where('user_id', $this->uid)
@ -330,7 +384,8 @@ class User extends ApiController
} }
} }
public function delFavorite(){ public function delFavorite()
{
try { try {
Db::name('user_favorite') Db::name('user_favorite')
->where('id', input('id')) ->where('id', input('id'))
@ -342,7 +397,8 @@ class User extends ApiController
} }
} }
public function uploadConnections(){ public function uploadConnections()
{
try { try {
Db::name('user') Db::name('user')
->where('user_id', $this->uid) ->where('user_id', $this->uid)
@ -355,7 +411,8 @@ class User extends ApiController
} }
} }
public function getFavorite(){ public function getFavorite()
{
try { try {
$uf = Db::name('user_favorite') $uf = Db::name('user_favorite')
->alias('uf') ->alias('uf')
@ -365,9 +422,6 @@ class User extends ApiController
->select() ->select()
->toArray(); ->toArray();
// print_r(Db::getLastSql());
// print_r($uf);
// return;
$aes = new Aes([]); $aes = new Aes([]);
foreach ($uf as $key => $val) { foreach ($uf as $key => $val) {
@ -379,7 +433,6 @@ class User extends ApiController
} }
$uf[$key]['nfcurl'] = getUrl() . '/card/?params=' . $params; $uf[$key]['nfcurl'] = getUrl() . '/card/?params=' . $params;
} }
return $this->success($uf); return $this->success($uf);
@ -388,7 +441,8 @@ class User extends ApiController
} }
} }
public function getConnections(){ public function getConnections()
{
try { try {
$result = Db::name('user') $result = Db::name('user')
->where('user_id', $this->uid) ->where('user_id', $this->uid)

@ -31,8 +31,8 @@ function getIdByLid($lid){
} }
function getUseridByCuid($cuid){ function getUseridByCuid($cuid){
$id =Db::name('user') $id =Db::name('user_sno')
->where('uniqid',$cuid) ->where('serial_no',$cuid)
->value('user_id'); ->value('user_id');
if(!$id){ if(!$id){
@ -70,8 +70,32 @@ function genUniqid($prefix='mc'){
* *
* @return array * @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'; $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rand = $code[rand(0,25)] $rand = $code[rand(0,25)]
.strtoupper(dechex(date('m'))) .strtoupper(dechex(date('m')))
.date('d').substr(time(),-5) .date('d').substr(time(),-5)
@ -83,7 +107,7 @@ function genSerialNo(){
$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
$d = '', $d = '',
$f = 0; $f = 0;
$f < 8; $f < $len;
$g = ord( $a[ $f ] ), $g = ord( $a[ $f ] ),
$d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ], $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
$f++ $f++
@ -91,7 +115,6 @@ function genSerialNo(){
return $d; return $d;
} }
function encodeRefer($userId) function encodeRefer($userId)
{ {
$sourceString = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ'; $sourceString = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';

@ -0,0 +1,39 @@
<?php
namespace app\common;
use think\facade\Db;
use app\common\lib\Aes;
class Activation
{
//驗證slashtoken
public static function checkSlashtoken($slashtoken)
{
$aes = new Aes([]);
parse_str($aes->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];
}
}

@ -12,7 +12,6 @@ class Aes{
*/ */
public function __construct($config){ public function __construct($config){
foreach($config as $k=>$v){ foreach($config as $k=>$v){
$this->$k = $v; $this->$k = $v;
} }
} }

Loading…
Cancel
Save