基础通用参数:
token* 用户token,由后端生成并返回给前端,是用户身份的唯一表示,需要获取用户ID的接口,必须传入该参数api_token* 前后端接口验证规则client_id* client_id:1 客户端表示device_token* 设备唯一标示码
参数解释: apiRoute:api路由 data: 请求参数
前端
采用flyio
一个支持所有JavaScript运行环境的基于Promise的、支持请求转发、强大的http请求库。可以让您在多个端上尽可能大限度的实现代码复用。
// 文件路径:~/api/config.jstoken和device_token写在request.body中fly.interceptors.request.use(request => {request.body.token = uni.getStorageSync('token')request.body.device_token = uni.getStorageSync('devicetoken')return request})// post方法封装api_token 由url+当前格式化日期入2020-01-20+前端约定字段baseAppoint 再经过md5加密生成let api_token = null;function post(url, data = {}) {api_token = md5(`${url}${utils.formartDate()}${baseAppoint}`);return request(url,{...data,api_token,client_id: 1,},"POST");}// 配置接口路 例:export default{getreadprofile(data) {return post(apiRoute, data);},getAddressdel(data) {return post(apiRoute, data);},....}
api调用
// 先在main.js中做全局挂载import api from "./api";Vue.prototype.$http = api; //全局挂载api// 具体引用this.$http.getreadprofile(data).then(res=>{console.log(res)// res:返回接口)
nvue中请求
在utils/nvueCommon.js 封装了在nvue中的请求方法 http()http(path = "", params = {}, method = "POST") {const token = uni.getStorageSync("token");return new Promise((resolve, reject) => {uni.request({url: `${this.baseUrl}/${path}?t=${new Date().getTime()}`,method: method,data: {api_token: md5(`${path}${this.formartDate()}${SECRETKEY}`),client_id: 1,token,...params,},success: (res) => {resolve(res);},fail: (err) => {reject(err);},});});},//调用方法://this.http(apiRoute, data).then(res => {console.log(res)// res:返回接口})
后端
接口验证方法:/application/apicloud/model/Common.php
/** 接口验证* @param $needUserToken 是否验证用户token,默认1:需要验证,0不验证*/public function apivalidate($needUserToken = 1){$clientId = input('post.client_id');$apiToken = input('post.api_token');if (!$clientId || !$apiToken) {return array('status' => 400, 'mess' => '接·口请求验证失败');}$module = request()->module();$controller = request()->controller();$action = request()->action(true);$secretstr = $module . '/' . $controller . '/' . $action;$clientSecret = Db::name('secret')->where('id', $clientId)->value('client_secret');if (!$clientSecret) {return array('status' => 400, 'mess' => '接口·请求验证失败');}$apiTokenServer = md5($secretstr . date('Y-m-d', time()) . $clientSecret);if ($apiToken != $apiTokenServer) {return array('status' => 400, 'mess' => '接口请·求验证失败');}//验证个人tokenif ($needUserToken) {$token = input('post.token');if (empty($token)) {return array('status' => 400, 'mess' => '请先登录');}//设备token$deviceToken = input('post.device_token');$rxins = Db::name('rxin')->where('token', $token)->find();if (empty($rxins)) {return array('status' => 400, 'mess' => '接口请求·验证失败');}$userInfo = Db::name('member')->where('id', $rxins['user_id'])->where('checked', 1)->field('id,appinfo_code')->find();if (!$userInfo) {return array('status' => 400, 'mess' => '接口请求验·证失败');}//查看当前用户表中存储的设备clientid值与传递的device_token值是否一致,不一致提示在其他设备登录,请重新登录if ($deviceToken && $deviceToken != $userInfo['appinfo_code']) {return array('status' => 400, 'mess' => '账号已在其他设备上登录,请重新登录');} else {return array('status' => 200, 'mess' => '接口请求验证成功', 'user_id' => $userInfo['id']);}} else {return array('status' => 200, 'mess' => '接口请求验证成功');}}
调用方式:
继承Common控制器,如:class Login extends Common 在接口开始位置加入checkToken验证方法
不需要获取$userId的情况
$tokenRes = $this->checkToken(0); // 传入0,代表不需要获取$user_idif($tokenRes['status'] == 400){ // 400返回错误描述datamsg(LOSE,$tokenRes['mess'],$tokenRes['data']);}
需要获取$userId的情况
$tokenRes = $this->checkToken();if($tokenRes['status'] == 400){ // 400返回错误描述datamsg(LOSE,$tokenRes['mess'],$tokenRes['data']);}else{ // 成功则返回$user_id$user_id = $tokenRes['user_id'];}
