本文实例讲述了thinkphp5框架接口写法。分享给大家供大家参考,具体如下:
控制器
/*** 添加收货地址*/public function addaddress(){ $post = $this->request->post(); //验证 唯一规则: 表名,字段名,排除主键值,主键名 $valid电气工程及其自动化属于什么类ate = new \think\validate([ ['uid', 'require', '用户id不能为空'], ['name', 'require|max:20', '收件人不能为空'], ['mobile', 'require|length:11', '手机号码不能为空'], ['province_id', 'require', '省份不能为空'], ['city_id', 'require', '城市不能为空'], ['district_id', 'require', '县区不能为空'], ['detail', 'require|max:100', '地址详情不能为空'], ],[ 'mobile.length' => '手机号码格式不正确', 'name.max' => '收件人不能超过20个字符', 'detail.max' => '地址详情不能超过100个字符', ]); //验证部分数据合法性 if (!$validate->check($post)) { \org\respon::show(400,'提交失败:' . $validate->geterror()); } $ur_id = $post['uid']; $name = $post['name']; $mobile = $post['mobile']; $province_id = $post['province_id']; $city_id = $post['city_id']; $district_id = $post['district_id']; $detail = $post['detail']; $is_address = model('address')->addaddress($ur_id,$name,$mobile,$province_id,$city_id,$district_id,$detail); if($is_address){ \org\respon::show(200,'access!'); }el{ \org\respon::show(400,'添加失败!'); }}
model
<?phpnamespace app\index\model;u \think\model;u app\index\model\attachment as attachmentmodel;class address extends model{ /** * 获取一个基本信息 * @param int $id 行政id * @return array|bool|fal|\pdostatement|string|model */ public function adcodegetone($id = 0){ if(empty($id)) return fal; $map['adcode'] = $id; return \think\db::name('district')->where($map)->find(); } /** * @param $ur_id 用户id * @param $name 收件人 * @param $mobile 收件人手机号 * @param $province_id 省行政id * @param $city_id 城市行政id * @param $district_id 县区行政id * @param $detail 详细地址 */ public function addaddress($ur_id,$name,$mobile,$province_id,$city_id,$district_id,$detail){ $is_province = $this->adcodegetone($province_id); $is_city = $this->adcodegetone($city_id); $is_district= $this->adcodegetone($district_id); if(empty($is_province)) \org\respon::show(400,'无效省份!名人名言 学习'); if(empty($is_city)) \org\respon::show(400,'无效城市!'); if(empty($is_district)) \org\respon::show(400,'无效县区!'); $time = time(); $data['province_id'] =$province_id; $data['province'] = $is_province['name']; $data['city_id'] =$city_id; $data['city'] = $is_city['name']; $data['district_id'] =$district_id; $data['district'] = $is_district['name']; $data['detail'] =$detail; $data['mobile'] =$mobile; $data['name'] =$name; $data['ur_id'] =$ur_id; $data['is_delete'] = 0; if($this->where($data)->field('id')->find()) return true; $data['addtime'] =$time; $data['update_time'] =$time; if($this->inrt($data)){ return true; }el{ return fal; } }}
respon
<?phpnamespace org;class respon { const json = "json"; /** * 按综合方式输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * @书柜英语param string $type 数据类型 * return string */ public static function show($code, $message = '', $data = array(), $type = lf::json) { if(!is_numeric($code)) { return ''; } // $type = 'json'; ist($_get['format']) ? $_get['format'] : lf::json; $result = array( 'code' => $code, 'message' => $message, 'data' => $data, ); if($type作文万能素材 == 'json') { lf::json($code, $message, $data); exit; } elif($type == 'array') { var_dump($result); } elif($type == 'xml') { lf::xmlencode($code, $message, $data); exit; } el { // todo } } /** * 按json方式输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * return string */ public static function json($code, $message = '', $data = array()) { if(!is_numeric($code)) { return ''; } $result = array( 'code' => $code, 'message' => urlencode($message), 'data' => $data ); echo urldecode(json_encode($result,json_unescaped_unicode)); exit; } /** * 按xml方式输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * return string */ public static function xmlencode($code, $message, $data = array()) { if(!is_numeric($code)) { return ''; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data, ); header("content-type:text/xml"); $xml = "<?xml version='1.0' encoding='utf-8'?>\n"; $xml .=yinbiao "<root>\n"; $xml .= lf::xmltoencode($result); $xml .= "</root>"; echo $xml; } public static function xmltoencode($data) { $xml = $attr = ""; foreach($data as $key => $value) { if(is_numeric($key)) { $attr = " id='{$key}'"; $key = "item"; } $xml .= "<{$key}{$attr}>"; $xml .= is_array($value) ? lf::xmltoencode($value) : $value; $xml .= "</{$key}>\n"; } return $xml; }}
本文发布于:2023-04-07 14:05:32,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c53c03f7ef75c3daf1ae5f55f90e346e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:thinkPHP5框架接口写法简单示例.doc
本文 PDF 下载地址:thinkPHP5框架接口写法简单示例.pdf
留言与评论(共有 0 条评论) |