您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關thinkPHP5框架接口寫法,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
控制器
/** * 添加收貨地址 */ 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
<?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
<?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; } }
以上就是thinkPHP5框架接口寫法,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。