| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\ApiController;
- use app\common\validate\IDMustBePositiveInt;
- use Lettered\Support\Auth as IAuth;
- use think\App;
- class Address extends ApiController
- {
- /**
- * 地址列表
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/6/30 16:45
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\FailedException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function index(){
- $model = model('common/UsersAddr')
- ->where(['user_id' => $this->auth->user()->id]);
- // 加载默认数据
- if (input('default') != ""){
- $model->where(['default' => 1]);
- return $this->ApiJson(0,'OK!', $model->find());
- }
- $addr = $model->order('default','desc')->select();
- return $this->ApiJson(0,'OK!', $addr);
- }
- /**
- * 新增数据
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/7 18:27
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\FailedException
- */
- public function save()
- {
- // 接收数据
- $params = $this->request->param();
- // 数据校验
- $valid = $this->validate($params, [
- 'name|姓名' => 'require',
- 'mobile|手机号' => 'require',
- 'province|省份' => 'require',
- 'city|城市' => 'require',
- 'country|县区' => 'require',
- 'detail|地址详细信息' => 'require',
- ]);
- // 错误返回
- if(true !== $valid){
- return $this->ApiJson(-1, $valid);
- };
- // 追加数据
- $params['user_id'] = $this->auth->user()['id'];
- $params['default'] = $params['default'] == 'true' ? 1 : 0;
- $ret = model('common/UsersAddr')->storeBy($params);
- if ($ret){
- // 默认处理
- if ($params['default']){
- model('common/UsersAddr')
- ->where(['user_id' => $this->auth->user()['id']])
- ->where('id','<>' , $ret)
- ->update(['default' => 0]);
- }
- return $this->ApiJson(0,"新增成功");
- }
- return $this->ApiJson(-1,"数据异常,请稍后重试");
- }
- /**
- * 地址更新
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/1 8:50
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\FailedException
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function update($id)
- {
- (new IDMustBePositiveInt())->valid();
- // 接收数据
- $params = $this->request->param();
- // 查询
- $addr = model('common/UsersAddr')->findBy($id);
- // 数据校验
- $valid = $this->validate($params, [
- 'name|姓名' => 'require',
- 'mobile|手机号' => 'require',
- 'province|省份' => 'require',
- 'city|城市' => 'require',
- 'country|县区' => 'require',
- 'detail|地址详细信息' => 'require',
- ]);
- // 错误返回
- if(true !== $valid){
- return $this->ApiJson(-1, $valid);
- };
- $params['default'] = $params['default'] == 'true' ? 1 : 0;
- // 更新
- $ret = $addr->updateBy($id, $params);
- // 默认处理
- if ($params['default'] == 1 && $params['default'] != $addr['default']){
- model('common/UsersAddr')
- ->where(['user_id' => $this->auth->user()['id']])
- ->where('id','<>' , $id)
- ->update(['default' => 0]);
- }
- if ($ret){
- return $this->ApiJson(0,"更新成功");
- }
- return $this->ApiJson(-1,"数据异常,请稍后重试");
- }
- /**
- * 删除地址
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/7/2 11:32
- *
- * @param $id
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- */
- public function delete($id)
- {
- (new IDMustBePositiveInt())->valid();
- $ret = model('common/UsersAddr')->deleteBy($id);
- if ($ret){
- return $this->ApiJson(0,"删除地址成功");
- }
- return $this->ApiJson(-1,"数据异常,请稍后重试");
- }
- }
|