UserController.php 19 KB

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