* Date: 2019/3/27 21:44 */ namespace app\models\user; use app\models\store\StoreOrder; use app\models\system\Message; use crmeb\services\WechatService; use think\Collection; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\facade\Cache; use crmeb\traits\ModelTrait; use crmeb\basic\BaseModel; /** * TODO 用户消费新增金额明细 model * Class UserBill * @package app\models\user */ class UserSpread extends BaseModel { /** * 数据表主键 * @var string */ protected $pk = 'id'; /** * 模型名称 * @var string */ protected $name = 'user_spread'; use ModelTrait; /** * @param $uid * @param $spread_uid * @return bool|UserSpread * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function setSpread($uid, $spread_uid) { if (!$spread_uid) { return true; } $sp = $spread_uid; $userlist = User::column('uid,spread_uid', 'uid'); while ($sp) { if ($sp == $uid) { $spread_uid = 0; break; } $sp = $userlist[$sp]['spread_uid']; } $user = User::where('uid', $uid)->find(); $spread_user = User::get($spread_uid); if ($user['site_id'] != $spread_user['site_id']) return true; if (!$user || !$spread_user || !$spread_user['is_promoter']) { return true; } //首先获取绑定方式 $bind_type = sys_config('bind_type', 1); switch ($bind_type) { case 2: if (self::where('uid', $uid)->where('lock', 1)->find()) { return true; } return self::create([ 'uid' => $uid, 'spread_uid' => $spread_uid, 'lock' => 0, 'spread_time' => time() ]); break; case 3: if (self::where('uid', $uid)->where('lock', 1)->find()) { return true; } self::create([ 'uid' => $uid, 'spread_uid' => $spread_uid, 'lock' => 0, 'spread_time' => time() ]); $user['spread_uid'] = $spread_uid; $user['spread_time'] = time(); return $user->save() && User::setUserSpreadCount($spread_uid); break; default: if (self::where('uid', $uid)->where('lock', 1)->find()) { return true; } self::create([ 'uid' => $uid, 'spread_uid' => $spread_uid, 'lock' => 1, 'spread_time' => time() ]); $user['spread_uid'] = $spread_uid; $user['spread_time'] = time(); $res = $user->save() && User::setUserSpreadCount($spread_uid) && Message::addMessage($spread_uid, '恭喜你,[' . $user['nickname'] . '](ID:' . $uid . ')在[' . date('Y-m-d H:i:s') . ']成为你的下线!', 199); $wuser = WechatUser::where('uid', $spread_uid)->field('nickname,subscribe,openid')->find(); if ($wuser && $wuser['subscribe'] && $wuser['openid']) { try { WechatService::staffService()->message('用户' . $uid . '已成功绑定为下级')->to($wuser['openid'])->send(); } catch (\Exception $e) { } } $uper = User::getUserInfo($uid); while ($uper) { UserLevel::setLevelComplete($uper['uid']); $uper = User::getUserInfo($uper['spread_uid']); } return $res; break; } } /** * @param $uid * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function setSpreadSure($uid) { //首先获取绑定方式 $bind_type = sys_config('bind_type', 1); if ($bind_type != 2) { return true; } if (self::where('uid', $uid)->where('lock', 1)->find()) { return true; } $log = self::where('uid', $uid)->order('spread_time', 'desc')->find(); if (!$log) { return true; } $sp = $log['spread_uid']; $userlist = User::column('uid,spread_uid', 'uid'); while ($sp) { if ($sp == $uid) { $log['spread_uid'] = 0; break; } $sp = $userlist[$sp]['spread_uid']; } if (!$log['spread_uid']) { return true; } $log['lock'] = 1; $uper = User::getUserInfo($uid); $res = $log->save() && User::where('uid', $uid)->update(['spread_uid' => $log['spread_uid'], 'spread_time' => $log['spread_time']]) && User::setUserSpreadCount($log['spread_uid']) && User::setUserSpreadCount($log['spread_uid']) && Message::addMessage($log['spread_uid'], '恭喜你,[' . $uper['nickname'] . '](ID:' . $uid . ')在[' . date('Y-m-d H:i:s') . ']成为你的下线!', 199); while ($uper) { UserLevel::setLevelComplete($uper['uid']); $uper = User::getUserInfo($uper['spread_uid']); } return $res; } }