request; if ($retuest->isGet) { $params = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($params, [ [['source'], 'required'], [['goods_id','source','open_group_id','bargain_order_id','active_id','activity_id','lucky_group_activity_id'], 'integer'], [['extra'],'safe'] ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $mallId = $this->mall_id; $sign = $this->mall['mall_sign']; // 获取个人信息 $data['user_id'] = $this->user->id ?? ''; $data['nickname'] = $this->user->nickname ?? ''; $data['avatar_url'] = $this->user->avatar_url ?? ''; $save_content = ['pid' => $data['user_id'],'invite_mobile'=>$this->user->mobile, 'mall_id' => $mallId, 'source' => $params['source']];// 分享码保存的基本信息 // 根据来源获取不同分享内容 $params['data'] = $data; if($params['source']==User::SOURCE_SHARE_GOODS){ $goods = Goods::getOne([['id' => $params['goods_id'], 'mall_id' => $mallId]],true,[],false,'id as item_id,goods_name as share_title,price,original_price,cover_pic as share_img'); $data = array_merge($data, $goods); $save_content['item_id'] = $goods['item_id']; $save_content['activity_id'] = $params['activity_id']??0; $page = DiyEnum::GOODS_DETAIL_PATH; }else{ //['page' => $page, 'data' => $data] $params['mall_id'] = $this->mall_id; $params['level'] = $this->user['level']; $return = GlobalInvoke::exec(GlobalInvoke::INVOKE_GET_POSTER,$this->mall_id,$params); if(!empty($return['page'])) $page = $return['page']; if(!empty($return['data'])) $data = $return['data']; if(!empty($return['save_content'])) $save_content = array_merge($save_content,$return['save_content']); } if(!empty($params['extra'])){ $extra = json_decode($params['extra'],true); if(is_array($extra)){ $save_content = array_merge($save_content,$extra); } } // 查询获取数据 $poster = PosterCode::setData($this->mall_id, $this->user->id, $params['source'], $save_content); if ($poster == false) return $this->error(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 其他平台海报由后台生成 } //浏览 $event = new ShareEvent($this->mall_id); \Yii::$app->services->events->dispatch($event, ['mall_id'=>$this->mall_id,'user_id'=>$this->user_id], $event->listeners); $this->success('获取成功1', $data); } } 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; } } /** * 海报分享 * @Author: lun * @DateTime: 2021/12/24 0024 15:19 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void */ public function actionShareList() { $retuest = Yii::$app->request; if ($retuest->isPost) { $mall_id = $this->mall_id; $sign = $this->mall['mall_sign']; $where[] = ['=', 'status', StatusEnum::ENABLED]; $where[] = ['=', 'mall_id', $mall_id]; $select = "id,name,key_word,poster_content"; $params = ['where' => $where, 'order' => 'sort desc, id desc', 'select' => $select]; $list = Poster::lists($params); if (empty($list)) return $this->success('获取成功', $list); $setting = Yii::$app->services->setting->mall->get($this->mall_id, 'user'); $is_scan_code_to_app = 0; if($is_scan_code_to_app == User::PLATFORM_H5){ $is_scan_code_to_app = $setting['is_scan_code_to_app'] ?? $is_scan_code_to_app; } elseif ($is_scan_code_to_app == User::PLATFORM_APP){ $is_scan_code_to_app = $setting['is_scan_code_to_h5'] ?? $is_scan_code_to_app; } foreach ($list as $key => $item) { $data = [ 'user_id' => $this->user->id, 'pid' => $this->user->id, 'nickname' => $this->user->nickname, // 'avatar_url' => $this->user->avatar_url, 'item_id' => $item['id'], ]; // 生成海报对应码 $select_code = "id,mall_id,user_id,source,code,content,applet_code"; if ($key == 0) $select_code.= ",applet_code"; $poster = PosterCode::setData($this->mall_id, $this->user->id, User::SOURCE_SHARE_POSTER, $data, $select_code); if ($poster == false) return $this->error(PosterCode::getStaticError()); $list[$key]['poster_code_id'] = $poster['id']; // 检查商城是否安装了门店管家插件,如果已安装则使用门店管家模板路径 $storeStewardSystem = Yii::$app->services->setting->addons->get($this->mall_id, 'StoreSteward', 'basic'); if (MallAddons::isInstall($mall_id, 'StoreSteward') && !empty($storeStewardSystem['index_type'])) { $prefix_path = DiyEnum::STORE_GUARDIAN_TEMPLATE; } else { $prefix_path = $is_scan_code_to_app == 0 ? DiyEnum::HOME_PAGE_PATH : DiyEnum::APP_DOWNLOAD_GUIDE; } $list[$key]['qrcode_url'] = $prefix_path . "?sign={$sign}&scene=" . $poster['code']; // 第一张图片请求获取小程序二维码 if ($this->platform == User::PLATFORM_MP_WX && $key == 0 && empty($poster['applet_code'])) { $applet_url = $this->getAppletQrCode(DiyEnum::HOME_PAGE_PATH , $poster['code']);// 获取小程序二维码base64数据 if ($applet_url == false) return $this->error('获取小程序二维码失败'); $list[$key]['applet_code'] = $applet_url; PosterCode::setData($this->mall_id, $this->user->id, User::SOURCE_SHARE_POSTER, array_merge($data,['id' => $poster['id'], 'applet_code' => $applet_url]), $select_code); } if ($key == 0) { // 统计浏览次数 Poster::updateAllCounters(['look_num' => 1],"id=" . $item['id']); } $list[$key]['is_scan_code_to_app'] = $is_scan_code_to_app; } //浏览 $event = new ShareEvent($this->mall_id); $data['mall_id'] =$this->mall_id; \Yii::$app->services->events->dispatch($event, ['mall_id'=>$this->mall_id,'user_id'=>$this->user_id], $event->listeners); $this->success('获取成功', $list); } } /** * 根据分享码获取分享内容 * @Author: lun * @DateTime: 2022/1/14 0014 17:28 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void */ public function actionGetShareContent() { $retuest = Yii::$app->request; if ($retuest->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['scene'], 'string'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $cache = Yii::$app->cache; $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::POSTER_CODE , "M{$this->mall_id}" . $params['scene'],true); $content = $cache->get($cache_key); $data = empty($content) ? [] : Json::decode($content, true); // 缓存无法读取小程序码内容,则直接读取数据库 if (empty($data)) $data = PosterCode::getShareByScene($this->mall_id, $params['scene']); if(isset($data['user_id'])) $data['share_user_id'] = $data['pid'] = $data['user_id']; if($data && $data['pid'] > 0 && $this->user_id && $data['pid'] != $this->user_id){ //查询用户是否有上级 $where[] = ['mall_id' => $this->mall_id]; $where[] = ['id' => $this->user_id]; $user = User::getUser($where); if($user && $user->temp_parent_id >= 0 && empty($user->parent_id) && $user->temp_parent_id != $data['pid']){ $user->temp_parent_id = $data['pid']; $user->save(); } } if (!empty($data['store_id'])) $data['marketing_type'] = 'Store'; $this->success('获取成功', $data, ApiCode::CODE_SUCCESS, JSON_BIGINT_AS_STRING); } } /** * 获取海报码详情 * @Author: lun * @DateTime: 2021/12/30 0030 14:07 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void */ public function actionPosterCodeDetail() { $retuest = Yii::$app->request; if ($retuest->isPost) { $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['scene'], 'string'], [['poster_code_id'], 'integer'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $mall_id = $this->mall_id; $is_save = false; !empty($params['poster_code_id']) && $where['id'] = $params['poster_code_id']; $where['mall_id'] = $mall_id; !empty($params['scene']) && $where['code'] = $params['scene']; $where['status'] = StatusEnum::ENABLED; $select = "id,applet_code,source,item_id,source,content,code,store_id"; $detail = PosterCode::find()->select($select)->where($where)->one(); if (empty($detail)) return $this->error('暂无内容'); // 检查客户是否拥有推广资格 $allow_inviter = 1;// 是否允许推广【0=不允许,1=允许】 $inviter_msg = [];// 邀请条件 if (!empty($this->user) && empty($this->user['is_inviter'])) { list($allow_inviter, $inviter_msg) = User::getInviterMsg($this->mall_id, $allow_inviter); } if (!empty($params['poster_code_id']) && empty($detail['applet_code']) && $allow_inviter == 1) { // 小程序二维码为空则去获取小程序二维码 if ($this->platform == User::PLATFORM_MP_WX) { // 检查商城是否安装了门店管家插件,如果已安装则使用门店管家模板路径 $storeStewardSystem = Yii::$app->services->setting->addons->get($this->mall_id, 'StoreSteward', 'basic'); if (MallAddons::isInstall($mall_id, 'StoreSteward') && !empty($storeStewardSystem['index_type'])) { $prefix_path = DiyEnum::STORE_GUARDIAN_TEMPLATE; } else { $prefix_path = DiyEnum::HOME_PAGE_PATH; } $applet_url = $this->getAppletQrCode($prefix_path, $detail['code']);// 获取小程序二维码base64数据 if ($applet_url == false) $this->error('获取小程序二维码失败'); // 获取到base64将其临时存服务器 $detail->applet_code = $applet_url; $is_save = true; } if($this->platform == User::PLATFORM_MP_ALI){ // 获取支付宝小程序二维码 $sign = $this->mall['mall_sign']; $applet_url = $this->getMpAliAppletQrCode(DiyEnum::HOME_PAGE_PATH, "sign={$sign}&scene=" . $detail['code'],'海报分享'); if ($applet_url == false) return $this->error('获取支付宝小程序二维码失败'); $detail->applet_code = $applet_url; $is_save = true; } } // 统计浏览次数 if (!empty($params['poster_code_id'])) { Poster::updateAllCounters(['look_num' => 1],"id=" . $detail->item_id); } // 统计海报扫码次数 if (!empty($params['scene']) && $detail['source'] == User::SOURCE_SHARE_POSTER) { Poster::updateAllCounters(['scan_code_num' => 1],"id=" . $detail->item_id); } // 保存数据 if ($is_save && !$detail->save()) return $this->error("获取错误:" . PosterCode::getStaticError()); // 解析json $content = Json::decode($detail['content'], true); if (!empty($content['user_id'])) { $content = array_merge(['share_user_id' => $content['user_id']], $content); $user = User::findOne(['id'=>$content['user_id']]); $content['invite_mobile'] = $user['mobile']??''; $detail['content'] = Json::encode($content); } $detail = $detail->toArray(); $detail['allow_inviter'] = $allow_inviter; $detail['inviter_msg'] = $inviter_msg; $this->success('获取成功', $detail); } } /** * 获取小程序二维码 * @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 { //返回文字 // $res = Json::decode($response->getBody()->getContents(), true); return $this->error('获取失败:' . $response->getBody()->getContents()); // return false; } }else{ return $this->error($response['errmsg']); // return false; } return $url; } /** * todo 画海报-暂写在控制器,后面需要移除 * @Author: lun * @DateTime: 2021/11/15 0015 15:12 * @Copyright: copyright (c) 2021 广东七件事集团 * @throws \Exception */ public function actionDraw() { $editor = Grafika::createEditor(); $font = Yii::$app->basePath.'/web/resources/img/poster/st-heiti-light.ttc'; $data = []; $canvas = ''; $list = self::getDefaultParams(); foreach ($list as $key => $item) { switch ($item['type']) { case 'background':// 背景 $canvas = Grafika::createBlankImage($item['w'],$item['h']); $editor->fill($canvas, new Color($item['color'])); break; case 'image':// 图片 $editor->open($data[$key], $item['content']); $editor->resizeFill($data[$key], $item['w'], $item['h']); $editor->blend($canvas, $data[$key], 'normal', 1, 'top-left', $item['x'], $item['y']); break; case 'text':// 文字 $editor->text($canvas ,$item['content'], $item['size'], $item['x'], $item['y'], new Color($item['color']),$font); break; case 'scribe':// 划线 $editor->draw($canvas, Grafika::createDrawingObject('Line',[$item['s_x'],$item['s_y']],[$item['e_x'] + 20 * mb_strlen($item['content'],'UTF8'),$item['e_y']],1,new Color($item['color']))); break; } } $editor->save($canvas,"edited.jpg"); } private static function getDefaultParams() { $data = [ [ 'type' => 'background', 'content' => '', 'color' => '#ffffff', 'w' => '590', 'h' => '942', ], [ 'type' => 'image', 'content' => 'resources/img/poster/goods.png', 'w' => '510', 'h' => '510', 'x' => '40', 'y' => '40', ], [ 'type' => 'text', 'content' => '摇滚便携式榨汁机', 'size' => '24', 'color' => '#000000', 'x' => '40', 'y' => '582', ], [ 'type' => 'text', 'content' => '¥', 'size' => '24', 'color' => '#DB0505', 'x' => '40', 'y' => '637', ], [ 'type' => 'text', 'content' => '128.06', 'size' => '30', 'color' => '#DB0505', 'x' => '65', 'y' => '630', ], [ 'type' => 'text', 'content' => '¥169', 'size' => '18', 'color' => '#9C9C9C', 'x' => '40', 'y' => '695', ], [ 'type' => 'scribe', 'content' => '¥169', 'size' => '22', 'color' => '#9C9C9C', 's_x' => 40, 's_y' => 707, 'e_x' => 40, 'e_y' => 707, ], [ 'type' => 'image', 'content' => 'resources/img/poster/avatar.jpg', 'w' => '64', 'h' => '64', 'x' => '40', 'y' => '754', ], [ 'type' => 'text', 'content' => '娜可露露', 'size' => '18', 'color' => '#2C2D2E', 'x' => '116', 'y' => '775', ], [ 'type' => 'text', 'content' => '长按识别小程序码进入', 'size' => '14', 'color' => '#B5B9BC', 'x' => '40', 'y' => '842', ], ]; return $data; } /** * 获取支付宝小程序二维码 * @param $page * @param $params * @param $desc * @return mixed|void * @throws */ 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; } public function actionUploadPoster() { $retuest = Yii::$app->request; if ($retuest->isPost) { $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['file'], 'string'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $content = $params['file']; $path = Yii::getAlias('@attachment') . '/poster/'. date('Ymd'); $fileName = $this->base64_image_content($content, $path); $url = ''; if ($fileName != false) { $url = Yii::getAlias('@attachurl') . '/poster/'. date('Ymd') . '/'.$fileName; } return $this->success('success',compact('url')); } } public function actionGetAgreement() { $user_id = $this->user_id; $mall_id = $this->mall_id; $config = Yii::$app->services->setting->mall->get($mall_id, 'user_agree'); $promote_code_agree = ''; $is_sign = 0; $enable_promote_code = $config['enable_promote_code']; $title = '产品推广中介服务合同'; if($config['enable_promote_code']){ $promote_code_agree = $config['promote_code_agree'] ?? ''; $count = UserSignAgreement::find()->where(['and', ['mall_id' => $mall_id], ['user_id' => $user_id], ['status' => StatusEnum::ENABLED], ['source' => UserSignAgreement::SOURCE_POSTER]]) ->limit(1)->count(); $is_sign = $count ? 1 : 0; } $data = compact('enable_promote_code', 'promote_code_agree', 'is_sign', 'title'); return $this->success('数据请求成功', $data); } public function actionSignAgreement() { try{ $user_id = $this->user_id; $mall_id = $this->mall_id; $params = \Yii::$app->request->post(); $is_agree = $params['is_agree']; if(UserSignAgreement::setData([ 'user_id' => $user_id, 'mall_id' => $mall_id, 'source' => UserSignAgreement::SOURCE_POSTER, ]) == false){ throw new \Exception(UserSignAgreement::getStaticError()); } return $this->success('签署成功'); }catch(\Exception $e){ return $this->error($e->getMessage()); } } }