| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- /**
- * 描述.
- *
- * @author nj
- * @date 2021-05-18 20:45:46
- */
- namespace App\Repositories;
- use App\Constants\AgentConstant;
- use App\Constants\RedisKeysConstant;
- use App\Events\AgentCreatedEvent;
- use App\Events\AgentUpdatedEvent;
- use App\Exceptions\ApiException;
- use App\Models\Mysql\AgentModel;
- use App\Models\Mysql\ChannelModel;
- use Illuminate\Support\Facades\Redis;
- use KZ2\Laravel\Repositories\Eloquent\Repository;
- use App\Models\Mysql\ManagerModel;
- use Otp\GoogleAuthenticator;
- use function PHPUnit\Framework\isEmpty;
- class AgentRepository extends Repository
- {
- public function model()
- {
- return AgentModel::class;
- }
- /**
- * 生成密码.
- * @param string $plain 明文密码.
- * @param string $salt 盐.
- * @return string
- */
- public function generatePassword($plain, $salt)
- {
- return md5(md5($plain) . $salt);
- }
- /**
- * 是否存在相同用户名.
- * @param string $username 用户名.
- * @param string $excludeAgentUid 排除的用户ID.
- * @return bool
- */
- public function isExistsUsername($username, $excludeAgentUid = '')
- {
- $model = new ManagerModel();
- if (!empty($excludeManagerUid)) {
- $model = $model->where('uid', '!=', $excludeAgentUid);
- }
- $model = $model->where('username', $username);
- return $model->exists();
- }
- /**
- * 创建一个代理.
- * @param array $item
- * @param string $parentUid
- * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
- * @throws ApiException
- */
- public function createOne(array $item, $parentUid = '')
- {
- unset($item['parent_uid'], $item['parent_uids'], $item['child_uids']); // 不信任的字段, 后面全部由业务程序处理.
- $item['username'] = strtolower($item['username']); // 用户名小写.
- $nick = !empty($item['nick']) ? $item['nick'] : $item['username'];
- if (empty(trim($item['password']))) {
- throw new ApiException('密码不能为空');
- }
- $salt = $this->makeSalt();
- $md5Password = $this->createHashPassword($item['password'], $salt);
- $otpScrect = !empty($item['otp_screct']) ? $item['otp_screct'] : GoogleAuthenticator::generateRandom();
- $agentLevel = AgentConstant::TOP_LEVEL; // 代理层级.
- $commissionDiffRatio = AgentConstant::MAX_COMMISSION_RATIO; // 父级的实际提成比例.
- if (!empty($parentUid)) {
- $parentObj = AgentModel::query()->where(['uid' => $parentUid])->first([
- 'uid',
- 'agent_level',
- 'commission_ratio',
- 'jdata',
- 'deduction_switch',
- 'cpa_deduction_rule',
- 'cpa_price'
- ]);
- $agentLevel = $parentObj->agent_level + 1; // 基于上级层级关系,进行+1;
- if ($agentLevel == 6) {
- throw new ApiException('该层级不可创建代理.');
- }
- if ($item['commission_ratio'] > $parentObj->commission_ratio) {
- throw new ApiException('提成比例不能大于父级的提成比例.');
- }
- $commissionDiffRatio = $parentObj->commission_ratio - $item['commission_ratio'];
- //继承上级的设置
- $setarr['jdata'] = $parentObj->jdata;
- $setarr['deduction_switch'] = $parentObj->deduction_switch;
- $setarr['cpa_deduction_rule'] = $parentObj->cpa_deduction_rule;
- $setarr['cpa_price'] = $parentObj->cpa_price;
- $setarr['preferential_order'] = $parentObj->preferential_order;
- } else {
- // 当顶级了,提成比例就是commission_ratio.
- $commissionDiffRatio = $item['commission_ratio'];
- $setarr['jdata'] = $item['jdata'] ?? '';
- $setarr['deduction_switch'] = $item['deduction_switch'] ?? 0;
- $setarr['cpa_deduction_rule'] = $item['cpa_deduction_rule'];
- $setarr['cpa_price'] = $item['cpa_price'];
- }
- $strOwnAppUids = $item['own_app_uids'];
- if (is_array($item['own_app_uids'])) {
- $strOwnAppUids = !empty($item['own_app_uids']) ? implode(',', $item['own_app_uids']) : '';
- }
- if (empty($item['withdraw_amount'])) {
- $item['withdraw_amount'] = 0;
- }
- if (empty($item['withdraw_usdt'])) {
- $item['withdraw_usdt'] = 0;
- }
- $setarr['username'] = $item['username'];
- $setarr['withdraw_pwd'] = $item['withdraw_pwd'];
- $setarr['nick'] = $nick;
- $setarr['password'] = $md5Password;
- $setarr['salt'] = $salt;
- $setarr['status'] = $item['status'];
- $setarr['otp_screct'] = $otpScrect;
- $setarr['agent_level'] = $agentLevel;
- $setarr['parent_uid'] = $parentUid;
- $setarr['own_app_uids'] = $strOwnAppUids;
- $setarr['commission_ratio'] = $item['commission_ratio'];
- $setarr['commission_diff_ratio'] = $commissionDiffRatio;
- $setarr['preferential_order'] = $item['preferential_order'] ?? 0;
- $setarr['withdraw_amount'] = $item['withdraw_amount'];
- $setarr['withdraw_usdt'] = $item['withdraw_usdt'];
- $obj = AgentModel::query()->create($setarr);
- // 触发事件.
- event(new AgentCreatedEvent($obj->uid));
- return $obj;
- }
- /**
- * 修改一个代理.
- * @param array $item
- * @param string $uid
- * @return bool
- * @throws ApiException
- */
- public function updateOne(array $item, string $uid)
- {
- unset($item['uid']);
- $columns = [
- 'id',
- 'uid',
- 'nick',
- 'password',
- 'salt',
- 'otp_secret',
- 'own_app_uids',
- 'commission_ratio',
- 'commission_diff_ratio',
- 'parent_uid',
- 'preferential_order',
- 'cpa_deduction_rule',
- 'cpa_price'
- ];
- $agentModel = new AgentModel();
- $obj = $agentModel->where(['uid' => $uid])->first($columns);
- $oldObj = clone $obj;
- $saveResult = false; // 预先定义保存失败.
- if ($obj->count()) {
- $nick = !empty($item['nick']) ? $item['nick'] : $item['username'];
- $salt = $obj->salt;
- if (!empty($item['own_app_uids'])) {
- $strOwnAppUids = $item['own_app_uids'];
- if (is_array($item['own_app_uids'])) {
- $strOwnAppUids = !empty($item['own_app_uids']) ? implode(',', $item['own_app_uids']) : '';
- }
- $obj->own_app_uids = $strOwnAppUids;
- }
- $parentUid = $obj->parent_uid;
- $lowerRatio = $agentModel->where('parent_uid', $obj['uid'])->orderBy('commission_ratio',
- 'DESC')->first('commission_ratio');
- if (!empty($lowerRatio)) {
- if (!empty($item['commission_ratio'])) {
- if ($item['commission_ratio'] < $lowerRatio->commission_ratio) {
- throw new ApiException('提成比例不能小与下级提成比例.');
- }
- }
- }
- if (!empty($parentUid)) {
- $parentObj = $agentModel->where(['uid' => $parentUid])->first([
- 'uid',
- 'agent_level',
- 'commission_ratio'
- ]);
- if ($item['commission_ratio'] > $parentObj->commission_ratio) {
- throw new ApiException('提成比例不能大于父级的提成比例.');
- }
- $commissionDiffRatio = $parentObj->commission_ratio - $item['commission_ratio'];
- $obj->commission_diff_ratio = $commissionDiffRatio;
- $obj->commission_ratio = $item['commission_ratio'];
- } else {
- // 当顶级了,提成比例就是commission_ratio.
- if (!empty($item['commission_ratio'])) {
- $commissionDiffRatio = $item['commission_ratio'];
- $obj->commission_ratio = $item['commission_ratio'];
- $obj->commission_diff_ratio = $commissionDiffRatio;
- }
- }
- $obj->nick = $nick;
- if (!empty($item['password'])) {
- $md5Password = $this->createHashPassword($item['password'], $salt);
- $obj->password = $md5Password;
- }
- if (!empty($item['withdraw_pwd'])) {
- $obj->withdraw_pwd = $item['withdraw_pwd'];
- }
- if (!empty($item['status'])) {
- $obj->status = $item['status'];
- }
- if (!empty($item['deduction_switch'])) {
- $obj->deduction_switch = $item['deduction_switch'];
- }
- if (!empty($item['jdata'])) {
- $obj->jdata = $item['jdata'];
- }
- if (!empty($item['cpa_deduction_rule'])) {
- $obj->cpa_deduction_rule = $item['cpa_deduction_rule'];
- }
- if (!empty($item['cpa_price'])) {
- $obj->cpa_price = $item['cpa_price'];
- }
- if (!empty($item['preferential_order'])) {
- $obj->preferential_order = $item['preferential_order'];
- }
- if (!empty($item['withdraw_amount'])) {
- $obj->withdraw_amount = $item['withdraw_amount'];
- }
- if (!empty($item['withdraw_usdt'])) {
- $obj->withdraw_usdt = $item['withdraw_usdt'];
- }
- $saveResult = $obj->save();
- // 前后对比提成比例,不相同则执行.
- if ($oldObj->commission_ratio != $obj->commission_ratio) {
- $childs = AgentModel::query()->where(['parent_uid' => $uid])->get([
- 'uid',
- 'parent_uid',
- 'commission_ratio',
- 'commission_diff_ratio'
- ]);
- if ($childs->isNotEmpty()) {
- // 水平纬度,重新计算子用户的提成差.
- foreach ($childs as $item) {
- $diffRatio = $obj->commission_ratio - $item->commission_ratio;
- AgentModel::query()->where('uid', $item->uid)->update(['commission_diff_ratio' => $diffRatio]);
- }
- }
- }
- // 触发更新事件.
- event(new AgentUpdatedEvent($uid));
- }
- return $saveResult;
- }
- /**
- * 代理修改自己的信息
- * @param array $array
- * @param string $uid
- * @return bool
- */
- public function agentEdit(array $array, string $uid)
- {
- $agent = (new AgentModel())->where('uid', $uid)->first();
- if (!empty($array['nick'])) {
- $agent->nick = $array['nick'];
- }
- if (!empty($array['password'])) {
- $md5Password = $this->createHashPassword($array['password'], $agent->salt);
- $agent->password = $md5Password;
- }
- if (!empty($array['withdraw_pwd'])) {
- $agent->withdraw_pwd = $array['withdraw_pwd'];
- }
- return $agent->save();
- }
- /**
- * 创建盐.
- * @return string
- */
- public function makeSalt()
- {
- return random(8);
- }
- /**
- * 创建hash密码.
- * @param string $password
- * @param string $salt
- * @return string
- */
- public function createHashPassword(string $password, string $salt)
- {
- return md5(md5($password) . $salt);
- }
- /**
- * 删除代理扣量缓存
- *
- * @param $deductionType
- */
- public function delAgentRuleCache($agentUid = '')
- {
- $channerList = ChannelModel::query()->where('agent_uid', $agentUid)->get(['mom_app_uid', 'uid']);
- if ($channerList->isEmpty()) {
- return '';
- }
- $cpsKey = RedisKeysConstant::DEDUCTION_RULES_HASH;
- $cpaKey = RedisKeysConstant::DEDUCTION_RULES_HASH_CPA;
- foreach ($channerList as $val) {
- Redis::del($cpsKey . $val->mom_app_uid . $val->uid);
- Redis::del($cpaKey . $val->mom_app_uid . $val->uid);
- }
- }
- }
|