AgentService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AgentModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 代理管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Api
  20. */
  21. class AgentService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new AgentModel();
  31. }
  32. /**
  33. * 静态入口
  34. * @return static|null
  35. */
  36. public static function make()
  37. {
  38. if (!self::$instance) {
  39. self::$instance = (new static());
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * @param $params
  45. * @param int $pageSize
  46. * @return array
  47. */
  48. public function getDataList($params, $pageSize = 15)
  49. {
  50. $where = ['a.mark' => 1];
  51. $status = isset($params['status']) ? $params['status'] : 0;
  52. if ($status > 0) {
  53. $where['a.status'] = $status;
  54. }
  55. $list = $this->model->with(['user'])->from('agent as a')
  56. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  57. ->where($where)
  58. ->where(function ($query) use ($params) {
  59. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  60. if ($keyword) {
  61. $query->where('a.real_name', 'like', "%{$keyword}%")
  62. ->orWhere('a.phone', 'like', "%{$keyword}%");
  63. }
  64. })
  65. ->select(['a.*'])
  66. ->orderBy('a.create_time', 'desc')
  67. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  68. $list = $list ? $list->toArray() : [];
  69. if ($list) {
  70. foreach ($list['data'] as &$item) {
  71. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  72. }
  73. }
  74. return [
  75. 'pageSize' => $pageSize,
  76. 'total' => isset($list['total']) ? $list['total'] : 0,
  77. 'list' => isset($list['data']) ? $list['data'] : []
  78. ];
  79. }
  80. /**
  81. * 申请
  82. * @param $userId
  83. * @param $params
  84. * @return mixed
  85. */
  86. public function apply($userId, $params)
  87. {
  88. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  89. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  90. $idcard = isset($params['idcard']) && $params['idcard']? trim($params['idcard']) : '';
  91. $data = [
  92. 'user_id' => $userId,
  93. 'real_name' => $realname,
  94. 'phone' => $phone,
  95. 'idcard' => $idcard,
  96. 'income' => 0,
  97. 'balance' => 0,
  98. 'withdraw_total' => 0,
  99. 'order_count' => 0,
  100. 'confirm_remark' => '',
  101. 'create_time' => time(),
  102. 'update_time' => time(),
  103. 'status' => 2,
  104. 'mark' => 1,
  105. ];
  106. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  107. $this->model->where(['id'=>$id])->update($data);
  108. }else{
  109. if (!$id = $this->model->insertGetId($data)) {
  110. $this->error = '代理申请失败';
  111. return false;
  112. }
  113. }
  114. RedisService::keyDel("caches:members:info_*");
  115. RedisService::keyDel("caches:agents:info*");
  116. $this->error = '代理申请成功,请耐心等候审核~';
  117. return ['id' => $id];
  118. }
  119. /**
  120. * 代理信息
  121. * @param $id
  122. * @param $userId
  123. * @return array|mixed
  124. */
  125. public function getInfo($uid)
  126. {
  127. $cacheKey = "caches:agents:info_{$uid}";
  128. $info = RedisService::get($cacheKey);
  129. if($info){
  130. return $info;
  131. }
  132. $where = ['user_id'=> $uid,'mark'=>1];
  133. $info = $this->model->with(['user'])->where($where)->first();
  134. $info = $info? $info->toArray() : [];
  135. if($info){
  136. RedisService::set($cacheKey, $info, rand(5, 10));
  137. }
  138. return $info;
  139. }
  140. }