AtlasController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\bonus\CalcCache;
  10. use common\helpers\Cache;
  11. use common\helpers\user\Info;
  12. use common\models\Period;
  13. use common\models\StorePerfLog;
  14. use common\models\UserNetwork;
  15. use common\models\UserNetworkHidden;
  16. use frontendApi\modules\v1\models\Relation;
  17. use Yii;
  18. use common\models\User;
  19. use yii\web\HttpException;
  20. class AtlasController extends BaseController {
  21. public $modelClass = User::class;
  22. const LOOP_FINISH = 1;
  23. const LOOP_CONTINUE = 2;
  24. private $_periodNum = 0;
  25. public function behaviors() {
  26. $behaviors = parent::behaviors();
  27. return $behaviors;
  28. }
  29. /**
  30. * 主点位信息
  31. * @return mixed
  32. * @throws \yii\base\Exception
  33. * @throws \yii\db\Exception
  34. * @throws \yii\web\HttpException
  35. */
  36. public function actionMainUserInfo() {
  37. $userName = Yii::$app->request->get('userName', '');
  38. $periodNum = Yii::$app->request->get('periodNum', null);
  39. if ($userName !== '') {
  40. if (!$userId = Info::getUserIdByUserName($userName)) {
  41. return static::notice('会员不存在', 400);
  42. }
  43. $topUid = \Yii::$app->user->id;
  44. $userNetInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'PARENT_UIDS');
  45. $parentUidsArr = explode(',', $userNetInfo['PARENT_UIDS']);
  46. if(!in_array($topUid,$parentUidsArr)){
  47. return static::notice('会员与当前用户不再同一安置网络内', 400);
  48. }
  49. } else {
  50. $userId = \Yii::$app->user->id;
  51. }
  52. $baseInfo = Info::baseInfo($userId, $periodNum);
  53. $decLevelConfig = Cache::getDecLevelConfig();
  54. $empLevelConfig = Cache::getEmpLevelConfig();
  55. return static::notice([
  56. [
  57. 'TOP_RELATION_DEEP' => $baseInfo['RELATION_DEEP'],
  58. 'TOP_NETWORK_DEEP' => $baseInfo['NETWORK_DEEP'],
  59. 'USER_ID' => $userId,
  60. 'USER_NAME' => $baseInfo['USER_NAME'],
  61. 'REAL_NAME' => $baseInfo['REAL_NAME'],
  62. 'PERIOD_AT' => $baseInfo['PERIOD_AT'],
  63. 'DEC_LV_NAME' => $decLevelConfig[$baseInfo['DEC_LV']]['LEVEL_NAME'],
  64. 'EMP_LV_NAME' => $empLevelConfig[$baseInfo['EMP_LV']]['LEVEL_NAME'],
  65. 'leaf' => false,
  66. 'icon' => 'el-icon-user-solid',
  67. 'children' => null,
  68. 'isExpanded' => false,
  69. 'displayNone' => 'display-none',
  70. 'className' => 'first-node',
  71. 'listPeriodNum' => $periodNum
  72. ],
  73. ]);
  74. }
  75. /**
  76. * 安置网络图
  77. * @return mixed
  78. * @throws \yii\web\HttpException
  79. */
  80. public function actionNetwork() {
  81. $userId = Yii::$app->request->get('id', \Yii::$app->user->id);
  82. $periodNum = Yii::$app->request->get('periodNum', null);
  83. $deep = Yii::$app->request->get('deep', 5);
  84. $myUserId = \Yii::$app->user->id;
  85. $hiddenUser = UserNetworkHidden::fetchHiddenUser($myUserId);
  86. if ($deep > 20) {
  87. return static::notice('最多查看会员的前20层子会员', 400);
  88. }
  89. $allData = UserNetwork::getChildrenWithDeepAndLayer($userId, $deep, 1, $periodNum, $hiddenUser);
  90. return static::notice(['allData' => $allData, 'periodNum' => $periodNum]);
  91. }
  92. /**
  93. * 安置网络列表
  94. * @return mixed
  95. * @throws \yii\base\Exception
  96. * @throws \yii\db\Exception
  97. * @throws \yii\web\HttpException
  98. */
  99. public function actionNetworkList() {
  100. $userName = Yii::$app->request->get('userName');
  101. $deep = Yii::$app->request->get('deep', 5);
  102. $periodNum = Yii::$app->request->get('periodNum', null);
  103. if (!$userName) {
  104. $userId = \Yii::$app->user->id;
  105. } else {
  106. if (!$userId = Info::getUserIdByUserName($userName)) {
  107. return static::notice('会员不存在', 400);
  108. }
  109. $topUid = \Yii::$app->user->id;
  110. $userNetInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'PARENT_UIDS');
  111. $parentUidsArr = explode(',', $userNetInfo['PARENT_UIDS']);
  112. if(!in_array($topUid,$parentUidsArr)){
  113. return static::notice('会员与当前用户不再同一安置网络内', 400);
  114. }
  115. }
  116. $period = Period::instance();
  117. $mainUserInfo = Info::baseInfo($userId, $periodNum);
  118. $decLevelConfig = Cache::getDecLevelConfig();
  119. $empLevelConfig = Cache::getEmpLevelConfig();
  120. $data = UserNetwork::getChildrenInDeepFromPeriodWithPage($userId, $deep, $periodNum, 1 ? ['pageSize' => 10] : []);
  121. foreach($data['list'] as $key=>$value){
  122. $userBaseInfo = Info::baseInfo($value['USER_ID'], $periodNum);
  123. $data['list'][$key]['LIST_PERIOD_NUM'] = $periodNum?$periodNum:$period->getNowPeriodNum();
  124. $data['list'][$key]['COUNT_DEEP'] = (int)($userBaseInfo['NETWORK_DEEP']);
  125. $data['list'][$key]['USER_NAME'] = $userBaseInfo['USER_NAME'];
  126. $data['list'][$key]['SEE_USER_NAME'] = $userBaseInfo['USER_NAME'];
  127. $data['list'][$key]['REAL_NAME'] = $userBaseInfo['REAL_NAME'];
  128. $data['list'][$key]['PERIOD_AT'] = $userBaseInfo['PERIOD_AT'];
  129. $data['list'][$key]['DEC_LV_NAME'] = $decLevelConfig[$userBaseInfo['DEC_LV']]['LEVEL_NAME'];
  130. $data['list'][$key]['EMP_LV_NAME'] = $empLevelConfig[$userBaseInfo['EMP_LV']]['LEVEL_NAME'];
  131. }
  132. $data['periodNum'] = $periodNum;
  133. $data['listTopDeep'] = $mainUserInfo['NETWORK_DEEP'];
  134. return static::notice($data);
  135. }
  136. /**
  137. * 获取期数
  138. * @return mixed
  139. * @throws \yii\web\HttpException
  140. */
  141. public function getPeriod(){
  142. $period = Period::instance();
  143. $periodNum = $period->getNowPeriodNum();
  144. return static::notice([
  145. 'periodNum' => $periodNum,
  146. ]);
  147. }
  148. /**
  149. * 查询推荐网络的指定节点.
  150. * @return void
  151. * @throws HttpException
  152. * @throws \Exception
  153. */
  154. public function actionScourRelation()
  155. {
  156. // 发起人
  157. $initiator = Yii::$app->request->get('initiator');
  158. // 接受者
  159. $recipient = Yii::$app->request->get('recipient');
  160. // 期数
  161. $this->_periodNum = Period::instance()->getNowPeriodNum();
  162. // 发起人
  163. $initiatorInfo = Info::getBaseUserByUserName($initiator);
  164. if (!$initiatorInfo) {
  165. return static::notice('会员不存在', 400);
  166. }
  167. // 发起人是否报单中心
  168. $initiatorIsDec = $initiatorInfo['IS_DEC'] == 1;
  169. $initiatorId = $initiatorInfo['ID'];
  170. // 接受者
  171. $recipientInfo = Info::getBaseUserByUserName($recipient);
  172. if (!$recipientInfo) {
  173. return static::notice('会员不存在', 400);
  174. }
  175. // 发起人是否报单中心
  176. $recipientIsDec = $recipientInfo['IS_DEC'] == 1;
  177. $recipientId = $recipientInfo['ID'];
  178. // 1. 如果发起人和接受者都是报单中心,则转账无限制
  179. if (($initiatorIsDec == 1) && ($recipientIsDec == 1)) {
  180. return static::notice(['allowable' => true]);
  181. }
  182. // 2. 如果发起人是普通会员,则只能转给自己推荐网的上级某个节点
  183. $relation = new Relation();
  184. if (!$initiatorIsDec) {
  185. // 判断接受者是否是自己的推荐网上级某个节点
  186. $allowable = $relation->loopRelationParentDo($initiatorId, function ($parent) use ($recipientId) {
  187. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
  188. if ($parentUser['ID'] == $recipientId) {
  189. return self::LOOP_FINISH;
  190. }
  191. unset($parent);
  192. });
  193. return static::notice(['allowable' => $allowable]);
  194. }
  195. // 3. 如果发起人是报单中心或者普通会员, 则可以转账给自己的推荐网下级
  196. // 判断接受者是否是自己的推荐网上级某个节点
  197. $allowable = $relation->loopRelationParentDo($recipientId, function ($parent) use ($initiatorId) {
  198. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
  199. if ($parentUser['ID'] == $initiatorId) {
  200. return self::LOOP_FINISH;
  201. }
  202. unset($parent);
  203. });
  204. return static::notice(['allowable' => $allowable]);
  205. }
  206. }