ApiUserCenter.Class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. foreach($appletsRegisterData as $key => $value){
  251. if (empty($value) && $value !== 0) {
  252. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  253. }
  254. }
  255. $appletsRegisterData['source'] = isset($params['type']) ? $params['type'] : StatusCode::$source['miniProgram'];//参数切换一下 是正确的
  256. $appletsRegisterData['unionId'] = isset($params['unionId']) ? $params['unionId'] : '';
  257. $appletsRegisterData['smsCode'] = isset($params['smsCode']) ? $params['smsCode'] : '';
  258. $appletsRegisterData['salesManId'] = isset($params['salesManId']) ? $params['salesManId'] : '';
  259. if($appletsRegisterData['source'] == StatusCode::$source['ios'] || $appletsRegisterData['source'] == StatusCode::$source['android']){
  260. //小程序支付需要openid app不需要 所以空 后面会判断unset app微信绑定手机号不需要openid
  261. $appletsRegisterData['openId'] = '';
  262. }
  263. //除了微信快捷登录其他都需要验证短信
  264. if($appletsRegisterData['type'] != 'wx' || $appletsRegisterData['type'] != 'byteDance'){
  265. //校验redis短信验证码
  266. $this->VerifyMobileCode($appletsRegisterData['mobile'], $appletsRegisterData['smsCode']);
  267. }
  268. unset($appletsRegisterData['type']);
  269. unset($appletsRegisterData['smsCode']);
  270. //校验手机号在当前企业是否作为客户注册过
  271. $bindResult = $this->objMApiUserCenter->bindMobileAndOpenId(
  272. $appletsRegisterData['source'],
  273. $appletsRegisterData['mobile'],
  274. $appletsRegisterData['openId'],
  275. $appletsRegisterData['unionId'],
  276. $this->onlineEnterpriseId,
  277. $appletsRegisterData['avatar'],
  278. $appletsRegisterData['name']
  279. );
  280. if($bindResult->isSuccess()) {
  281. parent::sendOutput($bindResult->getData(), $bindResult->getErrorCode());
  282. }
  283. $appletsRegisterData['isCustomer'] = StatusCode::$customerType['customer'];
  284. $appletsRegisterData['createTime'] = time();
  285. $appletsRegisterData['updateTime'] = time();
  286. //调用model
  287. $result = $this->objMApiUserCenter->register($appletsRegisterData);
  288. if ($result->isSuccess()) {
  289. $returnData = $this->objMApiUserCenter->createToken(['id'=>$result->getData()]);
  290. parent::sendOutput($returnData);
  291. } else {
  292. parent::sendOutput($result->getData(), $result->getErrorCode());
  293. }
  294. }
  295. /**
  296. * 修改手机号
  297. */
  298. public function updateUserCenterMobile()
  299. {
  300. $params = $this->request->getRawJson();
  301. if(empty($params)){
  302. parent::sendOutput('参数为空', ErrorCode::$paramError);
  303. }
  304. $data = [
  305. 'userCenterId' => isset($params['userCenterId']) ? $params['userCenterId'] : '',
  306. 'password' => isset($params['password']) ? $params['password'] : '',
  307. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  308. 'smsCode' => isset($params['smsCode']) ? $params['smsCode'] : '',
  309. ];
  310. //验证短信验证码
  311. $this->VerifyMobileCode($data['mobile'], $data['smsCode']);
  312. //验证手机号
  313. //校验手机号在当前企业是否作为客户注册过
  314. $isMobileReg = $this->objMUserCenterRegister->mobileIsRegister($data['mobile'], $this->onlineEnterpriseId);
  315. if($isMobileReg){
  316. parent::sendOutput('该手机号已经被注册', ErrorCode::$mobileishaved);
  317. }
  318. $update = [
  319. 'mobile' => $data['mobile'],
  320. 'updateTime' => time(),
  321. ];
  322. $result = $this->objMUserCenterRegister->updateUserCenter($update, ['id' => $data['userCenterId']]);
  323. if(!$result->isSuccess()){
  324. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  325. }
  326. return ResultWrapper::success($result->getData());
  327. }
  328. /**
  329. * 验证手机号是否存在(注册)
  330. */
  331. public function mobileIsRegister()
  332. {
  333. $mobile = $this->request->param('request_id');
  334. if( !$mobile ){
  335. parent::sendOutput('手机号为空', ErrorCode::$paramError);
  336. }
  337. $this->checkMobile($mobile);
  338. $result = $this->objMUserCenterRegister->mobileIsRegister($mobile, $this->onlineEnterpriseId);
  339. if ($result) {
  340. parent::sendOutput(true); //已注册
  341. }
  342. parent::sendOutput( false);//未注册
  343. }
  344. /**
  345. * 股东股份
  346. * @return void
  347. */
  348. public function holders()
  349. {
  350. $uid = $this->onlineUserId;
  351. $Mholders = new Holders();
  352. $data = $Mholders->getinfo(['en_id' => $this->onlineEnterpriseId, 'us_id' => 11])->getData();
  353. parent::sendOutput($data);
  354. }
  355. /**
  356. * 股东分红记录
  357. * @return void
  358. */
  359. public function holders_bonus()
  360. {
  361. $params = $this->request->getRawJson();
  362. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  363. $selectParams['limit'] = $pageParams['limit'];
  364. $selectParams['offset'] = $pageParams['offset'];
  365. $Mbonus = new MHoldersBonus($this->onlineEnterpriseId);
  366. $uid = $this->onlineUserId;
  367. $Mholders = new Holders();
  368. $data = $Mholders->getinfo(['en_id' => $this->onlineEnterpriseId, 'us_id' => 11])->getData();
  369. // if (empty($data)) parent::sendOutput('你不是股东', 1005);
  370. $selectParams['hol_id'] = $data['id'] ?? "";
  371. $result = $Mbonus->list($selectParams);
  372. $returnData = $result->getData();
  373. $pageData = [
  374. 'pageIndex' => $params['page'],
  375. 'pageSize' => $params['pageSize'],
  376. 'pageTotal' => $returnData['total'],
  377. ];
  378. parent::sendOutput($returnData['data'], 0, $pageData);
  379. }
  380. /**
  381. * 股东股份增加记录
  382. * @return void
  383. */
  384. public function holders_record()
  385. {
  386. $params = $this->request->getRawJson();
  387. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  388. $selectParams['limit'] = $pageParams['limit'];
  389. $selectParams['offset'] = $pageParams['offset'];
  390. $Mbonus = new MHoldersRecord($this->onlineEnterpriseId);
  391. $uid = $this->onlineUserId;
  392. $Mholders = new Holders();
  393. $data = $Mholders->getinfo(['en_id' => $this->onlineEnterpriseId, 'us_id' => 11])->getData();
  394. // if (empty($data)) parent::sendOutput('你不是股东', 1005);
  395. if ($params['pm']){
  396. $selectParams['pm'] = $params['pm'];
  397. }
  398. if ($params['type']){
  399. $selectParams['type'] = $params['type'];
  400. }
  401. $selectParams['hol_id'] = $data['id'] ?? "";
  402. $result = $Mbonus->list($selectParams);
  403. $returnData = $result->getData();
  404. $pageData = [
  405. 'pageIndex' => $params['page'],
  406. 'pageSize' => $params['pageSize'],
  407. 'pageTotal' => $returnData['total'],
  408. ];
  409. parent::sendOutput($returnData['data'], 0, $pageData);
  410. }
  411. }