UserCenterRegister.Class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * 用户中心注册管理Controller
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/01
  7. * Time: 14:00
  8. */
  9. namespace JinDouYun\Controller\UserCenter;
  10. use http\Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Cache\SmsVerification;
  14. use JinDouYun\Controller\BaseController;
  15. use JinDouYun\Model\UserCenter\MUserCenterRegister;
  16. use JinDouYun\Model\Enterprise\MEnterprise;
  17. class UserCenterRegister extends BaseController
  18. {
  19. private $objMUserCenterRegister;
  20. private $objMEnterprise;
  21. private $SmsVerification;
  22. public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken=false)
  23. {
  24. parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
  25. $this->objMUserCenterRegister = new MUserCenterRegister();
  26. $this->objMEnterprise = new MEnterprise();
  27. $this->SmsVerification = new SmsVerification();
  28. }
  29. /**
  30. * 获取参数
  31. * @param bool $verifyEnterprise
  32. * @return array
  33. */
  34. public function commonFieldFilter($verifyEnterprise = false)
  35. {
  36. //接收参数
  37. $params = $this->request->getRawJson();
  38. //映射参数
  39. $UserCenterData = [
  40. "mobile" => isset($params['mobile']) ? $params['mobile'] : '',
  41. "password" => isset($params['password']) ? $params['password'] : '',
  42. "repeatPassword" => isset($params['repeatPassword']) ? $params['repeatPassword'] : '',
  43. "smsCode" => isset($params['smsCode']) ? $params['smsCode'] : '',
  44. ];
  45. if($verifyEnterprise) {
  46. $UserCenterData["enterpriseId"] = isset($params['enterpriseId']) ? $params['enterpriseId'] : '';
  47. }
  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. unset($UserCenterData['repeatPassword']);
  59. //固定参数
  60. $UserCenterData['source'] = isset($params['source']) ? $params['source'] : StatusCode::$source['manage'];
  61. $UserCenterData['updateTime'] = time();
  62. //校验手机号格式
  63. $this->checkMobile($UserCenterData['mobile']);
  64. return $UserCenterData;
  65. }
  66. /**
  67. * 用户中心添加 / 后台用户注册
  68. * @throws \Exception
  69. */
  70. public function addUserCenter()
  71. {
  72. $UserCenterData = $this->commonFieldFilter();
  73. //校验redis短信验证码
  74. // $this->VerifyMobileCode($UserCenterData['mobile'], $UserCenterData['smsCode']);
  75. unset($UserCenterData['smsCode']);
  76. //格式化密码
  77. $UserCenterData['password'] = password_hash($UserCenterData['password'], PASSWORD_DEFAULT );
  78. //来源标识
  79. $UserCenterData['source'] = StatusCode::$source['manage'];
  80. $UserCenterData['createTime'] = time();
  81. $UserCenterData['updateTime'] = time();
  82. $result = $this->objMUserCenterRegister->addUserCenter($UserCenterData);
  83. if ($result->isSuccess()) {
  84. parent::sendOutput($result->getData());
  85. } else {
  86. parent::sendOutput($result->getData(), $result->getErrorCode());
  87. }
  88. }
  89. /**
  90. * 修改手机号(账号)
  91. */
  92. public function updateUserMobile()
  93. {
  94. $params = $this->request->getRawJson();
  95. if(empty($params)){
  96. parent::sendOutput('参数为空', ErrorCode::$paramError);
  97. }
  98. $data['mobile'] = isset($params['mobile']) ? $params['mobile'] : '';
  99. $data['userCenterId'] = isset($params['userCenterId']) ? $params['userCenterId'] : '';
  100. $data['password'] = isset($params['password']) ? $params['password'] : '';
  101. foreach($data as $key => $value){
  102. if(empty($value)){
  103. parent::sendOutput($key.'参数为空', ErrorCode::$paramError);
  104. }
  105. }
  106. $result = $this->objMUserCenterRegister->updateUserMobile($data);
  107. if ($result->isSuccess()) {
  108. parent::sendOutput($result->getData());
  109. } else {
  110. parent::sendOutput($result->getData(), $result->getErrorCode());
  111. }
  112. }
  113. /**
  114. * 用户中心删除
  115. */
  116. public function deleteUserCenter()
  117. {
  118. $params['id'] = $this->request->param('request_id');
  119. if (empty($params)) {
  120. parent::sendOutput('参数为空', ErrorCode::$paramError);
  121. }
  122. $result = $this->objMUserCenterRegister->deleteUserCenter($params);
  123. if ($result->isSuccess()) {
  124. parent::sendOutput($result->getData());
  125. } else {
  126. parent::sendOutput($result->getData(), $result->getErrorCode());
  127. }
  128. }
  129. /**
  130. * 用户中心修改
  131. */
  132. public function updateUserCenter()
  133. {
  134. $params['id'] = $this->request->param('request_id');
  135. if (empty($params)) {
  136. parent::sendOutput('参数为空', ErrorCode::$paramError);
  137. }
  138. $UserCenterData = $this->commonFieldFilter();
  139. $result = $this->objMUserCenterRegister->updateUserCenter($UserCenterData, $params);
  140. if ($result->isSuccess()) {
  141. parent::sendOutput($result->getData());
  142. } else {
  143. parent::sendOutput($result->getData(), $result->getErrorCode());
  144. }
  145. }
  146. /**
  147. * 后台用户中心忘记密码
  148. * @throws \Exception
  149. */
  150. public function forgetPassword()
  151. {
  152. $UserCenterData = $this->commonFieldFilter();
  153. //检验手机号是否存在
  154. $mobileResult = $this->objMUserCenterRegister->mobileIsRegister($UserCenterData['mobile']);
  155. if(!$mobileResult){
  156. parent::sendOutput('手机号不存在', ErrorCode::$mobileishaved);
  157. }
  158. //校验redis短信验证码
  159. $this->VerifyMobileCode($UserCenterData['mobile'], $UserCenterData['smsCode']);
  160. //格式化密码
  161. $password = password_hash($UserCenterData['password'], PASSWORD_DEFAULT );
  162. $where['mobile'] = $UserCenterData['mobile'];
  163. $result = $this->objMUserCenterRegister->updateUserCenterPassword($password, $where);
  164. if ($result->isSuccess()) {
  165. parent::sendOutput($result->getData());
  166. } else {
  167. parent::sendOutput($result->getData(), $result->getErrorCode());
  168. }
  169. }
  170. /**
  171. * 用户中心列表
  172. */
  173. public function getAllUserCenter()
  174. {
  175. $params = $this->request->getRawJson();
  176. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  177. $selectParams['limit'] = $pageParams['limit'];
  178. $selectParams['offset'] = $pageParams['offset'];
  179. $result = $this->objMUserCenterRegister->getAllUserCenter($selectParams);
  180. if ($result->isSuccess()) {
  181. $returnData = $result->getData();
  182. $pageData = [
  183. 'pageIndex' => $params['page'],
  184. 'pageSize' => $params['pageSize'],
  185. 'pageTotal' => $returnData['total'],
  186. ];
  187. parent::sendOutput($returnData['data'], 0, $pageData);
  188. } else {
  189. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  190. }
  191. }
  192. /**
  193. * 用户中心详情
  194. */
  195. public function getUserCenterInfo()
  196. {
  197. $params['id'] = $this->request->param('request_id');
  198. if (empty($params['id'])) {
  199. parent::sendOutput('参数为空', ErrorCode::$paramError);
  200. }
  201. $result = $this->objMUserCenterRegister->getUserCenterInfo($params);
  202. if ($result->isSuccess()) {
  203. parent::sendOutput($result->getData());
  204. } else {
  205. parent::sendOutput($result->getData(), $result->getErrorCode());
  206. }
  207. }
  208. /**
  209. * 公用方法
  210. */
  211. /**
  212. * 验证短信验证码
  213. *
  214. * @param $mobile
  215. * @param $mobileCode
  216. * @return void
  217. * @throws \Exception
  218. */
  219. public function VerifyMobileCode($mobile, $mobileCode)
  220. {
  221. // $cacheMobileCode = $this->SmsVerification->getMobileCode($mobile);
  222. // if( $mobileCode != $cacheMobileCode ){
  223. // parent::sendOutput('验证码有误', ErrorCode::$mobileCodeFail);
  224. // }
  225. }
  226. /**
  227. * 验证手机号是否存在(注册)
  228. */
  229. public function mobileIsRegister()
  230. {
  231. $mobile = $this->request->param('request_id');
  232. if( !$mobile ){
  233. parent::sendOutput('手机号为空', ErrorCode::$paramError);
  234. }
  235. $this->checkMobile($mobile);
  236. $result = $this->objMUserCenterRegister->mobileIsRegister($mobile);
  237. if ($result) {
  238. parent::sendOutput(true); //已注册
  239. }
  240. parent::sendOutput( false);//未注册
  241. }
  242. }