UserSpreadServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\model\user\UserSpread;
  14. use qiniu\basic\BaseServices;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. /**
  19. * Class UserSpreadServices
  20. * @package app\services\user
  21. * @mixin UserSpread
  22. */
  23. class UserSpreadServices extends BaseServices
  24. {
  25. /**
  26. * UserSpreadServices constructor.
  27. * @param UserSpread $model
  28. */
  29. public function __construct(UserSpread $model)
  30. {
  31. $this->model = $model;
  32. }
  33. /**
  34. * 记录推广关系
  35. * @param int $uid
  36. * @param int $spread_uid
  37. * @param int $spread_time
  38. * @return mixed
  39. * @throws DataNotFoundException
  40. * @throws DbException
  41. * @throws ModelNotFoundException
  42. */
  43. public function setSpread(int $uid, int $spread_uid, int $spread_time = 0)
  44. {
  45. if (!$uid || !$spread_uid) return false;
  46. /** @var UserServices $userServices */
  47. $userServices = app()->make(UserServices::class);
  48. $sp = $spread_uid;
  49. $userList = $userServices->getColumn([], 'uid,spread_uid', 'uid');
  50. while ($sp) {
  51. if ($sp == $uid) {
  52. $spread_uid = 0;
  53. break;
  54. }
  55. $sp = $userList[$sp]['spread_uid'] ?? 0;
  56. }
  57. $user = $userServices->getUserInfo($uid);
  58. if (!$userServices->userExist($uid)) {
  59. return false;
  60. }
  61. $spread_user = $userServices->getUserInfo($spread_uid);
  62. if (!$userServices->userExist($spread_uid)) {
  63. return false;
  64. }
  65. if (!$user || !$spread_user || !$spread_user['is_promoter']) {
  66. return true;
  67. }
  68. //首先获取绑定方式
  69. $bind_type = sys_config('bind_type', 1);
  70. if (!$spread_time) $spread_time = time();
  71. switch ($bind_type) {
  72. case 2:
  73. if ($this->be(['uid' => $uid, 'lock' => 1])) {
  74. return true;
  75. }
  76. return $this->create([
  77. 'uid' => $uid,
  78. 'spread_uid' => $spread_uid,
  79. 'lock' => 0,
  80. 'spread_time' => $spread_time
  81. ]);
  82. case 3:
  83. if ($this->be(['uid' => $uid, 'lock' => 1])) {
  84. return true;
  85. }
  86. $this->create([
  87. 'uid' => $uid,
  88. 'spread_uid' => $spread_uid,
  89. 'lock' => 0,
  90. 'spread_time' => $spread_time
  91. ]);
  92. $user['spread_uid'] = $spread_uid;
  93. $user['spread_time'] = $spread_time;
  94. return $user->save() && $userServices->setUserSpreadCount($spread_uid);
  95. break;
  96. default:
  97. if ($this->be(['uid' => $uid, 'lock' => 1])) {
  98. return true;
  99. }
  100. self::create([
  101. 'uid' => $uid,
  102. 'spread_uid' => $spread_uid,
  103. 'lock' => 1,
  104. 'spread_time' => $spread_time
  105. ]);
  106. $user['spread_uid'] = $spread_uid;
  107. $user['spread_time'] = $spread_time;
  108. return $user->save() && $userServices->setUserSpreadCount($spread_uid);
  109. break;
  110. }
  111. }
  112. /**
  113. * 记录推广关系
  114. * @param int $uid
  115. * @param int $spread_uid
  116. * @param int $spread_time
  117. * @return mixed
  118. * @throws DataNotFoundException
  119. * @throws DbException
  120. * @throws ModelNotFoundException
  121. */
  122. public function adminSetSpread(int $uid, int $spread_uid, int $spread_time = 0)
  123. {
  124. if (!$uid) return false;
  125. /** @var UserServices $userServices */
  126. $userServices = app()->make(UserServices::class);
  127. if (!$spread_uid) {
  128. $this->search(['uid' => $uid])->delete();
  129. $user = $userServices->getUserInfo($uid);
  130. $old_spread = $user['spread_uid'];
  131. $user['spread_uid'] = 0;
  132. $user->save();
  133. if ($old_spread > 0) $userServices->setUserSpreadCount($old_spread);
  134. }
  135. $sp = $spread_uid;
  136. $userList = $userServices->getColumn([], 'uid,spread_uid', 'uid');
  137. while ($sp) {
  138. if ($sp == $uid) {
  139. $spread_uid = 0;
  140. break;
  141. }
  142. $sp = $userList[$sp]['spread_uid'] ?? 0;
  143. }
  144. $user = $userServices->getUserInfo($uid);
  145. if (!$userServices->userExist($uid)) {
  146. return false;
  147. }
  148. $spread_user = $userServices->getUserInfo($spread_uid);
  149. if (!$userServices->userExist($spread_uid)) {
  150. return false;
  151. }
  152. if (!$user || !$spread_user || !$spread_user['is_promoter']) {
  153. return true;
  154. }
  155. if ($user['spread_uid'] == $spread_uid) {
  156. return true;
  157. }
  158. $old_spread = $user['spread_uid'];
  159. if (!$spread_time) $spread_time = time();
  160. $this->search(['uid' => $uid])->delete();
  161. //首先获取绑定方式
  162. self::create([
  163. 'uid' => $uid,
  164. 'spread_uid' => $spread_uid,
  165. 'lock' => 1,
  166. 'spread_time' => $spread_time
  167. ]);
  168. $user['spread_uid'] = $spread_uid;
  169. $user['spread_time'] = $spread_time;
  170. $user->save() && $userServices->setUserSpreadCount($spread_uid);
  171. if ($old_spread > 0) $userServices->setUserSpreadCount($old_spread);
  172. return true;
  173. }
  174. /**
  175. * @param $uid
  176. * @return bool
  177. * @throws DataNotFoundException
  178. * @throws DbException
  179. * @throws ModelNotFoundException
  180. */
  181. public function setSpreadSure($uid)
  182. {
  183. //首先获取绑定方式
  184. $bind_type = sys_config('bind_type', 1);
  185. if ($bind_type != 2) {
  186. return true;
  187. }
  188. /** @var UserServices $userServices */
  189. $userServices = app()->make(UserServices::class);
  190. if ($this->be(['uid' => $uid, 'lock' => 1])) {
  191. return true;
  192. }
  193. $log = $this->search(['uid' => $uid])->order('spread_time', 'desc')->find();
  194. if (!$log) {
  195. return true;
  196. }
  197. $sp = $log['spread_uid'];
  198. $userList = $userServices->getColumn([], 'uid,spread_uid', 'uid');
  199. while ($sp) {
  200. if ($sp == $uid) {
  201. $log['spread_uid'] = 0;
  202. break;
  203. }
  204. $sp = $userList[$sp]['spread_uid'];
  205. }
  206. if (!$log['spread_uid']) {
  207. return true;
  208. }
  209. $log['lock'] = 1;
  210. return $log->save() && $userServices->update($uid, ['spread_uid' => $log['spread_uid'], 'spread_time' => $log['spread_time']]) && $userServices->setUserSpreadCount($log['spread_uid']);
  211. }
  212. }