|
@@ -52,13 +52,21 @@ use yii\web\ServerErrorHttpException;
|
|
|
* @property int $blocked_at
|
|
|
* @property int $login_at
|
|
|
* @property int $type
|
|
|
+ * @property int $level_id
|
|
|
+ * @property int $is_vip
|
|
|
+ * @property int $vip_time
|
|
|
* @property string $password write-only password
|
|
|
* @property Profile $profile
|
|
|
+ * @property UserCar $car
|
|
|
* @property string $privilege
|
|
|
* @property string $openid
|
|
|
* @property string $unionid
|
|
|
* @property string $nickname
|
|
|
* @property Module $module
|
|
|
+ * @property Store $store
|
|
|
+ * @property ShareLevel $shareLevel
|
|
|
+ * @property Attachment $qrcode
|
|
|
+ * @property array $dataCount
|
|
|
*/
|
|
|
class User extends ActiveRecord implements IdentityInterface
|
|
|
{
|
|
@@ -86,6 +94,11 @@ class User extends ActiveRecord implements IdentityInterface
|
|
|
{
|
|
|
return [
|
|
|
TimestampBehavior::className(),
|
|
|
+ [
|
|
|
+ 'class' => UploadBehavior::className(),
|
|
|
+ 'attribute' => 'qrcode',
|
|
|
+ 'entity' => __CLASS__
|
|
|
+ ],
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -96,7 +109,7 @@ class User extends ActiveRecord implements IdentityInterface
|
|
|
'register' => ['username', 'openid', 'password', 'email', 'tel', 'pid'],
|
|
|
'connect' => ['username', 'email'],
|
|
|
'create' => ['jucai_id', 'email', 'tel'],
|
|
|
- 'update' => ['username', 'password', 'email', 'tel', 'nickname', 'pid'],
|
|
|
+ 'update' => ['username', 'password', 'email', 'tel', 'nickname', 'level_id', 'pid'],
|
|
|
'settings' => ['username', 'password', 'email', 'tel'],
|
|
|
'resetPassword' => ['password']
|
|
|
]);
|
|
@@ -115,6 +128,7 @@ class User extends ActiveRecord implements IdentityInterface
|
|
|
[['email'], 'match', 'pattern' => Regexp::$email],
|
|
|
[['privilege'], 'string', 'max' => 255],
|
|
|
|
|
|
+ [['qrcode', 'dataCount'], 'safe'],
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -135,6 +149,11 @@ class User extends ActiveRecord implements IdentityInterface
|
|
|
'type' => '用户类型',
|
|
|
'privilege' => '特权',//微信用户属性
|
|
|
'unionid' => '联合ID',//微信用户属性
|
|
|
+ 'level_id' => '分销等级',//微信用户属性
|
|
|
+ 'qrcode' => '分销码', //小程序码
|
|
|
+ 'is_vip' => '是否vip', //是否vip
|
|
|
+ 'vip_time' => 'vip有效期', //是否vip
|
|
|
+ 'dataCount' => '数据统计',
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -739,6 +758,14 @@ class User extends ActiveRecord implements IdentityInterface
|
|
|
public static function lists()
|
|
|
{
|
|
|
if (Yii::$app->user->identity->getIsAdmin()) {
|
|
|
+ //已经使用的用户ids
|
|
|
+// $usedUserIds = Company::find()->select('user_id')->distinct()->asArray()->indexBy('user_id')->column();
|
|
|
+// //去掉当前包含的当前公司id
|
|
|
+// if (!empty($this->user_id) && isset($usedUserIds[$this->user_id])) {
|
|
|
+// unset($usedUserIds[$this->user_id]);
|
|
|
+// }
|
|
|
+// return User::find()->where(['not in', 'id', $usedUserIds])->asArray()->indexBy('id')->all();
|
|
|
+
|
|
|
$lists = User::find()->indexBy('id')->all();
|
|
|
$lists = ArrayHelper::map($lists, 'id', 'nickname');
|
|
|
|
|
@@ -750,4 +777,211 @@ class User extends ActiveRecord implements IdentityInterface
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户的供应商
|
|
|
+ * @return \yii\db\ActiveQuery
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function getStore()
|
|
|
+ {
|
|
|
+ return $this->hasOne(Store::className(), ['user_id' => 'id']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询后对数据处理
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function afterFind()
|
|
|
+ {
|
|
|
+ parent::afterFind();
|
|
|
+// $this->nickname = $this->formatNickname();
|
|
|
+
|
|
|
+ //更新vip状态
|
|
|
+// $this->isVip();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取分销等级
|
|
|
+ * @return \yii\db\ActiveQuery
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function getShareLevel()
|
|
|
+ {
|
|
|
+ return $this->hasOne(ShareLevel::className(), ['id' => 'level_id']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分销海报
|
|
|
+ *
|
|
|
+ * @param bool $force
|
|
|
+ * @return bool|mixed|string
|
|
|
+ * @throws ServerErrorHttpException
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function generateQrcode($force = false)
|
|
|
+ {
|
|
|
+ //海报存在直接返回
|
|
|
+ if ($this->qrcode && !$force) {
|
|
|
+ return $this->qrcode->url;
|
|
|
+ }
|
|
|
+
|
|
|
+ //小程序码中 scene 放入用户id
|
|
|
+ $scene = 'pid:' . \Yii::$app->user->id;
|
|
|
+ $hash = md5($scene);
|
|
|
+ $qrcodeFile = \Yii::getAlias('@storagePath/upload/qrcode/' . $hash . '.png');//path
|
|
|
+ $qrcodeUrl = \Yii::getAlias('@storageUrl/upload/qrcode/' . $hash . '.png');//path
|
|
|
+
|
|
|
+ try {
|
|
|
+ ini_set('memory_limit', '256M');
|
|
|
+ $editor = Grafika::createEditor(GrafikaHelper::getSupportEditorLib());
|
|
|
+ //背景图
|
|
|
+ $editor->open($userPoster, Yii::$app->storage->getPath(\Yii::$app->config->get('share_poster_bg')));
|
|
|
+
|
|
|
+ //生成推广码
|
|
|
+ /* @var Qrcode $qrcode */
|
|
|
+ $qrcode = (new Application())->driver("mini.qrcode");
|
|
|
+ //小程序码 buffer,宽度220
|
|
|
+ $qrcodeStream = $qrcode->unLimit($scene, 'pages/index/index', $extra = []);//buffer
|
|
|
+ $tempFile = \Yii::getAlias('@storagePath/upload/thumb/' . $hash . '.png');
|
|
|
+ if (!file_exists(dirname($tempFile))) {
|
|
|
+ @mkdir(dirname($tempFile), 0755, true);
|
|
|
+ }
|
|
|
+ file_put_contents($tempFile, $qrcodeStream);
|
|
|
+// Yii::$app->storage->put($tempFile, $qrcodeStream);
|
|
|
+ $editor->open($wxappQrcode, $tempFile);
|
|
|
+ //测试二维码
|
|
|
+// $editor->open($wxappQrcode, Yii::$app->storage->getPath('https://es.jiangzi.xin/storage/upload/20220408/OGGUn-K8HxojZfSesNv0OuVuAhRLYPzSJX0zEiyE.png'));
|
|
|
+
|
|
|
+ $avatarPath = Yii::$app->storage->getPath($this->getAvatar(540));
|
|
|
+ if (strpos($avatarPath, 'http') !== false) {
|
|
|
+ $avatarPath = Yii::$app->storage->thumbnail(\Yii::$app->config->get('share_image'), 540, 540);
|
|
|
+ $editor->open($avatar, Yii::$app->storage->getPath($avatarPath));
|
|
|
+ } else {
|
|
|
+
|
|
|
+ $editor->open($avatar, $avatarPath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// $editor->resizeFill($goodsCover, 750, 750);
|
|
|
+ $editor->resizeExactWidth($avatar, 540);
|
|
|
+ //附加头像
|
|
|
+ $editor->blend($userPoster, $avatar, 'normal', 1.0, 'top-left', 105, 4900);
|
|
|
+
|
|
|
+ //写入文字
|
|
|
+ $fontFile = Yii::getAlias('@root/web/assets/fonts/st-heiti-light.ttc');
|
|
|
+ //加用户昵称
|
|
|
+ $editor->text($userPoster, mb_substr($this->nickname, 0, 12), 100, 750, 5000, new Color('#ffffff'), $fontFile, 0);
|
|
|
+ $editor->text($userPoster, Yii::$app->config->get('share_poster_text'), 70, 750, 5250, new Color('#ffffff'), $fontFile, 0);
|
|
|
+
|
|
|
+ //调整小程序码图片
|
|
|
+ $editor->resizeExactWidth($wxappQrcode, 660);
|
|
|
+ //附加小程序码图片
|
|
|
+ $editor->blend($userPoster, $wxappQrcode, 'normal', 1.0, 'top-right', -105, 4800);
|
|
|
+ //保存图片
|
|
|
+ $editor->save($userPoster, $qrcodeFile, 85);
|
|
|
+
|
|
|
+ //删除临时图片
|
|
|
+ unlink($tempFile);
|
|
|
+
|
|
|
+ //上传分销码
|
|
|
+ list($attachment, $error) = Attachment::uploadFromFile('qrcode', $qrcodeFile);
|
|
|
+ if ($error == null) {
|
|
|
+ $this->qrcode = [
|
|
|
+ 'id' => $attachment->id,
|
|
|
+ 'name' => $attachment->name,
|
|
|
+ 'hash' => $attachment->hash,
|
|
|
+ 'url' => $attachment->url,
|
|
|
+ 'path' => $attachment->path,
|
|
|
+ 'extension' => $attachment->extension,
|
|
|
+ 'type' => $attachment->type,
|
|
|
+ 'size' => $attachment->size
|
|
|
+ ];
|
|
|
+ if ($this->save(false)) {
|
|
|
+ return ArrayHelper::getValue($this, 'qrcode.url', '');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception $e) {
|
|
|
+ throw new ServerErrorHttpException($e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $qrcodeUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户是否vip,并且未过期
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function isVip()
|
|
|
+ {
|
|
|
+ //vip自动过期
|
|
|
+ if ($this->is_vip) {
|
|
|
+ $vip = VipUser::find()->where(['user_id' => $this->id])->orderBy(['end_time' => SORT_DESC])->one();
|
|
|
+ if (!empty($vip) && $vip->isExpire()) {
|
|
|
+ $this->is_vip = StatusEnum::STATUS_OFF;
|
|
|
+ $this->save(false);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->is_vip;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前vip
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function getCurrentVip()
|
|
|
+ {
|
|
|
+ $now = time();
|
|
|
+ return VipUser::find()->where(['user_id' => $this->id])->andWhere(['and', ['<=', 'start_time', $now], ['>', 'end_time', $now]])->one();
|
|
|
+ }
|
|
|
+
|
|
|
+//
|
|
|
+// public function fields() {
|
|
|
+// $fields = parent::fields();
|
|
|
+//
|
|
|
+// $fields[] = 'dataCount';
|
|
|
+// return $fields;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据统计
|
|
|
+ * @author nodelog
|
|
|
+ */
|
|
|
+ public function getDataCount()
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ [
|
|
|
+ 'text' => '日课',
|
|
|
+ 'count' => 0
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'text' => '总课',
|
|
|
+ 'count' => 0
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'text' => '辅助课',
|
|
|
+ 'count' => 0
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ //每日功课
|
|
|
+// $data[0]['count'] = Data::find()->active()->andWhere(['user_id' => $this->id])->today()->count();
|
|
|
+ //累积功课
|
|
|
+// $data[1]['count'] = Data::find()->active()->andWhere(['user_id' => $this->id])->count();
|
|
|
+ //辅助功课
|
|
|
+// $userIds = VipUser::find()->active()->andWhere(['pay_user_id' => $this->id])->andWhere(['!=', 'user_id', $this->id])->select(['user_id'])->distinct()->column();
|
|
|
+// $data[2]['count'] = Data::find()->active()->andWhere(['in', 'user_id', $userIds])->count();
|
|
|
+ //直接获取辅助榜数据
|
|
|
+// $helpCount = DataRank::find()->active()->andWhere(['user_id' => $this->id, 'level' => DataTypeEnum::TYPE_HELP])->total()->select(['count'])->scalar();
|
|
|
+// $data[2]['count'] = $helpCount ? $helpCount : 0;
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
}
|