UserController.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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\Form;
  12. use common\helpers\LoggerTool;
  13. use common\helpers\Tool;
  14. use common\helpers\user\Info;
  15. use common\models\BaUser;
  16. use common\models\Config;
  17. use common\models\Countries;
  18. use common\models\Currency;
  19. use common\models\CurrencyConversions;
  20. use common\models\DeclarationLevel;
  21. use common\models\DeclarationPackage;
  22. use common\models\DecOrder;
  23. use common\models\forms\ApproachDeclarationLoopForm;
  24. use common\models\forms\BaApproachDeclarationLoopForm;
  25. use common\models\forms\ApproachDeclarationUpgradeForm;
  26. use common\models\forms\BaUserForm;
  27. use common\models\forms\DeclarationForm;
  28. use common\models\forms\BaDeclarationForm;
  29. use common\models\forms\DeclarationLoopForm;
  30. use common\models\forms\BaDeclarationLoopForm;
  31. use common\models\forms\UploadForm;
  32. use common\models\forms\UserBindForm;
  33. use common\models\forms\UserForm;
  34. use common\models\Instalment;
  35. use common\models\OpenBank;
  36. use common\models\Period;
  37. use common\models\ReceiveAddress;
  38. use common\models\Region;
  39. use common\models\ShopGoods;
  40. use common\models\ShopGoodsNature;
  41. use common\models\UpgradeType;
  42. use common\models\User;
  43. use common\models\UserBind;
  44. use common\models\UserBonus;
  45. use common\models\UserInfo;
  46. use common\models\BaUserInfo;
  47. use common\models\UserNetwork;
  48. use common\models\forms\DeclarationUpgradeForm;
  49. use common\models\UserWallet;
  50. use frontendApi\modules\v1\models\Relation;
  51. use Yii;
  52. use yii\db\Exception;
  53. use yii\web\UploadedFile;
  54. class UserController extends BaseController {
  55. public $modelClass = UserInfo::class;
  56. const LOOP_FINISH = 1;
  57. /**
  58. * 会员资料
  59. * @return mixed
  60. * @throws \yii\web\HttpException
  61. */
  62. public function actionIndex() {
  63. $allNation = \Yii::$app->params['nation'];
  64. $allOpenBank = OpenBank::findAllAsArray('STATUS=1');
  65. $data['allNation'] = $allNation;
  66. $data['allOpenBank'] = $allOpenBank;
  67. $data['userInfo'] = User::getEnCodeInfo(\Yii::$app->user->id);
  68. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  69. return static::notice($data);
  70. }
  71. /**
  72. * 编辑会员资料
  73. * @return mixed
  74. * @throws \yii\web\HttpException
  75. */
  76. public function actionEdit() {
  77. if(\Yii::$app->request->isPost){
  78. $form = new UserForm();
  79. $post = \Yii::$app->request->post();
  80. $form->scenario = 'modifyProfile';
  81. if($form->load($post, '') && $result = $form->modifyProfile()){
  82. return static::notice(Yii::t('app', 'personalDataModifiedSuccessfully'), 400);
  83. } else {
  84. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  85. }
  86. }
  87. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  88. }
  89. /**
  90. * 修改登录密码
  91. */
  92. public function actionPassword(){
  93. if(\Yii::$app->request->isPost){
  94. $form = new UserForm();
  95. $form->scenario = 'modifyPassword';
  96. $post = \Yii::$app->request->post();
  97. if($form->load($post, '') && $result = $form->modifyPassword()){
  98. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  99. } else {
  100. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  101. }
  102. }
  103. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  104. }
  105. /**
  106. * 修改支付密码
  107. */
  108. public function actionPayPassword(){
  109. if(\Yii::$app->request->isPost){
  110. $form = new UserForm();
  111. $form->scenario = 'modifyPasswordPay';
  112. $post = \Yii::$app->request->post();
  113. $form->userId = \Yii::$app->user->id;
  114. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  115. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'));
  116. } else {
  117. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  118. }
  119. }
  120. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  121. }
  122. /**
  123. * 直推会员列表
  124. * @return mixed
  125. * @throws \yii\web\HttpException
  126. */
  127. public function actionRecUser() {
  128. $allData = UserInfo::lists('AND REC_UID=:REC_UID', [':REC_UID' => \Yii::$app->user->id], ['useSlaves' => true, 'select'=>'USER_ID,CREATED_AT']);
  129. foreach ($allData['list'] as $key => $data) {
  130. $userBaseInfo = User::getEnCodeInfo($data['USER_ID']);
  131. $userBaseInfo['NATION'] = \Yii::$app->params['nation'][$userBaseInfo['NATION']]['name'] ?? '';
  132. $allData['list'][$key]['BASE_INFO'] = $userBaseInfo;
  133. }
  134. return static::notice($allData);
  135. }
  136. /**
  137. * 上传身份证
  138. * @return mixed
  139. * @throws \yii\base\Exception
  140. * @throws \yii\web\HttpException
  141. */
  142. public function actionIdCard() {
  143. if (\Yii::$app->request->isPost) {
  144. $formModel = new UploadForm();
  145. $formModel->scenario = 'idCardFront';
  146. $formModel->file = UploadedFile::getInstanceByName('file');
  147. //$formModel->token = \Yii::$app->request->post('uploadToken');
  148. $formModel->token = \Yii::$app->request->request('uploadToken');
  149. if ($formModel->file && $formModel->upload()) {
  150. return static::notice(Yii::t('app', 'successfully'));
  151. } else {
  152. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  153. }
  154. }
  155. // 查看该用户是否已经上传过身份证
  156. $oneData = User::find()->select('ID_IMAGE')->where('ID=:ID', [':ID' => \Yii::$app->user->id])->asArray()->one();
  157. if ($oneData['ID_IMAGE']) {
  158. return static::notice($oneData);
  159. } else {
  160. $token = Cache::setUploadToken();
  161. return static::notice($token);
  162. }
  163. }
  164. /**
  165. * 点位绑定
  166. * @return mixed
  167. * @throws \yii\base\Exception
  168. * @throws \yii\web\HttpException
  169. */
  170. public function actionBind() {
  171. $userBind = UserBind::findOneAsArray('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => \Yii::$app->user->id]);
  172. $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');
  173. foreach ($allData['list'] as $key => $value) {
  174. $baseInfo = Info::baseInfoZh($value['USER_ID']);
  175. if ($baseInfo['STATUS'] != 1) {
  176. unset($allData['list'][$key]);
  177. continue;
  178. }
  179. $allData['list'][$key]['USER_NAME'] = $baseInfo['USER_NAME'];
  180. $allData['list'][$key]['REAL_NAME'] = $baseInfo['REAL_NAME'];
  181. $allData['list'][$key]['BANK_PROVINCE_NAME'] = $baseInfo['BANK_PROVINCE_NAME'];
  182. $allData['list'][$key]['BANK_CITY_NAME'] = $baseInfo['BANK_CITY_NAME'];
  183. $allData['list'][$key]['BANK_COUNTY_NAME'] = $baseInfo['BANK_COUNTY_NAME'];
  184. $allData['list'][$key]['OPEN_BANK_NAME'] = $baseInfo['OPEN_BANK_NAME'];
  185. $allData['list'][$key]['BANK_NO'] = $baseInfo['BANK_NO'];
  186. $allData['list'][$key]['MAIN_USER_NAME'] =Info::getUserNameByUserId($value['MAIN_UID']);
  187. }
  188. $allData['list'] = array_values($allData['list']);
  189. return static::notice($allData);
  190. }
  191. /**
  192. * 编辑点位绑定
  193. * @return mixed
  194. * @throws \yii\web\HttpException
  195. */
  196. public function actionBindEdit(){
  197. $id = \Yii::$app->request->get('id');
  198. if(\Yii::$app->request->isPost) {
  199. return parent::edit(UserBindForm::class, Yii::t('app', 'successfully'), 'frontEdit', ['frontEdit'], null, function($form, $result){
  200. //log
  201. });
  202. }
  203. // 获得当前会员的用户名等信息
  204. $userBind = UserBind::findOneAsArray('ID=:ID AND IS_DEL=0', [':ID' => $id]);
  205. $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');
  206. foreach($userBinds as $key=>$value){
  207. $status = Info::getStatusByUserId($value['USER_ID']);
  208. if ($status != 1) {
  209. unset($userBinds[$key]);
  210. continue;
  211. }
  212. $userBinds[$key]['USER_NAME'] = Info::getUserNameByUserId($value['USER_ID']);
  213. }
  214. $userBinds = array_values($userBinds);
  215. return static::notice(['userBinds' => $userBinds,'mainUid'=>$userBind['MAIN_UID']]);
  216. }
  217. // 会员升级,通过会员的编号,获取会员信息
  218. public function actionUpgradeInfo() {
  219. $isSwitchUpgrade = Config::find()
  220. ->where("CONFIG_NAME='isOpenUpgrade'")
  221. ->asArray()
  222. ->one();
  223. $isOpen = !empty($isSwitchUpgrade) && isset($isSwitchUpgrade['VALUE']) ? $isSwitchUpgrade['VALUE'] : 0;
  224. if ($isOpen < 1) {
  225. return static::notice(Yii::t('app', 'theFunctionIsNotAvailable'), 400);
  226. }
  227. $userNumber = \Yii::$app->request->request('userName');
  228. $baseInfo = Info::baseInfoZhByUserName($userNumber);
  229. if ($baseInfo['STATUS'] != 1) {
  230. return static::notice(Yii::t('app', 'inactiveUser'), 400);
  231. }
  232. // 1. 如果是最高级别,则只显示用户基本信息
  233. // 2. 如果不是最高级别,如果用户累计报单数据是0, 或者用户累计报单业绩不符合级别信息,则提示 请联系客服核对业绩
  234. $userId = $baseInfo['ID'];
  235. $userDecId = $baseInfo['DEC_LV'];// 用户当前的级别
  236. // 获取系统中的DEC 报单级别配置
  237. $decConfig = Cache::getDecLevelConfig();
  238. $userDecInfo = $decConfig[$userDecId]; // 会员的级别具体信息
  239. $maxPerfInfo = DeclarationLevel::getMaxDecPref();
  240. $maxDecId = $maxPerfInfo['ID']; // 级别配置中最高级别ID
  241. $observe = Config::getConfigByType('observe'); // 获取观察期配置信息
  242. $observeLimit = $observe['observePeriodLimit']['value']; // 月份限制
  243. $isObserve = User::checkIsObserve($baseInfo['CREATED_AT'], $observeLimit); // 判断用户是否再观察期中
  244. // 如果用户已经是最高级别,则只展示用户信息
  245. $isMax = false;
  246. if ($maxDecId == $userDecId) {
  247. $isMax = true;
  248. }
  249. // 分期的总期数
  250. $instalmentConfig = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  251. // 查询是否有FX的分期订单
  252. $stageFX = Instalment::getOneStage($baseInfo['ID'], 'FX');
  253. if ($stageFX && $stageFX < $instalmentConfig) {
  254. $instalment = false;
  255. } else {
  256. // 是否有未完成的BD类型的分期订单
  257. $stage = Instalment::getOneStage($userId, 'BD');
  258. if ($stage && $stage < $instalmentConfig) {
  259. $instalment = true;
  260. } else if ($isMax && $stage == $instalmentConfig) {
  261. $instalment = false;
  262. } else {
  263. $instalment = false;
  264. }
  265. }
  266. // 如果用户已经是最高级别,则只展示用户信息
  267. $userInfo = [
  268. 'DEC_NAME' => $baseInfo['DEC_LV_NAME'], // 用户级别中文
  269. 'DEC_ID' => $userDecId, // 用户级别id
  270. 'REAL_NAME' => $baseInfo['REAL_NAME'], // 真实姓名
  271. 'ADD_AT' => date('Y-m-d', $baseInfo['CREATED_AT']), // 加入时间
  272. 'COUNTRY_NAME' => $baseInfo['COUNTRY_NAME'], // 国家
  273. 'IS_OBSERVE' => $isObserve, // 是否是观察期 true为是观察期
  274. 'IS_MAX' => $isMax, // 是否已是最大级别 最大级别不需要判断报单总PV是多少 只展示基本信息
  275. 'INSTALMENT' => $instalment,
  276. 'NOW_PERF' => User::sumDevPvByUserId($userId), // 用户所有报单PV总和
  277. ];
  278. // 如果是最高级别了,则无需升级
  279. if ($isMax) {
  280. return static::notice(['baseInfo' => $userInfo]);
  281. }
  282. $levelPerf = $userDecInfo['PERF'];// 用户当前级别对应的业绩值
  283. if (!$isMax) {
  284. $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和
  285. // // 如果总和小于级别业绩 去掉这校验直接补比如3000-980的差值
  286. // if ($userDecPvSum < $levelPerf) {
  287. // return static::notice('请联系客服人员核对业绩',400);
  288. // }
  289. // 下一级业绩
  290. $nextLevelPerf = DeclarationLevel::getNextDecPref($levelPerf)['PERF'];
  291. // 如果总和超过了下一级业绩
  292. if ($userDecPvSum >= $nextLevelPerf) {
  293. return static::notice(Yii::t('app', 'checkPerformance'), 400);
  294. }
  295. $type = $isObserve ? 1 : 2;
  296. $userInfo['UPGRADE_FUNC'] = $isObserve ? Yii::t('app', 'fillingUpOfADeficit') : Yii::t('app', 'fullPayment');
  297. $upgradeType = UpgradeType::getOneByType($type);
  298. // 如果用户不是最大级别,则需要获取是否观察期,算出PV是否有问题,应该补多少,
  299. $userInfo['UPGRADE_TYPE'] = $upgradeType;
  300. $userInfo['NOW_PERF'] = $userDecPvSum;
  301. $userInfo['NEXT_PERF'] = $nextLevelPerf;
  302. // 用户可选择的级别列表
  303. $userInfo['LEVEL_LIST'] = DeclarationLevel::getNextAll($levelPerf);// 用户可选择的级别列表
  304. // 如果用户正处于报单分期中,则可以升级到Elite
  305. $userInstalmentInfo = Instalment::getInfo($userId);
  306. if($userInstalmentInfo){
  307. if ($userInstalmentInfo['ORDER_TYPE']=='BD' && $userInstalmentInfo['STAGE'] < 3){
  308. $userInfo['LEVEL_LIST'] = DeclarationLevel::getNextAll(12);// 用户可选择的级别列表
  309. unset($userInfo['LEVEL_LIST'][$userDecId]);
  310. }
  311. }
  312. // 循环列表,补充升级所需要的补差
  313. foreach ($userInfo['LEVEL_LIST'] as &$v) {
  314. $v['REPAIR_PV'] = $isObserve ? $v['PERF'] - $userInfo['NOW_PERF'] : $v['PERF'];
  315. }
  316. }
  317. return static::notice(['baseInfo' => $userInfo]);
  318. }
  319. // 会员升级管理
  320. public function actionUpgrade() {
  321. $isMax = \Yii::$app->request->get('isMax');
  322. $userNumber = \Yii::$app->request->get('userName');
  323. $isSwitchUpgrade = Config::find()
  324. ->where("CONFIG_NAME='isOpenUpgrade'")
  325. ->asArray()
  326. ->one();
  327. $isOpen = !empty($isSwitchUpgrade) && isset($isSwitchUpgrade['VALUE']) ? $isSwitchUpgrade['VALUE'] : 0;
  328. if ($isOpen < 1) {
  329. return static::notice(Yii::t('app', 'theFunctionIsNotAvailable'), 400);
  330. }
  331. // 开始升级
  332. if (\Yii::$app->request->isPost) {
  333. $post = \Yii::$app->request->post();
  334. // 根据支付方式区分逻辑
  335. $payMethod = \Yii::$app->request->post('payType', '');
  336. if ($payMethod === 'pay_stack') {
  337. $formModel = new ApproachDeclarationUpgradeForm();
  338. } else {
  339. unset($post['payType']);
  340. $formModel = new DeclarationUpgradeForm();
  341. }
  342. $post['type'] = DeclarationForm::TYPE_ZC;
  343. if ($formModel->load($post, '') && $result = $formModel->add($post)) {
  344. return static::notice($result);// Upgrade declaration succeeded升级报单成功
  345. } else {
  346. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  347. }
  348. }
  349. // 升级的会员
  350. $userName = \Yii::$app->request->get('userName');
  351. // 会员信息
  352. $user = User::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => trim($userName)]);
  353. if (!$user) {
  354. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  355. }
  356. //所有报单套餐
  357. $allDecPackage = DeclarationPackage::getAllData();
  358. $decLevels = Cache::getDecLevelConfig();
  359. foreach ($allDecPackage as $k=>$v){
  360. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  361. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  362. }
  363. $baseInfo = Info::baseInfoZhByUserName($userNumber);
  364. // 分期的总期数
  365. $instalmentConfig = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  366. // 查询是否有FX的分期订单
  367. $stageFX = Instalment::getOneStage($baseInfo['ID'], 'FX');
  368. if ($stageFX && $stageFX < $instalmentConfig) {
  369. if ($isMax === "true") {
  370. $condition = ") AND S.INSTALMENT = 1000";
  371. } else {
  372. $condition = ") AND (S.INSTALMENT = 0 OR S.INSTALMENT = 1000)";
  373. }
  374. } else {
  375. // 是否有未完成的BD类型的分期订单
  376. $stageBD = Instalment::getOneStage($baseInfo['ID'], 'BD');
  377. if ($isMax === "true") {
  378. if ($stageBD > 0 && $stageBD < $instalmentConfig) {
  379. $condition = ") AND (S.INSTALMENT = " . ($stageBD + 1) . ")";
  380. } else {
  381. $condition = ") AND S.INSTALMENT = 0";
  382. }
  383. } else {
  384. if ($stageBD > 0 && $stageBD < $instalmentConfig) {
  385. $condition = ") AND (S.INSTALMENT = 0 OR S.INSTALMENT = " . ($stageBD + 1) . ")";
  386. } else {
  387. $condition = ") AND (S.INSTALMENT = 0 OR S.INSTALMENT = 1)";
  388. }
  389. }
  390. }
  391. //所有自选商品
  392. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  393. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  394. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  395. $decUserName = User::getEnCodeInfo(\Yii::$app->user->id)['USER_NAME'];
  396. $query_condition= " AND (1<>1";
  397. if(!$isDecReg || ($isDecReg && $isDec==1)){
  398. $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  399. }
  400. if($isStudio==1){
  401. $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  402. }
  403. $query_condition .= $condition . ' AND N.COUNTRY_ID=:COUNTRY_ID ';
  404. $allGoods = ShopGoods::find()
  405. ->alias('S')
  406. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID')
  407. ->join('LEFT JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  408. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $user['COUNTRY_ID']])
  409. ->orderBy('SORT ASC')
  410. ->asArray()
  411. ->all();
  412. foreach ($allGoods as &$goods) {
  413. $goods['TAX'] = Tool::calculateTax($goods['SELL_PRICE'], $goods['TAX_RATE']);
  414. }
  415. // 报单中心汇率
  416. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  417. $decCountry = Countries::getById($decCountryId);
  418. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  419. // 升级会员汇率
  420. $country = Countries::getById($user['COUNTRY_ID']);
  421. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  422. $userBalance = [
  423. 'cash' => 0,
  424. 'localCash' => 0
  425. ];
  426. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  427. if ($userCash) {
  428. $userBalance['cash'] = $userCash['CASH'];
  429. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  430. }
  431. // 行政区划
  432. $region = Region::getByCountryId($user['COUNTRY_ID']);
  433. return static::notice([
  434. 'allDecPackage' => $allDecPackage,
  435. 'allGoods' => $allGoods,
  436. 'decUserName'=>$decUserName,
  437. 'payList' => ShopGoods::payTypes(),
  438. 'sellType' => ShopGoods::getCategoryType(),
  439. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  440. 'userBalance' => $userBalance,
  441. 'decUserCurrencyRate' => $decUserCurrencyRate,
  442. 'userCurrencyRate' => $currencyRate,
  443. 'region' => $region,
  444. ]);
  445. }
  446. /**
  447. * 报单管理
  448. */
  449. public function actionDec() {
  450. $redis = \Yii::$app->redis;
  451. $post = \Yii::$app->request->post();
  452. if (\Yii::$app->request->isPost) {
  453. $userInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  454. if($userInfo['IS_DEC'] == 1 && !empty($post['decUserName']) && $post['decUserName'] != $userInfo['USER_NAME']){
  455. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  456. }
  457. if($userInfo['IS_DEC'] == 0 && $post['decUserName']){
  458. //查询报单编号是否存在
  459. $userId = Info::getUserIdByUserName($post['decUserName']);
  460. if(!$userId){
  461. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  462. }
  463. $isDec = User::getEnCodeInfo($userId)['IS_DEC'];
  464. if(!$isDec){
  465. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  466. }
  467. }
  468. // 根据支付方式区分逻辑
  469. $payMethod = \Yii::$app->request->post('payType', '');
  470. if ($payMethod === 'pay_stack') {
  471. $formModel = new ApproachDeclarationLoopForm();
  472. } else {
  473. unset($post['payType']);
  474. $formModel = new DeclarationLoopForm();
  475. }
  476. $formModel->scenario = 'userDec';
  477. $post['province'] = $post['province'] ? :1;
  478. $post['city'] = $post['city'] ? :1;
  479. $post['county'] = $post['county'] ? :1;
  480. $conUserName = trim($post['conUserName']); // 实际推荐人
  481. $recUserName = trim($post['recUserName']);
  482. $post['cityName'] = trim($post['cityName']);
  483. $post['lgaName'] = trim($post['lgaName']);
  484. // 针对那群沙雕移除安置网络的行为,强制增加自动安置左侧
  485. $recUserId = Info::getUserIdByUserName($conUserName);
  486. $bottomPlace = UserNetwork::getBottomPlace($recUserId,'', 'left');
  487. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  488. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  489. $bottomUser = $bottomUser['USER_NAME']; // 推算出的安置人
  490. $post['conUserName'] = $bottomUser; // 安置人
  491. $post['recUserName'] = $conUserName; // 实际推荐人
  492. $post['bottomUserName'] = $recUserName; // 推荐人
  493. $post['location'] = 1;
  494. // 针对于会员编号的判断
  495. if($post['decType']=='normal'){ // 普通报单,判断Redis中的用户名是否一致
  496. $insertUserName = strtoupper($post['insertUserName']);
  497. $getRedisUserName = $redis->get('key_' . $insertUserName);
  498. if (!$getRedisUserName){
  499. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  500. }
  501. if ($insertUserName != $getRedisUserName){
  502. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  503. }
  504. }else{ // BA升级,需要判断BA用户是否存在
  505. $insertUserName = $post['insertUserName'];
  506. }
  507. $post['insertUserName'] = $insertUserName;
  508. $post['type'] = DeclarationForm::TYPE_ZC;
  509. $allData['data'][] = $post;
  510. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  511. return static::notice($result);//报单成功
  512. } else {
  513. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  514. }
  515. }
  516. // 默认国家
  517. $countryId = \Yii::$app->request->get('countryId');
  518. if (!$countryId) {
  519. return static::notice(Yii::t('app', 'countryDoesNotSelect'), 400);
  520. }
  521. // 生成随机码 , 初始化redis
  522. $userName = Info::generateWebName($countryId);
  523. //所有报单套餐
  524. $allDecPackage = DeclarationPackage::getAllData();
  525. $decLevels = Cache::getDecLevelConfig();
  526. foreach ($allDecPackage as $k=>$v){
  527. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  528. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  529. }
  530. //所有自选商品
  531. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  532. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  533. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  534. $query_condition = " AND (1<>1";
  535. if(!$isDecReg || ($isDecReg && $isDec==1)){
  536. $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  537. }
  538. if($isStudio==1){
  539. $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  540. }
  541. $query_condition .= ") AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID";
  542. // 只查询普通商品
  543. $allGoods = ShopGoods::find()
  544. ->alias('S')
  545. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  546. ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  547. ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  548. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $countryId])
  549. ->orderBy('SORT ASC')
  550. ->asArray()
  551. ->all();
  552. // 银行开户行
  553. $allOpenBank = OpenBank::find()
  554. ->where('STATUS=:STATUS AND COUNTRY_ID=:COUNTRY_ID', [':STATUS' => 1, ':COUNTRY_ID' => $countryId])
  555. ->orderBy('BANK_NAME ASC')
  556. ->asArray()
  557. ->all();
  558. if (!$userName) {
  559. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  560. }
  561. //随机码保存在redis中方便进行比对
  562. $redis->setex('key_'.$userName , 3600 , $userName);
  563. // 报单中心会员的本地汇率
  564. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  565. $decCountry = Countries::getById($decCountryId);
  566. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  567. // 新会员的本地汇率
  568. $country = Countries::getById($countryId);
  569. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  570. // 会员账户
  571. $userBalance = [
  572. 'cash' => 0,
  573. 'localCash' => 0
  574. ];
  575. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  576. if ($userCash) {
  577. $userBalance['cash'] = $userCash['CASH'];
  578. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  579. $userBalance['coin'] = $country['CODE'];
  580. }
  581. // 行政区划
  582. $region = Region::getByCountryId($countryId);
  583. return static::notice([
  584. 'allDecPackage' => $allDecPackage,
  585. 'allGoods' => $allGoods,
  586. 'allOpenBank' => $allOpenBank,
  587. 'userName' => $userName,
  588. 'payList' => ShopGoods::payTypes(),
  589. 'sellType' => ShopGoods::getCategoryType(),
  590. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  591. 'userBalance' => $userBalance,
  592. 'isDec' => $isDec,
  593. 'decUserCurrencyRate' => $decUserCurrencyRate,
  594. 'userCurrencyRate' => $currencyRate,
  595. 'region' => $region,
  596. ]);
  597. }
  598. /**
  599. * 报单级别套餐
  600. */
  601. public function actionDecPackage() {
  602. $decLv = \Yii::$app->request->get('id');
  603. //所有报单级别套餐
  604. $allDecPackage = DeclarationPackage::getPackageFromLevelId($decLv);
  605. return static::notice(['allDecPackage' => $allDecPackage]);
  606. }
  607. /*
  608. * 从会员名和左右,获取底部安置人
  609. *
  610. */
  611. public function actionGetAutoPlace() {
  612. $userName = \Yii::$app->request->get('userName');
  613. $side = \Yii::$app->request->get('side');
  614. $userId = Info::getUserIdByUserName($userName);
  615. $bottomPlace = UserNetwork::getBottomPlace($userId,'', $side);
  616. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  617. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  618. return static::notice($bottomUser);
  619. }
  620. /**
  621. * 会员信息查询
  622. * @return mixed
  623. * @throws \yii\web\HttpException
  624. */
  625. public function actionFullInfo()
  626. {
  627. $userName = \Yii::$app->request->get('userName');
  628. $userId = Info::getUserIdByUserName($userName);
  629. $userInfo['REAL_NAME'] = '';
  630. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  631. if($user){
  632. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  633. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  634. $isLocation = [1 => 'Left-Null', 2 => 'Right-Null'];
  635. if($allChildUser) {
  636. foreach ($allChildUser as $child) {
  637. if ($child['RELATIVE_LOCATION']<'3'){
  638. // $isLocation[$child['RELATIVE_LOCATION']].= 'Full';
  639. $isLocation[$child['RELATIVE_LOCATION']] = str_replace('Null', 'Full', $isLocation[$child['RELATIVE_LOCATION']]);
  640. }
  641. }
  642. }
  643. $userInfo['countryId'] = $user['COUNTRY_ID'];
  644. $userInfo['userId'] = $userId;
  645. $userInfo['isLocation'] = '('.implode(',',$isLocation).')';
  646. return static::notice($userInfo);
  647. }else{
  648. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  649. }
  650. }
  651. public function actionInspectNetwork()
  652. {
  653. // 推荐人信息
  654. $recommenderName= \Yii::$app->request->get('recommenderName');
  655. $userId = Info::getUserIdByUserName($recommenderName);
  656. $recommender = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  657. if (!$recommender) {
  658. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  659. }
  660. // 实际推荐人信息
  661. $recommenderActualName= \Yii::$app->request->get('recommenderActualName');
  662. $actualId = Info::getUserIdByUserName($recommenderActualName);
  663. $recommenderActual = User::findOneAsArray('ID=:ID', [':ID' => $actualId]);
  664. if (!$recommenderActual) {
  665. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  666. }
  667. // 实际推荐人是否是推荐人本人
  668. if ($actualId == $userId) {
  669. return static::notice([
  670. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  671. 'userId' => $recommenderActual['ID'],
  672. ]);
  673. }
  674. // 实际推荐人是否是推荐人的推荐网上级
  675. $periodNum = Period::instance()->getNowPeriodNum();
  676. $relation = new Relation();
  677. $allowable = $relation->loopRelationParentDo(
  678. /**
  679. * @throws \Exception
  680. */
  681. $userId, function ($parent) use ($actualId, $periodNum) {
  682. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $periodNum);
  683. if ($parentUser['USER_ID'] == $actualId) {
  684. return self::LOOP_FINISH;
  685. }
  686. unset($parent);
  687. });
  688. if ($allowable) {
  689. return static::notice([
  690. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  691. 'userId' => $recommenderActual['ID'],
  692. ]);
  693. }
  694. return static::notice(Yii::t('ctx', 'fillSponsorUpline'), 400);
  695. }
  696. /**
  697. * 复消会员信息查询
  698. * @return mixed
  699. * @throws \yii\web\HttpException
  700. */
  701. public function actionUserBaseInfo()
  702. {
  703. $userName = \Yii::$app->request->get('userName');
  704. $userId = Info::getUserIdByUserName($userName);
  705. if($userId){
  706. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  707. if($allAddress) {
  708. foreach ($allAddress as $key => $row) {
  709. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  710. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  711. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  712. }
  713. }
  714. $userInfo = Info::baseInfoWithNet($userId);
  715. // $decLevelConfig = Cache::getDecLevelConfig();
  716. // $empLevelConfig = Cache::getEmpLevelConfig();
  717. $arr = [
  718. 'REAL_NAME'=>$userInfo['REAL_NAME'],
  719. // 'DEC_LEVEL_NAME' => $decLevelConfig[$userInfo['DEC_LV']]['LEVEL_NAME'],
  720. // 'EMP_LEVEL_NAME'=>$empLevelConfig[$userInfo['EMP_LV']]['LEVEL_NAME'],
  721. // 'REC_UID'=>$userInfo['REC_USER_NAME'].'('.$userInfo['REC_REAL_NAME'].')',
  722. // 'CON_UID'=>$userInfo['CON_USER_NAME'].'('.$userInfo['CON_REAL_NAME'].')',
  723. 'allAddress'=>$allAddress
  724. ];
  725. return static::notice($arr);
  726. }else{
  727. return static::notice(Yii::t('app', 'repeatSalesMemberNoDoesNotExist'), 400);
  728. }
  729. }
  730. /**
  731. * 会员资料
  732. * @return mixed
  733. * @throws \yii\web\HttpException
  734. */
  735. public function actionBaInfo()
  736. {
  737. $data['userInfo'] = BaUser::getEnCodeInfo(\Yii::$app->user->id);
  738. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  739. // 推荐人
  740. $baUserInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  741. $sponsor = User::findOneAsArray('ID=:USER_ID', [':USER_ID' => $baUserInfo['REC_UID']]);
  742. $data['userInfo']['SponsorCode'] = $sponsor['USER_NAME'];
  743. $data['userInfo']['SponsorName'] = $sponsor['REAL_NAME'];
  744. return static::notice($data);
  745. }
  746. /**
  747. * 编辑会员资料
  748. * @return mixed
  749. * @throws \yii\web\HttpException
  750. */
  751. public function actionBaEdit()
  752. {
  753. if(\Yii::$app->request->isPost)
  754. {
  755. $form = new BaUserForm();
  756. $post = \Yii::$app->request->post();
  757. $form->scenario = 'modifyProfile';
  758. if ($form->load($post, '') && $result = $form->modifyProfile()){
  759. return static::notice(Yii::t('app', 'personalDataModifiedSuccessfully'), 400);
  760. } else {
  761. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  762. }
  763. }
  764. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  765. }
  766. /**
  767. * 修改登录密码
  768. */
  769. public function actionBaPassword(){
  770. if(\Yii::$app->request->isPost) {
  771. $form = new BaUserForm();
  772. $form->scenario = 'modifyPassword';
  773. $post = \Yii::$app->request->post();
  774. if($form->load($post, '') && $result = $form->modifyPassword()){
  775. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  776. } else {
  777. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  778. }
  779. }
  780. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  781. }
  782. /**
  783. * 修改支付密码
  784. */
  785. public function actionBaPayPassword(){
  786. if(\Yii::$app->request->isPost){
  787. $form = new BaUserForm();
  788. $form->scenario = 'modifyPasswordPay';
  789. $post = \Yii::$app->request->post();
  790. $form->userId = \Yii::$app->user->id;
  791. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  792. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  793. } else {
  794. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  795. }
  796. }
  797. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  798. }
  799. /**
  800. * BA报单管理
  801. */
  802. public function actionBaDec() {
  803. // 生成随机码 , 初始化redis
  804. $userName = Info::generateWebUserName('NG',9);
  805. $redis = \Yii::$app->redis;
  806. $post = \Yii::$app->request->post();
  807. if (\Yii::$app->request->isPost) {
  808. // 根据支付方式区分逻辑
  809. $payMethod = \Yii::$app->request->post('payType', '');
  810. if ($payMethod === 'pay_stack') {
  811. $formModel = new BaApproachDeclarationLoopForm();
  812. } else {
  813. unset($post['payType']);
  814. $formModel = new BaDeclarationLoopForm();
  815. }
  816. $formModel->scenario = 'userDec';
  817. $post['province'] = $post['province'] ? :1;
  818. $post['city'] = $post['city'] ? :1;
  819. $post['county'] = $post['county'] ? :1;
  820. // $post['conUserName'] = trim($post['conUserName']);
  821. $post['recUserName'] = trim($post['recUserName']);
  822. $post['cityName'] = trim($post['cityName']);
  823. $post['lgaName'] = trim($post['lgaName']);
  824. // 针对于会员编号的判断
  825. $insertUserName = strtoupper($post['insertUserName']);
  826. $getRedisUserName = $redis->get('key_' . $insertUserName);
  827. if (!$getRedisUserName){
  828. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  829. }
  830. if ($insertUserName != $getRedisUserName){
  831. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  832. }
  833. $post['insertUserName'] = $insertUserName;
  834. $post['type'] = BaDeclarationForm::TYPE_ZC;
  835. $allData['data'][] = $post;
  836. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  837. return static::notice($result); // 报单成功
  838. } else {
  839. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  840. }
  841. }
  842. // 只查询普通商品
  843. $allGoods = ShopGoods::find()->where("ID='375895243322691584'")->orderBy('SORT ASC')->asArray()->all();
  844. if (!$userName) {
  845. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  846. }
  847. //随机码保存在redis中方便进行比对
  848. $redis->setex('key_'.$userName , 3600 , $userName);
  849. // 会员账户
  850. $userBalance = [
  851. 'cash' => 0,
  852. // 'tourism_points' => 0,
  853. // 'garage_points' => 0,
  854. // 'villa_points' => 0,
  855. ];
  856. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  857. if ($userCash) {
  858. $userBalance['cash'] = $userCash['CASH'];
  859. }
  860. return static::notice([
  861. 'allGoods' => $allGoods,
  862. 'userName' => $userName,
  863. 'payList' => ShopGoods::payTypes(),
  864. 'sellType' => ShopGoods::getCategoryType(),
  865. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  866. 'userBalance' => $userBalance,
  867. ]);
  868. }
  869. /**
  870. * BA会员信息
  871. * @return mixed
  872. * @throws \yii\web\HttpException
  873. */
  874. public function actionBaUserInfo()
  875. {
  876. $userName = \Yii::$app->request->get('userName');
  877. $userId = Info::getBaUserIdByUserName($userName);
  878. $userInfo['REAL_NAME'] = '';
  879. $user = BaUser::findOneAsArray('ID=:ID', [':ID' => $userId], 'REAL_NAME');
  880. $uInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'REC_UID');
  881. if($user){
  882. $rec_user = UserInfo::findOneAsArray('USER_ID=:USER_ID',[':USER_ID' => $uInfo['REC_UID']], 'USER_ID, USER_NAME');
  883. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  884. $userInfo['REC_USER_ID'] = $rec_user['USER_ID'];
  885. $userInfo['REC_USER_NAME'] = $rec_user['USER_NAME'];
  886. return static::notice($userInfo);
  887. }else{
  888. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  889. }
  890. }
  891. }