UserController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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\Cache;
  10. use common\helpers\Form;
  11. use common\helpers\user\Info;
  12. use common\models\Config;
  13. use common\models\DeclarationLevel;
  14. use common\models\DeclarationPackage;
  15. use common\models\DecOrder;
  16. use common\models\forms\DeclarationForm;
  17. use common\models\forms\DeclarationLoopForm;
  18. use common\models\forms\UploadForm;
  19. use common\models\forms\UserBindForm;
  20. use common\models\forms\UserForm;
  21. use common\models\OpenBank;
  22. use common\models\ReceiveAddress;
  23. use common\models\Region;
  24. use common\models\ShopGoods;
  25. use common\models\UpgradeType;
  26. use common\models\User;
  27. use common\models\UserBind;
  28. use common\models\UserInfo;
  29. use common\models\UserNetwork;
  30. use common\models\forms\DeclarationUpgradeForm;
  31. use yii\db\Exception;
  32. use yii\web\UploadedFile;
  33. class UserController extends BaseController {
  34. public $modelClass = UserInfo::class;
  35. /**
  36. * 会员资料
  37. * @return mixed
  38. * @throws \yii\web\HttpException
  39. */
  40. public function actionIndex() {
  41. $allNation = \Yii::$app->params['nation'];
  42. $allOpenBank = OpenBank::findAllAsArray('STATUS=1');
  43. $data['allNation'] = $allNation;
  44. $data['allOpenBank'] = $allOpenBank;
  45. $data['userInfo'] = User::getEnCodeInfo(\Yii::$app->user->id);
  46. $data['userInfo']['NATION'] = $data['userInfo']['NATION_ID'];
  47. return static::notice($data);
  48. }
  49. /**
  50. * 编辑会员资料
  51. * @return mixed
  52. * @throws \yii\web\HttpException
  53. */
  54. public function actionEdit() {
  55. if(\Yii::$app->request->isPost){
  56. $form = new UserForm();
  57. $post = \Yii::$app->request->post();
  58. $form->scenario = 'modifyProfile';
  59. if($form->load($post, '') && $result = $form->modifyProfile()){
  60. return static::notice('Personal data modified successfully');//个人资料修改成功
  61. } else {
  62. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  63. }
  64. }
  65. return static::notice('非法访问', 400);
  66. }
  67. /**
  68. * 修改登录密码
  69. */
  70. public function actionPassword(){
  71. if(\Yii::$app->request->isPost){
  72. $form = new UserForm();
  73. $form->scenario = 'modifyPassword';
  74. $post = \Yii::$app->request->post();
  75. if($form->load($post, '') && $result = $form->modifyPassword()){
  76. return static::notice('Password modified successfully');//密码修改成功
  77. } else {
  78. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  79. }
  80. }
  81. return static::notice('非法访问', 400);
  82. }
  83. /**
  84. * 修改支付密码
  85. */
  86. public function actionPayPassword(){
  87. if(\Yii::$app->request->isPost){
  88. $form = new UserForm();
  89. $form->scenario = 'modifyPasswordPay';
  90. $post = \Yii::$app->request->post();
  91. $form->userId = \Yii::$app->user->id;
  92. if($form->load($post, '') && $result = $form->modifyPasswordPay()){
  93. return static::notice('支付密码修改成功');
  94. } else {
  95. return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
  96. }
  97. }
  98. return static::notice('非法访问', 400);
  99. }
  100. /**
  101. * 直推会员列表
  102. * @return mixed
  103. * @throws \yii\web\HttpException
  104. */
  105. public function actionRecUser() {
  106. $allData = UserInfo::lists('AND REC_UID=:REC_UID', [':REC_UID' => \Yii::$app->user->id], ['useSlaves' => true, 'select'=>'USER_ID,CREATED_AT']);
  107. foreach ($allData['list'] as $key => $data) {
  108. $userBaseInfo = User::getEnCodeInfo($data['USER_ID']);
  109. $userBaseInfo['NATION'] = \Yii::$app->params['nation'][$userBaseInfo['NATION']]['name'] ?? '';
  110. $allData['list'][$key]['BASE_INFO'] = $userBaseInfo;
  111. }
  112. return static::notice($allData);
  113. }
  114. /**
  115. * 上传身份证
  116. * @return mixed
  117. * @throws \yii\base\Exception
  118. * @throws \yii\web\HttpException
  119. */
  120. public function actionIdCard() {
  121. if (\Yii::$app->request->isPost) {
  122. $formModel = new UploadForm();
  123. $formModel->scenario = 'idCardFront';
  124. $formModel->file = UploadedFile::getInstanceByName('file');
  125. //$formModel->token = \Yii::$app->request->post('uploadToken');
  126. $formModel->token = \Yii::$app->request->request('uploadToken');
  127. if ($formModel->file && $formModel->upload()) {
  128. return static::notice('上传成功');
  129. } else {
  130. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  131. }
  132. }
  133. // 查看该用户是否已经上传过身份证
  134. $oneData = User::find()->select('ID_IMAGE')->where('ID=:ID', [':ID' => \Yii::$app->user->id])->asArray()->one();
  135. if ($oneData['ID_IMAGE']) {
  136. return static::notice($oneData);
  137. } else {
  138. $token = Cache::setUploadToken();
  139. return static::notice($token);
  140. }
  141. }
  142. /**
  143. * 点位绑定
  144. * @return mixed
  145. * @throws \yii\base\Exception
  146. * @throws \yii\web\HttpException
  147. */
  148. public function actionBind() {
  149. $userBind = UserBind::findOneAsArray('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => \Yii::$app->user->id]);
  150. $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');
  151. foreach ($allData['list'] as $key => $value) {
  152. $baseInfo = Info::baseInfoZh($value['USER_ID']);
  153. if ($baseInfo['STATUS'] != 1) {
  154. unset($allData['list'][$key]);
  155. continue;
  156. }
  157. $allData['list'][$key]['USER_NAME'] = $baseInfo['USER_NAME'];
  158. $allData['list'][$key]['REAL_NAME'] = $baseInfo['REAL_NAME'];
  159. $allData['list'][$key]['BANK_PROVINCE_NAME'] = $baseInfo['BANK_PROVINCE_NAME'];
  160. $allData['list'][$key]['BANK_CITY_NAME'] = $baseInfo['BANK_CITY_NAME'];
  161. $allData['list'][$key]['BANK_COUNTY_NAME'] = $baseInfo['BANK_COUNTY_NAME'];
  162. $allData['list'][$key]['OPEN_BANK_NAME'] = $baseInfo['OPEN_BANK_NAME'];
  163. $allData['list'][$key]['BANK_NO'] = $baseInfo['BANK_NO'];
  164. $allData['list'][$key]['MAIN_USER_NAME'] =Info::getUserNameByUserId($value['MAIN_UID']);
  165. }
  166. $allData['list'] = array_values($allData['list']);
  167. return static::notice($allData);
  168. }
  169. /**
  170. * 编辑点位绑定
  171. * @return mixed
  172. * @throws \yii\web\HttpException
  173. */
  174. public function actionBindEdit(){
  175. $id = \Yii::$app->request->get('id');
  176. if(\Yii::$app->request->isPost) {
  177. return parent::edit(UserBindForm::class, '修改主点位成功', 'frontEdit', ['frontEdit'], null, function($form, $result){
  178. //log
  179. });
  180. }
  181. // 获得当前会员的用户名等信息
  182. $userBind = UserBind::findOneAsArray('ID=:ID AND IS_DEL=0', [':ID' => $id]);
  183. $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');
  184. foreach($userBinds as $key=>$value){
  185. $status = Info::getStatusByUserId($value['USER_ID']);
  186. if ($status != 1) {
  187. unset($userBinds[$key]);
  188. continue;
  189. }
  190. $userBinds[$key]['USER_NAME'] = Info::getUserNameByUserId($value['USER_ID']);
  191. }
  192. $userBinds = array_values($userBinds);
  193. return static::notice(['userBinds' => $userBinds,'mainUid'=>$userBind['MAIN_UID']]);
  194. }
  195. // 会员升级,通过会员的编号,获取会员信息
  196. public function actionUpgradeInfo() {
  197. $isSwitchUpgrade = Config::find()
  198. ->where("CONFIG_NAME='isOpenUpgrade'")
  199. ->asArray()
  200. ->one();
  201. $isOpen = !empty($isSwitchUpgrade) && isset($isSwitchUpgrade['VALUE']) ? $isSwitchUpgrade['VALUE'] : 0;
  202. if ($isOpen < 1) {
  203. return static::notice('功能暂未开放',400);
  204. }
  205. $userNumber = \Yii::$app->request->request('userName');
  206. $baseInfo = Info::baseInfoZhByUserName($userNumber);
  207. if ($baseInfo['STATUS'] != 1) {
  208. return static::notice('非激活用户,请联系客服',400);
  209. }
  210. // 1. 如果是最高级别,则只显示用户基本信息
  211. // 2. 如果不是最高级别,如果用户累计报单数据是0, 或者用户累计报单业绩不符合级别信息,则提示 请联系客服核对业绩
  212. $userId = $baseInfo['ID'];
  213. $userDecId = $baseInfo['DEC_LV'];// 用户当前的级别
  214. // 获取系统中的DEC 报单级别配置
  215. $decConfig = Cache::getDecLevelConfig();
  216. $userDecInfo = $decConfig[$userDecId]; // 会员的级别具体信息
  217. $maxPerfInfo = DeclarationLevel::getMaxDecPref();
  218. $maxDecId = $maxPerfInfo['ID']; // 级别配置中最高级别ID
  219. $observe = Config::getConfigByType('observe'); // 获取观察期配置信息
  220. $observeLimit = $observe['observePeriodLimit']['value']; // 月份限制
  221. $isObserve = User::checkIsObserve($baseInfo['CREATED_AT'], $observeLimit); // 判断用户是否再观察期中
  222. // 如果用户已经是最高级别,则只展示用户信息
  223. $isMax = false;
  224. if ($maxDecId == $userDecId) {
  225. $isMax = true;
  226. }
  227. // 如果用户已经是最高级别,则只展示用户信息
  228. $userInfo = [
  229. 'DEC_NAME' => $baseInfo['DEC_LV_NAME'], // 用户级别中文
  230. 'DEC_ID' => $userDecId, // 用户级别id
  231. 'REAL_NAME' => $baseInfo['REAL_NAME'], // 真实姓名
  232. 'ADD_AT' => date('Y-m-d', $baseInfo['CREATED_AT']), // 加入时间
  233. 'IS_OBSERVE' => $isObserve, // 是否是观察期 true为是观察期
  234. 'IS_MAX' => $isMax, // 是否已是最大级别 最大级别不需要判断报单总PV是多少 只展示基本信息
  235. ];
  236. // 如果是最高级别了,则无需升级
  237. if ($isMax) {
  238. return static::notice(['baseInfo' => $userInfo]);
  239. }
  240. $levelPerf = $userDecInfo['PERF'];// 用户当前级别对应的业绩值
  241. if (!$isMax) {
  242. $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和
  243. // // 如果总和小于级别业绩 去掉这校验直接补比如3000-980的差值
  244. // if ($userDecPvSum < $levelPerf) {
  245. // return static::notice('请联系客服人员核对业绩',400);
  246. // }
  247. // 下一级业绩
  248. $nextLevelPerf = DeclarationLevel::getNextDecPref($levelPerf)['PERF'];
  249. // 如果总和超过了下一级业绩
  250. if ($userDecPvSum >= $nextLevelPerf) {
  251. return static::notice('请联系客服人员核对业绩',400);
  252. }
  253. $type = $isObserve ? 1 : 2;
  254. $userInfo['UPGRADE_FUNC'] = $isObserve ? '补差额升级' : '全额升级';// 升级方式
  255. $upgradeType = UpgradeType::getOneByType($type);
  256. // 如果用户不是最大级别,则需要获取是否观察期,算出PV是否有问题,应该补多少,
  257. $userInfo['UPGRADE_TYPE'] = $upgradeType;
  258. $userInfo['NOW_PERF'] = $userDecPvSum;
  259. $userInfo['NEXT_PERF'] = $nextLevelPerf;
  260. // 用户可选择的级别列表
  261. $userInfo['LEVEL_LIST'] = DeclarationLevel::getNextAll($levelPerf);// 用户可选择的级别列表
  262. // 循环列表,补充升级所需要的补差
  263. foreach ($userInfo['LEVEL_LIST'] as &$v) {
  264. $v['REPAIR_PV'] = $isObserve ? $v['PERF'] - $userInfo['NOW_PERF'] : $v['PERF'];
  265. }
  266. }
  267. return static::notice(['baseInfo' => $userInfo]);
  268. }
  269. // 会员升级管理
  270. public function actionUpgrade() {
  271. $isSwitchUpgrade = Config::find()
  272. ->where("CONFIG_NAME='isOpenUpgrade'")
  273. ->asArray()
  274. ->one();
  275. $isOpen = !empty($isSwitchUpgrade) && isset($isSwitchUpgrade['VALUE']) ? $isSwitchUpgrade['VALUE'] : 0;
  276. if ($isOpen < 1) {
  277. return static::notice('功能暂未开放',400);
  278. }
  279. // 开始升级
  280. if (\Yii::$app->request->isPost) {
  281. $formModel = new DeclarationUpgradeForm();
  282. $post = \Yii::$app->request->post();
  283. $post['type'] = DeclarationForm::TYPE_ZC;
  284. $allData['data'][] = $post;
  285. if ($formModel->load($post, '') && $formModel->add($post)) {
  286. return static::notice('Upgrade declaration succeeded');//升级报单成功
  287. } else {
  288. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  289. }
  290. }
  291. //所有报单套餐
  292. $allDecPackage = DeclarationPackage::getAllData();
  293. $decLevels = Cache::getDecLevelConfig();
  294. foreach ($allDecPackage as $k=>$v){
  295. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  296. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  297. }
  298. //所有自选商品
  299. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  300. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  301. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  302. $decUserName = User::getEnCodeInfo(\Yii::$app->user->id)['USER_NAME'];
  303. $query_condition= " AND (1<>1";
  304. if(!$isDecReg || ($isDecReg && $isDec==1)){
  305. $query_condition = " AND (FIND_IN_SET(1,GIFT_TYPE)>0";
  306. }
  307. if($isStudio==1){
  308. $query_condition.= " OR FIND_IN_SET(3,GIFT_TYPE)>0";
  309. }
  310. $query_condition.= ")";
  311. $allGoods = ShopGoods::find()->where("STATUS=1 ".$query_condition)->orderBy('SORT ASC')->asArray()->all();
  312. return static::notice(['allDecPackage' => $allDecPackage,'allGoods' => $allGoods,'decUserName'=>$decUserName]);
  313. }
  314. /**
  315. * 报单管理
  316. */
  317. public function actionDec() {
  318. // 生成随机码 , 初始化redis
  319. $userName = Info::generateWebUserName('NG',9);
  320. $redis = \Yii::$app->redis;
  321. if (\Yii::$app->request->isPost) {
  322. $formModel = new DeclarationLoopForm();
  323. $formModel->scenario = 'userDec';
  324. $post = \Yii::$app->request->post();
  325. $post['province'] = $post['province'] ? :1;
  326. $post['city'] = $post['city'] ? :1;
  327. $post['county'] = $post['county'] ? :1;
  328. $post['conUserName'] = trim($post['conUserName']);
  329. $post['recUserName'] = trim($post['recUserName']);
  330. $post['cityName'] = trim($post['cityName']);
  331. $post['lgaName'] = trim($post['lgaName']);
  332. // 针对于会员编号的判断
  333. $insertUserName = strtoupper($post['insertUserName']);
  334. $getRedisUserName = $redis->get('key_'.$insertUserName);
  335. if (!$getRedisUserName){
  336. return static::notice('Membership number expired',400);//会员编号过期
  337. }
  338. if ($insertUserName != $getRedisUserName){
  339. return static::notice('Member number does not conform to',400);//会员编号不符合
  340. }
  341. $post['insertUserName'] = $insertUserName;
  342. $post['type'] = DeclarationForm::TYPE_ZC;
  343. $allData['data'][] = $post;
  344. if ($formModel->load($allData, '') && $formModel->add()) {
  345. return static::notice('Entry success');//报单成功
  346. } else {
  347. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  348. }
  349. }
  350. //所有报单套餐
  351. $allDecPackage = DeclarationPackage::getAllData();
  352. $decLevels = Cache::getDecLevelConfig();
  353. foreach ($allDecPackage as $k=>$v){
  354. $levelName = $decLevels[$v['LEVEL_ID']]['LEVEL_NAME'] ?? '';
  355. $allDecPackage[$k]['LEVEL_NAME'] = $levelName;
  356. }
  357. //所有自选商品
  358. $isDecReg = Cache::getSystemConfig()['isDecReg']['VALUE'];
  359. $isDec = User::getEnCodeInfo(\Yii::$app->user->id)['IS_DEC'];
  360. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  361. $query_condition= " AND (1<>1";
  362. if(!$isDecReg || ($isDecReg && $isDec==1)){
  363. $query_condition = " AND (FIND_IN_SET(1,GIFT_TYPE)>0";
  364. }
  365. if($isStudio==1){
  366. $query_condition.= " OR FIND_IN_SET(3,GIFT_TYPE)>0";
  367. }
  368. $query_condition.= ")";
  369. $allGoods = ShopGoods::find()->where("STATUS=1 ".$query_condition)->orderBy('SORT ASC')->asArray()->all();
  370. //$allGoods = ShopGoods::findAllAsArray('STATUS=1');
  371. // 所有开户行
  372. $allOpenBank = OpenBank::find()->where('STATUS=1')->orderBy('LIST_ORDER ASC')->asArray()->all();
  373. if (!$userName) {
  374. return static::notice('Failed to generate member number', 400);//会员编号生成失败
  375. }
  376. //随机码保存在redis中方便进行比对
  377. $msg = $redis->setex('key_'.$userName , 3600 , $userName);
  378. return static::notice(['allDecPackage' => $allDecPackage,'allGoods' => $allGoods,'allOpenBank' => $allOpenBank, 'userName' => $userName]);
  379. }
  380. /**
  381. * 报单级别套餐
  382. */
  383. public function actionDecPackage() {
  384. $decLv = \Yii::$app->request->get('id');
  385. //所有报单级别套餐
  386. $allDecPackage = DeclarationPackage::getPackageFromLevelId($decLv);
  387. return static::notice(['allDecPackage' => $allDecPackage]);
  388. }
  389. /**
  390. * 会员信息查询
  391. * @return mixed
  392. * @throws \yii\web\HttpException
  393. */
  394. public function actionFullInfo()
  395. {
  396. $userName = \Yii::$app->request->get('userName');
  397. $userId = Info::getUserIdByUserName($userName);
  398. $userInfo['REAL_NAME'] = '';
  399. $user = User::findOneAsArray('ID=:ID', [':ID' => $userId], 'REAL_NAME');
  400. if($user){
  401. $userInfo['REAL_NAME'] = $user['REAL_NAME'];
  402. $allChildUser = UserNetwork::getFirstFloorChildren($userId);
  403. $isLocation = [1 => 'Left-', 2 => 'Right-'];
  404. if($allChildUser) {
  405. foreach ($allChildUser as $child) {
  406. if ($child['RELATIVE_LOCATION']<'3'){
  407. $isLocation[$child['RELATIVE_LOCATION']].= 'Full';
  408. }
  409. }
  410. }
  411. $userInfo['isLocation'] = '('.implode(',',$isLocation).')';
  412. return static::notice($userInfo);
  413. }else{
  414. return static::notice('Member number does not exist', 400);//会员编号不存在
  415. }
  416. }
  417. /**
  418. * 复消会员信息查询
  419. * @return mixed
  420. * @throws \yii\web\HttpException
  421. */
  422. public function actionUserBaseInfo()
  423. {
  424. $userName = \Yii::$app->request->get('userName');
  425. $userId = Info::getUserIdByUserName($userName);
  426. if($userId){
  427. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  428. if($allAddress) {
  429. foreach ($allAddress as $key => $row) {
  430. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  431. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  432. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  433. }
  434. }
  435. $userInfo = Info::baseInfoWithNet($userId);
  436. // $decLevelConfig = Cache::getDecLevelConfig();
  437. // $empLevelConfig = Cache::getEmpLevelConfig();
  438. $arr = [
  439. 'REAL_NAME'=>$userInfo['REAL_NAME'],
  440. // 'DEC_LEVEL_NAME' => $decLevelConfig[$userInfo['DEC_LV']]['LEVEL_NAME'],
  441. // 'EMP_LEVEL_NAME'=>$empLevelConfig[$userInfo['EMP_LV']]['LEVEL_NAME'],
  442. // 'REC_UID'=>$userInfo['REC_USER_NAME'].'('.$userInfo['REC_REAL_NAME'].')',
  443. // 'CON_UID'=>$userInfo['CON_USER_NAME'].'('.$userInfo['CON_REAL_NAME'].')',
  444. 'allAddress'=>$allAddress
  445. ];
  446. return static::notice($arr);
  447. }else{
  448. return static::notice('Repeat sales Member No. does not exist', 400);//复消会员编号不存在
  449. }
  450. }
  451. }