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','lucky_group_activity_id'], 'integer'], [['marketing_type'],'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'], 'mall_id' => $mallId, 'source' => $params['source']];// 分享码保存的基本信息 // 根据来源获取不同分享内容 $params['data'] = $data; if($params['source']==User::SOURCE_SHARE_GOODS){ $goods = StoreGoods::getOne([['id' => $params['goods_id'], 'mall_id' => $mallId]],true,[],false,'id as item_id,store_id,goods_name as share_title,price,original_price,cover_pic as share_img'); if(empty($goods)) return $this->error('商品不存在'); $data = array_merge($data, $goods); $save_content['item_id'] = $goods['item_id']; $params['store_id'] = $goods['store_id']; $save_content['store_id'] = $goods['store_id']; !empty($params['marketing_type']) && $save_content['marketing_type'] = $params['marketing_type']; $page = DiyEnum::O2O_GOODS_DETAIL_PATH; }else{ $params['mall_id'] = $this->mall_id; $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']); } // 查询获取数据 $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); } $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) { 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' => 'id desc', 'select' => $select]; $list = Poster::lists($params); if (empty($list)) return $this->success('获取成功', $list); foreach ($list as $key => $item) { $data = [ 'user_id' => $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']; $list[$key]['qrcode_url'] = DiyEnum::HOME_PAGE_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']); } } //浏览 $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(isset($data['user_id'])) $data['share_user_id'] = $data['user_id']; if($data && $data['pid'] > 0 && $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 && !$user->parent_id && $user->temp_parent_id != $data['pid']){ $user->temp_parent_id = $data['pid']; $user->save(); } } $this->success('获取成功', $data); } } /** * 获取海报码详情 * @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('暂无内容'); if (!empty($params['poster_code_id']) && $this->platform == User::PLATFORM_MP_WX) { // 小程序二维码为空则去获取小程序二维码 if (empty($detail['applet_code'])) { $applet_url = $this->getAppletQrCode(DiyEnum::HOME_PAGE_PATH, $detail['code']);// 获取小程序二维码base64数据 if ($applet_url == false) $this->error('获取小程序二维码失败'); // 获取到base64将其临时存服务器 $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); $detail['content'] = Json::encode($content); } $this->success('获取成功', $detail->toArray()); } } /** * 获取小程序二维码 * @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; } /** * 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; } }