UserController.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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('Unable to process guest order.', 500);
  633. }
  634. }
  635. public function actionGuestDecDebugPaid()
  636. {
  637. try {
  638. $input = Yii::$app->request->post();
  639. $result = (new GuestDeclarationOrderService())->debugCompletePaidDraft(
  640. (string) ($input['draftId'] ?? ''),
  641. (string) ($input['accessToken'] ?? '')
  642. );
  643. return static::notice($result);
  644. } catch (\Throwable $e) {
  645. LoggerTool::error([
  646. 'event' => 'guestDec_debug_paid_failed',
  647. 'exception' => get_class($e),
  648. 'message' => $e->getMessage(),
  649. 'file' => basename($e->getFile()),
  650. 'line' => $e->getLine(),
  651. ]);
  652. if ($e instanceof \yii\web\HttpException) {
  653. return static::notice($e->getMessage(), $e->statusCode);
  654. }
  655. return static::notice('Unable to debug-complete guest order.', 500);
  656. }
  657. }
  658. public function actionGuestDecStatus()
  659. {
  660. try {
  661. $result = (new GuestDeclarationOrderService())->getDraftStatus(
  662. (string) Yii::$app->request->get('draftId', ''),
  663. (string) Yii::$app->request->get('accessToken', '')
  664. );
  665. return static::notice($result);
  666. } catch (\Throwable $e) {
  667. return static::notice('Guest order draft was not found.', 404);
  668. }
  669. }
  670. public function actionDec() {
  671. $redis = \Yii::$app->redis;
  672. $post = \Yii::$app->request->post();
  673. if (\Yii::$app->request->isPost) {
  674. $email = $post['email'] ?? '';
  675. if ($email && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  676. return static::notice(Yii::t('app', 'invalidEmail'), 400);
  677. }
  678. $stockist = $post['decUserName'] ?? '';
  679. if (!$stockist) {
  680. return static::notice(Yii::t('app', 'stockistDoesNotExist'), 400);
  681. }
  682. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  683. if($loginUserInfo['IS_DEC'] == 1 && !empty($post['decUserName']) && $post['decUserName'] != $loginUserInfo['USER_NAME']){
  684. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  685. }
  686. if($loginUserInfo['IS_DEC'] == 0 && $post['decUserName']){
  687. //查询报单编号是否存在
  688. $userId = Info::getUserIdByUserName($post['decUserName']);
  689. if(!$userId){
  690. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  691. }
  692. $isDec = User::getEnCodeInfo($userId)['IS_DEC'];
  693. if(!$isDec){
  694. return static::notice(Yii::t('app', 'decUserNameIsWrong'), 400);
  695. }
  696. }
  697. $conUserName = trim($post['conUserName']); // 实际推荐人
  698. $recUserName = trim($post['recUserName']);
  699. // 如果推荐人和实际推荐人是同一人,则跳过检查
  700. if ($recUserName != $conUserName) {
  701. // 实际推荐人必须是推荐人的推荐网上级
  702. $sqlUp = <<<SQL
  703. WITH recursive t_rec AS
  704. (
  705. SELECT
  706. m.USER_ID,
  707. U.USER_NAME,
  708. m.PARENT_UID,
  709. U2.USER_NAME AS PARENT_NAME,
  710. 1 AS node_level
  711. FROM
  712. AR_USER_RELATION_NEW m
  713. LEFT JOIN AR_USER U ON U.ID = m.USER_ID
  714. LEFT JOIN AR_USER U2 ON U2.ID = m.PARENT_UID
  715. WHERE
  716. U.USER_NAME = :USER_NAME
  717. UNION ALL
  718. SELECT
  719. t1.USER_ID,
  720. U.USER_NAME,
  721. t1.PARENT_UID,
  722. U2.USER_NAME AS PARENT_NAME,
  723. t2.node_level + 1 -- 结点层级
  724. FROM
  725. AR_USER_RELATION_NEW t1
  726. JOIN t_rec t2 ON t2.PARENT_UID = t1.USER_ID
  727. LEFT JOIN AR_USER U ON U.ID = t1.USER_ID
  728. LEFT JOIN AR_USER U2 ON U2.ID = t1.PARENT_UID
  729. )
  730. SELECT
  731. *
  732. FROM
  733. t_rec
  734. SQL;
  735. // 查询推荐人的所有上级
  736. $relationNodeUp = \Yii::$app->db->createCommand($sqlUp)
  737. ->bindValue(':USER_NAME', $post['recUserName'])
  738. ->queryAll();
  739. $relationNodeUp = array_column($relationNodeUp, 'USER_NAME');
  740. unset($relationNodeUp[$post['recUserName']]);
  741. if (!in_array($post['conUserName'], $relationNodeUp)) {
  742. return static::notice(Yii::t('app', 'fillSponsorUpline'), 400);
  743. }
  744. }
  745. // 根据支付方式区分逻辑
  746. $payMethod = \Yii::$app->request->post('payType', '');
  747. if ($payMethod === 'pay_stack') {
  748. $formModel = new ApproachDeclarationLoopForm();
  749. } else {
  750. unset($post['payType']);
  751. $formModel = new DeclarationLoopForm();
  752. }
  753. $formModel->scenario = 'userDec';
  754. $post['province'] = $post['province'] ? :1;
  755. $post['city'] = $post['city'] ? :1;
  756. $post['county'] = $post['county'] ? :1;
  757. $post['cityName'] = trim($post['cityName']);
  758. $post['lgaName'] = trim($post['lgaName']);
  759. // 针对那群沙雕移除安置网络的行为,强制增加自动安置左侧
  760. $recUserId = Info::getUserIdByUserName($conUserName);
  761. $bottomPlace = UserNetwork::getBottomPlace($recUserId,'', 'left');
  762. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  763. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  764. $bottomUser = $bottomUser['USER_NAME']; // 推算出的安置人
  765. $post['conUserName'] = $bottomUser; // 安置人
  766. $post['recUserName'] = $conUserName; // 实际推荐人
  767. $post['bottomUserName'] = $recUserName; // 推荐人
  768. $post['location'] = 1;
  769. // 针对于会员编号的判断
  770. $insertUserName = strtoupper($post['insertUserName']);
  771. $getRedisUserName = $redis->get('key_' . $insertUserName);
  772. if (!$getRedisUserName){
  773. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  774. }
  775. if ($insertUserName != $getRedisUserName){
  776. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  777. }
  778. $post['insertUserName'] = $insertUserName;
  779. $post['type'] = DeclarationForm::TYPE_ZC;
  780. $allData['data'][] = $post;
  781. try {
  782. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  783. //发送邮件
  784. // Email::sendRegistrationEmail($post['email'], $post['insertUserName'], $post['password']);
  785. return static::notice($result);//报单成功
  786. } else {
  787. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  788. }
  789. } catch (\Exception $e) {
  790. LoggerTool::error($e->getMessage() . '; ' . $e->getLine() . '; ' . $e->getFile());
  791. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  792. }
  793. }
  794. // 默认国家
  795. $countryId = \Yii::$app->request->get('countryId');
  796. if (!$countryId) {
  797. return static::notice(Yii::t('app', 'countryDoesNotSelect'), 400);
  798. }
  799. // 生成随机码 , 初始化redis
  800. $userName = Info::generateWebName($countryId);
  801. //所有报单套餐
  802. $allDecPackage = DeclarationPackage::getAllData();
  803. $decLevels = Cache::getDecLevelConfig();
  804. foreach ($allDecPackage as $k=>$v){
  805. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  806. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  807. }
  808. //所有自选商品
  809. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  810. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  811. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  812. $query_condition = " AND (1<>1";
  813. if(!$isDecReg || ($isDecReg && $isDec==1)){
  814. $query_condition = " AND (FIND_IN_SET(1, S.GIFT_TYPE)>0";
  815. }
  816. if($isStudio==1){
  817. $query_condition .= " OR FIND_IN_SET(3, S.GIFT_TYPE)>0";
  818. }
  819. $query_condition .= ") AND (S.INSTALMENT=0 OR S.INSTALMENT=1) AND N.COUNTRY_ID=:COUNTRY_ID";
  820. // 只查询普通商品
  821. $allGoods = ShopGoods::find()
  822. ->alias('S')
  823. ->select('S.*,N.MARKET_PRICE,N.SELL_PRICE,N.TAX_RATE,N.LOCAL_CURRENCY_ID,N.COUNTRY_ID,C.CODE')
  824. ->join('INNER JOIN', ShopGoodsNature::tableName() . ' AS N', 'N.GOODS_ID=S.ID')
  825. ->join('INNER JOIN', Currency::tableName() . ' AS C', 'C.ID=N.LOCAL_CURRENCY_ID')
  826. ->where("STATUS=1 AND CATEGORY_TYPE=1 " . $query_condition, [':COUNTRY_ID' => $countryId])
  827. ->orderBy('SORT ASC')
  828. ->asArray()
  829. ->all();
  830. // 银行开户行
  831. $allOpenBank = OpenBank::find()
  832. ->where('STATUS=:STATUS AND COUNTRY_ID=:COUNTRY_ID', [':STATUS' => 1, ':COUNTRY_ID' => $countryId])
  833. ->orderBy('BANK_NAME ASC')
  834. ->asArray()
  835. ->all();
  836. if (!$userName) {
  837. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  838. }
  839. //随机码保存在redis中方便进行比对
  840. $redis->setex('key_'.$userName , 3600 , $userName);
  841. // 报单中心会员的本地汇率
  842. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  843. $decCountry = Countries::getById($decCountryId);
  844. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  845. // 新会员的本地汇率
  846. $country = Countries::getById($countryId);
  847. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  848. // 新会员币种
  849. $currency = Currency::getById($country['LOCAL_CURRENCY_ID']);
  850. // 会员账户
  851. $userBalance = [
  852. 'cash' => 0,
  853. 'localCash' => 0,
  854. 'coin' => '',
  855. ];
  856. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  857. if ($userCash) {
  858. $userBalance['cash'] = $userCash['CASH'];
  859. $userBalance['localCash'] = Tool::convertAmount($userCash['CASH'], $decUserCurrencyRate, $currencyRate);
  860. $userBalance['coin'] = $currency['CODE'] ?? '';
  861. }
  862. // 行政区划
  863. $region = Region::getByCountryId($countryId);
  864. // 支付方式
  865. $payList = ShopGoods::payTypes();
  866. unset($payList['prp']);
  867. // 售卖方式
  868. $sellType = ShopGoods::getCategoryType();
  869. unset($sellType[0]['sell_type'][1]);
  870. return static::notice([
  871. 'allDecPackage' => $allDecPackage,
  872. 'allGoods' => $allGoods,
  873. 'allOpenBank' => $allOpenBank,
  874. 'userName' => $userName,
  875. 'payList' => $payList,
  876. 'sellType' => $sellType,
  877. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  878. 'userBalance' => $userBalance,
  879. 'isDec' => $isDec,
  880. 'decUserCurrencyRate' => $decUserCurrencyRate,
  881. 'userCurrencyRate' => $currencyRate,
  882. 'region' => $region,
  883. 'decLevel' => DeclarationLevel::getData(),
  884. ]);
  885. }
  886. /**
  887. * 报单级别套餐
  888. */
  889. public function actionDecPackage() {
  890. $decLv = \Yii::$app->request->get('id');
  891. //所有报单级别套餐
  892. $allDecPackage = DeclarationPackage::getPackageFromLevelId($decLv);
  893. return static::notice(['allDecPackage' => $allDecPackage]);
  894. }
  895. /*
  896. * 从会员名和左右,获取底部安置人
  897. *
  898. */
  899. public function actionGetAutoPlace() {
  900. $userName = \Yii::$app->request->get('userName');
  901. $side = \Yii::$app->request->get('side');
  902. $userId = Info::getUserIdByUserName($userName);
  903. $bottomPlace = UserNetwork::getBottomPlace($userId,'', $side);
  904. $bottomUser = UserNetwork::find()->select('USER_ID')->where('LOCATION_TAG=:LOCATION_TAG', ['LOCATION_TAG' => $bottomPlace])->asArray()->one();
  905. $bottomUser = User::findOneAsArray('ID=:ID', [':ID' => $bottomUser['USER_ID']], 'USER_NAME');
  906. return static::notice($bottomUser);
  907. }
  908. /**
  909. * 会员信息查询
  910. * @return mixed
  911. * @throws \yii\web\HttpException
  912. */
  913. public function actionFullInfo()
  914. {
  915. $userName = \Yii::$app->request->get('userName');
  916. $userId = Info::getUserIdByUserName($userName);
  917. $userInfo['REAL_NAME'] = '';
  918. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  919. $loginUserInfo = User::getEnCodeInfo(\Yii::$app->user->id);
  920. if ($user['COUNTRY_ID'] !== $loginUserInfo['COUNTRY_ID']) {
  921. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  922. }
  923. if($user){
  924. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  925. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  926. $isLocation = [1 => 'Left-Null', 2 => 'Right-Null'];
  927. if($allChildUser) {
  928. foreach ($allChildUser as $child) {
  929. if ($child['RELATIVE_LOCATION']<'3'){
  930. // $isLocation[$child['RELATIVE_LOCATION']].= 'Full';
  931. $isLocation[$child['RELATIVE_LOCATION']] = str_replace('Null', 'Full', $isLocation[$child['RELATIVE_LOCATION']]);
  932. }
  933. }
  934. }
  935. $userInfo['countryId'] = $user['COUNTRY_ID'];
  936. $userInfo['userId'] = $userId;
  937. $userInfo['isLocation'] = '('.implode(',',$isLocation).')';
  938. return static::notice($userInfo);
  939. }else{
  940. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  941. }
  942. }
  943. /**
  944. * Public member information lookup for guest declaration flows.
  945. * @return mixed
  946. */
  947. public function actionGuestFullInfo()
  948. {
  949. $userName = trim((string) \Yii::$app->request->get('userName', ''));
  950. $userId = Info::getUserIdByUserName($userName);
  951. $user = $userId ? User::findOneAsArray('ID=:ID', [':ID' => $userId]) : null;
  952. if (!$user) {
  953. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  954. }
  955. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  956. $isLocation = [1 => 'Left-Null', 2 => 'Right-Null'];
  957. if ($allChildUser) {
  958. foreach ($allChildUser as $child) {
  959. if ($child['RELATIVE_LOCATION'] < '3' && isset($isLocation[$child['RELATIVE_LOCATION']])) {
  960. $isLocation[$child['RELATIVE_LOCATION']] = str_replace('Null', 'Full', $isLocation[$child['RELATIVE_LOCATION']]);
  961. }
  962. }
  963. }
  964. return static::notice([
  965. 'REAL_NAME' => $user['REAL_NAME'],
  966. 'countryId' => $user['COUNTRY_ID'],
  967. 'userId' => $userId,
  968. 'isLocation' => '(' . implode(',', $isLocation) . ')',
  969. ]);
  970. }
  971. public function actionInspectNetwork()
  972. {
  973. // 推荐人信息
  974. $recommenderName= \Yii::$app->request->get('recommenderName');
  975. $userId = Info::getUserIdByUserName($recommenderName);
  976. $recommender = User::findOneAsArray('ID=:ID', [':ID' => $userId]);
  977. if (!$recommender) {
  978. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  979. }
  980. // 实际推荐人信息
  981. $recommenderActualName= \Yii::$app->request->get('recommenderActualName');
  982. $actualId = Info::getUserIdByUserName($recommenderActualName);
  983. $recommenderActual = User::findOneAsArray('ID=:ID', [':ID' => $actualId]);
  984. if (!$recommenderActual) {
  985. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  986. }
  987. // 实际推荐人是否是推荐人本人
  988. if ($actualId == $userId) {
  989. return static::notice([
  990. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  991. 'userId' => $recommenderActual['ID'],
  992. ]);
  993. }
  994. // 实际推荐人必须是推荐人的推荐网上级
  995. $sqlUp =<<<SQL
  996. WITH recursive t_rec AS
  997. (
  998. SELECT
  999. m.USER_ID,
  1000. U.USER_NAME,
  1001. m.PARENT_UID,
  1002. U2.USER_NAME AS PARENT_NAME,
  1003. 1 AS node_level
  1004. FROM
  1005. AR_USER_RELATION_NEW m
  1006. LEFT JOIN AR_USER U ON U.ID = m.USER_ID
  1007. LEFT JOIN AR_USER U2 ON U2.ID = m.PARENT_UID
  1008. WHERE
  1009. U.USER_NAME = :USER_NAME
  1010. UNION ALL
  1011. SELECT
  1012. t1.USER_ID,
  1013. U.USER_NAME,
  1014. t1.PARENT_UID,
  1015. U2.USER_NAME AS PARENT_NAME,
  1016. t2.node_level + 1 -- 结点层级
  1017. FROM
  1018. AR_USER_RELATION_NEW t1
  1019. JOIN t_rec t2 ON t2.PARENT_UID = t1.USER_ID
  1020. LEFT JOIN AR_USER U ON U.ID = t1.USER_ID
  1021. LEFT JOIN AR_USER U2 ON U2.ID = t1.PARENT_UID
  1022. )
  1023. SELECT
  1024. *
  1025. FROM
  1026. t_rec
  1027. SQL;
  1028. // 查询推荐人的所有上级
  1029. $relationNodeUp = \Yii::$app->db->createCommand($sqlUp)
  1030. ->bindValue(':USER_NAME' , $recommenderName)
  1031. ->queryAll();
  1032. $relationNodeUp = array_column($relationNodeUp, 'USER_NAME');
  1033. unset($relationNodeUp[$recommenderName]);
  1034. if (in_array($recommenderActualName, $relationNodeUp)) {
  1035. return static::notice([
  1036. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  1037. 'userId' => $recommenderActual['ID'],
  1038. ]);
  1039. }
  1040. return static::notice(Yii::t('ctx', 'fillSponsorUpline'), 400);
  1041. }
  1042. /**
  1043. * Public sponsor relationship validation for guest declaration flows.
  1044. * @return mixed
  1045. */
  1046. public function actionGuestInspectNetwork()
  1047. {
  1048. $recommenderName = trim((string) \Yii::$app->request->get('recommenderName', ''));
  1049. $recommenderActualName = trim((string) \Yii::$app->request->get('recommenderActualName', ''));
  1050. if ($recommenderName === '' || $recommenderActualName === '') {
  1051. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1052. }
  1053. $userId = Info::getUserIdByUserName($recommenderName);
  1054. $recommender = $userId ? User::findOneAsArray('ID=:ID', [':ID' => $userId]) : null;
  1055. if (!$recommender) {
  1056. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1057. }
  1058. $actualId = Info::getUserIdByUserName($recommenderActualName);
  1059. $recommenderActual = $actualId ? User::findOneAsArray('ID=:ID', [':ID' => $actualId]) : null;
  1060. if (!$recommenderActual) {
  1061. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1062. }
  1063. if ($actualId == $userId) {
  1064. return static::notice([
  1065. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  1066. 'userId' => $recommenderActual['ID'],
  1067. ]);
  1068. }
  1069. $sqlUp = <<<SQL
  1070. WITH recursive t_rec AS
  1071. (
  1072. SELECT m.USER_ID, U.USER_NAME, m.PARENT_UID, U2.USER_NAME AS PARENT_NAME, 1 AS node_level
  1073. FROM AR_USER_RELATION_NEW m
  1074. LEFT JOIN AR_USER U ON U.ID = m.USER_ID
  1075. LEFT JOIN AR_USER U2 ON U2.ID = m.PARENT_UID
  1076. WHERE U.USER_NAME = :USER_NAME
  1077. UNION ALL
  1078. SELECT t1.USER_ID, U.USER_NAME, t1.PARENT_UID, U2.USER_NAME AS PARENT_NAME,
  1079. t2.node_level + 1
  1080. FROM AR_USER_RELATION_NEW t1
  1081. JOIN t_rec t2 ON t2.PARENT_UID = t1.USER_ID
  1082. LEFT JOIN AR_USER U ON U.ID = t1.USER_ID
  1083. LEFT JOIN AR_USER U2 ON U2.ID = t1.PARENT_UID
  1084. )
  1085. SELECT * FROM t_rec
  1086. SQL;
  1087. $relationNodeUp = \Yii::$app->db->createCommand($sqlUp)
  1088. ->bindValue(':USER_NAME', $recommenderName)
  1089. ->queryAll();
  1090. $relationNodeUp = array_column($relationNodeUp, 'USER_NAME');
  1091. if (in_array($recommenderActualName, $relationNodeUp, true)) {
  1092. return static::notice([
  1093. 'REAL_NAME' => $recommenderActual['REAL_NAME'],
  1094. 'userId' => $recommenderActual['ID'],
  1095. ]);
  1096. }
  1097. return static::notice(Yii::t('ctx', 'fillSponsorUpline'), 400);
  1098. }
  1099. /**
  1100. * 复消会员信息查询
  1101. * @return mixed
  1102. * @throws \yii\web\HttpException
  1103. */
  1104. public function actionUserBaseInfo()
  1105. {
  1106. $userName = \Yii::$app->request->get('userName');
  1107. $userId = Info::getUserIdByUserName($userName);
  1108. if($userId){
  1109. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  1110. if($allAddress) {
  1111. foreach ($allAddress as $key => $row) {
  1112. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  1113. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  1114. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  1115. }
  1116. }
  1117. $userInfo = Info::baseInfoWithNet($userId);
  1118. // $decLevelConfig = Cache::getDecLevelConfig();
  1119. // $empLevelConfig = Cache::getEmpLevelConfig();
  1120. $arr = [
  1121. 'REAL_NAME'=>$userInfo['REAL_NAME'],
  1122. // 'DEC_LEVEL_NAME' => $decLevelConfig[$userInfo['DEC_LV']]['LEVEL_NAME'],
  1123. // 'EMP_LEVEL_NAME'=>$empLevelConfig[$userInfo['EMP_LV']]['LEVEL_NAME'],
  1124. // 'REC_UID'=>$userInfo['REC_USER_NAME'].'('.$userInfo['REC_REAL_NAME'].')',
  1125. // 'CON_UID'=>$userInfo['CON_USER_NAME'].'('.$userInfo['CON_REAL_NAME'].')',
  1126. 'allAddress'=>$allAddress
  1127. ];
  1128. return static::notice($arr);
  1129. }else{
  1130. return static::notice(Yii::t('app', 'repeatSalesMemberNoDoesNotExist'), 400);
  1131. }
  1132. }
  1133. /**
  1134. * 会员资料
  1135. * @return mixed
  1136. * @throws \yii\web\HttpException
  1137. */
  1138. public function actionBaInfo()
  1139. {
  1140. $data['userInfo'] = BaUser::getEnCodeInfo(\Yii::$app->user->id);
  1141. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  1142. // 推荐人
  1143. $baUserInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  1144. $sponsor = User::findOneAsArray('ID=:USER_ID', [':USER_ID' => $baUserInfo['REC_UID']]);
  1145. $data['userInfo']['SponsorCode'] = $sponsor['USER_NAME'];
  1146. $data['userInfo']['SponsorName'] = $sponsor['REAL_NAME'];
  1147. return static::notice($data);
  1148. }
  1149. /**
  1150. * 编辑会员资料
  1151. * @return mixed
  1152. * @throws \yii\web\HttpException
  1153. */
  1154. public function actionBaEdit()
  1155. {
  1156. if(\Yii::$app->request->isPost)
  1157. {
  1158. $form = new BaUserForm();
  1159. $post = \Yii::$app->request->post();
  1160. $form->scenario = 'modifyProfile';
  1161. if ($form->load($post, '') && $result = $form->modifyProfile()){
  1162. return static::notice(Yii::t('app', 'personalDataModifiedSuccessfully'), 400);
  1163. } else {
  1164. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  1165. }
  1166. }
  1167. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  1168. }
  1169. /**
  1170. * 修改登录密码
  1171. */
  1172. public function actionBaPassword(){
  1173. if(\Yii::$app->request->isPost) {
  1174. $form = new BaUserForm();
  1175. $form->scenario = 'modifyPassword';
  1176. $post = \Yii::$app->request->post();
  1177. if($form->load($post, '') && $result = $form->modifyPassword()){
  1178. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  1179. } else {
  1180. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  1181. }
  1182. }
  1183. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  1184. }
  1185. /**
  1186. * 修改支付密码
  1187. */
  1188. public function actionBaPayPassword(){
  1189. if(\Yii::$app->request->isPost){
  1190. $form = new BaUserForm();
  1191. $form->scenario = 'modifyPasswordPay';
  1192. $post = \Yii::$app->request->post();
  1193. $form->userId = \Yii::$app->user->id;
  1194. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  1195. return static::notice(Yii::t('app', 'passwordModifiedSuccessfully'), 400);
  1196. } else {
  1197. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  1198. }
  1199. }
  1200. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  1201. }
  1202. /**
  1203. * BA报单管理
  1204. */
  1205. public function actionBaDec() {
  1206. // 生成随机码 , 初始化redis
  1207. $userName = Info::generateWebUserName('NG',9);
  1208. $redis = \Yii::$app->redis;
  1209. $post = \Yii::$app->request->post();
  1210. if (\Yii::$app->request->isPost) {
  1211. // 根据支付方式区分逻辑
  1212. $payMethod = \Yii::$app->request->post('payType', '');
  1213. if ($payMethod === 'pay_stack') {
  1214. $formModel = new BaApproachDeclarationLoopForm();
  1215. } else {
  1216. unset($post['payType']);
  1217. $formModel = new BaDeclarationLoopForm();
  1218. }
  1219. $formModel->scenario = 'userDec';
  1220. $post['province'] = $post['province'] ? :1;
  1221. $post['city'] = $post['city'] ? :1;
  1222. $post['county'] = $post['county'] ? :1;
  1223. // $post['conUserName'] = trim($post['conUserName']);
  1224. $post['recUserName'] = trim($post['recUserName']);
  1225. $post['cityName'] = trim($post['cityName']);
  1226. $post['lgaName'] = trim($post['lgaName']);
  1227. // 针对于会员编号的判断
  1228. $insertUserName = strtoupper($post['insertUserName']);
  1229. $getRedisUserName = $redis->get('key_' . $insertUserName);
  1230. if (!$getRedisUserName){
  1231. return static::notice(Yii::t('app', 'memberNumberExpired'), 400);
  1232. }
  1233. if ($insertUserName != $getRedisUserName){
  1234. return static::notice(Yii::t('app', 'memberNumberDoesNotConformTo'), 400);
  1235. }
  1236. $post['insertUserName'] = $insertUserName;
  1237. $post['type'] = BaDeclarationForm::TYPE_ZC;
  1238. $allData['data'][] = $post;
  1239. if ($formModel->load($allData, '') && $result = $formModel->add()) {
  1240. return static::notice($result); // 报单成功
  1241. } else {
  1242. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  1243. }
  1244. }
  1245. // 只查询普通商品
  1246. $allGoods = ShopGoods::find()->where("ID='375895243322691584'")->orderBy('SORT ASC')->asArray()->all();
  1247. if (!$userName) {
  1248. return static::notice(Yii::t('app', 'failedToGenerateMemberNumber'), 400);
  1249. }
  1250. //随机码保存在redis中方便进行比对
  1251. $redis->setex('key_'.$userName , 3600 , $userName);
  1252. // 会员账户
  1253. $userBalance = [
  1254. 'cash' => 0,
  1255. // 'tourism_points' => 0,
  1256. // 'garage_points' => 0,
  1257. // 'villa_points' => 0,
  1258. ];
  1259. $userCash = UserWallet::findOneAsArray(['USER_ID' => \Yii::$app->user->id]);
  1260. if ($userCash) {
  1261. $userBalance['cash'] = $userCash['CASH'];
  1262. }
  1263. return static::notice([
  1264. 'allGoods' => $allGoods,
  1265. 'userName' => $userName,
  1266. 'payList' => ShopGoods::payTypes(),
  1267. 'sellType' => ShopGoods::getCategoryType(),
  1268. 'categoryType' => ShopGoods::getCategoryType()[0]['id'],
  1269. 'userBalance' => $userBalance,
  1270. ]);
  1271. }
  1272. /**
  1273. * BA会员信息
  1274. * @return mixed
  1275. * @throws \yii\web\HttpException
  1276. */
  1277. public function actionBaUserInfo()
  1278. {
  1279. $userName = \Yii::$app->request->get('userName');
  1280. $userId = Info::getBaUserIdByUserName($userName);
  1281. $userInfo['REAL_NAME'] = '';
  1282. $user = BaUser::findOneAsArray('ID=:ID', [':ID' => $userId], 'REAL_NAME');
  1283. $uInfo = BaUserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'REC_UID');
  1284. if($user){
  1285. $rec_user = UserInfo::findOneAsArray('USER_ID=:USER_ID',[':USER_ID' => $uInfo['REC_UID']], 'USER_ID, USER_NAME');
  1286. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  1287. $userInfo['REC_USER_ID'] = $rec_user['USER_ID'];
  1288. $userInfo['REC_USER_NAME'] = $rec_user['USER_NAME'];
  1289. return static::notice($userInfo);
  1290. }else{
  1291. return static::notice(Yii::t('app', 'memberNumberDoesNotExist'), 400);
  1292. }
  1293. }
  1294. }