255], [['title', 'show_type', 'share_reward_content', 'view_reward_content'], 'string', 'max' => 100], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'user_id' => '发布者id(后台发布为0)', 'goods_id' => '关联商品id', 'cate_id' => '分类id', 'sort' => '排序(数字越大排在前面)', 'note_type' => '笔记类型(1文章/2视频/3广告)', 'cover_img' => '封面/数组', 'cover_type' => '封面类型', // 封面类型:1小单图2大单图3二图4三图5竖屏6横屏 'title' => '笔记标题', 'content' => '笔记内容', 'content_href' => '笔记为视频/广告使用', 'hidden_comment' => '是否显示评价 1/是 0/否', 'show_type' => '展示内容(阅读数/点赞数/发布时间/发布者)', 'views_people_num' => '浏览人数', 'views_num' => '实际阅读数量', 'likes_num' => '实际点赞数量', 'share_num' => '分享次数', 'virtual_views_num' => '虚拟阅读数量', 'virtual_likes_num' => '虚拟点赞数量', 'is_top' => '是否置顶 1/是 0/否', 'is_top_updated_at' => '置顶时间', 'share_title' => '分享标题类型 1/文章标题 2/自定义', 'share_title_type' => '分享标题', 'view_limit' => '浏览限制类型', 'view_limit_content' => '浏览限制类容', 'pay_people_num' => '付费观看人数', 'pay_amount' => '付费总额', 'share_reward' => '分享奖励是否开启1/是 0/否', 'share_reward_max_time' => '分享最多奖励次数', 'share_reward_content' => '分享奖励内容', 'view_reward' => '浏览奖励是否开启', 'view_reward_max_time' => '浏览最多奖励次数', 'view_reward_content' => '浏览奖励内容', 'is_release' => '是否发布 1/是 0/否', 'status' => '状态,-1是删除,0是禁用,1是启用', 'check_status' => '审核状态:0:未审核 1:审核通过 2:审核不通过', 'check_remark' => '审核原因', 'check_time' => '审核时间', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } // public function scenarios() // { // return [ // 'create' => [], // 'update' => ['goods_id','cate_id','sort','note_type','cover_img','title','content','content_href','hidden_comment','show_type','virtual_views_num','virtual_likes_num','is_top','is_top_updated_at','share_title','share_title_type','view_limit','view_limit_content','share_reward','share_reward_max_time','share_reward_content','view_reward','view_reward_max_time','view_reward_content','is_release','status','check_status','check_remark','check_time'], // 'default' => [], // ]; // } public function getUser() { // return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username,user_id'); return $this->hasOne(User::class, ['id' => 'user_id']); } public function getGoods() { // return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,cover_pic,sales_num,virtual_sales'); return $this->hasOne(Goods::class, ['id' => 'goods_id']); } /** * @throws \yii\db\Exception */ public static function setData(array $params, string $scenarios = 'create', $is_admin = true) { if (isset($params['content_href']) && !is_string($params['content_href'])) { $params['content_href'] = json_encode($params['content_href'], JSON_UNESCAPED_UNICODE); } $transaction = \Yii::$app->db->beginTransaction(); try { if ($scenarios == 'update') { $model = self::findOne(['id' => $params['id']]); if (!$is_admin && $model->user_id != $params['user_id']) { return false; } if (!$is_admin && isset($params['cover_img'])) { // 用户重新编辑验证封面图 $cover_count = NoteEnum::getCoverTypeCount($model->cover_type); if (is_array($params['cover_img']) && count($params['cover_img']) != $cover_count) { throw new Exception('请上传'. $cover_count . '张封面图'); } } } else { $model = new self(); } if (isset($params['cover_img'])) { $params['cover_img'] = is_array($params['cover_img']) ? json_encode($params['cover_img']) : $params['cover_img']; } if (empty($params['goods_id'])) { unset($params['goods_id']); } if (!$model->load($params, '') || false === $model->validate()) { $errors = $model->errors; $error_str = ''; foreach ($errors as $error) { $error_str .= implode(',', $error) . ','; } $error_str = trim($error_str, ','); throw new \Exception($error_str); } if (!$model->save()) throw new Exception($model->getErrorMessage()); $transaction->commit(); return true; } catch (Exception $e) { $transaction->rollBack(); self::setStaticError($e->getMessage()); //var_dump($e->getMessage()); //throw new Exception($e->getMessage()); } } public static function getDetail($id, $user) { $is_like = false; $is_follow = StatusEnum::DISABLED; $view_record_count = 0; $share_record_count = 0; $detail = AddonsOperatingNoteContent::getOne([['id' => $id]], true, ['user', 'goods']); if ($detail['status'] == StatusEnum::DELETE) { self::$static_error = '文章已删除'; return false; } $can_ready = true; if ($detail['note_type'] != NoteEnum::STATUS_THREE) { if ($user['id'] == $detail['user_id']) { $can_ready = true; } else { if ($detail['view_limit'] == NoteEnum::STATUS_ZERO) { if ($detail['view_limit_content'] == 'all') { $can_ready = true; } else { $view_limit_content = \Qiniu\json_decode($detail['view_limit_content']); if (!in_array($user['level'], $view_limit_content)) { $can_ready = false; } } } else { //获取是否购买 $can_ready = AddonsOperatingNoteBuy::count(['where' => [['=', 'user_id', $user->id], ['=', 'note_id', $id], ['=', 'is_pay', NoteEnum::STATUS_ONE]]]) ? true : false; } } } if (!$can_ready) { $view_limit_content = $detail['view_limit_content']; $view_limit = $detail['view_limit']; $detail = [ 'view_limit_content' => $view_limit_content, 'view_limit' => $view_limit, 'title' => $detail['title'], 'created_at' => $detail['created_at'], ]; } else { $view_record_count = AddonsOperatingNoteRewardRecord::count(['where' => [['=', 'user_id', $user['id']], ['=', 'reward_source', NoteEnum::STATUS_ONE], ['=', 'note_content_id', $id]]]); $share_record_count = AddonsOperatingNoteShareRecord::count(['where' => [['=', 'user_id', $user['id']], ['=', 'note_id', $id], ['=', 'has_gift', 1]]]); $like = AddonsOperatingNoteUserLike::getOne([['user_id' => $user->id, 'note_content_id' => $id, 'is_like' => 1]]); if ($like) { $is_like = true; } $follow = UserFollow::getOne([['follow_id' => $detail['user_id']], ['user_id' => $user['id']]]); if ($follow && $follow['status'] == StatusEnum::ENABLED) { $is_follow = StatusEnum::ENABLED; } $model = AddonsOperatingNoteContent::getOne([['id' => $id]]); $model->updateCounters(['views_num' => 1]); //todo 浏览量新增 } return ['can_ready' => $can_ready, 'detail' => $detail, 'is_like' => $is_like, 'is_follow' => $is_follow, 'share_record_count' => $share_record_count, 'view_record_count' => $view_record_count]; } public static function viewReward($id, $user) { $state = false; $num = 0; $content = ''; $transaction = \Yii::$app->db->beginTransaction(); try { $note = AddonsOperatingNoteContent::getOne([['id' => $id]], false, [], false); if ($note->note_type == NoteEnum::STATUS_TWO) { $note->updateCounters(['views_num' => 1]); } if ($note->view_reward == 1) { $can_ready = true; if ($note->note_type != NoteEnum::STATUS_THREE) { if ($user['id'] == $note->user_id) { $can_ready = true; } else { if ($note->view_limit == NoteEnum::STATUS_ZERO) { if ($note->view_limit_content == 'all') { $can_ready = true; } else { $view_limit_content = \Qiniu\json_decode($note->view_limit_content); if (!in_array($user['level'], $view_limit_content)) { $can_ready = false; } } } else { //获取是否购买 $can_ready = AddonsOperatingNoteBuy::count(['where' => [['=', 'user_id', $user['id']], ['=', 'note_id', $id], ['=', 'is_pay', NoteEnum::STATUS_ONE]]]) ? true : false; } } } if ($can_ready) { $record_count = AddonsOperatingNoteRewardRecord::count(['where' => [['=', 'user_id', $user['id']], ['=', 'reward_source', NoteEnum::STATUS_ONE], ['=', 'note_content_id', $id]]]); if ($record_count < $note->view_reward_max_time) { // 积分 余额 if (in_array($note->view_reward_type, [NoteEnum::STATUS_ZERO, NoteEnum::STATUS_ONE])) { if ($note->view_reward_type == NoteEnum::STATUS_ZERO) { $content = '积分'; $wallet_type = UserWalletService::WALLET_TYPE_SCORE; } if ($note->view_reward_type == NoteEnum::STATUS_ONE) { $wallet_type = UserWalletService::WALLET_TYPE_BALANCE; $content = '余额'; } $handle_wallet = [ 'message_id' => 0, 'mall_id' => $note->mall_id, 'user_id' => $user['id'], 'wallet_type' => $wallet_type, 'money' => $note->view_reward_content, 'desc' => '浏览文章奖励', 'source_table' => AssetsType::OPERATING_NOTE, 'source_table_id' => $note->id, 'from_type' => UserWalletEnum::REWARD, 'capital_type' => UserWalletEnum::OPERATING_NOTE_VIEW, ]; $state = UserWalletService::handle(0, $handle_wallet); } else { // 红包 $state = RedpackWallet::setData([ 'mall_id' => $note->mall_id, 'user_id' => $user['id'], 'is_frozen' => false, 'money' => $note->share_reward_content, 'source_table' => static::tableName(), 'source_table_id' => $note->id, 'from_type' => RedpackEnum::FROM_RECEIVE, 'desc' => "运营笔记ID: {$id}阅读奖励", ]); } if ($state) { $model = new AddonsOperatingNoteRewardRecord(); $model->mall_id = $note->mall_id; $model->user_id = $user['id']; $model->note_content_id = $id; $model->note_cate_id = $note->cate_id; $model->note_type = $note->note_type; $model->reward_type = $note->view_reward_type; $model->reward_source = NoteEnum::STATUS_ONE; $model->reward_content = $note->view_reward_content; $model->save(); $num = $note->view_reward_content; } } } } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); } return ['state' => $state, 'content' => $content, 'num' => $num]; } public static function getUrlContent($url) { //设置 header header("Content-type:application/json"); //初始化 CURL $ch = curl_init(); //目标服务器地址 curl_setopt($ch, CURLOPT_URL, $url); // 对认证证书来源的检查 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //获取的信息以文件流的形式返回,而不是直接输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 添加浏览器用户代理和其他必要的头信息 curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'Referer: https://mp.weixin.qq.com/' ]); //发起请求 $result = curl_exec($ch); $str_7 = substr($result, strripos($result, "twitter:title") + 24); $str_8 = substr($str_7, 0, strrpos($str_7, "twitter:creator") - 23); //公众号文章标题 $str_16 = substr($result, strripos($result, "div class=\"rich_media_content") + 76); $str_17 = substr($str_16, 0, strrpos($str_16, "first_sceen__time") - 83); //公众号文章正文 //关闭请求 curl_close($ch); return ['title' => $str_8, 'content' => trim($str_17)]; } }