ApiUserCenter.Class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. /**
  3. * 前台会员注册登录控制器
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/19
  7. * Time: 15:57
  8. */
  9. namespace JinDouYun\Controller\UserCenter;
  10. use Exception;
  11. use JinDouYun\Model\Holders\Holders;
  12. use JinDouYun\Model\Holders\MHoldersBonus;
  13. use JinDouYun\Model\Holders\MHoldersRecord;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use Mall\Framework\Core\StatusCode;
  17. use JinDouYun\Controller\BaseController;
  18. use JinDouYun\Model\UserCenter\MApiUserCenter;
  19. use JinDouYun\Model\UserCenter\MUserCenterRegister;
  20. use JinDouYun\Model\Customer\MCustomer;
  21. use JinDouYun\Cache\SmsVerification;
  22. class ApiUserCenter extends BaseController
  23. {
  24. private $objMApiUserCenter;
  25. private $objMUserCenterRegister;
  26. private $SmsVerification;
  27. public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken=true)
  28. {
  29. parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
  30. $this->objMApiUserCenter = new MApiUserCenter($this->onlineEnterpriseId, $this->onlineUserId);
  31. $this->objMUserCenterRegister = new MUserCenterRegister();
  32. $this->SmsVerification = new SmsVerification();
  33. }
  34. /**
  35. * 获取参数
  36. *
  37. * @return array
  38. */
  39. public function commonFieldFilter()
  40. {
  41. $params = $this->request->getRawJson();
  42. $UserCenterData = [
  43. "mobile" => isset($params['mobile']) ? $params['mobile'] : '',
  44. "password" => isset($params['password']) ? $params['password'] : '',
  45. "repeatPassword" => isset($params['repeatPassword']) ? $params['repeatPassword'] : '',
  46. "smsCode" => isset($params['smsCode']) ? $params['smsCode'] : '',
  47. "source" => isset($params['source']) ? $params['source'] : '',
  48. ];
  49. foreach ($UserCenterData as $key => $value) {
  50. if (empty($value) && $value !== 0) {
  51. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  52. }
  53. }
  54. //校验两次密码
  55. if($UserCenterData['password'] != $UserCenterData['repeatPassword']){
  56. parent::sendOutput('两次输入密码不一致', ErrorCode::$paramError);
  57. }
  58. $UserCenterData['avatar'] = isset($params['avatar']) ? $params['avatar'] : '';
  59. $UserCenterData['name'] = isset($params['name']) ? $params['name'] : $UserCenterData['mobile'];
  60. $UserCenterData['salesManId'] = isset($params['salesManId']) ? $params['salesManId'] : '';
  61. unset($UserCenterData['repeatPassword']);
  62. $UserCenterData['updateTime'] = time();
  63. $UserCenterData['createTime'] = time();
  64. //校验手机号格式
  65. $this->checkMobile($UserCenterData['mobile']);
  66. return $UserCenterData;
  67. }
  68. /**
  69. * 前台用户注册
  70. * @throws Exception
  71. */
  72. public function register()
  73. {
  74. $UserCenterData = $this->commonFieldFilter();
  75. //校验手机号在当前企业是否作为客户注册过
  76. $isMobileReg = $this->objMUserCenterRegister->mobileIsRegister($UserCenterData['mobile'], $this->onlineEnterpriseId);
  77. if($isMobileReg){
  78. parent::sendOutput('该手机号已经被注册', ErrorCode::$mobileishaved);
  79. }
  80. //校验redis短信验证码
  81. // $this->VerifyMobileCode($UserCenterData['mobile'], $UserCenterData['smsCode']);
  82. // unset($UserCenterData['smsCode']);
  83. //格式化密码
  84. $UserCenterData['password'] = password_hash($UserCenterData['password'], PASSWORD_DEFAULT );
  85. //用户类型
  86. $UserCenterData['isCustomer'] = StatusCode::$customerType['customer'];
  87. $result = $this->objMApiUserCenter->register($UserCenterData);
  88. if ($result->isSuccess()) {
  89. parent::sendOutput($result->getData());
  90. } else {
  91. parent::sendOutput($result->getData(), $result->getErrorCode());
  92. }
  93. }
  94. /**
  95. * 验证短信验证码
  96. *
  97. * @param $mobile
  98. * @param $mobileCode
  99. * @return void
  100. * @throws Exception
  101. */
  102. public function VerifyMobileCode($mobile, $mobileCode)
  103. {
  104. $cacheMobileCode = $this->SmsVerification->getMobileCode($mobile);
  105. if( $mobileCode != $cacheMobileCode ){
  106. parent::sendOutput('验证码有误', ErrorCode::$mobileCodeFail);
  107. }
  108. }
  109. /**
  110. * 短信验证码登录
  111. */
  112. public function smsLogin()
  113. {
  114. //接收参数
  115. $params = $this->request->getRawJson();
  116. $data = [
  117. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  118. 'smsCode' => isset($params['smsCode']) ? $params['smsCode'] : '',
  119. 'source' => isset($params['source']) ? $params['source'] : '',
  120. ];
  121. foreach($data as $key => $value){
  122. if(empty($value) && $value !== 0){
  123. parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
  124. }
  125. }
  126. $data['salesManId'] = isset($params['salesManId']) ? $params['salesManId'] : '';
  127. //校验redis短信验证码
  128. // $this->VerifyMobileCode($data['mobile'], $data['smsCode']);
  129. $result = $this->objMApiUserCenter->smsLogin($data);
  130. if ($result->isSuccess()) {
  131. parent::sendOutput($result->getData());
  132. } else {
  133. parent::sendOutput($result->getData(), $result->getErrorCode());
  134. }
  135. }
  136. /**
  137. * 前台用户中心登陆
  138. */
  139. public function login()
  140. {
  141. //接收参数
  142. $params = $this->request->getRawJson();
  143. $userData = [
  144. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  145. 'password' => isset($params['password']) ? $params['password'] : '',
  146. 'source' => isset($params['source']) ? $params['source'] : '',//来源
  147. ];
  148. foreach($userData as $key => $value){
  149. if(empty($value) && $value !== 0){
  150. parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
  151. }
  152. }
  153. //校验手机号格式
  154. parent::checkMobile($userData['mobile']);
  155. $result = $this->objMApiUserCenter->login($userData);
  156. if ($result->isSuccess()) {
  157. parent::sendOutput($result->getData());
  158. } else {
  159. parent::sendOutput($result->getData(), $result->getErrorCode());
  160. }
  161. }
  162. /**
  163. * 用户中心修改密码
  164. * @throws \Exception
  165. */
  166. public function changePassword()
  167. {
  168. $params = $this->request->getRawJson();
  169. $userData = [
  170. "mobile" => isset($params['mobile']) ? $params['mobile'] : '',
  171. "password" => isset($params['password']) ? $params['password'] : '',
  172. "repeatPassword" => isset($params['repeatPassword']) ? $params['repeatPassword'] : '',
  173. "smsCode" => isset($params['smsCode']) ? $params['smsCode'] : '',
  174. ];
  175. foreach($userData as $key => $value){
  176. if(empty($value) && $value !== 0){
  177. parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
  178. }
  179. }
  180. $mobileResult = $this->objMUserCenterRegister->mobileIsRegister($userData['mobile'], $this->onlineEnterpriseId);
  181. if(!$mobileResult){
  182. parent::sendOutput('手机号不存在', ErrorCode::$mobileishaved);
  183. }
  184. //校验两次密码
  185. if($userData['password'] != $userData['repeatPassword']){
  186. parent::sendOutput('两次输入密码不一致', ErrorCode::$paramError);
  187. }
  188. //校验redis短信验证码
  189. $this->VerifyMobileCode($userData['mobile'], $userData['smsCode']);
  190. //格式化密码
  191. $updateUserCenterData = [
  192. 'password' => password_hash($userData['password'], PASSWORD_DEFAULT ),
  193. 'updateTime' => time(),
  194. ];
  195. $where = [
  196. 'mobile' => $userData['mobile'],
  197. 'deleteStatus' => StatusCode::$standard
  198. ];
  199. $result = $this->objMApiUserCenter->changePassword($updateUserCenterData, $where);
  200. if ($result->isSuccess()) {
  201. parent::sendOutput($result->getData());
  202. } else {
  203. parent::sendOutput($result->getData(), $result->getErrorCode());
  204. }
  205. }
  206. /**
  207. * 判断openId或者unionId是否授权绑定手机号 (微信小程序, 抖音小程序, app微信授权)
  208. */
  209. public function checkBindMiniProgram () {
  210. $params = $this->request->getRawJson();
  211. $postData = [
  212. 'openId' => isset($params['openId']) ? $params['openId'] : '',
  213. 'source' => isset($params['source']) ? $params['source'] : '',
  214. ];
  215. foreach($postData as $key => $value){
  216. if (empty($value) && $value !== 0) {
  217. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  218. }
  219. }
  220. $postData['unionId'] = isset($params['unionId']) ? $params['unionId'] : '';
  221. if($postData['source'] == StatusCode::$source['ios'] || $postData['source'] == StatusCode::$source['android']){
  222. if(!isset($params['unionId']) || empty($params['unionId'])){
  223. parent::sendOutput('unionId参数错误', ErrorCode::$paramError);
  224. }
  225. }
  226. $result = $this->objMApiUserCenter->checkBind($postData);
  227. if ($result->isSuccess()) {
  228. parent::sendOutput($result->getData());
  229. } else {
  230. parent::sendOutput($result->getData(), $result->getErrorCode());
  231. }
  232. }
  233. /**
  234. * 授权绑定手机号(微信小程序,app,抖音小程序)
  235. * @throws Exception
  236. */
  237. public function appletsBindMobile()
  238. {
  239. $params = $this->request->getRawJson();
  240. if(empty($params)){
  241. parent::sendOutput('参数为空', ErrorCode::$paramError);
  242. }
  243. $appletsRegisterData = [
  244. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  245. 'openId' => isset($params['openId']) ? $params['openId'] : '',
  246. 'avatar' => isset($params['avatar']) ? $params['avatar'] : '',
  247. 'name' => isset($params['name']) ? $params['name'] : '',
  248. 'type' => isset($params['source']) ? $params['source'] : '',//参数切换一下 是正确的
  249. ];
  250. //【影子】设置默认昵称为用户手机号码
  251. if(!empty($appletsRegisterData['mobile']) && (empty($appletsRegisterData['name']) || $appletsRegisterData['name']=='微信用户')){
  252. $appletsRegisterData['name'] = $appletsRegisterData['mobile'];
  253. }
  254. foreach($appletsRegisterData as $key => $value){
  255. if (empty($value) && $value !== 0) {
  256. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  257. }
  258. }
  259. $appletsRegisterData['source'] = isset($params['type']) ? $params['type'] : StatusCode::$source['miniProgram'];//参数切换一下 是正确的
  260. $appletsRegisterData['unionId'] = isset($params['unionId']) ? $params['unionId'] : '';
  261. $appletsRegisterData['smsCode'] = isset($params['smsCode']) ? $params['smsCode'] : '';
  262. $appletsRegisterData['salesManId'] = isset($params['salesManId']) ? $params['salesManId'] : '';
  263. if($appletsRegisterData['source'] == StatusCode::$source['ios'] || $appletsRegisterData['source'] == StatusCode::$source['android']){
  264. //小程序支付需要openid app不需要 所以空 后面会判断unset app微信绑定手机号不需要openid
  265. $appletsRegisterData['openId'] = '';
  266. }
  267. //除了微信快捷登录其他都需要验证短信
  268. if($appletsRegisterData['type'] != 'wx' || $appletsRegisterData['type'] != 'byteDance'){
  269. //校验redis短信验证码
  270. $this->VerifyMobileCode($appletsRegisterData['mobile'], $appletsRegisterData['smsCode']);
  271. }
  272. unset($appletsRegisterData['type']);
  273. unset($appletsRegisterData['smsCode']);
  274. //校验手机号在当前企业是否作为客户注册过
  275. $bindResult = $this->objMApiUserCenter->bindMobileAndOpenId(
  276. $appletsRegisterData['source'],
  277. $appletsRegisterData['mobile'],
  278. $appletsRegisterData['openId'],
  279. $appletsRegisterData['unionId'],
  280. $this->onlineEnterpriseId,
  281. $appletsRegisterData['avatar'],
  282. $appletsRegisterData['name']
  283. );
  284. if($bindResult->isSuccess()) {
  285. parent::sendOutput($bindResult->getData(), $bindResult->getErrorCode());
  286. }
  287. $appletsRegisterData['isCustomer'] = StatusCode::$customerType['customer'];
  288. $appletsRegisterData['createTime'] = time();
  289. $appletsRegisterData['updateTime'] = time();
  290. //调用model
  291. $result = $this->objMApiUserCenter->register($appletsRegisterData);
  292. if ($result->isSuccess()) {
  293. $returnData = $this->objMApiUserCenter->createToken(['id'=>$result->getData()]);
  294. parent::sendOutput($returnData);
  295. } else {
  296. parent::sendOutput($result->getData(), $result->getErrorCode());
  297. }
  298. }
  299. /**
  300. * 修改手机号
  301. */
  302. public function updateUserCenterMobile()
  303. {
  304. $params = $this->request->getRawJson();
  305. if(empty($params)){
  306. parent::sendOutput('参数为空', ErrorCode::$paramError);
  307. }
  308. $data = [
  309. 'userCenterId' => isset($params['userCenterId']) ? $params['userCenterId'] : '',
  310. 'password' => isset($params['password']) ? $params['password'] : '',
  311. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  312. 'smsCode' => isset($params['smsCode']) ? $params['smsCode'] : '',
  313. ];
  314. //验证短信验证码
  315. $this->VerifyMobileCode($data['mobile'], $data['smsCode']);
  316. //验证手机号
  317. //校验手机号在当前企业是否作为客户注册过
  318. $isMobileReg = $this->objMUserCenterRegister->mobileIsRegister($data['mobile'], $this->onlineEnterpriseId);
  319. if($isMobileReg){
  320. parent::sendOutput('该手机号已经被注册', ErrorCode::$mobileishaved);
  321. }
  322. $update = [
  323. 'mobile' => $data['mobile'],
  324. 'updateTime' => time(),
  325. ];
  326. $result = $this->objMUserCenterRegister->updateUserCenter($update, ['id' => $data['userCenterId']]);
  327. if(!$result->isSuccess()){
  328. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  329. }
  330. return ResultWrapper::success($result->getData());
  331. }
  332. /**
  333. * 验证手机号是否存在(注册)
  334. */
  335. public function mobileIsRegister()
  336. {
  337. $mobile = $this->request->param('request_id');
  338. if( !$mobile ){
  339. parent::sendOutput('手机号为空', ErrorCode::$paramError);
  340. }
  341. $this->checkMobile($mobile);
  342. $result = $this->objMUserCenterRegister->mobileIsRegister($mobile, $this->onlineEnterpriseId);
  343. if ($result) {
  344. parent::sendOutput(true); //已注册
  345. }
  346. parent::sendOutput( false);//未注册
  347. }
  348. /**
  349. * 股东股份
  350. * @return void
  351. */
  352. public function holders()
  353. {
  354. $uid = $this->onlineUserId;
  355. $Mholders = new Holders();
  356. $data = $Mholders->getinfo(['en_id' => $this->onlineEnterpriseId, 'us_id' => $uid])->getData();
  357. parent::sendOutput($data);
  358. }
  359. /**
  360. * 股东分红记录
  361. * @return void
  362. */
  363. public function holders_bonus()
  364. {
  365. $params = $this->request->getRawJson();
  366. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  367. $selectParams['limit'] = $pageParams['limit'];
  368. $selectParams['offset'] = $pageParams['offset'];
  369. $Mbonus = new MHoldersBonus($this->onlineEnterpriseId);
  370. $uid = $this->onlineUserId;
  371. $Mholders = new Holders();
  372. $data = $Mholders->getinfo(['en_id' => $this->onlineEnterpriseId, 'us_id' => $uid])->getData();
  373. // if (empty($data)) parent::sendOutput('你不是股东', 1005);
  374. $selectParams['hol_id'] = $data['id'] ?? "";
  375. $result = $Mbonus->list($selectParams);
  376. $returnData = $result->getData();
  377. $pageData = [
  378. 'pageIndex' => $params['page'],
  379. 'pageSize' => $params['pageSize'],
  380. 'pageTotal' => $returnData['total'],
  381. ];
  382. parent::sendOutput($returnData['data'], 0, $pageData);
  383. }
  384. /**
  385. * 股东股份增加记录
  386. * @return void
  387. */
  388. public function holders_record()
  389. {
  390. $params = $this->request->getRawJson();
  391. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  392. $selectParams['limit'] = $pageParams['limit'];
  393. $selectParams['offset'] = $pageParams['offset'];
  394. $Mbonus = new MHoldersRecord($this->onlineEnterpriseId);
  395. $uid = $this->onlineUserId;
  396. $Mholders = new Holders();
  397. $data = $Mholders->getinfo(['en_id' => $this->onlineEnterpriseId, 'us_id' => $uid])->getData();
  398. // if (empty($data)) parent::sendOutput('你不是股东', 1005);
  399. if ($params['pm']){
  400. $selectParams['pm'] = $params['pm'];
  401. }
  402. if ($params['type']){
  403. $selectParams['type'] = $params['type'];
  404. }
  405. $selectParams['hol_id'] = $data['id'] ?? "";
  406. $result = $Mbonus->list($selectParams);
  407. $returnData = $result->getData();
  408. $pageData = [
  409. 'pageIndex' => $params['page'],
  410. 'pageSize' => $params['pageSize'],
  411. 'pageTotal' => $returnData['total'],
  412. ];
  413. parent::sendOutput($returnData['data'], 0, $pageData);
  414. }
  415. }