123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\exception\UploadException;
- use app\common\library\Upload;
- use app\common\model\Area;
- use app\common\model\Category;
- use app\common\model\Selection;
- use app\common\model\Version;
- use fast\Random;
- use think\Config;
- use think\Hook;
- class Common extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
-
- public function init()
- {
- if ($version = $this->request->request('version')) {
- $lng = $this->request->request('lng');
- $lat = $this->request->request('lat');
-
- $upload = Config::get('upload');
-
- if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
-
- set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
- $upload = \app\common\model\Config::upload();
-
- Hook::listen("upload_config_init", $upload);
- $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
- }
- $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
- $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
- $content = [
- 'citydata' => Area::getCityFromLngLat($lng, $lat),
- 'versiondata' => Version::check($version),
- 'uploaddata' => $upload,
- 'coverdata' => Config::get("cover"),
- ];
- $this->success('', $content);
- } else {
- $this->error(__('Invalid parameters'));
- }
- }
-
- public function upload()
- {
- Config::set('default_return_type', 'json');
-
- Config::set('upload.cdnurl', '');
- $chunkid = $this->request->post("chunkid");
- if ($chunkid) {
- if (!Config::get('upload.chunking')) {
- $this->error(__('Chunk file disabled'));
- }
- $action = $this->request->post("action");
- $chunkindex = $this->request->post("chunkindex/d");
- $chunkcount = $this->request->post("chunkcount/d");
- $filename = $this->request->post("filename");
- $method = $this->request->method(true);
- if ($action == 'merge') {
- $attachment = null;
-
- try {
- $upload = new Upload();
- $attachment = $upload->merge($chunkid, $chunkcount, $filename);
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
- } elseif ($method == 'clean') {
-
- try {
- $upload = new Upload();
- $upload->clean($chunkid);
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success();
- } else {
-
-
- $file = $this->request->file('file');
- try {
- $upload = new Upload($file);
- $upload->chunk($chunkid, $chunkindex, $chunkcount);
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success();
- }
- } else {
- $attachment = null;
-
- $file = $this->request->file('file');
- try {
- $upload = new Upload($file);
- $attachment = $upload->upload();
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
- }
- }
-
- public function system_configuration(){
- $this->success('请求成功',config('site.'.input('site')));
- }
-
- public function category(){
- $this->success('请求成功',Category::where('type',input('type'))->select());
- }
-
- function getOrderId()
- {
- return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
- }
-
- function getOpenid($code_ = ''){
- $code = $code_;
- $appid = 'wx5e111b790c719cae';
- $secret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
- $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$get_token_url);
- curl_setopt($ch,CURLOPT_HEADER,0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- $res = curl_exec($ch);
- curl_close($ch);
- $json_obj = json_decode($res,true);
- if($json_obj['openid']){
- return $json_obj['openid'];
- }else{
- $this->error('请重新登录');
- }
- $this->error('0',$json_obj);
- }
-
- function getUserinfo($code_ = ''){
- $code = $code_;
- $appid = 'wx5e111b790c719cae';
- $secret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
-
- $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
- $wx_info_json_obj = json_decode(self::curl($get_token_url),true);
-
- $get_userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$wx_info_json_obj['access_token'].'&openid='.$wx_info_json_obj['openid'].'&lang=zh_CN';
- $get_userinfo_json_obj = json_decode(self::curl($get_userinfo_url),true);
- if($get_userinfo_json_obj){
- return $get_userinfo_json_obj;
- }else{
- $this->error('请重新登录');
- }
- $this->error('0',$get_userinfo_json_obj);
- }
-
- function curl($url = ''){
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_HEADER,0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- $res = curl_exec($ch);
- curl_close($ch);
- return $res;
- }
-
- public function selection(){
- $this->success('',Selection::all(['type'=>input('type'),'status'=>1]));
- }
-
- public function getAllArea()
- {
- $field = ['id', 'pid', 'shortname', 'name', 'first'];
- $list = Area::with(['children' => function ($query) use ($field) {
- $query->field($field)->with(['children' => function ($query) use ($field) {
- $query->field($field);
- }]);
- }])
- ->where('pid', 0)->field($field)->select();
- $this->success('获取成功', $list);
- }
-
- public function get_area_city()
- {
- $field = ['id', 'pid', 'shortname', 'name', 'first'];
- $list = Area::with(['children' => function ($query) use ($field) {
- $query->field($field);
- }])
- ->where('pid', 0)->field($field)->select();
- $this->success('获取成功', $list);
- }
-
- public function get_area()
- {
- $pid = $this->request->get('pid', 0);
- $field = ['id', 'pid', 'shortname', 'name'];
- $list = Area::where('pid', $pid)->field($field)->select();
- $this->success('获取成功', $list);
- }
-
- function getRandomString($len, $chars=null)
- {
- if (is_null($chars)) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- }
- mt_srand(10000000*(double)microtime());
- for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
- $str .= $chars[mt_rand(0, $lc)];
- }
- return $str;
- }
-
- public function get_sign(){
-
- $appid = 'wx5e111b790c719cae';
- $secret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
-
- $token_data = file_get_contents(__DIR__.'/wechat_token.txt');
- $time = 7200;
- if (!empty($token_data)) {
- $token_data = json_decode($token_data, true);
- $time = time() - $token_data['time'];
- }
- if ($time > 3600) {
- $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
- $token_res = $this->https_request($token_url);
- $token_res = json_decode($token_res, true);
- $token = $token_res['access_token'];
- $data = array(
- 'time' =>time(),
- 'token' =>$token
- );
- $res = file_put_contents(__DIR__.'wechat_token.txt', json_encode($data));
- } else {
- $token = $token_data['token'];
- }
-
- $ticket_data = file_get_contents(__DIR__.'/wechat_ticket.txt');
- $time1 = 7200;
- if (!empty($ticket_data)) {
- $ticket_data = json_decode($ticket_data, true);
- $time1 = time() - $ticket_data['time'];
- }
- if ($time1 > 3600) {
- $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
- $ticket_res = $this->https_request($ticket_url);
- $ticket_res = json_decode($ticket_res, true);
- $ticket = $ticket_res['ticket'];
- $data = array(
- 'time' =>time(),
- 'ticket' =>$ticket
- );
- $res = file_put_contents(__DIR__.'wechat_ticket.txt', json_encode($data));
- } else {
- $ticket = $ticket_data['ticket'];
- }
-
- $timestamp = time();
- $nonceStr = $this->createNonceStr();
-
- $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
- $this->request->post('url')?$url = $this->request->post('url'):$this->error('参数缺失');
-
- $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}";
- $sha_str = sha1($str);
- $signPackage = array(
- "appId" => $appid,
- "nonceStr" => $nonceStr,
- "timestamp" => $timestamp,
- "signature" => $sha_str,
- );
- $this->success($signPackage);
- }
-
- public function createNonceStr($length = 16) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- $str = "";
- for ($i = 0; $i < $length; $i++) {
- $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
- }
- return $str;
- }
-
- public function https_request($url, $data = null){
-
- $curl = curl_init();
-
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
-
- if ( !empty($data) ) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- public function fasong(){
- echo Date('Y-m-d H:i:s',time());
- }
-
- public function sendWechatMessage($openid,$template_id,$data,$url)
- {
- $data = array(
- "touser" => $openid,
- "template_id" => $template_id,
- "url" => $url,
- "data" => $data
- );
-
- $access_token = $this->access_token();
- if ($access_token == 400){
- return ['code'=>0,'message'=>'获取access_token失败'];
- }
- $urls = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
- $result = $this->http_post_json($urls,json_encode($data));
- if($result[0] == 200){
- return ['code'=>1,'data'=>'推送成功'];
- }
- return ['code'=>0,'message'=>$result[1]['errmsg']];
- }
- public function http_post_json($url, $jsonStr)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- return [$httpCode,$response];
-
-
- }
-
- public function access_token(){
- $appId = 'wx5e111b790c719cae';
- $appSecret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $data = json_decode(curl_exec($ch),true);
- if(curl_errno($ch)){
- var_dump(curl_error($ch));
- }
- curl_close($ch);
- return $data['access_token'];
- }
-
- function get_birthday($idcard) {
- if(empty($idcard)) return null;
- $bir = substr($idcard, 6, 8);
- $year = (int) substr($bir, 0, 4);
- $month = (int) substr($bir, 4, 2);
- $day = (int) substr($bir, 6, 2);
- return $year . '-' . $month . '-' . $day;
- }
- }
|