123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php
- namespace app\common\model;
- use app\common\service\DiscountService;
- use think\Cache;
- use think\Db;
- use think\db\Query;
- use think\Model;
- use think\model\Collection;
- use think\model\relation\BelongsToMany;
- use think\model\relation\HasMany;
- /**
- * 会员模型
- * @property int id
- * @property int coupon_num
- * @method static Query|self expired()
- * @property float level
- * @property int gender
- * @property int age
- * @property int county_id
- * @property int city_id
- * @property int province_id
- * @property int admin_id
- * @property string avatar
- * @property string username
- * @property string mobile
- * @property string email
- * @property string bio
- * @property string nickname
- * @property string password
- * @property string salt
- * @property UserInfo userinfo
- * @property boolean wx_authed 是否已微信授权
- * @property boolean is_vip
- * @property boolean has_answered
- * @property boolean is_visitor
- * @property Collection orders
- * @property UserLevel level_obj
- * @property UserLevel level_text
- * @property Admin admin
- */
- class User extends Model
- {
- protected $hidden=[
- 'password',
- 'salt',
- 'status',
- 'token',
- 'updatetime',
- 'loginfailure',
- 'url',
- 'type',
- 'openid',
- 'unionid',
- ];
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'level_text',
- ];
- const LEVEL_COMM=0;
- const LEVEL_ZS=10;
- const LEVEL_BJ=20;
- const LEVEL_JK=30;
- public static $levels=[
- self::LEVEL_COMM=>'普通用户',
- self::LEVEL_ZS=>'钻石',
- self::LEVEL_BJ=>'白金',
- self::LEVEL_JK=>'金卡',
- ];
- const SCORE_EDITINFO=1;
- const SCORE_VIEWVIDEO=2;
- const SCORE_WENDA=3;
- const SCORE_COMMENTVIDEO=4;
- const SCORE_SIGN=5;
- const SCORE_GOODS=6;
- public static $scoreTypes=[
- self::SCORE_EDITINFO=>'完善资料',
- self::SCORE_VIEWVIDEO=>'看视频',
- self::SCORE_WENDA=>'答题',
- self::SCORE_COMMENTVIDEO=>'评论',
- self::SCORE_SIGN=>'签到',
- self::SCORE_GOODS=>'兑换商品',
- ];
- /**
- * 获取个人URL
- * @param string $value
- * @param array $data
- * @return string
- */
- public function getUrlAttr($value, $data)
- {
- return fullUrl("/u/" . $data['id']);
- }
- public function getLevelTextAttr($value, $data)
- {
- $names=self::getLevels();
- return $names[$data['level']]??'-';
- }
- /**
- * @
- */
- public static function getLevels()
- {
- //return self::$levels;
- $levels=UserLevel::column('name','id');
- $levels[0]='普通用户';
- $obj=[];
- foreach ($levels as $id=>$name){
- $obj[$id]=$name;
- }
- return $obj;
- }
- /**
- * 获取会员的组别
- */
- public function getGroupAttr($value, $data)
- {
- return UserGroup::get($data['group_id']);
- }
- /**
- * 变更会员余额
- * @param float $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function money($money, $user_id, $type,$memo='',$extra=[],$changeMoney=true)
- {
- $user = self::lock(true)->find($user_id);
- if($changeMoney && !$user){
- throw_user('用户不存在:'.$user_id);
- }
- if ($money != 0) {
- if($changeMoney){
- $before = $user->money;
- //$after = $user->money + $money;
- $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
- if($after<0){
- throw_user("余额不足");
- }
- //更新会员信息
- $user->save(['money' => $after]);
- }
- }
- //写入日志
- MoneyLog::create(array_merge([
- 'user_id' => $user_id,
- 'type'=>$type,
- 'money' => $money,
- 'before' => $before??$user->money??0,
- 'after' => $after??$user->money??0,
- 'memo' => $memo,
- ],$extra));
- }
- /**
- * 变更会员积分
- * @param int $score 积分
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function score($score, $user_id, $memo,$field,$type,$extra=[])
- {
- $user = self::lock(true)->find($user_id);
- if ($user && $score != 0) {
- $before = $user->$field;
- $after = $user->$field + $score;
- if($after<0){
- throw_user('积分不足');
- }
- //更新会员信息
- $user->save([$field => $after]);
- //写入日志
- ScoreLog::create(array_merge([
- 'field' =>$field,
- 'user_id' => $user_id,
- 'type'=>$type,
- 'score' => $score,
- 'before' => $before,
- 'after' => $after,
- 'memo' => $memo,
- ],$extra));
- }
- }
- /**
- *@return HasMany|UserAddress
- */
- public function address(){
- return $this->hasMany(UserAddress::class);
- }
- public function userinfo(){
- return $this->hasOne(UserInfo::class);
- }
- public function payment(){
- return $this->hasMany(Payment::class);
- }
- public function levelObj(){
- return $this->belongsTo(UserLevel::class,'level');
- }
- public function comments(){
- return $this->hasMany(Comment::class);
- }
- /**
- *@return HasMany|Favourite
- */
- public function favourite(){
- return $this->hasMany(Favourite::class)->where('fav_type','goods');
- }
- public function feedback(){
- return $this->hasMany(Feedback::class);
- }
- public function moneylog(){
- return $this->hasMany(MoneyLog::class);
- }
- public function tax(){
- return $this->hasMany(UserTax::class);
- }
- /**
- * @return Orders|HasMany
- */
- public function orders(){
- return $this->hasMany(Orders::class);
- }
- /**
- * @return Refund|HasMany
- */
- public function refund(){
- return $this->hasMany(Refund::class);
- }
- public function programmes(){
- return $this->hasMany(Programme::class);
- }
- /**
- * @return OrderInfo|HasMany
- */
- public function orderInfo(){
- return $this->hasMany(OrderInfo::class);
- }
- /**
- *@return ScoreLog|HasMany
- */
- public function scorelog(){
- return $this->hasMany(ScoreLog::class);
- }
- public function cart(){
- return $this->hasMany(GoodsCart::class);
- }
- /**
- * @return UserCoupon|HasMany
- */
- public function coupon(){
- return $this->hasMany(UserCoupon::class);
- }
- public function categoryView(){
- return $this->hasMany(UserCategoryView::class);
- }
- public function loginRange(){
- return $this->hasMany(UserLoginRange::class);
- }
- public static function recharge($params,Payment $payment){
- self::money($payment['amount'],$payment['uer_id'],MoneyLog::TYPE_CHARGE,'充值');
- }
- public function getShowName(){
- if($this['username']){
- return $this['username'];
- }
- return $this['mobile'];
- }
- public function verification(){
- return $this->hasOne(UserVerification::class);
- }
- /**
- * 计算等级折扣金额
- * @param $bool bool 是否计算
- * @param float $amount
- */
- public function getLevelDiscount($bool,$amount){
- if(!$bool){
- return 0;
- }
- $radio=DiscountService::getById($this->level);
- if($radio>10){
- $radio=10;
- }
- if($radio<=0){
- $radio=10;
- }
- $sub=10-$radio;
- return bcmul($amount,$sub/10);
- }
- /** scope and expired */
- protected static function init()
- {
- self::beforeWrite(function (self $user){
- $data=$user->getChangedData();
- if(empty($user['level_expire'])){
- $user['level_expire']=null;
- }
- if(empty($user['jointime'])){
- $user['jointime']=time();
- }
- if(empty($user['email'])){
- $user['email']=null;
- }
- if(empty($user['openid'])){
- $user['openid']=null;
- }
- if(empty($user['admin_id'])){
- $user['admin_id']=null;
- }
- });
- self::afterDelete(function (self $user){
- });
- self::afterInsert(function (self $user){
- $user->userinfo()->save([]);
- });
- }
- public function scopeExpired(Query $query){
- $query
- ->whereBetween('level_close_at',[0,time()])
- ->where('level','>',0);
- }
- public function scopeType(Query $query,$type){
- $query->where('type',$type);
- }
- public function scopeUser(Query $query,$level){
- $query->where($this->getTable().'.level',$level);
- }
- /** attr */
- #获取地区
- public function appendArea(){
- $this->city_name=Area::where('id',$this->county_id?:0)->cache(true)->value('mergename','');
- }
- public function province(){
- return $this->belongsTo(Area::class,'province_id')->cache(true);
- }
- public function admin(){
- return $this->belongsTo(Admin::class);
- }
- public function city(){
- return $this->belongsTo(Area::class,'city_id')->cache(true);
- }
- public function county(){
- return $this->belongsTo(Area::class,'county_id')->cache(true);
- }
- protected function getAvatarAttr($a){
- if(!$a){
- return request()->domain().'/assets/img/avatar.png';
- }
- return $a;
- }
- }
|