You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
5.8 KiB
195 lines
5.8 KiB
<?php
|
|
|
|
namespace app\api\controller\v1;
|
|
|
|
use app\api\ApiController;
|
|
use think\facade\Db;
|
|
|
|
use app\api\validate\User as UserValidate;
|
|
use think\exception\ValidateException;
|
|
|
|
use app\service\Sso;
|
|
|
|
class User extends ApiController
|
|
{
|
|
public function list()
|
|
{
|
|
}
|
|
|
|
public function add()
|
|
{
|
|
|
|
// //參數檢查
|
|
// try {
|
|
// validate(UserValidate::class)->check(input());
|
|
// } catch (ValidateException $e) {
|
|
// // 驗證失敗 輸出錯誤信息
|
|
|
|
// // dump($e->getError());
|
|
// return $this->Error($e->getError(),501,'參數錯誤');
|
|
// }
|
|
$params = input();
|
|
|
|
//檢查user_id是否存在
|
|
$user = Db::name('user')
|
|
->where('user_id', $params['user_id'])
|
|
->find();
|
|
if (!$user) {
|
|
// 用戶不存在處理
|
|
//使用user_id至sso server取得用戶資料
|
|
$user_data = [
|
|
'user_id' => input('user_id'),
|
|
];
|
|
|
|
$sso = Sso::getUserInfo($user_data);
|
|
|
|
if(!$sso['code']==200){
|
|
return $this->error('get sso user info error!!!');
|
|
}
|
|
|
|
$sso_data = $sso['data'];
|
|
|
|
try{
|
|
|
|
$sso_data['cus_card']='';
|
|
$sso_data['create_time']=date('Y-m-d H:i:s');
|
|
$sso_data['overdue_time'] = time() + (60 * 60 * 24 * 365);
|
|
$sso_data['status'] = 1;
|
|
$sso_data['level'] = 1;
|
|
|
|
|
|
Db::name('user')
|
|
->insert($sso_data);
|
|
|
|
|
|
$qrcodeUrl = genQrCode('https://' . $_SERVER['HTTP_HOST'] . '/home/?refer='.$sso_data['code'], $sso_data['user_id'], 'refer');
|
|
|
|
$nfcUrl = genQrCode('https://' . $_SERVER['HTTP_HOST'] . '/card?userid=' . $sso_data['user_id'], $sso_data['user_id'], 'nfc');
|
|
|
|
}catch(\Exception $e){
|
|
print_r($e->getMessage());
|
|
return $this->error('sync sso user info error!!!');
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
//用戶存在處理
|
|
//取得用戶到期日
|
|
$user_info = $user;
|
|
|
|
//判斷order_sn是否執行過
|
|
if ($user_info['note'] == input('order_sn')) {
|
|
return $this->Error('重覆更新', 201, '新增失敗');
|
|
}
|
|
|
|
$update_data = [
|
|
'status' => 1,
|
|
'level' => 1,
|
|
'overdue_time' => intval($user_info['overdue_time']) + (365 * 24 * 60 * 60),
|
|
'note' => input('order_sn'),
|
|
'update_time' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
try{
|
|
Db::name('user')
|
|
->where('user_id', $params['user_id'])
|
|
->update($update_data);
|
|
|
|
|
|
}catch(\Exception $e){
|
|
return $this->Error($e->getMessage(), 500, '新增失敗');
|
|
}
|
|
}
|
|
|
|
//取得到期時間
|
|
$overdue_time = Db::name('user')
|
|
->where('user_id', $params['user_id'])
|
|
->value('overdue_time');
|
|
|
|
return $this->Success($overdue_time);
|
|
}
|
|
|
|
public function cancel(){
|
|
// //參數檢查
|
|
// try {
|
|
// validate(UserValidate::class)->check(input());
|
|
// } catch (ValidateException $e) {
|
|
// // 驗證失敗 輸出錯誤信息
|
|
|
|
// // dump($e->getError());
|
|
// return $this->Error($e->getError(),501,'參數錯誤');
|
|
// }
|
|
$params = input();
|
|
|
|
//檢查user_id是否存在
|
|
$user = Db::name('user')
|
|
->where('user_id', $params['user_id'])
|
|
->find();
|
|
|
|
if (!$user) {
|
|
return $this->Error('用戶不存在', 500);
|
|
} else {
|
|
//用戶存在處理
|
|
//取得用戶到期日
|
|
$user_info = $user;
|
|
|
|
//判斷order_sn是否執行過,沒有的話就不處理
|
|
if (trim($user_info['note']) != trim(input('order_sn'))) {
|
|
return $this->Error('未自動新增過', 501);
|
|
}
|
|
|
|
$overdue_time = intval($user_info['overdue_time']) - (365 * 24 * 60 * 60);
|
|
if($overdue_time < time()){
|
|
$level = 0;
|
|
}else{
|
|
$level = 1;
|
|
}
|
|
|
|
$update_data = [
|
|
'status' => 1,
|
|
'level' => $level,
|
|
'overdue_time' => $overdue_time,
|
|
'note' => '',
|
|
'update_time' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
try{
|
|
Db::name('user')
|
|
->where('user_id', $params['user_id'])
|
|
->update($update_data);
|
|
|
|
echo Db::getLastSql();
|
|
}catch(\Exception $e){
|
|
return $this->Error($e->getMessage(), 500, '新增失敗');
|
|
}
|
|
return $this->Success('更新成功');
|
|
}
|
|
}
|
|
|
|
public function setLevel()
|
|
{
|
|
//參數檢查
|
|
try {
|
|
validate(\app\api\validate\UserLevel::class)->check(input());
|
|
} catch (ValidateException $e) {
|
|
return $this->Error($e->getError(), 501, '參數錯誤');
|
|
}
|
|
|
|
$prefix = getPrefixByAppId(input('appid'));
|
|
$user_id = $prefix . input('user_id');
|
|
$level = input('level') ? input('level') : 0;
|
|
$overdue_time = input('overdue_time') ? input('overdue_time') : (time() + (60 * 60 * 24 * 365));
|
|
|
|
try {
|
|
$result = Db::name('user')
|
|
->where('user_id', $user_id)
|
|
->update(['level' => $level, 'overdue_time' => $overdue_time]);
|
|
} catch (\Exception $e) {
|
|
return $this->Error('系統錯誤', 500, '新增失敗');
|
|
}
|
|
|
|
return $this->Success('更新成功');
|
|
}
|
|
}
|