| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\db\ActiveRecord;
- use yii\db\ActiveQuery;
- use yii\web\IdentityInterface;
- use yii\behaviors\TimestampBehavior;
- use app\librarys\shareTree\behaviors\ClosureTableBehavior;
- use app\models\userTreeQuery\UserQuery;
- use app\librarys\shareTree\behaviors\OldClosureTableBehavior;
- use app\models\userTreeQuery\OldUserQuery;
- use app\modules\admin\models\pospal\PospalForm;
- use app\models\ShareHolderLevel;
- use app\models\SaasUser;
- use app\models\ShareHolder;
- use app\utils\Share\BonusPool;
- /**
- * Class User
- * @package app\modules\common\models
- *
- * @property integer $id
- * @property integer $type
- * @property string $username
- * @property string $password
- * @property string $auth_key
- * @property string $access_token
- * @property integer $is_delete
- * @property string $wechat_open_id
- * @property string $wechat_union_id
- * @property string $nickname
- * @property string $avatar_url
- * @property integer $store_id
- * @property integer $mch_id
- * @property integer $is_distributor
- * @property integer $parent_id
- * @property integer $time
- * @property string $total_price
- * @property string $price
- * @property integer $is_clerk
- * @property integer $we7_uid
- * @property integer $shop_id
- * @property integer $level
- * @property string $integral
- * @property string $total_integral
- * @property string $coin
- * @property string $total_coin
- * @property string $money
- * @property string $contact_way
- * @property string $comments
- * @property string $binding
- * @property string $wechat_platform_open_id
- * @property integer $platform
- * @property integer $blacklist
- * @property integer $is_admin
- * @property integer $parent_user_id
- * @property string $appcid
- * @property integer $is_delivery
- * @property integer $delivery_office
- * @property string $alipay_open_id
- * @property integer $is_saas_clerk
- * @property string $created_at
- * @property string $updated_at
- * @property string $bytedance_open_id
- * @property integer $is_holder
- * @property string $wechat_app_open_id
- * @property string $user_remark
- * @property integer $old_parent_id
- * @property integer $adapay_user_huifu_id
- * @property string $remark_name
- * @property integer $is_student
- * @property string $ali_openId
- * @property string $user_level_expires_ext
- * @property string $user_level_expires
- * @property string $purchase_money
- * @property integer $is_cloud_inventory
- * @property integer $cloud_inventory_level
- */
- class User extends ActiveRecord implements IdentityInterface
- {
- //从银豹同步时设置为1
- public $yinbaoSync = 0;
- /**
- * 用户类型
- */
- CONST USER_TYPE_ADMIN = 0;
- CONST USER_TYPE_NORMAL = 1;
- public static $valid_user_type = [
- self::USER_TYPE_ADMIN,
- self::USER_TYPE_NORMAL
- ];
- /**
- * 是否分销商
- */
- CONST IS_DISTRIBUTOR = 1;
- CONST NOT_DISTRIBUTOR = 0;
- CONST APPLY_DISTRIBUTOR = 2;
- public static $valid_distributor_type = [
- self::IS_DISTRIBUTOR,
- self::NOT_DISTRIBUTOR,
- self::APPLY_DISTRIBUTOR
- ];
- /**
- * 是否核销员
- */
- CONST IS_CLERK = 1;
- CONST NOT_CLERK = 0;
- public static $valid_clerk_type = [
- self::IS_CLERK,
- self::NOT_CLERK
- ];
- /**
- * 用户来源平台:微信小程序
- */
- CONST USER_FROM_WECHAT = 0;
- /**
- * 用户来源平台:支付宝
- */
- CONST USER_FROM_ALIPAY = 1;
- /**
- * 用户来源平台:公众号
- */
- CONST USER_FROM_OFFICIAL_ACCOUNTS = 2;
- /**
- * 用户来源平台:app微信
- */
- CONST USER_FROM_APP = 3;
- /**
- * 用户来源平台:手机号注册
- */
- CONST USER_FROM_PHONE = 4;
- /**
- * 用户来源平台: 抖音
- */
- CONST USER_FROM_BYTEDANCE = 5;
- /**
- * 用户来源平台: 收银台添加
- */
- CONST USER_FROM_CASHIER = 6;
- /**
- * 用户来源平台: 后台添加
- */
- CONST USER_FROM_OPERATOR_BACK = 8;
- public static $valid_user_platform_type = [
- self::USER_FROM_WECHAT,
- self::USER_FROM_ALIPAY,
- self::USER_FROM_OFFICIAL_ACCOUNTS,
- self::USER_FROM_APP,
- self::USER_FROM_PHONE,
- ];
- /**
- * 是否在黑名单
- */
- CONST USER_IN_BLACKLIST = 1;
- CONST USER_NOT_IN_BLACKLIST = 0;
- public static $valid_user_blacklist_type = [
- self::USER_IN_BLACKLIST,
- self::USER_NOT_IN_BLACKLIST
- ];
- /**
- * 是否删除
- */
- CONST USER_IS_DELETE = 1;
- CONST USER_NOT_DELETE = 0;
- public static $valid_user_delete_type = [
- self::USER_IS_DELETE,
- self::USER_NOT_DELETE
- ];
- /**
- * 是否配送员
- */
- CONST IS_DELIVERY = 1;
- CONST NOT_DELIVERY = 0;
- public static $valid_delivery_type = [
- self::IS_DELIVERY,
- self::NOT_DELIVERY
- ];
- /**
- * 骑手是否在线
- */
- CONST DELIVERY_ONLINE = 1;
- CONST DELIVERY_OFFLINE = 0;
- public static $valid_delivery_status_type = [
- self::DELIVERY_ONLINE,
- self::DELIVERY_OFFLINE
- ];
- /**
- * 用户充值类型 1:积分 2:余额
- */
- CONST RECHARGE_INTEGRAL = 1;
- CONST RECHARGE_MONEY = 2;
- public static $valid_recharge_type = [
- self::RECHARGE_INTEGRAL,
- self::RECHARGE_MONEY
- ];
- /**
- * 用户充值增加或减少 1:增加 2:减少
- */
- CONST RECHARGE_ADD = 1;
- CONST RECHARGE_REDUCE = 2;
- public static $valid_recharge_status = [
- self::RECHARGE_ADD,
- self::RECHARGE_ADD
- ];
- /**
- * @return string
- */
- public static function tableName()
- {
- return '{{%user}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class
- ],
- 'treePath' => [
- 'class' => ClosureTableBehavior::class,
- 'treePathModelClass' => UserTreePath::class,
- 'ownerParentIdAttribute' => 'parent_id',
- ],
- 'oldTreePath' => [
- 'class' => OldClosureTableBehavior::class,
- 'treePathModelClass' => OldUserTreePath::class,
- 'ownerParentIdAttribute' => 'old_parent_id',
- ]
- ];
- }
- //会员到期时间格式
- //user_level_expires_ext:[['level' => 1, 'expires_in' => 到期时间戳], ['level' => 2, 'expires_in' => 到期时间戳]]
- public function rules()
- {
- return [
- [['id', 'platform', 'type', 'is_delete', 'store_id', 'is_distributor', 'parent_id', 'old_parent_id', 'time', 'is_clerk',
- 'we7_uid', 'shop_id', 'level', 'is_delivery', 'delivery_office', 'is_saas_clerk','is_admin', 'is_holder'],
- 'integer'],
- [['total_price', 'price', 'money', 'integral', 'total_integral','free_money','total_free_money', 'user_level_expires','global_money_total','global_money'], 'number'],
- [['avatar_url', 'binding', 'alipay_open_id', 'bytedance_open_id', 'user_level_expires_ext'], 'string'],
- [['username', 'password', 'auth_key', 'access_token', 'wechat_open_id', 'wechat_union_id', 'nickname',
- 'contact_way', 'comments', 'wechat_app_open_id', 'remark_name', 'ali_openId'], 'string', 'max' => 255],
- [['access_token', 'binding', 'user_remark'], 'string', 'max' => 60],
- [['username', 'access_token'], 'required'],
- [['access_token', 'create_at', 'updated_at', 'adapay_user_huifu_id'], 'safe'],
- [['money_month', 'money_month_used'], 'safe'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'type' => '用户类型:0=管理员,1=普通用户',
- 'username' => 'Username',
- 'password' => 'Password',
- 'auth_key' => 'Auth Key',
- 'access_token' => 'Access Token',
- 'is_delete' => 'Is Delete',
- 'wechat_open_id' => '微信openid',
- 'wechat_union_id' => '微信用户union id',
- 'nickname' => '昵称',
- 'avatar_url' => '头像url',
- 'store_id' => '商城id',
- 'mch_id' => '配送员绑定商户id',
- 'is_distributor' => '是否是分销商 0--不是 1--是 2--申请中',
- 'parent_id' => '父级ID',
- 'time' => '成为分销商的时间',
- 'total_price' => '累计佣金',
- 'price' => '可提现佣金',
- 'is_clerk' => '是否是核销员 0--不是 1--是',
- 'we7_uid' => '微擎账户id',
- 'shop_id' => 'Shop ID',
- 'level' => '会员等级',
- 'integral' => '用户当前积分',
- 'total_integral' => '用户总积分',
- 'coin' => '用户当前贡献积分',
- 'total_coin' => '用户总贡献积分',
- 'money' => '余额',
- 'contact_way' => '联系方式',
- 'comments' => '备注',
- 'binding' => '授权手机号',
- 'wechat_platform_open_id' => '微信公众号openid',
- 'platform' => '小程序平台 微信:wx,支付宝:my(0、微信小程序,1、微信公众号2、APP端)',
- 'blacklist' => '黑名单 0.否 | 1.是',
- 'parent_user_id' => '可能成为上级的ID',
- 'appcid' => 'app唯一标识',
- 'is_admin' => '是否为手机端管理员',
- 'is_delivery'=>'是否是配送员 0--不是 1--是',
- 'delivery_office'=>'骑手是否在线(0、在线,1离线)',
- 'alipay_open_id' => '支付宝user_id',
- 'is_saas_clerk' => '是否为saas版核销员',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'wechat_app_open_id' => '微信app端openid',
- 'old_parent_id' => '旧父级ID',
- 'user_remark' => '用户备注',
- 'remark_name' => '备注名',
- 'is_student' => '是否通过学生认证:0=否;1=是;',
- 'ali_openId' => '支付宝新openid',
- 'user_level_expires_ext' => '用户等级到期时间备注',
- 'user_level_expires' => '用户等级到期时间'
- ];
- }
- public function beforeSave($insert)
- {
- // 创建用户自动赋值access_token
- if (parent::beforeSave($insert)) {
- if ($this->isNewRecord) {
- $this->access_token = \Yii::$app->security->generateRandomString();
- }
- return true;
- }
- return false;
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- if ($insert && ($this->type == 1) && ($this->yinbaoSync == 0)) {
- (new PospalForm(['store_id' => $this->store_id]))->afterUserSave($this);
- }
- if($insert && $this->alipay_open_id){
- UserCoupon::updateAll(['user_id' => $this->id], ['alipay_open_id' => $this->alipay_open_id]);
- }
- $changeIntegral = isset($changedAttributes['integral']) && ($this->integral != $changedAttributes['integral']);
- $changeMoney = isset($changedAttributes['money']) && ($this->money != $changedAttributes['money']);
- if(!$insert && ($changeIntegral || $changeMoney)){
- \Yii::error([__METHOD__, $insert, $changedAttributes]);
- (new \app\modules\admin\models\alipay\Card(['store_id' => $this->store_id]))->cardUpdateByOpenid($this->alipay_open_id);
- }
- if($insert){
- (new \app\utils\OrderUtil())->userAutoBecomeShare($this->id);
- // 赠送优惠券
- \app\utils\AutoSendCoupon::send($this->id, CouponAutoSend::EVENT_NEWUSER, $this->store_id, 0, CouponAutoSend::EXT_REG);
- // 自动注册成为股东
- $share_level_list = ShareHolderLevel::find()->where([ 'store_id' => $this->store_id, 'is_delete' => 0])->all();
- // 查找是否有设置消费金额为0的等级条件
- $currentLevel = null;
- foreach ($share_level_list as $k => $v) {
- $share_level_list_condition = json_decode($v->condition, true);
- if (!isset($share_level_list_condition['self'])) {
- continue;
- }
- $shareholder = $share_level_list_condition['self'];
- if ($shareholder['is_open'] && $shareholder['value']['price'] === '0') {
- $currentLevel = $v;
- break;
- }
- }
- if ($currentLevel) {
- $saas_user = SaasUser::findOne(['mobile' => $this->binding]);
- $shareHolderModel = new ShareHolder();
- $shareHolderModel->level_id = $currentLevel->id;
- $shareHolderModel->store_id = $this->store_id;
- $shareHolderModel->user_id = $this->id;
- $shareHolderModel->name = $saas_user->name;
- $shareHolderModel->address = '';
- $shareHolderModel->mobile = $saas_user->mobile;
- $shareHolderModel->province = 0;
- $shareHolderModel->city = 0;
- $shareHolderModel->district = 0;
- $shareHolderModel->province_name = '';
- $shareHolderModel->city_name = '';
- $shareHolderModel->district_name = '';
- $shareHolderModel->status = 1;
- $shareHolderModel->audit_time = time();
- if ($shareHolderModel->save()) {
- // BonusPool::checkChildHolderUpdateLevel($this->id, $currentLevel->id);
- BonusPool::ShareHolderLevelJob($shareHolderModel->store_id, 0, $shareHolderModel->user_id);
- if ((int)$currentLevel->member_level > (int)$this->level) {
- $this->level = $currentLevel->member_level;
- }
- $this->is_holder = 1;
- $this->save();
- }
- }
- }
- if ($this->parent_id > 0 && !cache_lock(['ShareLevelJob', $this->store_id, $this->parent_id], 30)) {
- \queue_push(new \app\jobs\ShareLevelJob(['store_id' => $this->store_id, 'user_id' => $this->parent_id]), 30, 1);
- }
- if (isset($changedAttributes['level'])) {
- $user = User::findOne($this->id);
- $level = Level::findOne(['level' => $this->level, 'store_id' => $this->store_id, 'is_delete' => 0]);
- $result = self::handleUserLevelExpires($level, $user);
- $user->user_level_expires_ext = json_encode($result['user_level_expires_ext'], JSON_UNESCAPED_UNICODE);
- $user->user_level_expires = $result['expires_in'];
- $user->save();
- }
- }
- public static function find(): UserQuery
- {
- return new UserQuery(get_called_class());
- }
- public static function findOld(): OldUserQuery
- {
- return new OldUserQuery(get_called_class());
- }
- /**
- * @return ActiveQuery
- */
- public function getTreePathOwner(): ActiveQuery
- {
- return $this->hasOne(UserTreePath::class, ['parent_id' => 'id', 'child_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getOldTreePathOwner(): ActiveQuery
- {
- return $this->hasOne(OldUserTreePath::class, ['parent_id' => 'id', 'child_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getTreePathsChild(): ActiveQuery
- {
- return $this->hasMany(UserTreePath::class, ['child_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getOldTreePathsChild(): ActiveQuery
- {
- return $this->hasMany(OldUserTreePath::class, ['child_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getTreePathsNearestParent(): ActiveQuery
- {
- return $this->hasMany(UserTreePath::class, ['nearest_parent_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getOldTreePathsNearestParent(): ActiveQuery
- {
- return $this->hasMany(OldUserTreePath::class, ['nearest_parent_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getTreePathsParent(): ActiveQuery
- {
- return $this->hasMany(UserTreePath::class, ['parent_id' => 'id']);
- }
- /**
- * @return ActiveQuery
- */
- public function getOldTreePathsParent(): ActiveQuery
- {
- return $this->hasMany(OldUserTreePath::class, ['parent_id' => 'id']);
- }
- /**
- * {@inheritdoc}
- */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- /**
- * {@inheritdoc}
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return static::findOne(['access_token' => $token]);
- }
- /**
- * {@inheritdoc}
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * {@inheritdoc}
- */
- public function getAuthKey()
- {
- }
- /**
- * {@inheritdoc}
- */
- public function validateAuthKey($authKey)
- {
- }
- /**
- * 刷新token
- * @return bool
- */
- public function refreshToken()
- {
- $this->access_token = \Yii::$app->security->generateRandomString();
- return $this->save();
- }
- public static function handleUserLevelExpires($level, $user) {
- if (!$level || !$user) {
- return ['user_level_expires_ext' => [], 'expires_in' => 0];
- }
- // 会员等级新增有效期时间
- $expires_in = 0;
- //购买会员有限期 0为永久
- if ($level->expires_in > 0) {
- $expires_in = strtotime('+' . $level->expires_in . ' day');
- if ($level->expires_type) {
- $expires_in = strtotime('+' . $level->expires_in . ' year');
- }
- }
- $user_level_expires_ext = [['level' => $user->level, 'expires_in' => $expires_in]];
- if ($user->user_level_expires_ext) {
- $user_level_expires_ext = json_decode($user->user_level_expires_ext, true);
- //给新的加上 排序(按等级) 并且 去除已经过期的 作为新的一个json
- array_push($user_level_expires_ext, ['level' => $user->level, 'expires_in' => $expires_in]);
- //排序
- $last_names = array_column($user_level_expires_ext, 'level');
- array_multisort($last_names, SORT_DESC, $user_level_expires_ext);
- //去除已经过期的
- foreach ($user_level_expires_ext as $user_level_index => $user_level_item) {
- // 去除已经过期的
- if ($user_level_item['expires_in'] > 0 && $user_level_item['expires_in'] <= time()) {
- unset($user_level_expires_ext[$user_level_index]);
- }
- }
- $user_level_expires_ext = array_values($user_level_expires_ext);
- }
- return ['user_level_expires_ext' => $user_level_expires_ext, 'expires_in' => $expires_in];
- }
- public static function findUserByStore($store_id,$mobile,$nick_name,$avatar){
- $user = User::find()->where(['store_id'=>$store_id,'binding'=>$mobile])->one();
- if (!empty($user)){
- return $user->id;
- }
- $user = new User();
- $user->type = User::USER_TYPE_NORMAL;
- $user->binding = $mobile;
- $user->nickname = $nick_name;
- $user->avatar_url = $avatar;
- $user->username = \Yii::$app->security->generateRandomString();
- $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
- $user->auth_key = \Yii::$app->security->generateRandomString();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->is_delete = User::USER_NOT_DELETE;
- $user->store_id = $store_id;
- $user->parent_id = 0;
- $user->old_parent_id = 0;
- $user->platform = User::USER_FROM_OPERATOR_BACK;
- if (!$user->save()) {
- return 0;
- }
- return $user->id;
- }
- }
|