123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use App\Models\CustomAddr as Model;
- use App\Http\Requests\Api\CustomAddr as Request;
- use App\Models\City;
- use App\Models\Custom;
- /**
- * 客户地址
- *
- * @author 刘相欣
- *
- * */
- class CustomAddr extends Api{
-
- /**
- * 获取列表 /api/custom_addr/get_list
- *
- * @param string $code 授权码
- *
- * */
- public function get_list(Model $Model){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 查询信息
- $list = $Model->getListByCustom($uid);
- // 返回结果
- return json_send(['code'=>'success','msg'=>'获取成功','data'=>$list]);
- }
-
- /**
- * 添加 /api/custom_addr/add
- *
- * @param string $code 授权码
- *
- * */
- public function add(Request $request,Model $Model,Custom $Custom,City $City){
- // 接口验签
- // $this->verify_sign();
- // 验证参数
- $request->scene('add')->validate();
- // 检查登录
- $uid = $this->checkLogin();
- // 接收参数
- $data['contact_province'] = trim(request('contact_province',''));
- $data['contact_city'] = trim(request('contact_city',''));
- $data['contact_area'] = trim(request('contact_area',''));
- $data['contact_addr'] = trim(request('contact_addr',''));
- $data['contact_name'] = trim(request('contact_name',''));
- $data['contact_shop'] = trim(request('contact_shop',''));
- $data['shop_type'] = trim(request('shop_type',0));
- $data['contact_phone'] = trim(request('contact_phone',''));
- $data['is_default'] = request('is_default',0);
- $data['custom_uid'] = $uid;
- // 获取客户城市ID
- $cityId = (int) $Custom->getValue($uid,'city_id');
- $cityName = $City->getOne($cityId,'name');
- $pid = $City->getOne($cityId,'pid');
- $province = $City->getOne($pid,'name');
- // 判断选择的城市名称是不是一致
- if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.($province=='直辖县级'?$cityName:$province).'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
- // 替换地址中间的空格
- $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
- // 最大数量
- $limitMax = 20;
- $havaNum = $Model->query()->where([['custom_uid','=',$uid]])->count();
- // 如果是最大数量的话
- if( $havaNum > $limitMax ) return json_send(['code'=>'error','msg'=>'您已经有'.$limitMax.'条地址了','data'=>['error'=>'最多只能保20条地址']]);
- // 如果需要默认当前的话
- if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
- // 查询是否已经提交过
- $result = $Model->add($data);
- // 如果用户状态被拉黑,不允许登录
- if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
- // 返回结果
- return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
- }
- /**
- * 修改 /api/custom_addr/edit
- *
- * @param string $code 授权码
- *
- * */
- public function edit(Request $request,Model $Model,Custom $Custom,City $City){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 验证参数
- $request->scene('edit')->validate();
- // 接收参数
- $id = request('id',0);
- $data['contact_province'] = trim(request('contact_province',''));
- $data['contact_city'] = trim(request('contact_city',''));
- $data['contact_area'] = trim(request('contact_area',''));
- $data['contact_addr'] = trim(request('contact_addr',''));
- $data['contact_name'] = trim(request('contact_name',''));
- $data['contact_shop'] = trim(request('contact_shop',''));
- $data['shop_type'] = trim(request('shop_type',0));
- $data['contact_phone'] = trim(request('contact_phone',''));
- $data['is_default'] = request('is_default',0);
- // 获取客户城市ID
- $cityId = (int) $Custom->getValue($uid,'city_id');
- $cityName = $City->getOne($cityId,'name');
- $pid = $City->getOne($cityId,'pid');
- $province = $City->getOne($pid,'name');
- // 判断选择的城市名称是不是一致
- if( trim($cityName) != trim($data['contact_city']) ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.($province=='直辖县级'?$cityName:$province).'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
- // 替换地址中间的空格
- $data['contact_addr'] = str_ireplace(' ','',$data['contact_addr']);
- // 如果需要默认当前的话
- if( $data['is_default'] ) $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
- // 查询是否已经提交过
- $result = $Model->edit($id,$data);
- // 如果用户状态被拉黑,不允许登录
- if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
- // 返回结果
- return json_send(['code'=>'success','msg'=>'保存成功','data'=>$data]);
- }
-
- /**
- * 删除 /api/custom_addr/del
- *
- * @param string $code 授权码
- *
- * */
- public function del(Request $request,Model $Model){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 验证参数
- $request->scene('del')->validate();
- // 接收参数
- $id = request('id',0);
- // 如果需要默认当前的话
- $result = $Model->del($id);
- // 如果用户状态被拉黑,不允许登录
- if( !$result ) return json_send(['code'=>'success','msg'=>'删除失败','data'=>['error'=>'删除失败']]);
- // 返回结果
- return json_send(['code'=>'success','msg'=>'删除成功','data'=>['id'=>$id]]);
- }
- /**
- * 修改 /api/custom_addr/set_default
- *
- * @param string $code 授权码
- *
- * */
- public function set_default(Request $request,Model $Model){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 验证参数
- $request->scene('set_default')->validate();
- // 接收参数
- $id = request('id',0);
- // 如果需要默认当前的话
- $Model->query()->where([['custom_uid','=',$uid]])->update(['is_default'=>0]);
- // 查询是否已经提交过
- $result = $Model->edit($id,['is_default'=>1]);
- // 如果用户状态被拉黑,不允许登录
- if( !$result ) return json_send(['code'=>'error','msg'=>'保存失败,请重试','data'=>['error'=>'写入失败']]);
- // 返回结果
- return json_send(['code'=>'success','msg'=>'保存成功','data'=>['id'=>$id]]);
- }
-
- }
|