当前位置:编程学习 > > 正文

thinkphp5开发教程(thinkPHP5框架接口写法简单示例)

时间:2022-01-15 01:35:22类别:编程学习

thinkphp5开发教程

thinkPHP5框架接口写法简单示例

本文实例讲述了thinkPHP5框架接口写法。分享给大家供大家参考,具体如下:

控制器

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • /**
  • * 添加收货地址
  • */
  • public function addAddress(){
  •     $post = $this->request->post();
  •     //验证 唯一规则: 表名,字段名,排除主键值,主键名
  •     $validate = 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\Response::show(400,'提交失败:' . $validate->getError());
  •     }
  •     $user_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($user_id,$name,$mobile,$province_id,$city_id,$district_id,$detail);
  •     if($is_address){
  •       \Org\Response::show(200,'access!');
  •     }else{
  •       \Org\Response::show(400,'添加失败!');
  •     }
  • }
  • model

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • <?php
  • namespace app\index\model;
  • use \think\Model;
  • use app\index\model\Attachment as AttachmentModel;
  • class Address extends Model
  • {
  •   /**
  •    * 获取一个基本信息
  •    * @param int $id   行政id
  •    * @return array|bool|false|\PDOStatement|string|Model
  •    */
  •   public function adcodeGetOne($id = 0){
  •     if(empty($id)) return false;
  •     $map['adcode'] = $id;
  •     return \think\Db::name('district')->where($map)->find();
  •   }
  •   /**
  •    * @param $user_id   用户id
  •    * @param $name     收件人
  •    * @param $mobile    收件人手机号
  •    * @param $province_id 省行政id
  •    * @param $city_id   城市行政id
  •    * @param $district_id 县区行政id
  •    * @param $detail    详细地址
  •    */
  •   public function addAddress($user_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\Response::show(400,'无效省份!');
  •     if(empty($is_city)) \Org\Response::show(400,'无效城市!');
  •     if(empty($is_district)) \Org\Response::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['user_id'] =$user_id;
  •     $data['is_delete'] = 0;
  •     if($this->where($data)->field('id')->find()) return true;
  •     $data['addtime'] =$time;
  •     $data['update_time'] =$time;
  •     if($this->insert($data)){
  •       return true;
  •     }else{
  •       return false;
  •     }
  •   }
  • }
  • Response

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • <?php
  • namespace Org;
  • class Response {
  •  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 = self::JSON) {
  •  if(!is_numeric($code)) {
  •   return '';
  •  }
  •  // $type = 'json';
  •  isset($_GET['format']) ? $_GET['format'] : self::JSON;
  •  $result = array(
  •   'code' => $code,
  •   'message' => $message,
  •   'data' => $data,
  •  );
  •  if($type == 'json') {
  •   self::json($code, $message, $data);
  •   exit;
  •  } elseif($type == 'array') {
  •   var_dump($result);
  •  } elseif($type == 'xml') {
  •   self::xmlEncode($code, $message, $data);
  •   exit;
  •  } else {
  •   // 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 .= "<root>\n";
  •  $xml .= self::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) ? self::xmlToEncode($value) : $value;
  •   $xml .= "</{$key}>\n";
  •  }
  •  return $xml;
  •  }
  • }
  • 希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

    原文链接:https://blog.csdn.net/qq_32562501/article/details/82684030

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐