UserController.php 20 KB

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