result($msg, $data, $is_login,$is_disable, $code, $type, $header); } /** * 操作失败返回的数据 * @param string $msg 提示信息 * @param mixed $data 要返回的数据 * @param int $code 错误码,默认为0 * @param string $type 输出类型 * @param array $header 发送的 Header 信息 */ protected function error($msg = '', $data = null, $is_login = 1, $is_disable = 0, $code = 0, $type = null, array $header = []) { $this->result($msg, $data, $is_login, $is_disable, $code, $type, $header); } /** * 返回封装后的 API 数据到客户端 * @access protected * @param mixed $msg 提示信息 * @param mixed $data 要返回的数据 * @param int $code 错误码,默认为0 * @param string $type 输出类型,支持json/xml/jsonp * @param array $header 发送的 Header 信息 * @return void * @throws HttpResponseException */ protected function result($msg, $data = null, $is_login,$is_disable, $code = 0, $type = null, array $header = []) { $result = [ 'code' => $code, 'is_login' => $is_login, 'is_disable' => $is_disable, 'msg' => $msg, 'time' => Request::instance()->server('REQUEST_TIME'), 'data' => $data, ]; // 如果未设置类型则自动判断 $type = $type ? $type : 'json'; if (isset($header['statuscode'])) { $code = $header['statuscode']; unset($header['statuscode']); } else { //未设置状态码,根据code值判断 $code = $code >= 1000 || $code < 200 ? 200 : $code; } $response = Response::create($result, $type, $code)->header($header); throw new HttpResponseException($response); } /** * 判断登录 * @return bool */ protected function check_login(){ $user = app()->session->get('us'); if (!$user){ $this->error('请先登录','',0); } $userinfo = User::where('id',$user['id'])->find(); if ($userinfo['is_del']!=1){ $this->error('请先登录','',0); } if ($userinfo['status']!=1){ // app()->session->clear(); // app()->session->destroy(); $data['disable_reason'] = $userinfo['disable_reason']; $data['disable_time'] = $userinfo['disable_time']; $this->error('账号被禁用',$data,0,1); } return true; } /** * 微信文字和图片违规检测 */ public static function wx_check($param,$type){ $appid = Config::get_values('wechat_appid'); $secret = Config::get_values('wechat_appsecret'); $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; $result = Common::curlRequest($url); if ($type==1){ $url2 = "https://api.weixin.qq.com/wxa/img_sec_check?access_token={$result['access_token']}"; $data = ['media'=>$param]; }elseif ($type==2){ $url2 = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token={$result['access_token']}"; $data = ['content'=>$param]; } $headers = ['Content-Type:application/json']; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 关键点 curl_setopt($ch, CURLOPT_URL, $url2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE)); // 【* 关键点】 $res = curl_exec($ch); curl_close($ch); $res = json_decode($res,true); return $res; } }