User.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\wap\model\user;
  8. use basic\ModelBasic;
  9. use service\SystemConfigService;
  10. use think\Request;
  11. use think\response\Redirect;
  12. use think\Session;
  13. use think\Url;
  14. use traits\ModelTrait;
  15. class User extends ModelBasic
  16. {
  17. use ModelTrait;
  18. protected $insert = ['add_time','add_ip','last_time','last_ip'];
  19. protected function setAddTimeAttr($value)
  20. {
  21. return time();
  22. }
  23. protected function setAddIpAttr($value)
  24. {
  25. return Request::instance()->ip();
  26. }
  27. protected function setLastTimeAttr($value)
  28. {
  29. return time();
  30. }
  31. protected function setLastIpAttr($value)
  32. {
  33. return Request::instance()->ip();
  34. }
  35. public static function setWechatUser($wechatUser,$spread_uid = 0)
  36. {
  37. return self::set([
  38. 'account'=>'wx'.$wechatUser['uid'].time(),
  39. 'pwd'=>md5(123456),
  40. 'nickname'=>$wechatUser['nickname']?:'',
  41. 'avatar'=>$wechatUser['headimgurl']?:'',
  42. 'spread_uid'=>$spread_uid,
  43. 'uid'=>$wechatUser['uid'],
  44. 'user_type'=>'wechat'
  45. ]);
  46. }
  47. public static function updateWechatUser($wechatUser,$uid)
  48. {
  49. return self::edit([
  50. 'nickname'=>$wechatUser['nickname']?:'',
  51. 'avatar'=>$wechatUser['headimgurl']?:''
  52. ],$uid,'uid');
  53. }
  54. public static function setSpreadUid($uid,$spreadUid)
  55. {
  56. return self::where('uid',$uid)->update(['spread_uid'=>$spreadUid]);
  57. }
  58. public static function getUserInfo($uid)
  59. {
  60. $userInfo = self::where('uid',$uid)->find();
  61. if(!$userInfo) exception('读取用户信息失败!');
  62. return $userInfo->toArray();
  63. }
  64. /**
  65. * 获得当前登陆用户UID
  66. * @return int $uid
  67. */
  68. public static function getActiveUid()
  69. {
  70. $uid = null;
  71. if(Session::has('loginUid','wap')) $uid = Session::get('loginUid','wap');
  72. if(!$uid && Session::has('loginOpenid','wap') && ($openid = Session::get('loginOpenid','wap')))
  73. $uid = WechatUser::openidToUid($openid);
  74. if(!$uid) exit(exception('请登陆!'));
  75. return $uid;
  76. }
  77. public static function backOrderBrokerage($orderInfo)
  78. {
  79. $userInfo = User::getUserInfo($orderInfo['uid']);
  80. if(!$userInfo || !$userInfo['spread_uid']) return true;
  81. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ? : 1;//获取后台分销类型
  82. if($storeBrokerageStatu == 1){
  83. if(!User::be(['uid'=>$userInfo['spread_uid'],'is_promoter'=>1])) return true;
  84. }
  85. $brokerageRatio = (SystemConfigService::get('store_brokerage_ratio') ?: 0)/100;
  86. if($brokerageRatio <= 0) return true;
  87. $cost = isset($orderInfo['cost']) ? $orderInfo['cost'] : 0;//成本价
  88. $brokeragePrice = bcmul(bcsub($orderInfo['pay_price'],$cost,2),$brokerageRatio,2);
  89. if($brokeragePrice <= 0) return true;
  90. $mark = $userInfo['nickname'].'成功消费'.floatval($orderInfo['pay_price']).'元,奖励推广佣金'.floatval($brokeragePrice);
  91. self::beginTrans();
  92. $res1 = UserBill::income('获得推广佣金',$userInfo['spread_uid'],'now_money','brokerage',$brokeragePrice,$orderInfo['id'],0,$mark);
  93. $res2 = self::bcInc($userInfo['spread_uid'],'now_money',$brokeragePrice,'uid');
  94. $res = $res1 && $res2;
  95. self::checkTrans($res);
  96. return $res;
  97. }
  98. }