request->isGet) { $getData = \Yii::$app->request->get(); //var_dump($getData['page']); $res = Yii::$app->services->validate->validate($getData, [ [['page', 'page_size', 'parent_id'], 'safe'], [['page_size'], 'default', 'value' => '15'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $params['where'][] = ['mall_id' => $this->mall_id]; $params['where'][] = ['>', 'status', StatusEnum::DISABLED]; $params['page'] = empty($getData['page']) ? 1 : $getData['page']; $params['select'] = "id,sort,name,parent_id"; $params['order'] = "sort asc"; $params['limit'] = $getData['page_size'] ?? $this->pageSize; $params['where'][] = ['=', 'parent_id', $getData['parent_id'] ?? 0]; $materil_cate_list = MaterialCate::listPage($params); //获取配置 $data = Yii::$app->services->setting->addons->get($this->mall_id, MaterialEnum::ADDONS_NAME, 'basic'); $result_array = explode(',', $data['user_level_limit']); if (in_array($this->user['level'], $result_array) || empty($data['user_level_limit'])) $config['is_audit'] = '1'; else $config['is_audit'] = '0'; $config['is_show_download'] = empty($data['is_show_download']) ? 0 : 1; $config['download_type'] = empty($data['download_type']) ? 0 : 1; $config['is_show_mall_bottom_nav'] = empty($data['is_show_mall_bottom_nav']) ? 0 : 1; //是否显示商城底部导航栏 return $this->success('操作成功', compact('materil_cate_list', 'config')); } } /** * ltm * 2021/12/14 14:39 * 获取素材列表 * @return string */ public function actionGetMaterialList() { if (\Yii::$app->request->isGet) { $getData = \Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($getData, [ [['page','user_name','keyword','user_id','cate_id', 'second_cate_id', 'page_size','material_id'], 'safe'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $params['page'] = empty($getData['page']) ? 1 : $getData['page']; $params['limit'] = empty($getData['page_size']) ? 1 : $getData['page_size']; if ($getData['user_name']) $params['where'][] = ['like', 'describe', $getData['user_name']]; if ($getData['user_id']) $params['where'][] = ['=', 'user_id', $getData['user_id']]; if ($getData['cate_id']) $params['where'][] = ['=', 'cate_id', $getData['cate_id']]; if ($getData['second_cate_id']) $params['where'][] = ['=', 'second_cate_id', $getData['second_cate_id']]; $params['where'][] = ['mall_id' => $this->mall_id]; $params['where'][] = ['>','status',StatusEnum::DISABLED]; $params['with'] = ['cate','goods','user']; if (!empty($this->user_id) && $getData['user_id'] == $this->user_id) { $params['order'] = 'check_status asc,id desc'; } else { $params['where'][] = ['=', 'check_status', StatusEnum::ENABLED]; $params['order'] = 'id desc'; } $config = Yii::$app->services->setting->addons->get($this->mall_id, MaterialEnum::ADDONS_NAME, 'basic'); $materil_list = Material::listPage($params); $material_id = $getData['material_id']??0; $materil_list['share_style'] = 0; if($material_id > 0 && !empty($config['share_style'])){ // 首页 $materil_list['share_style'] = 1; } if($materil_list['list']){ foreach ($materil_list['list'] as $k => $v) { if($v['pic_url']) { $materil_list['list'][$k]['pic_url'] = json_decode($v['pic_url']); } else { $materil_list['list'][$k]['pic_url'] = []; } $materil_list['list'][$k]['user_avatar_url'] = $v['user_avatar_url']?$v['user_avatar_url']:$v['user']['avatar_url']; $materil_list['list'][$k]['user_name'] = $v['user_name']?$v['user_name']:$v['user']['nickname']; if($v['id']==$material_id){ $v['pic_url'] = json_decode($v['pic_url']); $temp_v = $v; unset( $materil_list['list'][$k]); array_unshift($materil_list['list'],$temp_v); } } if($material_id>0&&!isset($temp_v)){ $where = [['id'=>$material_id],['>','status',StatusEnum::DISABLED]]; if (empty($this->user_id) && $getData['user_id'] != $this->user_id) { $where[] = ['=', 'check_status', StatusEnum::ENABLED]; } $material = Material::getOne($where,true,['cate','goods','user']); if(!empty($material)){ if($material['pic_url']) { $material['pic_url'] = json_decode($material['pic_url']); } else { $material['pic_url'] = []; } $material['user_avatar_url'] = $material['user_avatar_url']?$material['user_avatar_url']:$material['user']['avatar_url']; $material['user_name'] = $material['user_name']?$material['user_name']:$material['user']['nickname']; array_unshift($materil_list['list'],$material); } } } return $this->success('操作成功',$materil_list); } } /** * ltm * 2021/12/14 14:39 * 获取素材列表 * @return string */ public function actionGetConfig() { if (\Yii::$app->request->isGet) { $data = Yii::$app->services->setting->addons->get($this->mall_id, MaterialEnum::ADDONS_NAME, 'basic'); $config['user_level_limit'] = $data['user_level_limit']; $config['is_audit'] = $data['is_audit']; $this->success('获取成功', compact('config')); } } /** * ltm * 2021/12/14 14:39 * 提交素材 * @return string */ public function actionEdit() { if (\Yii::$app->request->isPost) { $params = Yii::$app->request->post(); if($params['type'] == 1){ //禁用操作和删除操作和审核操作 $res = Yii::$app->services->validate->validate($params, [ [['id','status'],'safe'] ]); }else{ $res = Yii::$app->services->validate->validate($params, [ [['cate_id','describe','download_type'], 'required'], [['second_cate_id', 'pic_url','id','pic_url','virtual_download_num','goods_id','thumb_url','video_url'],'safe'] ]); } if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['second_cate_id'] = empty($params['second_cate_id']) ? 0 : $params['second_cate_id']; $params = Material::exceptNullVal($params); $config = Yii::$app->services->setting->addons->get($this->mall_id, MaterialEnum::ADDONS_NAME, 'basic'); if($config['is_audit']=='0'){ //开启不需要审核 $params['check_status'] = StatusEnum::ENABLED; }else{ $params['check_status'] = StatusEnum::DISABLED; } $params['status'] = StatusEnum::ENABLED; //判断后台是否开启会员等级限制 if(empty($config['user_level_limit']) && $config['user_level_limit'] === '')return $this->error('暂无发布权限'); $result_array = explode(',',$config['user_level_limit']); if(!in_array($this->user['level'],$result_array)) return $this->error('暂无发布权限'); $params['mall_id'] = $this->mall_id; if($params['id']) $params['updated_at'] = time(); else $params['create_at'] = time();//更新时间 $params['user_id'] = $this->user_id?$this->user_id:0; //验证标题内容是否有敏感词汇 if ($config['is_filter'] == '1') { $common = new SensitiveWords(); $textStatus = $common->getTextStatus($params['describe'],$this->mall_id); if ($textStatus['status'] != 0) { return $this->error('敏感词过滤报错:'.$textStatus['msg']); } }; if($params['pic_url']) $params['pic_url'] = json_encode($params['pic_url']); if (!empty($params['video_url']) && empty($params['thumb_url'])) return $this->error('请上传视频封面'); $res = Material::setData($params); if ($res === false) throw new Exception(MaterialCate::getStaticError()); return $this->success('操作成功'); } } /** * @Author: ltm * @DateTime: 2022/1/18 0018 10:52 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $data * @return string */ public function actionDetail(){ if (\Yii::$app->request->isGet) { $get = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($get,[ [['id'], 'required'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['>=', 'status', StatusEnum::DISABLED]; $where[] = ['=', 'id', $get['id']]; $detail = Material::getOne($where, true,['goods','user']); $detail['pic_url'] = json_decode($detail['pic_url']); return $this->success('操作成功',compact('detail')); } } /** * ltm * 2021/12/20 14:39 * 提交素材 * @return string */ public function actionDel() { if (\Yii::$app->request->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id'],'safe'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['status'] = StatusEnum::DELETE; $params['updated_at'] = time(); $params['user_id'] = $this->user_id; $params['mall_id'] = $this->mall_id; //var_dump($params);die; $find = Material::getOne([['id'=>$params['id']]],true); if($find['user_id'] != $this->user_id) return $this->error('操作失败,你无权限操作'); $res = Material::setData($params); //var_dump($res); if ($res === false) return $this->error('操作失败');//throw new Exception(MaterialCate::getStaticCodeError()['error']); return $this->success('操作成功'); } } /** * ltm * 2021/12/14 14:39 * 提交素材 * @return string */ public function actionDownload() { if (\Yii::$app->request->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['material_id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $params['created_at'] = time(); $params['user_id'] = $this->user_id?$this->user_id:0; $res = MaterialDownload::setData($params); if($res){ $model = Material::findOne([['id'=>$params['material_id']]]); if ($model) { $model->download_num += 1; $model->save(); } } if ($res === false) throw new Exception(MaterialDownload::getStaticError()); return $this->success('操作成功'); } } public function actionDownloadData(){ try { if (!\Yii::$app->request->isPost) { return $this->error('请求错误'); } $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['material_id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $material_model = Material::findOne(['id'=>$params['material_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]); if(empty($material_model)) return $this->error('素材不存在'); $config = Yii::$app->services->setting->addons->get($this->mall_id, MaterialEnum::ADDONS_NAME, 'basic'); $data = [ 'download_slogans' =>'', ]; if(isset($config['download_slogans'])) $data['download_slogans'] = $config['download_slogans']; $sign = $this->mall['mall_sign']; $page = '/plugins/material/index'; // 之前功能设置首页或者素材都是跳转素材。这里添加跳转首页 $share_style = $config['share_style'] ?? 0; if ($share_style == 1) { // 跳转首页 $page = '/pages/index/index'; } // 判断平台是否需要后端生成分享海报 $data['avatar_url'] =$data['nickname'] = ''; if(isset($this->user)){ $data['avatar_url']=$this->user->avatar_url; $data['nickname']=$this->user->nickname; } $save_content = [ 'pid' => $this->user_id, 'avatar_url'=>$data['avatar_url'], 'nickname'=>$data['nickname'], 'invite_mobile'=>$this->user->mobile, 'mall_id' => $this->mall_id, 'source' => User::SOURCE_MATERIAL, 'item_id' => $params['material_id'], 'cate_id' => $material_model['cate_id'], ];// 分享码保存的基本信息 $poster = PosterCode::setData($this->mall_id, $this->user->id,User::SOURCE_MATERIAL, $save_content); if ($poster === false) throw new \Exception(PosterCode::getStaticError()); $data['qrcode_url'] = $page . "?sign={$sign}&scene=" . $poster['code']; if (in_array($this->platform, [User::PLATFORM_WECHAT,User::PLATFORM_MP_WX,User::PLATFORM_H5,User::PLATFORM_APP,User::PLATFORM_MP_ALI])) { // 小程序生成二维码 if ($this->platform == User::PLATFORM_MP_WX) { $applet_url = $this->getAppletQrCode($page, $poster['code']);// 获取小程序二维码 if ($applet_url == false) return $this->error('获取小程序二维码失败'); $data['applet_url'] = $applet_url; } if($this->platform == User::PLATFORM_MP_ALI){ // 获取支付宝小程序二维码 $applet_url = $this->getMpAliAppletQrCode($page, "sign={$sign}&scene=" . $poster['code'],$data['share_title'] ?? ''); if ($applet_url == false) return $this->error('获取支付宝小程序二维码失败'); $data['applet_url'] = $applet_url; } // TODO 其他平台海报由后台生成 } return $this->success('操作成功',$data); }catch (\Exception $e){ \Yii::error('addons\Material\api\modules\v1\controllers->actionDownloadData'.$e->getMessage()); return $this->error('系统错误'); } } /** * 获取小程序二维码 * @Author: lun * @DateTime: 2021/12/27 0027 17:25 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $page * @param $scene * @return mixed|void */ public function getAppletQrCode($page, $scene) { $page = ltrim($page, '/'); $url = ''; /** @var Wechat $wechat */ Yii::$app->params['wechatMiniProgramConfig'] = Yii::$app->services->setting->mall->get($this->mall_id, 'mini_program'); $miniProgram = Yii::$app->wechat->miniProgram; $response = $miniProgram->app_code->getUnlimit($scene, ['width' => 200, 'page' => $page]); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { if (in_array('image/jpeg', $response->getHeader('Content-Type'))) { //返回base64图片 $content = 'data:image/jpg;base64,'. base64_encode($response->getBody()->getContents()); $path = Yii::getAlias('@attachment') . '/download/qrcode'; if (!file_exists($path)) {// 异步生成小程序图片文件夹 Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['path' => $path], PosterCode::class, 'mkdirQrcode'); } $fileName = $this->base64_image_content($content, $path); if ($fileName == false) return false; // 图片上传到云存储 $csv = new CsvTool(); $url = $csv->upload($this->mall_id, 'download/qrcode/' . $fileName); $config = Yii::$app->services->attachment->getStorageConfig($this->mall_id); if ($config['drive'] != Attachment::DRIVE_LOCAL) unlink($path .'/'. $fileName);// 删除临时存储的文件 } else { //返回文字 return $this->error('获取失败:' . $response->getBody()->getContents()); } }else{ return $this->error($response['errmsg']); } return $url; } public function getMpAliAppletQrCode($page, $params,$desc = '') { $page = ltrim($page, '/'); $form = new AlipayMiniForm($this->mall_id); $res = $form->qrcode($page,$params,$desc); if(!$res){ return $this->error($form->getError()); } return $res; } function base64_image_content($base64_image_content,$path, $fileName = ''){ try { //匹配出图片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2]; if(!file_exists($path)){ //检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir($path, 0777, true); } $fileName = !empty($fileName) ? $fileName : md5(microtime(true)); if (file_put_contents($path . '/' . $fileName. ".{$type}", base64_decode(str_replace($result[1],'', $base64_image_content)))){ return $fileName . ".{$type}"; }else{ return false; } }else{ return false; } } catch (\Exception $e) { // echo $e->getMessage(); return false; } } }