123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace app\controller\api;
- use app\service\ConfServiceFacade;
- use laytp\library\Random;
- use laytp\library\UploadDomain;
- use plugin\ali_oss\service\Oss;
- use plugin\qiniu_kodo\service\Kodo;
- use laytp\library\Tree;
- use laytp\controller\Api;
- use plugin\ali_sms\service\AliSmsServiceFacade;
- use app\service\api\MiniappServiceFacade;
- use think\facade\Env;
- use think\facade\Filesystem;
- /**
- * 公用接口
- * @ApiWeigh (100)
- */
- class Common extends Api
- {
- public $noNeedLogin = ['template','upload','sendMobileCode','checkMobileCode','qrcodePath'];
- public $noNeedCheckSign = [];
- public function template()
- {
- global $_GPC;
- $template = [];
- $where = ['status'=>1,'uniacid'=>$_GPC['uniacid']];
- $order = ['sort' => 'DESC','id'=>'DESC'];
- $template['banner'] = \app\model\Banner::where($where)->order($order)->with(['img_file'])->select();
- $template['hot'] = \app\model\Hot::orderRaw("rand() , id desc")->where($where)->limit(3)->select();
- $template['mode'] = \app\model\Mode::where($where)->order($order)->with('img_file')->select();
- // $sourceData =\app\model\Template::where($where)->order($order);
- // $menuTreeObj = Tree::instance();
- // $menuTreeObj->init($sourceData->select()->toArray());
- // $data = $menuTreeObj->getRootTrees();
- // $template['template_tree'] = $data;
- $template['template'] = \app\model\Template::order($order)->where([['uniacid','=',$_GPC['uniacid']],['status','=',1],['pid','<>',0]])->order($order)->limit(4)->select();
- return $this->success('数据获取成功', $template);
- }
- /*@formatter:off*/
- /**
- * @ApiTitle (文件上传)
- * @ApiSummary (文件上传,兼容阿里云OSS、七牛云KODO和本地上传,自行在接口中传递参数选择上传方式,阿里云OSS和七牛云KODO需要后端安装相应插件)
- * @ApiMethod (POST)
- * @ApiRoute (/api.common/upload)
- * @ApiHeaders (name="token", type="string", required="true", description="用户登录后得到的Token")
- * @ApiParams (name="file", type="file", required="true", description="文件")
- * @ApiParams (name="upload_type", type="string", required="false", description="上传方式,允许为空,local=本地上传,ali-oss=阿里云OSS上传,qiniu-kodo=七牛云KODO上传,默认为local", sample="avatar")
- * @ApiParams (name="upload_dir", type="string", required="false", description="上传目录,允许为空", sample="avatar")
- * @ApiReturnParams (name="code", type="integer", description="错误码.0=没有错误,表示操作成功;1=常规错误码,客户端仅需提示msg;其他错误码与具体业务相关,其他错误码举例:10401。前端需要跳转至登录界面。")
- * @ApiReturnParams (name="msg", type="string", description="返回描述")
- * @ApiReturnParams (name="time", type="integer", description="请求时间,Unix时间戳,单位秒")
- * @ApiReturnParams (name="data", type="null", description="null")
- * @ApiReturn
- ({
- 'code':0,
- 'msg':'上传成功',
- 'time':'15632654875',
- 'data':null
- })
- */
- /*@formatter:on*/
- public function upload()
- {
- global $_GPC;
- try {
- $uploadType = $this->request->param('upload_type', 'local');
- if (!in_array($uploadType, ['local', 'ali-oss', 'qiniu-kodo'])) {
- return $this->error($uploadType . '上传方式未定义');
- }
- $file = $this->request->file('file'); // 获取上传的文件
- if (!$file) {
- return $this->error('上传失败,请选择需要上传的文件');
- }
- $fileExt = strtolower($file->getOriginalExtension());
- $uploadDomain = new UploadDomain();
- if (!$uploadDomain->check($file->getOriginalName(), $file->getSize(), $fileExt,true)) {
- return $this->error($uploadDomain->getError());
- }
- $saveName = date("Ymd") . "/" . md5(uniqid(mt_rand())) . ".{$fileExt}";
- /**
- * 不能以斜杆开头
- * - 因为OSS存储时,不允许以/开头
- */
- $uploadDir = $this->request->param('dir');
- $object = $uploadDir ? $uploadDir . '/' . $saveName : $saveName;//设置了上传目录的上传文件名
- $filePath = $object; //保存到lt_files中的path
- //如果上传的是图片,验证图片的宽和高
- $accept = $this->request->param('accept');
- if ($accept == "image") {
- $width = $this->request->param('width');
- $height = $this->request->param('height');
- if ($width || $height) {
- $imageInfo = getimagesize($file->getFileInfo());
- if (($width && $imageInfo[0] > $width) || ($height && $imageInfo[1] > $height)) {
- return $this->error('上传失败,图片尺寸要求宽:' . $width . 'px,高:' . $height . 'px,实际上传文件[ ' . $file->getOriginalName() . ' ]的尺寸为宽' . $imageInfo[0] . 'px,高:' . $imageInfo[1] . 'px');
- }
- }
- }
- $inputValue = "";
- //上传至七牛云
- if ($uploadType == 'qiniu-kodo') {
- if(ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1){
- return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启');
- }
- $kodoConf = [
- 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKeyID'),
- 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'),
- 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
- 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
- ];
- $kodo = Kodo::instance();
- $kodoRes = $kodo->upload($file->getPathname(), $object, $kodoConf);
- if ($kodoRes) {
- $inputValue = $kodoRes;
- } else {
- return $this->error($kodo->getError());
- }
- }
- //上传至阿里云
- if ($uploadType == 'ali-oss') {
- if(ConfServiceFacade::get('plugin.ali_oss.switch') != 1){
- return $this->error('未开启阿里云OSS存储,请到阿里云OSS配置中开启');
- }
- $ossConf = [
- 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss.accessKeyID'),
- 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss.accessKeySecret'),
- 'bucket' => ConfServiceFacade::get('plugin.ali_oss.bucket'),
- 'endpoint' => ConfServiceFacade::get('plugin.ali_oss.endpoint'),
- 'domain' => ConfServiceFacade::get('plugin.ali_oss.domain'),
- ];
- $oss = Oss::instance();
- $ossUploadRes = $oss->upload($file->getPathname(), $object, $ossConf);
- if ($ossUploadRes) {
- $inputValue = $ossUploadRes;
- } else {
- return $this->error($oss->getError());
- }
- }
- //本地上传
- if ($uploadType == 'local') {
- $uploadDir = ltrim('/', $uploadDir);
- $saveName = Filesystem::putFileAs('/' . $uploadDir, $file, '/' . $object);
- $filePath = $saveName;
- $staticDomain = Env::get('domain.static');
- if ($staticDomain) {
- $inputValue = $staticDomain . '/storage/' . $saveName;
- } else {
- $inputValue = request()->domain() . STATIC_PATH . '/storage/' . $saveName;
- }
- }
- //将inputValue存入lt_files表中
- $filesModel = new \app\model\Files();
- $fileId = $filesModel->insertGetId([
- 'category_id' => (int)$this->request->param('file_category_id', 0),
- 'name' => $file->getOriginalName(),
- 'file_type' => $this->request->param('accept'),
- 'path' => $filePath,
- 'upload_type' => $uploadType,
- 'size' => $file->getSize(),
- 'ext' => $file->getExtension(),
- 'create_admin_user_id' => 0,
- 'update_admin_user_id' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s'),
- 'uniacid' =>$_GPC['uniacid']
- ]);
- return $this->success('上传成功', [
- 'id' => $fileId,
- 'path' => $inputValue,
- 'name' => $file->getOriginalName(),
- ]);
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- /*@formatter:off*/
- /**
- * @ApiTitle (发送手机验证码)
- * @ApiSummary (发送手机验证码)
- * @ApiMethod (POST)
- * @ApiRoute (/api.common/sendMobileCode)
- * @ApiHeaders (name="token", type="string", required="true", description="用户登录后得到的Token")
- * @ApiParams (name="mobile", type="string", required="true", description="手机号码")
- * @ApiParams (name="event", type="string", required="true", sample="reg_login",description="事件名称,reg_login=使用手机号+验证码的方式进行注册或登录")
- * @ApiReturnParams (name="code", type="integer", required="true", sample="0")
- * @ApiReturnParams (name="msg", type="string", required="true", sample="返回成功")
- * @ApiReturnParams (name="time", type="integer", description="请求时间,Unix时间戳,单位秒")
- * @ApiReturnParams (name="data", type="null", description="只会返回null")
- * @ApiReturn
- ({
- "code": 1,
- "msg": "发送失败,触发分钟级流控Permits:1",
- "time": 1584667483,
- "data": null
- })
- */
- /*@formatter:on*/
- public function sendMobileCode(){
- // $aliSmsStatus = ConfServiceFacade::groupGet('ali_sms.conf');
- // if(!$aliSmsStatus){
- // return $this->error('请先到插件市场安装阿里云手机短信插件,并进行相关配置');
- // }
- $post['mobile'] = $this->request->post('mobile');
- $post['event'] = $this->request->post('event');
- $validate = new \plugin\ali_sms\validate\Send();
- if(!$validate->check($post)) return $this->error('发送失败,'.$validate->getError());
- if(AliSmsServiceFacade::send($post['mobile'],$post['event'],['code'=>Random::numeric()])){
- return $this->success('发送成功');
- }else{
- return $this->error('发送失败,'.AliSmsServiceFacade::getError());
- }
- }
- public function checkMobileCode(){
- $posts['mobile'] = $this->request->post('mobile');
- $posts['event'] = $this->request->post('event');
- $posts['code'] = $this->request->post('code');
- $check = AliSmsServiceFacade::checkCode($posts['mobile'],$posts['event'],$posts['code']);
- if($check){
- return $this->success('验证成功');
- }else{
- return $this->error('验证失败,'.AliSmsServiceFacade::getError());
- }
- }
- public function qrcodePath()
- {
- global $_GPC;
- $platform = $this->request->header('platform');
- // print_r($platform);
- $spm = $this->request->param('spm',0);
- if($platform == 'wxOfficialAccount' || $platform == 'H5'){
- $link = request()->domain().SURL . 'h5/?uniacid=' . $_GPC['uniacid'] .'&spm='.$spm;
- }elseif($platform == 'wxMiniProgram'){
- $conf = ConfServiceFacade::groupGet('system.miniapp', true);
- if(!$conf['appid'] || !$conf['appsecret']){
- return $this->error('操作失败');
- }
- $app = MiniappServiceFacade::option();
- $response = $app->app_code->getUnlimit('?uniacid='.$_GPC['uniacid'].'&spm='.$spm, [
- 'page' =>'pages/index/index',
- 'width' => 600
- ]);
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- $time = time();
- $filename = $response->saveAs(IA_ROOT_WK.'/public/static/storage/qrcode/', $time.'appcode.png');
- }
- $link = request()->domain() . STATIC_PATH . '/storage/qrcode/' . $time.'appcode.png';
- }
- return $this->success('获取成功',$link);
- }
- }
|