thinkPHP5框架接口写法简单示例

这篇文章主要介绍了thinkPHP5框架接口写法,结合实例形式分析了thinkPHP5框架数据处理接口的具体实现技巧,需要的朋友可以参考下。

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

控制器

  1. /**
  2. * 添加收货地址
  3. */
  4. public function addAddress(){
  5. $post = $this->request->post();
  6. //验证 唯一规则: 表名,字段名,排除主键值,主键名
  7. $validate = new \think\Validate([
  8. ['uid', 'require', '用户id不能为空'],
  9. ['name', 'require|max:20', '收件人不能为空'],
  10. ['mobile', 'require|length:11', '手机号码不能为空'],
  11. ['province_id', 'require', '省份不能为空'],
  12. ['city_id', 'require', '城市不能为空'],
  13. ['district_id', 'require', '县区不能为空'],
  14. ['detail', 'require|max:100', '地址详情不能为空'],
  15. ],[
  16. 'mobile.length' => '手机号码格式不正确',
  17. 'name.max' => '收件人不能超过20个字符',
  18. 'detail.max' => '地址详情不能超过100个字符',
  19. ]);
  20. //验证部分数据合法性
  21. if (!$validate->check($post)) {
  22. \Org\Response::show(400,'提交失败:' . $validate->getError());
  23. }
  24. $user_id = $post['uid'];
  25. $name = $post['name'];
  26. $mobile = $post['mobile'];
  27. $province_id = $post['province_id'];
  28. $city_id = $post['city_id'];
  29. $district_id = $post['district_id'];
  30. $detail = $post['detail'];
  31. $is_address = model('address')->addAddress($user_id,$name,$mobile,$province_id,$city_id,$district_id,$detail);
  32. if($is_address){
  33. \Org\Response::show(200,'access!');
  34. }else{
  35. \Org\Response::show(400,'添加失败!');
  36. }
  37. }

model

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

Response

  1. <?php
  2. namespace Org;
  3. class Response {
  4. const JSON = "json";
  5. /**
  6. * 按综合方式输出通信数据
  7. * @param integer $code 状态码
  8. * @param string $message 提示信息
  9. * @param array $data 数据
  10. * @param string $type 数据类型
  11. * return string
  12. */
  13. public static function show($code, $message = '', $data = array(), $type = self::JSON) {
  14. if(!is_numeric($code)) {
  15. return '';
  16. }
  17. // $type = 'json';
  18. isset($_GET['format']) ? $_GET['format'] : self::JSON;
  19. $result = array(
  20. 'code' => $code,
  21. 'message' => $message,
  22. 'data' => $data,
  23. );
  24. if($type == 'json') {
  25. self::json($code, $message, $data);
  26. exit;
  27. } elseif($type == 'array') {
  28. var_dump($result);
  29. } elseif($type == 'xml') {
  30. self::xmlEncode($code, $message, $data);
  31. exit;
  32. } else {
  33. // TODO
  34. }
  35. }
  36. /**
  37. * 按json方式输出通信数据
  38. * @param integer $code 状态码
  39. * @param string $message 提示信息
  40. * @param array $data 数据
  41. * return string
  42. */
  43. public static function json($code, $message = '', $data = array()) {
  44. if(!is_numeric($code)) {
  45. return '';
  46. }
  47. $result = array(
  48. 'code' => $code,
  49. 'message' => urlencode($message),
  50. 'data' => $data
  51. );
  52. echo urldecode(json_encode($result,JSON_UNESCAPED_UNICODE));
  53. exit;
  54. }
  55. /**
  56. * 按xml方式输出通信数据
  57. * @param integer $code 状态码
  58. * @param string $message 提示信息
  59. * @param array $data 数据
  60. * return string
  61. */
  62. public static function xmlEncode($code, $message, $data = array()) {
  63. if(!is_numeric($code)) {
  64. return '';
  65. }
  66. $result = array(
  67. 'code' => $code,
  68. 'message' => $message,
  69. 'data' => $data,
  70. );
  71. header("Content-Type:text/xml");
  72. $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
  73. $xml .= "<root>\n";
  74. $xml .= self::xmlToEncode($result);
  75. $xml .= "</root>";
  76. echo $xml;
  77. }
  78. public static function xmlToEncode($data) {
  79. $xml = $attr = "";
  80. foreach($data as $key => $value) {
  81. if(is_numeric($key)) {
  82. $attr = " ";
  83. $key = "item";
  84. }
  85. $xml .= "<{$key}{$attr}>";
  86. $xml .= is_array($value) ? self::xmlToEncode($value) : $value;
  87. $xml .= "</{$key}>\n";
  88. }
  89. return $xml;
  90. }
  91. }