UserController.php 19 KB

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