| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 | <?phpnamespace app\api\controller;use app\common\controller\Api;use app\common\Enum\ChannelEnum;use app\common\exception\UploadException;use app\common\library\Upload;use app\common\model\Area;use app\common\model\Version;use fast\Random;use think\captcha\Captcha;use think\Config;use think\Hook;use app\common\library\Theme;use app\common\model\Navigation;use app\common\model\Block;use app\common\model\Category;use app\common\model\SearchLog;use app\common\Enum\StatusEnum;use app\common\Enum\FeedbackEnum;use app\common\Enum\UserEnum;use app\common\Service\ShopConfigService;/** * 公共接口 */class Common extends Base{    protected $noNeedLogin = ['init', 'area', 'getBannerList','getDataByIndex'];    protected $noNeedRight = '*';       protected function convertPlatformName($platform)    {        // 将驼峰命名转换为下划线格式        return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));    }    /**     * 初始化     */    public function init()    {        //配置信息        $upload = Config::get('upload');        $platform  = $this->request->header('platform');        //  转换        //如果非服务端中转模式需要修改为中转        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'] = url('/api/common/upload', '', false, true);        $arrConfig      = ShopConfigService::getConfigs('shop.basic',false);               $arrGoodsConfig = ShopConfigService::getConfigs('shop.goods',false);        $orderTimeout = ShopConfigService::getConfigs('shop.order.order_timeout',false);        $data = [            'upload'         => $upload,        ];        $data['order_timeout'] = $orderTimeout;        $data['sitename'] =  $arrConfig['site_name']?? "";           // 手机号         $data['phone'] = $arrConfig['phone']??"";        // 工作时间        $data['workinghours'] = $arrConfig['working_hours']??"";        // 客服邮箱        $data['email'] =$arrConfig['email']??"";        // 公司名称        $data['company'] = $arrConfig['company']??"";        // 站点logo        $data['sitelogo'] = $arrConfig['site_logo']?  cdnurl($arrConfig['site_logo'], true) : '';        // 公安备案        $data['gwabeian'] = $arrConfig['public_network_filing']??"";        // icp备案        $data['icpbeian'] = $arrConfig['icp']??"";            // 默认商品图片         $defaultGoodsImg   =  $arrGoodsConfig['default_goods_img']??"";        $data['default_goods_img'] = !empty($defaultGoodsImg ) ? cdnurl( $defaultGoodsImg, true) : '';        // 默认分类图片        $defaultCategoryImg   =   $arrGoodsConfig['default_category_img']??"";        $data['default_category_img'] = !empty($defaultCategoryImg)? cdnurl($defaultCategoryImg, true) :'';               $platformKey = $this->convertPlatformName($platform);        $arrPlatformConfigs = ShopConfigService::getConfigs('shop.platform.' . $platformKey, false);        // app 处理        if ($platform == ChannelEnum::CHANNEL_IOS_APP){            // 微信appid            $data['wx_universal_link'] =$arrPlatformConfigs['wx_universal_link'] ?? "";            $data['app_wx_appid'] =  $arrPlatformConfigs['app_id'] ?? "";            $data['wechat_mini_program_original'] =  $arrPlatformConfigs['wechat_mini_program_original'] ?? "";            $data['wechat_mini_program_url'] =  $arrPlatformConfigs['wechat_mini_program_url'] ?? "";            $data['wechat_mini_program_type'] =  $arrPlatformConfigs['wechat_mini_program_type'] ?? "";        }        if ($platform == ChannelEnum::CHANNEL_ANDROID_APP){            $data['wechat_mini_program_original'] =  $arrPlatformConfigs['wechat_mini_program_original'] ?? "";            $data['wechat_mini_program_url'] =  $arrPlatformConfigs['wechat_mini_program_url'] ?? "";            $data['app_wx_appid'] =  $arrPlatformConfigs['app_id'] ?? "";            $data['wechat_mini_program_type'] =  $arrPlatformConfigs['wechat_mini_program_type'] ?? "";        }        // 微信小程序处理        if ($platform == ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM){            // 微信appid            $data['wx_appid'] =  $arrPlatformConfigs['app_id'] ?? "";            $data['app_url'] =  !empty($arrPlatformConfigs['app_url']) ? cdnurl($arrPlatformConfigs['app_url'], true) : '';        }               // 反馈类型        $data['feedback_type'] = FeedbackEnum::getFeedbackTypeMap();        // 性别        $data['gender'] = UserEnum::getGenderMap();            $this->success('', $data);    }    /**     * 读取省市区数据,联动列表     */    public function area()    {        $province = $this->request->param('province', '');        $city = $this->request->param('city', '');        $where = ['pid' => 0, 'level' => 1];        $provincelist = null;        if ($province !== '') {            $where['pid'] = $province;            $where['level'] = 2;        }        if ($city !== '') {            $where['pid'] = $city;            $where['level'] = 3;        }        $provincelist = Area::where($where)->field('id as value,name as label')->where('status', 'normal')->select();        $this->success('', $provincelist);    }    public function getBannerList()    {        $type = $this->request->param('type');        // 验证参数        if (!$type) {            $this->error('参数错误');        }        $list = Block::getBlockList(['type' => $type]);        $this->success('', $list);    }    public function getDataByIndex()    {        // 固定         $type = ['首页BANNER', '首页LOTTERY','首页ARTICE','首页MEASURE'];        $field = 'id,type,name,image,url,page_name,status,begintime,endtime,title';        $list = Block::getBlockList(['type' => $type, 'field' => $field]);        // 根据type  区分新的数组         $newList = [            'banner' => [],            'lottery' => [],            'article' => [],            'measure' => [],        ];        foreach ($list as $item) {            if ($item['type'] == '首页BANNER') {                $bannerList[] = $item;            }            if ($item['type'] == '首页LOTTERY') {                $lotteryList[] = $item;            }            if ($item['type'] == '首页ARTICE') {                $articleList[] = $item;            }            if ($item['type'] == '首页MEASURE') {                $measureList[] = $item;            }        }        $newList['banner'] = $bannerList;        $newList['lottery'] = $lotteryList;        $newList['article'] = $articleList;        $newList['measure'] = $measureList;        // todo  查询活动商品        //  首页推荐的分类        $indexCategoryList = Category::where('status', StatusEnum::ENABLED)        ->field('id,name,image')        ->where('is_recommend',StatusEnum::YES)        ->order('weigh desc')        ->limit(8)        ->select();        //热门分类        $indexHotCategoryList = Category::where('status', StatusEnum::ENABLED)        ->field('id,name,image')        ->where('is_hot',StatusEnum::YES)        ->order('weigh desc')        ->limit(4)        ->select();        //查询 活动商品        $newList['indexCategoryList'] = $indexCategoryList;        $newList['indexHotCategoryList'] = $indexHotCategoryList;        $this->success('', $newList);    }    /**     * 上传文件     * @ApiMethod (POST)     * @ApiParams (name="file", type="file", required=true, description="文件流")     */    public function upload()    {        Config::set('default_return_type', 'json');        //必须设定cdnurl为空,否则cdnurl函数计算错误        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());            } catch (\Exception $e) {                $this->error($e->getMessage());            }            $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);        }    }    /**     * 验证码     * @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")     * @return \think\Response     */    public function captcha($id = "")    {        \think\Config::set([            'captcha' => array_merge(config('captcha'), [                'fontSize' => 44,                'imageH'   => 150,                'imageW'   => 350,            ])        ]);        $captcha = new Captcha((array)Config::get('captcha'));        return $captcha->entry($id);    }}
 |