UserController.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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\Email;
  12. use common\helpers\Form;
  13. use common\helpers\LoggerTool;
  14. use common\helpers\Tool;
  15. use common\helpers\user\Info;
  16. use common\models\BaUser;
  17. use common\models\Config;
  18. use common\models\Countries;
  19. use common\models\Currency;
  20. use common\models\CurrencyConversions;
  21. use common\models\DeclarationLevel;
  22. use common\models\DeclarationPackage;
  23. use common\models\DecOrder;
  24. use common\models\forms\ApproachDeclarationLoopForm;
  25. use common\models\forms\BaApproachDeclarationLoopForm;
  26. use common\models\forms\ApproachDeclarationUpgradeForm;
  27. use common\models\forms\BaUserForm;
  28. use common\models\forms\DeclarationForm;
  29. use common\models\forms\BaDeclarationForm;
  30. use common\models\forms\DeclarationLoopForm;
  31. use common\models\forms\BaDeclarationLoopForm;
  32. use common\models\forms\UploadForm;
  33. use common\models\forms\UserBindForm;
  34. use common\models\forms\UserForm;
  35. use common\models\Instalment;
  36. use common\models\OpenBank;
  37. use common\models\OrderDec;
  38. use common\models\OriginDecPv;
  39. use common\models\Period;
  40. use common\models\ReceiveAddress;
  41. use common\models\Region;
  42. use common\models\ShopGoods;
  43. use common\models\ShopGoodsNature;
  44. use common\services\GuestDeclarationOrderService;
  45. use common\models\UpgradeType;
  46. use common\models\User;
  47. use common\models\UserBind;
  48. use common\models\UserBonus;
  49. use common\models\UserInfo;
  50. use common\models\BaUserInfo;
  51. use common\models\UserNetwork;
  52. use common\models\forms\DeclarationUpgradeForm;
  53. use common\models\UserPerformance;
  54. use common\models\UserWallet;
  55. use frontendApi\modules\v1\models\Relation;
  56. use Yii;
  57. use yii\db\Exception;
  58. use yii\web\UploadedFile;
  59. class UserController extends BaseController {
  60. public $modelClass = UserInfo::class;
  61. const LOOP_FINISH = 1;
  62. /**
  63. * 会员资料
  64. * @return mixed
  65. * @throws \yii\web\HttpException
  66. */
  67. public function actionIndex() {
  68. $allNation = \Yii::$app->params['nation'];
  69. $allOpenBank = OpenBank::findAllAsArray('STATUS=1');
  70. $data['allNation'] = $allNation;
  71. $data['allOpenBank'] = $allOpenBank;
  72. $data['userInfo'] = User::getEnCodeInfo(\Yii::$app->user->id);
  73. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  74. return static::notice($data);
  75. }
  76. /**
  77. * 编辑会员资料
  78. * @return mixed
  79. * @throws \yii\web\HttpException
  80. */
  81. public function actionEdit() {
  82. if(\Yii::$app->request->isPost){
  83. $form = new UserForm();
  84. $post = \Yii::$app->request->post();
  85. $form->scenario = 'modifyProfile';
  86. if($form->load($post, '') && $result = $form->modifyProfile()){
  87. return static::notice(Yii::t('app', 'personalDataModifiedSuccessfully'), 400);
  88. } else {
  89. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  90. }
  91. }
  92. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  93. }
  94. /**
  95. * 修改登录密码
  96. */
  97. public function actionPassword(){
  98. if(\Yii::$app->request->isPost){
  99. $form = new UserForm();
  100. $form->scenario = 'modifyPassword';
  101. $post = \Yii::$app->request->post();
  102. if($form->load($post, '') && $result = $form->modifyPassword()){
  103. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  104. } else {
  105. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  106. }
  107. }
  108. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  109. }
  110. /**
  111. * 修改支付密码
  112. */
  113. public function actionPayPassword(){
  114. if(\Yii::$app->request->isPost){
  115. $form = new UserForm();
  116. $form->scenario = 'modifyPasswordPay';
  117. $post = \Yii::$app->request->post();
  118. $form->userId = \Yii::$app->user->id;
  119. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  120. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'));
  121. } else {
  122. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  123. }
  124. }
  125. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  126. }
  127. /**
  128. * 直推会员列表
  129. * @return mixed
  130. * @throws \yii\web\HttpException
  131. */
  132. public function actionRecUser() {
  133. $allData = UserInfo::lists('AND REC_UID=:REC_UID', [':REC_UID' => \Yii::$app->user->id], ['useSlaves' => true, 'select'=>'USER_ID,CREATED_AT']);
  134. foreach ($allData['list'] as $key => $data) {
  135. $userBaseInfo = User::getEnCodeInfo($data['USER_ID']);
  136. $userBaseInfo['NATION'] = \Yii::$app->params['nation'][$userBaseInfo['NATION']]['name'] ?? '';
  137. $allData['list'][$key]['BASE_INFO'] = $userBaseInfo;
  138. }
  139. return static::notice($allData);
  140. }
  141. /**
  142. * 当前用户的安置网络下线统计
  143. * @return mixed
  144. */
  145. public function actionNetworkStats() {
  146. $userId = Yii::$app->user->id;
  147. $networkInfo = UserNetwork::find()
  148. ->select(['LOCATION_TAG', 'TOP_DEEP'])
  149. ->where(['USER_ID' => $userId])
  150. ->asArray()
  151. ->one();
  152. $descendants = [];
  153. if ($networkInfo) {
  154. $descendants = UserNetwork::find()
  155. ->alias('N')
  156. ->select(['N.USER_ID', 'N.PARENT_UID', 'N.TOP_DEEP'])
  157. ->innerJoin(User::tableName() . ' U', 'U.ID = N.USER_ID')
  158. ->where('N.LOCATION_TAG LIKE :LOCATION_TAG', [':LOCATION_TAG' => $networkInfo['LOCATION_TAG'] . '%'])
  159. ->andWhere(['>', 'N.TOP_DEEP', $networkInfo['TOP_DEEP']])
  160. ->andWhere(['U.DELETED' => 0])
  161. ->asArray()
  162. ->all();
  163. }
  164. $userIds = array_column($descendants, 'USER_ID');
  165. $pcv = 0;
  166. if ($userIds) {
  167. $pcv += (float) DecOrder::find()->where(['TO_USER_ID' => $userIds, 'IS_DEL' => 0])->sum('DEC_PV');
  168. $pcv += (float) OrderDec::find()->where(['USER_ID' => $userIds, 'IS_DELETE' => 0])->sum('PAY_PV');
  169. $pcv += (float) OriginDecPv::find()->where(['USER_ID' => $userIds])->sum('DEC_PV');
  170. }
  171. $maxDeep = $descendants ? max(array_column($descendants, 'TOP_DEEP')) : $networkInfo['TOP_DEEP'] ?? 0;
  172. return static::notice([
  173. 'DOWNLINE_COUNT' => count($descendants),
  174. 'DIRECT_DOWNLINE_COUNT' => count(array_filter($descendants, function ($downline) use ($userId) {
  175. return $downline['PARENT_UID'] === $userId;
  176. })),
  177. 'MAX_DOWNLINE_DEEP' => max(0, (int) $maxDeep - (int) ($networkInfo['TOP_DEEP'] ?? 0)),
  178. 'DOWNLINE_PCV' => $pcv,
  179. ]);
  180. }
  181. /**
  182. * 上传身份证
  183. * @return mixed
  184. * @throws \yii\base\Exception
  185. * @throws \yii\web\HttpException
  186. */
  187. public function actionIdCard() {
  188. if (\Yii::$app->request->isPost) {
  189. $formModel = new UploadForm();
  190. $formModel->scenario = 'idCardFront';
  191. $formModel->file = UploadedFile::getInstanceByName('file');
  192. //$formModel->token = \Yii::$app->request->post('uploadToken');
  193. $formModel->token = \Yii::$app->request->request('uploadToken');
  194. if ($formModel->file && $formModel->upload()) {
  195. return static::notice(Yii::t('app', 'successfully'));
  196. } else {
  197. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  198. }
  199. }
  200. // 查看该用户是否已经上传过身份证
  201. $oneData = User::find()->select('ID_IMAGE')->where('ID=:ID', [':ID' => \Yii::$app->user->id])->asArray()->one();
  202. if ($oneData['ID_IMAGE']) {
  203. return static::notice($oneData);
  204. } else {
  205. $token = Cache::setUploadToken();
  206. return static::notice($token);
  207. }
  208. }
  209. /**
  210. * 点位绑定
  211. * @return mixed
  212. * @throws \yii\base\Exception
  213. * @throws \yii\web\HttpException
  214. */
  215. public function actionBind() {
  216. $userBind = UserBind::findOneAsArray('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => \Yii::$app->user->id]);
  217. $allData['list']=UserBind::findAllAsArray('MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $userBind['MAIN_UID']], 'ID,USER_ID,MAIN_UID,CREATED_AT,UPDATED_AT');
  218. foreach ($allData['list'] as $key => $value) {
  219. $baseInfo = Info::baseInfoZh($value['USER_ID']);
  220. if ($baseInfo['STATUS'] != 1) {
  221. unset($allData['list'][$key]);
  222. continue;
  223. }
  224. $allData['list'][$key]['USER_NAME'] = $baseInfo['USER_NAME'];
  225. $allData['list'][$key]['REAL_NAME'] = $baseInfo['REAL_NAME'];
  226. $allData['list'][$key]['BANK_PROVINCE_NAME'] = $baseInfo['BANK_PROVINCE_NAME'];
  227. $allData['list'][$key]['BANK_CITY_NAME'] = $baseInfo['BANK_CITY_NAME'];
  228. $allData['list'][$key]['BANK_COUNTY_NAME'] = $baseInfo['BANK_COUNTY_NAME'];
  229. $allData['list'][$key]['OPEN_BANK_NAME'] = $baseInfo['OPEN_BANK_NAME'];
  230. $allData['list'][$key]['BANK_NO'] = $baseInfo['BANK_NO'];
  231. $allData['list'][$key]['MAIN_USER_NAME'] =Info::getUserNameByUserId($value['MAIN_UID']);
  232. }
  233. $allData['list'] = array_values($allData['list']);
  234. return static::notice($allData);
  235. }
  236. /**
  237. * 编辑点位绑定
  238. * @return mixed
  239. * @throws \yii\web\HttpException
  240. */
  241. public function actionBindEdit(){
  242. $id = \Yii::$app->request->get('id');
  243. if(\Yii::$app->request->isPost) {
  244. return parent::edit(UserBindForm::class, Yii::t('app', 'successfully'), 'frontEdit', ['frontEdit'], null, function($form, $result){
  245. //log
  246. });
  247. }
  248. // 获得当前会员的用户名等信息
  249. $userBind = UserBind::findOneAsArray('ID=:ID AND IS_DEL=0', [':ID' => $id]);
  250. $userBinds = UserBind::findAllAsArray('MAIN_UID=:MAIN_UID AND IS_DEL=0',[':MAIN_UID'=>$userBind['MAIN_UID']], 'ID,USER_ID,MAIN_UID,CREATED_AT,UPDATED_AT');
  251. foreach($userBinds as $key=>$value){
  252. $status = Info::getStatusByUserId($value['USER_ID']);
  253. if ($status != 1) {
  254. unset($userBinds[$key]);
  255. continue;
  256. }
  257. $userBinds[$key]['USER_NAME'] = Info::getUserNameByUserId($value['USER_ID']);
  258. }
  259. $userBinds = array_values($userBinds);
  260. return static::notice(['userBinds' => $userBinds,'mainUid'=>$userBind['MAIN_UID']]);
  261. }
  262. // 会员升级,通过会员的编号,获取会员信息
  263. public function actionUpgradeInfo() {
  264. $isSwitchUpgrade = Config::find()
  265. ->where("CONFIG_NAME='isOpenUpgrade'")
  266. ->asArray()
  267. ->one();
  268. $isOpen = !empty($isSwitchUpgrade) && isset($isSwitchUpgrade['VALUE']) ? $isSwitchUpgrade['VALUE'] : 0;
  269. if ($isOpen < 1) {
  270. return static::notice(Yii::t('app', 'theFunctionIsNotAvailable'), 400);
  271. }
  272. $userNumber = \Yii::$app->request->request('userName');
  273. $baseInfo = Info::baseInfoZhByUserName($userNumber);
  274. if ($baseInfo['STATUS'] != 1) {
  275. return static::notice(Yii::t('app', 'inactiveUser'), 400);
  276. }
  277. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  278. if ($baseInfo['COUNTRY_ID'] !== $loginUserInfo['COUNTRY_ID']) {
  279. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  280. }
  281. // 1. 如果是最高级别,则只显示用户基本信息
  282. // 2. 如果不是最高级别,如果用户累计报单数据是0, 或者用户累计报单业绩不符合级别信息,则提示 请联系客服核对业绩
  283. $userId = $baseInfo['ID'];
  284. $userDecId = $baseInfo['DEC_LV'];// 用户当前的级别
  285. // 获取系统中的DEC 报单级别配置
  286. $decConfig = DeclarationLevel::getAllData();
  287. $userDecInfo = $decConfig[$userDecId]; // 会员的级别具体信息
  288. $maxPerfInfo = DeclarationLevel::getMaxDecPref();
  289. $maxDecId = $maxPerfInfo['ID']; // 级别配置中最高级别ID
  290. $observe = Config::getConfigByType('observe'); // 获取观察期配置信息
  291. $observeLimit = $observe['observePeriodLimit']['value']; // 月份限制
  292. $isObserve = User::checkIsObserve($baseInfo['CREATED_AT'], $observeLimit); // 判断用户是否再观察期中
  293. // 如果用户已经是最高级别,则只展示用户信息
  294. $isMax = false;
  295. if ($maxDecId == $userDecId) {
  296. $isMax = true;
  297. }
  298. // NA级别暂时不提供升级
  299. if ($userDecInfo['ID'] == '67ABC6AO8YTK2LN0E055736AECE8644D') {
  300. return static::notice(Yii::t('app', 'upgradeNotSupport'), 400);
  301. }
  302. // 分期的总期数
  303. $instalmentConfig = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  304. // 查询是否有FX的分期订单
  305. $stageFX = Instalment::getOneStage($baseInfo['ID'], 'FX');
  306. if ($stageFX && $stageFX < $instalmentConfig) {
  307. $instalment = false;
  308. } else {
  309. // 是否有未完成的BD类型的分期订单
  310. $stage = Instalment::getOneStage($userId, 'BD');
  311. if ($stage && $stage < $instalmentConfig) {
  312. $instalment = true;
  313. } else if ($isMax && $stage == $instalmentConfig) {
  314. $instalment = false;
  315. } else {
  316. $instalment = false;
  317. }
  318. }
  319. // 如果用户已经是最高级别,则只展示用户信息
  320. $userInfo = [
  321. 'DEC_NAME' => $baseInfo['DEC_LV_NAME'], // 用户级别中文
  322. 'DEC_ID' => $userDecId, // 用户级别id
  323. 'REAL_NAME' => $baseInfo['REAL_NAME'], // 真实姓名
  324. 'ADD_AT' => date('Y-m-d', $baseInfo['CREATED_AT']), // 加入时间
  325. 'COUNTRY_NAME' => $baseInfo['COUNTRY_NAME'], // 国家
  326. 'IS_OBSERVE' => $isObserve, // 是否是观察期 true为是观察期
  327. 'IS_MAX' => $isMax, // 是否已是最大级别 最大级别不需要判断报单总PV是多少 只展示基本信息
  328. 'INSTALMENT' => $instalment,
  329. 'NOW_PERF' => User::sumDevPvByUserId($userId), // 用户所有报单PV总和
  330. ];
  331. // 如果是最高级别了,则无需升级
  332. if ($isMax) {
  333. return static::notice(['baseInfo' => $userInfo]);
  334. }
  335. $levelPerf = $userDecInfo['PERF'];// 用户当前级别对应的业绩值
  336. if (!$isMax) {
  337. $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和
  338. // // 如果总和小于级别业绩 去掉这校验直接补比如3000-980的差值
  339. // if ($userDecPvSum < $levelPerf) {
  340. // return static::notice('请联系客服人员核对业绩',400);
  341. // }
  342. // 下一级业绩
  343. $nextLevelPerf = DeclarationLevel::getNextDecPref($levelPerf)['PERF'];
  344. // 如果总和超过了下一级业绩
  345. if ($userDecPvSum >= $nextLevelPerf) {
  346. return static::notice(Yii::t('app', 'checkPerformance'), 400);
  347. }
  348. $type = $isObserve ? 1 : 2;
  349. $userInfo['UPGRADE_FUNC'] = $isObserve ? Yii::t('app', 'fillingUpOfADeficit') : Yii::t('app', 'fullPayment');
  350. $upgradeType = UpgradeType::getOneByType($type);
  351. // 如果用户不是最大级别,则需要获取是否观察期,算出PV是否有问题,应该补多少,
  352. $userInfo['UPGRADE_TYPE'] = $upgradeType;
  353. $userInfo['NOW_PERF'] = $userDecPvSum;
  354. $userInfo['NEXT_PERF'] = $nextLevelPerf;
  355. // 用户可选择的级别列表
  356. $userInfo['LEVEL_LIST'] = DeclarationLevel::getNextAll($levelPerf);// 用户可选择的级别列表
  357. // 如果用户正处于报单分期中,则可以升级到Elite
  358. $userInstalmentInfo = Instalment::getInfo($userId);
  359. if($userInstalmentInfo){
  360. if ($userInstalmentInfo['ORDER_TYPE']=='BD' && $userInstalmentInfo['STAGE'] < 3){
  361. $userInfo['LEVEL_LIST'] = DeclarationLevel::getNextAll(12);// 用户可选择的级别列表
  362. unset($userInfo['LEVEL_LIST'][$userDecId]);
  363. }
  364. }
  365. // 循环列表,补充升级所需要的补差
  366. foreach ($userInfo['LEVEL_LIST'] as &$v) {
  367. $v['REPAIR_PV'] = $isObserve ? $v['PERF'] - $userInfo['NOW_PERF'] : $v['PERF'];
  368. }
  369. }
  370. return static::notice(['baseInfo' => $userInfo]);
  371. }
  372. // 会员升级管理
  373. public function actionUpgrade() {
  374. $isMax = \Yii::$app->request->get('isMax');
  375. $userNumber = \Yii::$app->request->get('userName');
  376. $isSwitchUpgrade = Config::find()
  377. ->where("CONFIG_NAME='isOpenUpgrade'")
  378. ->asArray()
  379. ->one();
  380. $isOpen = !empty($isSwitchUpgrade) && isset($isSwitchUpgrade['VALUE']) ? $isSwitchUpgrade['VALUE'] : 0;
  381. if ($isOpen < 1) {
  382. return static::notice(Yii::t('app', 'theFunctionIsNotAvailable'), 400);
  383. }
  384. // 开始升级
  385. if (\Yii::$app->request->isPost) {
  386. $post = \Yii::$app->request->post();
  387. // 根据支付方式区分逻辑
  388. $payMethod = \Yii::$app->request->post('payType', '');
  389. if ($payMethod === 'pay_stack') {
  390. $formModel = new ApproachDeclarationUpgradeForm();
  391. } else {
  392. unset($post['payType']);
  393. $formModel = new DeclarationUpgradeForm();
  394. }
  395. $stockist = $post['decUserName'] ?? '';
  396. if (!$stockist) {
  397. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  398. }
  399. $post['type'] = DeclarationForm::TYPE_ZC;
  400. if ($formModel->load($post, '') && $result = $formModel->add($post)) {
  401. return static::notice($result);// Upgrade declaration succeeded升级报单成功
  402. } else {
  403. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  404. }
  405. }
  406. // 升级的会员
  407. $userName = \Yii::$app->request->get('userName');
  408. // 会员信息
  409. $user = User::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => trim($userName)]);
  410. if (!$user) {
  411. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  412. }
  413. //所有报单套餐
  414. $allDecPackage = DeclarationPackage::getAllData();
  415. $decLevels = Cache::getDecLevelConfig();
  416. foreach ($allDecPackage as $k=>$v){
  417. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  418. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  419. }
  420. $baseInfo = Info::baseInfoZhByUserName($userNumber);
  421. // 分期的总期数
  422. $instalmentConfig = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  423. // 查询是否有FX的分期订单
  424. $stageFX = Instalment::getOneStage($baseInfo['ID'], 'FX');
  425. if ($stageFX && $stageFX < $instalmentConfig) {
  426. if ($isMax === "true") {
  427. $condition = ") AND S.INSTALMENT = 1000";
  428. } else {
  429. $condition = ") AND (S.INSTALMENT = 0 OR S.INSTALMENT = 1000)";
  430. }
  431. } else {
  432. // 是否有未完成的BD类型的分期订单
  433. $stageBD = Instalment::getOneStage($baseInfo['ID'], 'BD');
  434. if ($isMax === "true") {
  435. if ($stageBD > 0 && $stageBD < $instalmentConfig) {
  436. $condition = ") AND (S.INSTALMENT = " . ($stageBD + 1) . ")";
  437. } else {
  438. $condition = ") AND S.INSTALMENT = 0";
  439. }
  440. } else {
  441. if ($stageBD > 0 && $stageBD < $instalmentConfig) {
  442. $condition = ") AND (S.INSTALMENT = 0 OR S.INSTALMENT = " . ($stageBD + 1) . ")";
  443. } else {
  444. $condition = ") AND (S.INSTALMENT = 0 OR S.INSTALMENT = 1)";
  445. }
  446. }
  447. }
  448. //所有自选商品
  449. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  450. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  451. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  452. $decUserName = User::getEnCodeInfo(\Yii::$app->user->id)['USER_NAME'];
  453. $query_condition= " AND (1<>1";
  454. if(!$isDecReg || ($isDecReg && $isDec==1)){
  455. $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  456. }
  457. if($isStudio==1){
  458. $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  459. }
  460. $query_condition .= $condition . ' AND N.COUNTRY_ID=:COUNTRY_ID ';
  461. $allGoods = ShopGoods::find()
  462. ->alias('S')
  463. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  464. ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  465. ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  466. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $user['COUNTRY_ID']])
  467. ->orderBy('SORT ASC')
  468. ->asArray()
  469. ->all();
  470. foreach ($allGoods as &$goods) {
  471. $goods['TAX'] = Tool::calculateTax($goods['SELL_PRICE'], $goods['TAX_RATE']);
  472. }
  473. // 报单中心汇率
  474. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  475. $decCountry = Countries::getById($decCountryId);
  476. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  477. // 升级会员汇率
  478. $country = Countries::getById($user['COUNTRY_ID']);
  479. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  480. // 升级会员币种
  481. $currency = Currency::getById($country['LOCAL_CURRENCY_ID']);
  482. $userBalance = [
  483. 'cash' => 0,
  484. 'localCash' => 0,
  485. 'coin' => '',
  486. ];
  487. // 会员余额
  488. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  489. if ($userCash) {
  490. $userBalance['cash'] = $userCash['CASH'];
  491. $userBalance['coin'] = $currency['CODE'] ?? '';
  492. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  493. }
  494. // 会员绩效奖金
  495. $userPerformanceAmount = UserPerformance::getAmounts(\Yii::$app->user->id);
  496. $userBalance['prp'] = $userPerformanceAmount ?? 0;
  497. $userBalance['localPrp'] = Tool::convertAmount($userPerformanceAmount, $decUserCurrencyRate, $currencyRate);
  498. // 行政区划
  499. $region = Region::getByCountryId($user['COUNTRY_ID']);
  500. // 支付方式
  501. $payList = ShopGoods::payTypes();
  502. unset($payList['prp']);
  503. // 售卖方式
  504. $sellType = ShopGoods::getCategoryType();
  505. unset($sellType[0]['sell_type'][1]);
  506. return static::notice([
  507. 'allDecPackage' => $allDecPackage,
  508. 'allGoods' => $allGoods,
  509. 'decUserName'=>$decUserName,
  510. 'payList' => $payList,
  511. 'sellType' => $sellType,
  512. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  513. 'userBalance' => $userBalance,
  514. 'decUserCurrencyRate' => $decUserCurrencyRate,
  515. 'userCurrencyRate' => $currencyRate,
  516. 'region' => $region,
  517. ]);
  518. }
  519. /**
  520. * 报单管理
  521. */
  522. public function actionGuestDec()
  523. {
  524. $service = new GuestDeclarationOrderService();
  525. try {
  526. if (Yii::$app->request->isPost) {
  527. $input = Yii::$app->request->post();
  528. $result = $service->submitDraft(
  529. (string) ($input['draftId'] ?? ''),
  530. (string) ($input['accessToken'] ?? ''),
  531. $input
  532. );
  533. return static::notice($result);
  534. }
  535. $countryId = (string) Yii::$app->request->get('countryId', '');
  536. if ($countryId === '') {
  537. return static::notice(Yii::t('app', 'countryDoesNotSelect'), 400);
  538. }
  539. $draft = $service->createDraft($countryId);
  540. $decLevels = Cache::getDecLevelConfig();
  541. $packages = DeclarationPackage::getAllData();
  542. foreach ($packages as $index => $package) {
  543. $packages[$index]['LEVEL_NAME'] = $decLevels[$package['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  544. }
  545. // $query_condition = " AND (1<>1";
  546. // if(!$isDecReg || ($isDecReg && $isDec==1)){
  547. // $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  548. // }
  549. // if($isStudio==1){
  550. // $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  551. // }
  552. // $query_condition .= ") AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID";
  553. // 只查询普通商品
  554. // $allGoods = ShopGoods::find()
  555. // ->alias('S')
  556. // ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  557. // ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  558. // ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  559. // ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $countryId])
  560. // ->orderBy('SORT ASC')
  561. // ->asArray()
  562. // ->all();
  563. $goods = ShopGoods::find()
  564. ->alias('S')
  565. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  566. ->innerJoin(ShopGoodsNature::tableName() . ' N', 'N.GOODS_ID=S.ID')
  567. ->innerJoin(Currency::tableName() . ' C', 'C.ID=N.LOCAL_CURRENCY_ID')
  568. ->where('S.STATUS=1 AND S.CATEGORY_TYPE=1 AND FIND_IN_SET(1,S.GIFT_TYPE)>0 AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID', [':COUNTRY_ID' => $countryId])
  569. ->orderBy('S.SORT ASC')
  570. ->asArray()->all();
  571. // 银行开户行
  572. $allOpenBank = OpenBank::find()
  573. ->where('STATUS=:STATUS AND COUNTRY_ID=:COUNTRY_ID', [':STATUS' => 1, ':COUNTRY_ID' => $countryId])
  574. ->orderBy('BANK_NAME ASC')
  575. ->asArray()
  576. ->all();
  577. // 售卖方式
  578. $sellType = ShopGoods::getCategoryType();
  579. unset($sellType[0]['sell_type'][1]);
  580. return static::notice([
  581. 'draft' => $draft,
  582. 'allDecPackage' => $packages,
  583. 'allGoods' => $goods,
  584. 'allOpenBank' => $allOpenBank,
  585. 'sellType' => $sellType,
  586. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  587. 'region' => Region::getByCountryId($countryId),
  588. 'payList' => ['pay_stack' => ['name' => 'PayStack']],
  589. 'decLevel' => DeclarationLevel::getData(),
  590. ]);
  591. } catch (\Throwable $e) {
  592. LoggerTool::error([
  593. 'event' => 'guestDec_failed',
  594. 'exception' => get_class($e),
  595. 'message' => $e->getMessage(),
  596. 'file' => basename($e->getFile()),
  597. 'line' => $e->getLine(),
  598. ]);
  599. if ($e instanceof \yii\web\HttpException) {
  600. return static::notice($e->getMessage(), $e->statusCode);
  601. }
  602. return static::notice('Unable to process guest order.', 500);
  603. }
  604. }
  605. public function actionGuestDecDebugPaid()
  606. {
  607. try {
  608. $input = Yii::$app->request->post();
  609. $result = (new GuestDeclarationOrderService())->debugCompletePaidDraft(
  610. (string) ($input['draftId'] ?? ''),
  611. (string) ($input['accessToken'] ?? '')
  612. );
  613. return static::notice($result);
  614. } catch (\Throwable $e) {
  615. LoggerTool::error([
  616. 'event' => 'guestDec_debug_paid_failed',
  617. 'exception' => get_class($e),
  618. 'message' => $e->getMessage(),
  619. 'file' => basename($e->getFile()),
  620. 'line' => $e->getLine(),
  621. ]);
  622. if ($e instanceof \yii\web\HttpException) {
  623. return static::notice($e->getMessage(), $e->statusCode);
  624. }
  625. return static::notice('Unable to debug-complete guest order.', 500);
  626. }
  627. }
  628. public function actionGuestDecStatus()
  629. {
  630. try {
  631. $result = (new GuestDeclarationOrderService())->getDraftStatus(
  632. (string) Yii::$app->request->get('draftId', ''),
  633. (string) Yii::$app->request->get('accessToken', '')
  634. );
  635. return static::notice($result);
  636. } catch (\Throwable $e) {
  637. return static::notice('Guest order draft was not found.', 404);
  638. }
  639. }
  640. public function actionDec() {
  641. $redis = \Yii::$app->redis;
  642. $post = \Yii::$app->request->post();
  643. if (\Yii::$app->request->isPost) {
  644. $email = $post['email'] ?? '';
  645. if ($email && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  646. return static::notice(Yii::t('app', 'invalidEmail'), 400);
  647. }
  648. $stockist = $post['decUserName'] ?? '';
  649. if (!$stockist) {
  650. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  651. }
  652. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  653. if($loginUserInfo['IS_DEC'] == 1 && !empty($post['decUserName']) && $post['decUserName'] != $loginUserInfo['USER_NAME']){
  654. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  655. }
  656. if($loginUserInfo['IS_DEC'] == 0 && $post['decUserName']){
  657. //查询报单编号是否存在
  658. $userId = Info::getUserIdByUserName($post['decUserName']);
  659. if(!$userId){
  660. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  661. }
  662. $isDec = User::getEnCodeInfo($userId)['IS_DEC'];
  663. if(!$isDec){
  664. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  665. }
  666. }
  667. $conUserName = trim($post['conUserName']); // 实际推荐人
  668. $recUserName = trim($post['recUserName']);
  669. // 如果推荐人和实际推荐人是同一人,则跳过检查
  670. if ($recUserName != $conUserName) {
  671. // 实际推荐人必须是推荐人的推荐网上级
  672. $sqlUp = <<<SQL
  673. WITH recursive t_rec AS
  674. (
  675. SELECT
  676. m.USER_ID,
  677. U.USER_NAME,
  678. m.PARENT_UID,
  679. U2.USER_NAME AS PARENT_NAME,
  680. 1 AS node_level
  681. FROM
  682. AR_USER_RELATION_NEW m
  683. LEFT JOIN AR_USER U ON U.ID = m.USER_ID
  684. LEFT JOIN AR_USER U2 ON U2.ID = m.PARENT_UID
  685. WHERE
  686. U.USER_NAME = :USER_NAME
  687. UNION ALL
  688. SELECT
  689. t1.USER_ID,
  690. U.USER_NAME,
  691. t1.PARENT_UID,
  692. U2.USER_NAME AS PARENT_NAME,
  693. t2.node_level + 1 -- 结点层级
  694. FROM
  695. AR_USER_RELATION_NEW t1
  696. JOIN t_rec t2 ON t2.PARENT_UID = t1.USER_ID
  697. LEFT JOIN AR_USER U ON U.ID = t1.USER_ID
  698. LEFT JOIN AR_USER U2 ON U2.ID = t1.PARENT_UID
  699. )
  700. SELECT
  701. *
  702. FROM
  703. t_rec
  704. SQL;
  705. // 查询推荐人的所有上级
  706. $relationNodeUp = \Yii::$app->db->createCommand($sqlUp)
  707. ->bindValue(':USER_NAME', $post['recUserName'])
  708. ->queryAll();
  709. $relationNodeUp = array_column($relationNodeUp, 'USER_NAME');
  710. unset($relationNodeUp[$post['recUserName']]);
  711. if (!in_array($post['conUserName'], $relationNodeUp)) {
  712. return static::notice(Yii::t('app', 'fillSponsorUpline'), 400);
  713. }
  714. }
  715. // 根据支付方式区分逻辑
  716. $payMethod = \Yii::$app->request->post('payType', '');
  717. if ($payMethod === 'pay_stack') {
  718. $formModel = new ApproachDeclarationLoopForm();
  719. } else {
  720. unset($post['payType']);
  721. $formModel = new DeclarationLoopForm();
  722. }
  723. $formModel->scenario = 'userDec';
  724. $post['province'] = $post['province'] ? :1;
  725. $post['city'] = $post['city'] ? :1;
  726. $post['county'] = $post['county'] ? :1;
  727. $post['cityName'] = trim($post['cityName']);
  728. $post['lgaName'] = trim($post['lgaName']);
  729. // 针对那群沙雕移除安置网络的行为,强制增加自动安置左侧
  730. $recUserId = Info::getUserIdByUserName($conUserName);
  731. $bottomPlace = UserNetwork::getBottomPlace($recUserId,'', 'left');
  732. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  733. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  734. $bottomUser = $bottomUser['USER_NAME']; // 推算出的安置人
  735. $post['conUserName'] = $bottomUser; // 安置人
  736. $post['recUserName'] = $conUserName; // 实际推荐人
  737. $post['bottomUserName'] = $recUserName; // 推荐人
  738. $post['location'] = 1;
  739. // 针对于会员编号的判断
  740. $insertUserName = strtoupper($post['insertUserName']);
  741. $getRedisUserName = $redis->get('key_' . $insertUserName);
  742. if (!$getRedisUserName){
  743. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  744. }
  745. if ($insertUserName != $getRedisUserName){
  746. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  747. }
  748. $post['insertUserName'] = $insertUserName;
  749. $post['type'] = DeclarationForm::TYPE_ZC;
  750. $allData['data'][] = $post;
  751. try {
  752. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  753. //发送邮件
  754. // Email::sendRegistrationEmail($post['email'], $post['insertUserName'], $post['password']);
  755. return static::notice($result);//报单成功
  756. } else {
  757. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  758. }
  759. } catch (\Exception $e) {
  760. LoggerTool::error($e->getMessage() . '; ' . $e->getLine() . '; ' . $e->getFile());
  761. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  762. }
  763. }
  764. // 默认国家
  765. $countryId = \Yii::$app->request->get('countryId');
  766. if (!$countryId) {
  767. return static::notice(Yii::t('app', 'countryDoesNotSelect'), 400);
  768. }
  769. // 生成随机码 , 初始化redis
  770. $userName = Info::generateWebName($countryId);
  771. //所有报单套餐
  772. $allDecPackage = DeclarationPackage::getAllData();
  773. $decLevels = Cache::getDecLevelConfig();
  774. foreach ($allDecPackage as $k=>$v){
  775. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  776. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  777. }
  778. //所有自选商品
  779. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  780. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  781. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  782. $query_condition = " AND (1<>1";
  783. if(!$isDecReg || ($isDecReg && $isDec==1)){
  784. $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  785. }
  786. if($isStudio==1){
  787. $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  788. }
  789. $query_condition .= ") AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID";
  790. // 只查询普通商品
  791. $allGoods = ShopGoods::find()
  792. ->alias('S')
  793. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  794. ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  795. ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  796. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $countryId])
  797. ->orderBy('SORT ASC')
  798. ->asArray()
  799. ->all();
  800. // 银行开户行
  801. $allOpenBank = OpenBank::find()
  802. ->where('STATUS=:STATUS AND COUNTRY_ID=:COUNTRY_ID', [':STATUS' => 1, ':COUNTRY_ID' => $countryId])
  803. ->orderBy('BANK_NAME ASC')
  804. ->asArray()
  805. ->all();
  806. if (!$userName) {
  807. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  808. }
  809. //随机码保存在redis中方便进行比对
  810. $redis->setex('key_'.$userName , 3600 , $userName);
  811. // 报单中心会员的本地汇率
  812. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  813. $decCountry = Countries::getById($decCountryId);
  814. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  815. // 新会员的本地汇率
  816. $country = Countries::getById($countryId);
  817. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  818. // 新会员币种
  819. $currency = Currency::getById($country['LOCAL_CURRENCY_ID']);
  820. // 会员账户
  821. $userBalance = [
  822. 'cash' => 0,
  823. 'localCash' => 0,
  824. 'coin' => '',
  825. ];
  826. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  827. if ($userCash) {
  828. $userBalance['cash'] = $userCash['CASH'];
  829. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  830. $userBalance['coin'] = $currency['CODE'] ?? '';
  831. }
  832. // 行政区划
  833. $region = Region::getByCountryId($countryId);
  834. // 支付方式
  835. $payList = ShopGoods::payTypes();
  836. unset($payList['prp']);
  837. // 售卖方式
  838. $sellType = ShopGoods::getCategoryType();
  839. unset($sellType[0]['sell_type'][1]);
  840. return static::notice([
  841. 'allDecPackage' => $allDecPackage,
  842. 'allGoods' => $allGoods,
  843. 'allOpenBank' => $allOpenBank,
  844. 'userName' => $userName,
  845. 'payList' => $payList,
  846. 'sellType' => $sellType,
  847. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  848. 'userBalance' => $userBalance,
  849. 'isDec' => $isDec,
  850. 'decUserCurrencyRate' => $decUserCurrencyRate,
  851. 'userCurrencyRate' => $currencyRate,
  852. 'region' => $region,
  853. 'decLevel' => DeclarationLevel::getData(),
  854. ]);
  855. }
  856. /**
  857. * 报单级别套餐
  858. */
  859. public function actionDecPackage() {
  860. $decLv = \Yii::$app->request->get('id');
  861. //所有报单级别套餐
  862. $allDecPackage = DeclarationPackage::getPackageFromLevelId($decLv);
  863. return static::notice(['allDecPackage' => $allDecPackage]);
  864. }
  865. /*
  866. * 从会员名和左右,获取底部安置人
  867. *
  868. */
  869. public function actionGetAutoPlace() {
  870. $userName = \Yii::$app->request->get('userName');
  871. $side = \Yii::$app->request->get('side');
  872. $userId = Info::getUserIdByUserName($userName);
  873. $bottomPlace = UserNetwork::getBottomPlace($userId,'', $side);
  874. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  875. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  876. return static::notice($bottomUser);
  877. }
  878. /**
  879. * 会员信息查询
  880. * @return mixed
  881. * @throws \yii\web\HttpException
  882. */
  883. public function actionFullInfo()
  884. {
  885. $userName = \Yii::$app->request->get('userName');
  886. $userId = Info::getUserIdByUserName($userName);
  887. $userInfo['REAL_NAME'] = '';
  888. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  889. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  890. if ($user['COUNTRY_ID'] !== $loginUserInfo['COUNTRY_ID']) {
  891. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  892. }
  893. if($user){
  894. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  895. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  896. $isLocation = [1 => 'Left-Null', 2 => 'Right-Null'];
  897. if($allChildUser) {
  898. foreach ($allChildUser as $child) {
  899. if ($child['RELATIVE_LOCATION']<'3'){
  900. // $isLocation[$child['RELATIVE_LOCATION']].= 'Full';
  901. $isLocation[$child['RELATIVE_LOCATION']] = str_replace('Null', 'Full', $isLocation[$child['RELATIVE_LOCATION']]);
  902. }
  903. }
  904. }
  905. $userInfo['countryId'] = $user['COUNTRY_ID'];
  906. $userInfo['userId'] = $userId;
  907. $userInfo['isLocation'] = '('.implode(',',$isLocation).')';
  908. return static::notice($userInfo);
  909. }else{
  910. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  911. }
  912. }
  913. /**
  914. * Public member information lookup for guest declaration flows.
  915. * @return mixed
  916. */
  917. public function actionGuestFullInfo()
  918. {
  919. $userName = trim((string) \Yii::$app->request->get('userName', ''));
  920. $userId = Info::getUserIdByUserName($userName);
  921. $user = $userId ? User::findOneAsArray('ID=:ID', [':ID' => $userId]) : null;
  922. if (!$user) {
  923. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  924. }
  925. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  926. $isLocation = [1 => 'Left-Null', 2 => 'Right-Null'];
  927. if ($allChildUser) {
  928. foreach ($allChildUser as $child) {
  929. if ($child['RELATIVE_LOCATION'] < '3' && isset($isLocation[$child['RELATIVE_LOCATION']])) {
  930. $isLocation[$child['RELATIVE_LOCATION']] = str_replace('Null', 'Full', $isLocation[$child['RELATIVE_LOCATION']]);
  931. }
  932. }
  933. }
  934. return static::notice([
  935. 'REAL_NAME' => $user['REAL_NAME'],
  936. 'countryId' => $user['COUNTRY_ID'],
  937. 'userId' => $userId,
  938. 'isLocation' => '(' . implode(',', $isLocation) . ')',
  939. ]);
  940. }
  941. public function actionInspectNetwork()
  942. {
  943. // 推荐人信息
  944. $recommenderName= \Yii::$app->request->get('recommenderName');
  945. $userId = Info::getUserIdByUserName($recommenderName);
  946. $recommender = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  947. if (!$recommender) {
  948. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  949. }
  950. // 实际推荐人信息
  951. $recommenderActualName= \Yii::$app->request->get('recommenderActualName');
  952. $actualId = Info::getUserIdByUserName($recommenderActualName);
  953. $recommenderActual = User::findOneAsArray('ID=:ID', [':ID' => $actualId]);
  954. if (!$recommenderActual) {
  955. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  956. }
  957. // 实际推荐人是否是推荐人本人
  958. if ($actualId == $userId) {
  959. return static::notice([
  960. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  961. 'userId' => $recommenderActual['ID'],
  962. ]);
  963. }
  964. // 实际推荐人必须是推荐人的推荐网上级
  965. $sqlUp =<<<SQL
  966. WITH recursive t_rec AS
  967. (
  968. SELECT
  969. m.USER_ID,
  970. U.USER_NAME,
  971. m.PARENT_UID,
  972. U2.USER_NAME AS PARENT_NAME,
  973. 1 AS node_level
  974. FROM
  975. AR_USER_RELATION_NEW m
  976. LEFT JOIN AR_USER U ON U.ID = m.USER_ID
  977. LEFT JOIN AR_USER U2 ON U2.ID = m.PARENT_UID
  978. WHERE
  979. U.USER_NAME = :USER_NAME
  980. UNION ALL
  981. SELECT
  982. t1.USER_ID,
  983. U.USER_NAME,
  984. t1.PARENT_UID,
  985. U2.USER_NAME AS PARENT_NAME,
  986. t2.node_level + 1 -- 结点层级
  987. FROM
  988. AR_USER_RELATION_NEW t1
  989. JOIN t_rec t2 ON t2.PARENT_UID = t1.USER_ID
  990. LEFT JOIN AR_USER U ON U.ID = t1.USER_ID
  991. LEFT JOIN AR_USER U2 ON U2.ID = t1.PARENT_UID
  992. )
  993. SELECT
  994. *
  995. FROM
  996. t_rec
  997. SQL;
  998. // 查询推荐人的所有上级
  999. $relationNodeUp = \Yii::$app->db->createCommand($sqlUp)
  1000. ->bindValue(':USER_NAME' , $recommenderName)
  1001. ->queryAll();
  1002. $relationNodeUp = array_column($relationNodeUp, 'USER_NAME');
  1003. unset($relationNodeUp[$recommenderName]);
  1004. if (in_array($recommenderActualName, $relationNodeUp)) {
  1005. return static::notice([
  1006. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  1007. 'userId' => $recommenderActual['ID'],
  1008. ]);
  1009. }
  1010. return static::notice(Yii::t('ctx', 'fillSponsorUpline'), 400);
  1011. }
  1012. /**
  1013. * Public sponsor relationship validation for guest declaration flows.
  1014. * @return mixed
  1015. */
  1016. public function actionGuestInspectNetwork()
  1017. {
  1018. $recommenderName = trim((string) \Yii::$app->request->get('recommenderName', ''));
  1019. $recommenderActualName = trim((string) \Yii::$app->request->get('recommenderActualName', ''));
  1020. if ($recommenderName === '' || $recommenderActualName === '') {
  1021. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1022. }
  1023. $userId = Info::getUserIdByUserName($recommenderName);
  1024. $recommender = $userId ? User::findOneAsArray('ID=:ID', [':ID' => $userId]) : null;
  1025. if (!$recommender) {
  1026. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1027. }
  1028. $actualId = Info::getUserIdByUserName($recommenderActualName);
  1029. $recommenderActual = $actualId ? User::findOneAsArray('ID=:ID', [':ID' => $actualId]) : null;
  1030. if (!$recommenderActual) {
  1031. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1032. }
  1033. if ($actualId == $userId) {
  1034. return static::notice([
  1035. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  1036. 'userId' => $recommenderActual['ID'],
  1037. ]);
  1038. }
  1039. $sqlUp = <<<SQL
  1040. WITH recursive t_rec AS
  1041. (
  1042. SELECT m.USER_ID, U.USER_NAME, m.PARENT_UID, U2.USER_NAME AS PARENT_NAME, 1 AS node_level
  1043. FROM AR_USER_RELATION_NEW m
  1044. LEFT JOIN AR_USER U ON U.ID = m.USER_ID
  1045. LEFT JOIN AR_USER U2 ON U2.ID = m.PARENT_UID
  1046. WHERE U.USER_NAME = :USER_NAME
  1047. UNION ALL
  1048. SELECT t1.USER_ID, U.USER_NAME, t1.PARENT_UID, U2.USER_NAME AS PARENT_NAME,
  1049. t2.node_level + 1
  1050. FROM AR_USER_RELATION_NEW t1
  1051. JOIN t_rec t2 ON t2.PARENT_UID = t1.USER_ID
  1052. LEFT JOIN AR_USER U ON U.ID = t1.USER_ID
  1053. LEFT JOIN AR_USER U2 ON U2.ID = t1.PARENT_UID
  1054. )
  1055. SELECT * FROM t_rec
  1056. SQL;
  1057. $relationNodeUp = \Yii::$app->db->createCommand($sqlUp)
  1058. ->bindValue(':USER_NAME', $recommenderName)
  1059. ->queryAll();
  1060. $relationNodeUp = array_column($relationNodeUp, 'USER_NAME');
  1061. if (in_array($recommenderActualName, $relationNodeUp, true)) {
  1062. return static::notice([
  1063. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  1064. 'userId' => $recommenderActual['ID'],
  1065. ]);
  1066. }
  1067. return static::notice(Yii::t('ctx', 'fillSponsorUpline'), 400);
  1068. }
  1069. /**
  1070. * 复消会员信息查询
  1071. * @return mixed
  1072. * @throws \yii\web\HttpException
  1073. */
  1074. public function actionUserBaseInfo()
  1075. {
  1076. $userName = \Yii::$app->request->get('userName');
  1077. $userId = Info::getUserIdByUserName($userName);
  1078. if($userId){
  1079. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  1080. if($allAddress) {
  1081. foreach ($allAddress as $key => $row) {
  1082. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  1083. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  1084. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  1085. }
  1086. }
  1087. $userInfo = Info::baseInfoWithNet($userId);
  1088. // $decLevelConfig = Cache::getDecLevelConfig();
  1089. // $empLevelConfig = Cache::getEmpLevelConfig();
  1090. $arr = [
  1091. 'REAL_NAME'=>$userInfo['REAL_NAME'],
  1092. // 'DEC_LEVEL_NAME' => $decLevelConfig[$userInfo['DEC_LV']]['LEVEL_NAME'],
  1093. // 'EMP_LEVEL_NAME'=>$empLevelConfig[$userInfo['EMP_LV']]['LEVEL_NAME'],
  1094. // 'REC_UID'=>$userInfo['REC_USER_NAME'].'('.$userInfo['REC_REAL_NAME'].')',
  1095. // 'CON_UID'=>$userInfo['CON_USER_NAME'].'('.$userInfo['CON_REAL_NAME'].')',
  1096. 'allAddress'=>$allAddress
  1097. ];
  1098. return static::notice($arr);
  1099. }else{
  1100. return static::notice(Yii::t('app', 'repeatSalesMemberNoDoesNotExist'), 400);
  1101. }
  1102. }
  1103. /**
  1104. * 会员资料
  1105. * @return mixed
  1106. * @throws \yii\web\HttpException
  1107. */
  1108. public function actionBaInfo()
  1109. {
  1110. $data['userInfo'] = BaUser::getEnCodeInfo(\Yii::$app->user->id);
  1111. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  1112. // 推荐人
  1113. $baUserInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  1114. $sponsor = User::findOneAsArray('ID=:USER_ID', [':USER_ID' => $baUserInfo['REC_UID']]);
  1115. $data['userInfo']['SponsorCode'] = $sponsor['USER_NAME'];
  1116. $data['userInfo']['SponsorName'] = $sponsor['REAL_NAME'];
  1117. return static::notice($data);
  1118. }
  1119. /**
  1120. * 编辑会员资料
  1121. * @return mixed
  1122. * @throws \yii\web\HttpException
  1123. */
  1124. public function actionBaEdit()
  1125. {
  1126. if(\Yii::$app->request->isPost)
  1127. {
  1128. $form = new BaUserForm();
  1129. $post = \Yii::$app->request->post();
  1130. $form->scenario = 'modifyProfile';
  1131. if ($form->load($post, '') && $result = $form->modifyProfile()){
  1132. return static::notice(Yii::t('app', 'personalDataModifiedSuccessfully'), 400);
  1133. } else {
  1134. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  1135. }
  1136. }
  1137. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  1138. }
  1139. /**
  1140. * 修改登录密码
  1141. */
  1142. public function actionBaPassword(){
  1143. if(\Yii::$app->request->isPost) {
  1144. $form = new BaUserForm();
  1145. $form->scenario = 'modifyPassword';
  1146. $post = \Yii::$app->request->post();
  1147. if($form->load($post, '') && $result = $form->modifyPassword()){
  1148. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  1149. } else {
  1150. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  1151. }
  1152. }
  1153. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  1154. }
  1155. /**
  1156. * 修改支付密码
  1157. */
  1158. public function actionBaPayPassword(){
  1159. if(\Yii::$app->request->isPost){
  1160. $form = new BaUserForm();
  1161. $form->scenario = 'modifyPasswordPay';
  1162. $post = \Yii::$app->request->post();
  1163. $form->userId = \Yii::$app->user->id;
  1164. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  1165. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  1166. } else {
  1167. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  1168. }
  1169. }
  1170. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  1171. }
  1172. /**
  1173. * BA报单管理
  1174. */
  1175. public function actionBaDec() {
  1176. // 生成随机码 , 初始化redis
  1177. $userName = Info::generateWebUserName('NG',9);
  1178. $redis = \Yii::$app->redis;
  1179. $post = \Yii::$app->request->post();
  1180. if (\Yii::$app->request->isPost) {
  1181. // 根据支付方式区分逻辑
  1182. $payMethod = \Yii::$app->request->post('payType', '');
  1183. if ($payMethod === 'pay_stack') {
  1184. $formModel = new BaApproachDeclarationLoopForm();
  1185. } else {
  1186. unset($post['payType']);
  1187. $formModel = new BaDeclarationLoopForm();
  1188. }
  1189. $formModel->scenario = 'userDec';
  1190. $post['province'] = $post['province'] ? :1;
  1191. $post['city'] = $post['city'] ? :1;
  1192. $post['county'] = $post['county'] ? :1;
  1193. // $post['conUserName'] = trim($post['conUserName']);
  1194. $post['recUserName'] = trim($post['recUserName']);
  1195. $post['cityName'] = trim($post['cityName']);
  1196. $post['lgaName'] = trim($post['lgaName']);
  1197. // 针对于会员编号的判断
  1198. $insertUserName = strtoupper($post['insertUserName']);
  1199. $getRedisUserName = $redis->get('key_' . $insertUserName);
  1200. if (!$getRedisUserName){
  1201. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  1202. }
  1203. if ($insertUserName != $getRedisUserName){
  1204. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  1205. }
  1206. $post['insertUserName'] = $insertUserName;
  1207. $post['type'] = BaDeclarationForm::TYPE_ZC;
  1208. $allData['data'][] = $post;
  1209. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  1210. return static::notice($result); // 报单成功
  1211. } else {
  1212. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  1213. }
  1214. }
  1215. // 只查询普通商品
  1216. $allGoods = ShopGoods::find()->where("ID='375895243322691584'")->orderBy('SORT ASC')->asArray()->all();
  1217. if (!$userName) {
  1218. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  1219. }
  1220. //随机码保存在redis中方便进行比对
  1221. $redis->setex('key_'.$userName , 3600 , $userName);
  1222. // 会员账户
  1223. $userBalance = [
  1224. 'cash' => 0,
  1225. // 'tourism_points' => 0,
  1226. // 'garage_points' => 0,
  1227. // 'villa_points' => 0,
  1228. ];
  1229. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  1230. if ($userCash) {
  1231. $userBalance['cash'] = $userCash['CASH'];
  1232. }
  1233. return static::notice([
  1234. 'allGoods' => $allGoods,
  1235. 'userName' => $userName,
  1236. 'payList' => ShopGoods::payTypes(),
  1237. 'sellType' => ShopGoods::getCategoryType(),
  1238. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  1239. 'userBalance' => $userBalance,
  1240. ]);
  1241. }
  1242. /**
  1243. * BA会员信息
  1244. * @return mixed
  1245. * @throws \yii\web\HttpException
  1246. */
  1247. public function actionBaUserInfo()
  1248. {
  1249. $userName = \Yii::$app->request->get('userName');
  1250. $userId = Info::getBaUserIdByUserName($userName);
  1251. $userInfo['REAL_NAME'] = '';
  1252. $user = BaUser::findOneAsArray('ID=:ID', [':ID' => $userId], 'REAL_NAME');
  1253. $uInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'REC_UID');
  1254. if($user){
  1255. $rec_user = UserInfo::findOneAsArray('USER_ID=:USER_ID',[':USER_ID' => $uInfo['REC_UID']], 'USER_ID, USER_NAME');
  1256. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  1257. $userInfo['REC_USER_ID'] = $rec_user['USER_ID'];
  1258. $userInfo['REC_USER_NAME'] = $rec_user['USER_NAME'];
  1259. return static::notice($userInfo);
  1260. }else{
  1261. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1262. }
  1263. }
  1264. }