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,'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,'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]]); } }