UserController.php 40 KB

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