UserSpread.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/3/27 21:44
  7. */
  8. namespace app\models\user;
  9. use app\models\store\StoreOrder;
  10. use app\models\system\Message;
  11. use crmeb\services\WechatService;
  12. use think\Collection;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use think\facade\Cache;
  17. use crmeb\traits\ModelTrait;
  18. use crmeb\basic\BaseModel;
  19. /**
  20. * TODO 用户消费新增金额明细 model
  21. * Class UserBill
  22. * @package app\models\user
  23. */
  24. class UserSpread extends BaseModel
  25. {
  26. /**
  27. * 数据表主键
  28. * @var string
  29. */
  30. protected $pk = 'id';
  31. /**
  32. * 模型名称
  33. * @var string
  34. */
  35. protected $name = 'user_spread';
  36. use ModelTrait;
  37. /**
  38. * @param $uid
  39. * @param $spread_uid
  40. * @return bool|UserSpread
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. */
  45. public static function setSpread($uid, $spread_uid)
  46. {
  47. if (!$spread_uid) {
  48. return true;
  49. }
  50. $sp = $spread_uid;
  51. $userlist = User::column('uid,spread_uid', 'uid');
  52. while ($sp) {
  53. if ($sp == $uid) {
  54. $spread_uid = 0;
  55. break;
  56. }
  57. $sp = $userlist[$sp]['spread_uid'];
  58. }
  59. $user = User::where('uid', $uid)->find();
  60. $spread_user = User::get($spread_uid);
  61. if ($user['site_id'] != $spread_user['site_id']) return true;
  62. if (!$user || !$spread_user || !$spread_user['is_promoter']) {
  63. return true;
  64. }
  65. //首先获取绑定方式
  66. $bind_type = sys_config('bind_type', 1);
  67. switch ($bind_type) {
  68. case 2:
  69. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  70. return true;
  71. }
  72. return self::create([
  73. 'uid' => $uid,
  74. 'spread_uid' => $spread_uid,
  75. 'lock' => 0,
  76. 'spread_time' => time()
  77. ]);
  78. break;
  79. case 3:
  80. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  81. return true;
  82. }
  83. self::create([
  84. 'uid' => $uid,
  85. 'spread_uid' => $spread_uid,
  86. 'lock' => 0,
  87. 'spread_time' => time()
  88. ]);
  89. $user['spread_uid'] = $spread_uid;
  90. $user['spread_time'] = time();
  91. return $user->save() && User::setUserSpreadCount($spread_uid);
  92. break;
  93. default:
  94. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  95. return true;
  96. }
  97. self::create([
  98. 'uid' => $uid,
  99. 'spread_uid' => $spread_uid,
  100. 'lock' => 1,
  101. 'spread_time' => time()
  102. ]);
  103. $user['spread_uid'] = $spread_uid;
  104. $user['spread_time'] = time();
  105. $res = $user->save() && User::setUserSpreadCount($spread_uid) && Message::addMessage($spread_uid, '恭喜你,[' . $user['nickname'] . '](ID:' . $uid . ')在[' . date('Y-m-d H:i:s') . ']成为你的下线!', 199);
  106. $wuser = WechatUser::where('uid', $spread_uid)->field('nickname,subscribe,openid')->find();
  107. if ($wuser && $wuser['subscribe'] && $wuser['openid']) {
  108. try {
  109. WechatService::staffService()->message('用户' . $uid . '已成功绑定为下级')->to($wuser['openid'])->send();
  110. } catch (\Exception $e) {
  111. }
  112. }
  113. $uper = User::getUserInfo($uid);
  114. while ($uper) {
  115. UserLevel::setLevelComplete($uper['uid']);
  116. $uper = User::getUserInfo($uper['spread_uid']);
  117. }
  118. return $res;
  119. break;
  120. }
  121. }
  122. /**
  123. * @param $uid
  124. * @return bool
  125. * @throws DataNotFoundException
  126. * @throws DbException
  127. * @throws ModelNotFoundException
  128. */
  129. public static function setSpreadSure($uid)
  130. {
  131. //首先获取绑定方式
  132. $bind_type = sys_config('bind_type', 1);
  133. if ($bind_type != 2) {
  134. return true;
  135. }
  136. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  137. return true;
  138. }
  139. $log = self::where('uid', $uid)->order('spread_time', 'desc')->find();
  140. if (!$log) {
  141. return true;
  142. }
  143. $sp = $log['spread_uid'];
  144. $userlist = User::column('uid,spread_uid', 'uid');
  145. while ($sp) {
  146. if ($sp == $uid) {
  147. $log['spread_uid'] = 0;
  148. break;
  149. }
  150. $sp = $userlist[$sp]['spread_uid'];
  151. }
  152. if (!$log['spread_uid']) {
  153. return true;
  154. }
  155. $log['lock'] = 1;
  156. $uper = User::getUserInfo($uid);
  157. $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);
  158. while ($uper) {
  159. UserLevel::setLevelComplete($uper['uid']);
  160. $uper = User::getUserInfo($uper['spread_uid']);
  161. }
  162. return $res;
  163. }
  164. }