UserController.php 39 KB

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