UserController.php 57 KB

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