## 基礎控制器
```
~~~
namespace app\api\controller;
use app\api\service\Token;
use think\Controller;
class BaseController extends Controller
{
protected function checkExclusiveScope()
{
Token::needExclusiveScope();
}
protected function checkPrimaryScope()
{
Token::needPrimaryScope();
}
protected function checkSuperScope()
{
Token::needSuperScope();
}
}
~~~
```
##
## 其他控制器繼承這個
```
~~~
namespace app\api\controller\v1;
use app\api\controller\BaseController;
use app\api\model\User;
use app\api\model\UserAddress;
use app\api\service\Token;
use app\api\service\Token as TokenService;
use app\api\validate\AddressNew;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UserException;
use think\Controller;
use think\Exception;
class Address extends BaseController
{
protected $beforeActionList = [
'checkPrimaryScope' => ['only' => 'createOrUpdateAddress,getUserAddress']
];
/**
* 獲取用戶地址信息
* @return UserAddress
* @throws UserException
*/
public function getUserAddress(){
$uid = Token::getCurrentUid();
$userAddress = UserAddress::where('user_id', $uid)
->find();
if(!$userAddress){
throw new UserException([
'msg' => '用戶地址不存在',
'errorCode' => 60001
]);
}
return $userAddress;
}
/**
* 更新或者創建用戶收獲地址
*/
public function createOrUpdateAddress()
{
$validate = new AddressNew();
$validate->goCheck();
$uid = TokenService::getCurrentUid();
$user = User::get($uid);
if(!$user){
throw new UserException([
'code' => 404,
'msg' => '用戶收獲地址不存在',
'errorCode' => 60001
]);
}
$userAddress = $user->address;
// 根據規則取字段是很有必要的,防止惡意更新非客戶端字段
$data = $validate->getDataByRule(input('post.'));
if (!$userAddress )
{
// 關聯屬性不存在,則新建
$user->address()
->save($data);
}
else
{
// 存在則更新
// fromArrayToModel($user->address, $data);
// 新增的save方法和更新的save方法并不一樣
// 新增的save來自于關聯關系
// 更新的save來自于模型
$user->address->save($data);
}
return new SuccessMessage();
}
}
~~~
```
- tp5圖片上傳
- 文件上傳到七牛云
- 上傳到阿里云
- 富文本編輯器
- phpexcel和spreadsheet
- phpexcel導出
- phpexcel導入
- spreadsheet
- tp5_api接口
- 跨域請求
- JWT
- 圖片和視頻上傳接口
- 驗證碼
- tp5小程序登錄
- tp5小程序支付
- tp5基礎架構
- 驗證層
- 模型層Model
- 控制器構找
- tp5.0支付寶
- 海報二維碼
- 輪播圖
- echarts柱狀圖
- layui的圖片彈窗
- p標簽顯示指定行數(全部)
- jquery和layerdate調用日期
- ajax發送文件和圖片的坑啊
- JS日期點擊上一天和下一天
- 百度分享js
- POST請求
- 商品數據表
- tp5.0支付寶最全
- tp5路由的坑
- 二維數組排序
- tp5模型分組group錯誤
- 二維變一維數組
- 無限樹形結構
- json對象轉數組
- 模型關聯查詢
- tp5的模型獲取器和字段設定
- 經緯度獲取距離排序