UserController.php 58 KB

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