佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > thinkPHP5框架接口寫法簡單示例

thinkPHP5框架接口寫法簡單示例

熱門標(biāo)簽:服務(wù)器配置 阿里云 Mysql連接數(shù)設(shè)置 Linux服務(wù)器 電子圍欄 科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 銀行業(yè)務(wù)

本文實例講述了thinkPHP5框架接口寫法。分享給大家供大家參考,具體如下:

控制器

/**
* 添加收貨地址
*/
public function addAddress(){
    $post = $this->request->post();
    //驗證 唯一規(guī)則: 表名,字段名,排除主鍵值,主鍵名
    $validate = new \think\Validate([
      ['uid', 'require', '用戶id不能為空'],
      ['name', 'require|max:20', '收件人不能為空'],
      ['mobile', 'require|length:11', '手機號碼不能為空'],
      ['province_id', 'require', '省份不能為空'],
      ['city_id', 'require', '城市不能為空'],
      ['district_id', 'require', '縣區(qū)不能為空'],
      ['detail', 'require|max:100', '地址詳情不能為空'],
    ],[
      'mobile.length' => '手機號碼格式不正確',
      'name.max' => '收件人不能超過20個字符',
      'detail.max' => '地址詳情不能超過100個字符',
    ]);
    //驗證部分?jǐn)?shù)據(jù)合法性
    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

?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 縣區(qū)行政id
   * @param $detail    詳細(xì)地址
   */
  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,'無效縣區(qū)!');
    $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

?php
namespace Org;
class Response {
 const JSON = "json";
 /**
 * 按綜合方式輸出通信數(shù)據(jù)
 * @param integer $code 狀態(tài)碼
 * @param string $message 提示信息
 * @param array $data 數(shù)據(jù)
 * @param string $type 數(shù)據(jù)類型
 * 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方式輸出通信數(shù)據(jù)
 * @param integer $code 狀態(tài)碼
 * @param string $message 提示信息
 * @param array $data 數(shù)據(jù)
 * 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方式輸出通信數(shù)據(jù)
 * @param integer $code 狀態(tài)碼
 * @param string $message 提示信息
 * @param array $data 數(shù)據(jù)
 * 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;
 }
}

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • Thinkphp5微信小程序獲取用戶信息接口的實例詳解
  • 使用Thinkphp框架開發(fā)移動端接口
  • ThinkPHP實現(xiàn)支付寶接口功能實例
  • Thinkphp微信公眾號支付接口
  • thinkPHP框架對接支付寶即時到賬接口回調(diào)操作示例
  • thinkPHP微信分享接口JSSDK用法實例
  • Thinkphp框架開發(fā)移動端接口(1)
  • thinkPHP框架實現(xiàn)的短信接口驗證碼功能示例
  • Thinkphp框架開發(fā)移動端接口(2)
  • ThinkPHP和UCenter接口沖突的解決方法
  • ThinkPHP框架實現(xiàn)的微信支付接口開發(fā)完整示例

標(biāo)簽:棗莊 江蘇 萍鄉(xiāng) 大理 蚌埠 廣元 衡水 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《thinkPHP5框架接口寫法簡單示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    赞皇县| 海口市| 洮南市| 徐闻县| 禄丰县| 潼南县| 忻城县| 金堂县| 木兰县| 罗山县| 南京市| 澎湖县| 南涧| 博罗县| 东港市| 天津市| 罗江县| 泰兴市| 西城区| 诸城市| 尼木县| 灵石县| 余庆县| 江安县| 吉隆县| 玉龙| 塔城市| 天津市| 鲜城| 昌邑市| 句容市| 玉龙| 绥德县| 新安县| 临安市| 河津市| 鹤庆县| 莲花县| 阿克陶县| 乃东县| 永昌县|