| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- namespace addons\OperatingNote\common\models;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCouponCatRelate;
- use addons\Coupon\common\models\AddonsCouponGoodsRelate;
- use addons\Coupon\common\models\AddonsCouponMemberRelate;
- use addons\Coupon\common\models\AddonsCouponReward;
- use addons\OperatingNote\common\enums\NoteEnum;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\models\RedpackWallet;
- use common\enums\AssetsType;
- use common\enums\PaymentTradeOrderEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\goods\Goods;
- use common\models\user\User;
- use common\models\user\UserFollow;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_operating_note_content}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 发布者id(后台发布为0)
- * @property int $goods_id 关联商品id
- * @property int $cate_id 分类id
- * @property int $sort 排序(数字越大排在前面)
- * @property int $note_type 笔记类型(1文章/2视频/3广告)
- * @property string $cover_img 封面/数组
- * @property int $cover_type 封面类型:1小单图2大单图3二图4三图5竖屏6横屏
- * @property string $title 笔记标题
- * @property string $content 笔记内容
- * @property string $content_href 笔记为视频/广告使用
- * @property int $hidden_comment 是否显示评价 1/是 0/否
- * @property string $show_type 展示内容(阅读数/点赞数/发布时间/发布者)
- * @property int $views_people_num 浏览人数
- * @property int $views_num 实际阅读数量
- * @property int $likes_num 实际点赞数量
- * @property int $share_num 分享次数
- * @property int $virtual_views_num 虚拟阅读数量
- * @property int $virtual_likes_num 虚拟点赞数量
- * @property int $is_top 是否置顶 1/是 0/否
- * @property int $is_top_updated_at 置顶时间
- * @property string $share_title 分享标题类型 1/文章标题 2/自定义
- * @property int $share_title_type 分享标题
- * @property int $view_limit 浏览限制类型
- * @property string $view_limit_content 浏览限制类容
- * @property int $pay_people_num 付费观看人数
- * @property float $pay_amount 付费总额
- * @property int $share_reward 分享奖励是否开启1/是 0/否
- * @property int $share_reward_type 分享奖励类型 0/积分 1/余额 2/红包
- * @property int $share_reward_max_time 分享最多奖励次数
- * @property string $share_reward_content 分享奖励内容
- * @property int $view_reward 浏览奖励是否开启1/是 0/否
- * @property int $view_reward_type 浏览奖励类型
- * @property int $view_reward_max_time 浏览最多奖励次数
- * @property string $view_reward_content 浏览奖励内容
- * @property int $is_release 是否发布 1/是 0/否
- * @property int $status 状态,-1是删除,0是禁用,1是启用
- * @property int $check_status 审核状态:0:未审核 1:审核通过 2:审核不通过
- * @property string|null $check_remark 审核原因
- * @property int $check_time 审核时间
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsOperatingNoteContent extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operating_note_content}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'cate_id', 'note_type', 'view_limit', 'share_reward', 'share_reward_max_time', 'view_reward', 'view_reward_max_time'], 'required'],
- [['mall_id', 'user_id', 'goods_id', 'cate_id', 'sort', 'note_type', 'hidden_comment', 'views_people_num', 'views_num', 'likes_num', 'share_num', 'virtual_views_num', 'virtual_likes_num', 'is_top', 'is_top_updated_at', 'share_title_type', 'view_limit', 'pay_people_num', 'share_reward', 'share_reward_type', 'share_reward_max_time', 'view_reward', 'view_reward_type', 'view_reward_max_time', 'is_release', 'status', 'check_status', 'check_time', 'created_at', 'updated_at', 'cover_type'], 'integer'],
- [['content', 'cover_img', 'content_href'], 'string'],
- [['pay_amount'], 'number'],
- [['share_title', 'view_limit_content', 'check_remark'], 'string', 'max' => 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)];
- }
- }
|