UserController.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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,C.CODE')
  407. ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  408. ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  409. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $user['COUNTRY_ID']])
  410. ->orderBy('SORT ASC')
  411. ->asArray()
  412. ->all();
  413. foreach ($allGoods as &$goods) {
  414. $goods['TAX'] = Tool::calculateTax($goods['SELL_PRICE'], $goods['TAX_RATE']);
  415. }
  416. // 报单中心汇率
  417. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  418. $decCountry = Countries::getById($decCountryId);
  419. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  420. // 升级会员汇率
  421. $country = Countries::getById($user['COUNTRY_ID']);
  422. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  423. // 升级会员币种
  424. $currency = Currency::getById($country['LOCAL_CURRENCY_ID']);
  425. $userBalance = [
  426. 'cash' => 0,
  427. 'localCash' => 0,
  428. 'coin' => '',
  429. ];
  430. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  431. if ($userCash) {
  432. $userBalance['cash'] = $userCash['CASH'];
  433. $userBalance['coin'] = $currency['CODE'] ?? '';
  434. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  435. }
  436. // 行政区划
  437. $region = Region::getByCountryId($user['COUNTRY_ID']);
  438. return static::notice([
  439. 'allDecPackage' => $allDecPackage,
  440. 'allGoods' => $allGoods,
  441. 'decUserName'=>$decUserName,
  442. 'payList' => ShopGoods::payTypes(),
  443. 'sellType' => ShopGoods::getCategoryType(),
  444. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  445. 'userBalance' => $userBalance,
  446. 'decUserCurrencyRate' => $decUserCurrencyRate,
  447. 'userCurrencyRate' => $currencyRate,
  448. 'region' => $region,
  449. ]);
  450. }
  451. /**
  452. * 报单管理
  453. */
  454. public function actionDec() {
  455. $redis = \Yii::$app->redis;
  456. $post = \Yii::$app->request->post();
  457. if (\Yii::$app->request->isPost) {
  458. $userInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  459. if($userInfo['IS_DEC'] == 1 && !empty($post['decUserName']) && $post['decUserName'] != $userInfo['USER_NAME']){
  460. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  461. }
  462. if($userInfo['IS_DEC'] == 0 && $post['decUserName']){
  463. //查询报单编号是否存在
  464. $userId = Info::getUserIdByUserName($post['decUserName']);
  465. if(!$userId){
  466. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  467. }
  468. $isDec = User::getEnCodeInfo($userId)['IS_DEC'];
  469. if(!$isDec){
  470. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  471. }
  472. }
  473. // 根据支付方式区分逻辑
  474. $payMethod = \Yii::$app->request->post('payType', '');
  475. if ($payMethod === 'pay_stack') {
  476. $formModel = new ApproachDeclarationLoopForm();
  477. } else {
  478. unset($post['payType']);
  479. $formModel = new DeclarationLoopForm();
  480. }
  481. $formModel->scenario = 'userDec';
  482. $post['province'] = $post['province'] ? :1;
  483. $post['city'] = $post['city'] ? :1;
  484. $post['county'] = $post['county'] ? :1;
  485. $conUserName = trim($post['conUserName']); // 实际推荐人
  486. $recUserName = trim($post['recUserName']);
  487. $post['cityName'] = trim($post['cityName']);
  488. $post['lgaName'] = trim($post['lgaName']);
  489. // 针对那群沙雕移除安置网络的行为,强制增加自动安置左侧
  490. $recUserId = Info::getUserIdByUserName($conUserName);
  491. $bottomPlace = UserNetwork::getBottomPlace($recUserId,'', 'left');
  492. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  493. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  494. $bottomUser = $bottomUser['USER_NAME']; // 推算出的安置人
  495. $post['conUserName'] = $bottomUser; // 安置人
  496. $post['recUserName'] = $conUserName; // 实际推荐人
  497. $post['bottomUserName'] = $recUserName; // 推荐人
  498. $post['location'] = 1;
  499. // 针对于会员编号的判断
  500. if($post['decType']=='normal'){ // 普通报单,判断Redis中的用户名是否一致
  501. $insertUserName = strtoupper($post['insertUserName']);
  502. $getRedisUserName = $redis->get('key_' . $insertUserName);
  503. if (!$getRedisUserName){
  504. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  505. }
  506. if ($insertUserName != $getRedisUserName){
  507. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  508. }
  509. }else{ // BA升级,需要判断BA用户是否存在
  510. $insertUserName = $post['insertUserName'];
  511. }
  512. $post['insertUserName'] = $insertUserName;
  513. $post['type'] = DeclarationForm::TYPE_ZC;
  514. $allData['data'][] = $post;
  515. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  516. return static::notice($result);//报单成功
  517. } else {
  518. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  519. }
  520. }
  521. // 默认国家
  522. $countryId = \Yii::$app->request->get('countryId');
  523. if (!$countryId) {
  524. return static::notice(Yii::t('app', 'countryDoesNotSelect'), 400);
  525. }
  526. // 生成随机码 , 初始化redis
  527. $userName = Info::generateWebName($countryId);
  528. //所有报单套餐
  529. $allDecPackage = DeclarationPackage::getAllData();
  530. $decLevels = Cache::getDecLevelConfig();
  531. foreach ($allDecPackage as $k=>$v){
  532. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  533. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  534. }
  535. //所有自选商品
  536. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  537. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  538. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  539. $query_condition = " AND (1<>1";
  540. if(!$isDecReg || ($isDecReg && $isDec==1)){
  541. $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  542. }
  543. if($isStudio==1){
  544. $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  545. }
  546. $query_condition .= ") AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID";
  547. // 只查询普通商品
  548. $allGoods = ShopGoods::find()
  549. ->alias('S')
  550. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  551. ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  552. ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  553. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $countryId])
  554. ->orderBy('SORT ASC')
  555. ->asArray()
  556. ->all();
  557. // 银行开户行
  558. $allOpenBank = OpenBank::find()
  559. ->where('STATUS=:STATUS AND COUNTRY_ID=:COUNTRY_ID', [':STATUS' => 1, ':COUNTRY_ID' => $countryId])
  560. ->orderBy('BANK_NAME ASC')
  561. ->asArray()
  562. ->all();
  563. if (!$userName) {
  564. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  565. }
  566. //随机码保存在redis中方便进行比对
  567. $redis->setex('key_'.$userName , 3600 , $userName);
  568. // 报单中心会员的本地汇率
  569. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  570. $decCountry = Countries::getById($decCountryId);
  571. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  572. // 新会员的本地汇率
  573. $country = Countries::getById($countryId);
  574. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  575. // 新会员币种
  576. $currency = Currency::getById($country['LOCAL_CURRENCY_ID']);
  577. // 会员账户
  578. $userBalance = [
  579. 'cash' => 0,
  580. 'localCash' => 0,
  581. 'coin' => '',
  582. ];
  583. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  584. if ($userCash) {
  585. $userBalance['cash'] = $userCash['CASH'];
  586. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  587. $userBalance['coin'] = $currency['CODE'] ?? '';
  588. }
  589. // 行政区划
  590. $region = Region::getByCountryId($countryId);
  591. return static::notice([
  592. 'allDecPackage' => $allDecPackage,
  593. 'allGoods' => $allGoods,
  594. 'allOpenBank' => $allOpenBank,
  595. 'userName' => $userName,
  596. 'payList' => ShopGoods::payTypes(),
  597. 'sellType' => ShopGoods::getCategoryType(),
  598. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  599. 'userBalance' => $userBalance,
  600. 'isDec' => $isDec,
  601. 'decUserCurrencyRate' => $decUserCurrencyRate,
  602. 'userCurrencyRate' => $currencyRate,
  603. 'region' => $region,
  604. ]);
  605. }
  606. /**
  607. * 报单级别套餐
  608. */
  609. public function actionDecPackage() {
  610. $decLv = \Yii::$app->request->get('id');
  611. //所有报单级别套餐
  612. $allDecPackage = DeclarationPackage::getPackageFromLevelId($decLv);
  613. return static::notice(['allDecPackage' => $allDecPackage]);
  614. }
  615. /*
  616. * 从会员名和左右,获取底部安置人
  617. *
  618. */
  619. public function actionGetAutoPlace() {
  620. $userName = \Yii::$app->request->get('userName');
  621. $side = \Yii::$app->request->get('side');
  622. $userId = Info::getUserIdByUserName($userName);
  623. $bottomPlace = UserNetwork::getBottomPlace($userId,'', $side);
  624. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  625. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  626. return static::notice($bottomUser);
  627. }
  628. /**
  629. * 会员信息查询
  630. * @return mixed
  631. * @throws \yii\web\HttpException
  632. */
  633. public function actionFullInfo()
  634. {
  635. $userName = \Yii::$app->request->get('userName');
  636. $userId = Info::getUserIdByUserName($userName);
  637. $userInfo['REAL_NAME'] = '';
  638. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  639. if($user){
  640. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  641. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  642. $isLocation = [1 => 'Left-Null', 2 => 'Right-Null'];
  643. if($allChildUser) {
  644. foreach ($allChildUser as $child) {
  645. if ($child['RELATIVE_LOCATION']<'3'){
  646. // $isLocation[$child['RELATIVE_LOCATION']].= 'Full';
  647. $isLocation[$child['RELATIVE_LOCATION']] = str_replace('Null', 'Full', $isLocation[$child['RELATIVE_LOCATION']]);
  648. }
  649. }
  650. }
  651. $userInfo['countryId'] = $user['COUNTRY_ID'];
  652. $userInfo['userId'] = $userId;
  653. $userInfo['isLocation'] = '('.implode(',',$isLocation).')';
  654. return static::notice($userInfo);
  655. }else{
  656. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  657. }
  658. }
  659. public function actionInspectNetwork()
  660. {
  661. // 推荐人信息
  662. $recommenderName= \Yii::$app->request->get('recommenderName');
  663. $userId = Info::getUserIdByUserName($recommenderName);
  664. $recommender = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  665. if (!$recommender) {
  666. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  667. }
  668. // 实际推荐人信息
  669. $recommenderActualName= \Yii::$app->request->get('recommenderActualName');
  670. $actualId = Info::getUserIdByUserName($recommenderActualName);
  671. $recommenderActual = User::findOneAsArray('ID=:ID', [':ID' => $actualId]);
  672. if (!$recommenderActual) {
  673. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  674. }
  675. // 实际推荐人是否是推荐人本人
  676. if ($actualId == $userId) {
  677. return static::notice([
  678. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  679. 'userId' => $recommenderActual['ID'],
  680. ]);
  681. }
  682. // 实际推荐人是否是推荐人的推荐网上级
  683. $periodNum = Period::instance()->getNowPeriodNum();
  684. $relation = new Relation();
  685. $allowable = $relation->loopRelationParentDo($userId, function ($parent) use ($actualId, $periodNum) {
  686. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $periodNum);
  687. if ($parentUser['USER_ID'] == $actualId) {
  688. return self::LOOP_FINISH;
  689. }
  690. unset($parent);
  691. });
  692. if ($allowable) {
  693. return static::notice([
  694. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  695. 'userId' => $recommenderActual['ID'],
  696. ]);
  697. }
  698. return static::notice(Yii::t('ctx', 'fillSponsorUpline'), 400);
  699. }
  700. /**
  701. * 复消会员信息查询
  702. * @return mixed
  703. * @throws \yii\web\HttpException
  704. */
  705. public function actionUserBaseInfo()
  706. {
  707. $userName = \Yii::$app->request->get('userName');
  708. $userId = Info::getUserIdByUserName($userName);
  709. if($userId){
  710. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  711. if($allAddress) {
  712. foreach ($allAddress as $key => $row) {
  713. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  714. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  715. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  716. }
  717. }
  718. $userInfo = Info::baseInfoWithNet($userId);
  719. // $decLevelConfig = Cache::getDecLevelConfig();
  720. // $empLevelConfig = Cache::getEmpLevelConfig();
  721. $arr = [
  722. 'REAL_NAME'=>$userInfo['REAL_NAME'],
  723. // 'DEC_LEVEL_NAME' => $decLevelConfig[$userInfo['DEC_LV']]['LEVEL_NAME'],
  724. // 'EMP_LEVEL_NAME'=>$empLevelConfig[$userInfo['EMP_LV']]['LEVEL_NAME'],
  725. // 'REC_UID'=>$userInfo['REC_USER_NAME'].'('.$userInfo['REC_REAL_NAME'].')',
  726. // 'CON_UID'=>$userInfo['CON_USER_NAME'].'('.$userInfo['CON_REAL_NAME'].')',
  727. 'allAddress'=>$allAddress
  728. ];
  729. return static::notice($arr);
  730. }else{
  731. return static::notice(Yii::t('app', 'repeatSalesMemberNoDoesNotExist'), 400);
  732. }
  733. }
  734. /**
  735. * 会员资料
  736. * @return mixed
  737. * @throws \yii\web\HttpException
  738. */
  739. public function actionBaInfo()
  740. {
  741. $data['userInfo'] = BaUser::getEnCodeInfo(\Yii::$app->user->id);
  742. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  743. // 推荐人
  744. $baUserInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  745. $sponsor = User::findOneAsArray('ID=:USER_ID', [':USER_ID' => $baUserInfo['REC_UID']]);
  746. $data['userInfo']['SponsorCode'] = $sponsor['USER_NAME'];
  747. $data['userInfo']['SponsorName'] = $sponsor['REAL_NAME'];
  748. return static::notice($data);
  749. }
  750. /**
  751. * 编辑会员资料
  752. * @return mixed
  753. * @throws \yii\web\HttpException
  754. */
  755. public function actionBaEdit()
  756. {
  757. if(\Yii::$app->request->isPost)
  758. {
  759. $form = new BaUserForm();
  760. $post = \Yii::$app->request->post();
  761. $form->scenario = 'modifyProfile';
  762. if ($form->load($post, '') && $result = $form->modifyProfile()){
  763. return static::notice(Yii::t('app', 'personalDataModifiedSuccessfully'), 400);
  764. } else {
  765. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  766. }
  767. }
  768. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  769. }
  770. /**
  771. * 修改登录密码
  772. */
  773. public function actionBaPassword(){
  774. if(\Yii::$app->request->isPost) {
  775. $form = new BaUserForm();
  776. $form->scenario = 'modifyPassword';
  777. $post = \Yii::$app->request->post();
  778. if($form->load($post, '') && $result = $form->modifyPassword()){
  779. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  780. } else {
  781. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  782. }
  783. }
  784. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  785. }
  786. /**
  787. * 修改支付密码
  788. */
  789. public function actionBaPayPassword(){
  790. if(\Yii::$app->request->isPost){
  791. $form = new BaUserForm();
  792. $form->scenario = 'modifyPasswordPay';
  793. $post = \Yii::$app->request->post();
  794. $form->userId = \Yii::$app->user->id;
  795. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  796. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  797. } else {
  798. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  799. }
  800. }
  801. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  802. }
  803. /**
  804. * BA报单管理
  805. */
  806. public function actionBaDec() {
  807. // 生成随机码 , 初始化redis
  808. $userName = Info::generateWebUserName('NG',9);
  809. $redis = \Yii::$app->redis;
  810. $post = \Yii::$app->request->post();
  811. if (\Yii::$app->request->isPost) {
  812. // 根据支付方式区分逻辑
  813. $payMethod = \Yii::$app->request->post('payType', '');
  814. if ($payMethod === 'pay_stack') {
  815. $formModel = new BaApproachDeclarationLoopForm();
  816. } else {
  817. unset($post['payType']);
  818. $formModel = new BaDeclarationLoopForm();
  819. }
  820. $formModel->scenario = 'userDec';
  821. $post['province'] = $post['province'] ? :1;
  822. $post['city'] = $post['city'] ? :1;
  823. $post['county'] = $post['county'] ? :1;
  824. // $post['conUserName'] = trim($post['conUserName']);
  825. $post['recUserName'] = trim($post['recUserName']);
  826. $post['cityName'] = trim($post['cityName']);
  827. $post['lgaName'] = trim($post['lgaName']);
  828. // 针对于会员编号的判断
  829. $insertUserName = strtoupper($post['insertUserName']);
  830. $getRedisUserName = $redis->get('key_' . $insertUserName);
  831. if (!$getRedisUserName){
  832. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  833. }
  834. if ($insertUserName != $getRedisUserName){
  835. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  836. }
  837. $post['insertUserName'] = $insertUserName;
  838. $post['type'] = BaDeclarationForm::TYPE_ZC;
  839. $allData['data'][] = $post;
  840. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  841. return static::notice($result); // 报单成功
  842. } else {
  843. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  844. }
  845. }
  846. // 只查询普通商品
  847. $allGoods = ShopGoods::find()->where("ID='375895243322691584'")->orderBy('SORT ASC')->asArray()->all();
  848. if (!$userName) {
  849. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  850. }
  851. //随机码保存在redis中方便进行比对
  852. $redis->setex('key_'.$userName , 3600 , $userName);
  853. // 会员账户
  854. $userBalance = [
  855. 'cash' => 0,
  856. // 'tourism_points' => 0,
  857. // 'garage_points' => 0,
  858. // 'villa_points' => 0,
  859. ];
  860. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  861. if ($userCash) {
  862. $userBalance['cash'] = $userCash['CASH'];
  863. }
  864. return static::notice([
  865. 'allGoods' => $allGoods,
  866. 'userName' => $userName,
  867. 'payList' => ShopGoods::payTypes(),
  868. 'sellType' => ShopGoods::getCategoryType(),
  869. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  870. 'userBalance' => $userBalance,
  871. ]);
  872. }
  873. /**
  874. * BA会员信息
  875. * @return mixed
  876. * @throws \yii\web\HttpException
  877. */
  878. public function actionBaUserInfo()
  879. {
  880. $userName = \Yii::$app->request->get('userName');
  881. $userId = Info::getBaUserIdByUserName($userName);
  882. $userInfo['REAL_NAME'] = '';
  883. $user = BaUser::findOneAsArray('ID=:ID', [':ID' => $userId], 'REAL_NAME');
  884. $uInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'REC_UID');
  885. if($user){
  886. $rec_user = UserInfo::findOneAsArray('USER_ID=:USER_ID',[':USER_ID' => $uInfo['REC_UID']], 'USER_ID, USER_NAME');
  887. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  888. $userInfo['REC_USER_ID'] = $rec_user['USER_ID'];
  889. $userInfo['REC_USER_NAME'] = $rec_user['USER_NAME'];
  890. return static::notice($userInfo);
  891. }else{
  892. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  893. }
  894. }
  895. }