MApiUserCenter.Class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * 前台注册登录模型
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/20
  7. * Time: 9:12
  8. */
  9. namespace JinDouYun\Model\UserCenter;
  10. use Exception;
  11. use JinDouYun\Dao\Enterprise\DEnterprise;
  12. use JinDouYun\Dao\Shop\DShop;
  13. use JinDouYun\Model\MBaseModel;
  14. use JinDouYun\Model\System\MCustomerSource;
  15. use Mall\Framework\Core\ErrorCode;
  16. use Mall\Framework\Core\StatusCode;
  17. use Mall\Framework\Core\ResultWrapper;
  18. use JinDouYun\Dao\UserCenter\DUserCenter;
  19. use JinDouYun\Dao\Enterprise\DUserBindEnterprise;
  20. use JinDouYun\Dao\Customer\DCustomer;
  21. use JinDouYun\Model\Customer\MCustomer;
  22. use JinDouYun\Model\UserCenter\MUserCenterRegister;
  23. use JinDouYun\Model\System\MBasicSetup;
  24. use Mall\Framework\Core\Jwt;
  25. use Util\Common\ChineseCharacter;
  26. class MApiUserCenter extends MBaseModel
  27. {
  28. private $objDUserCenter;
  29. private $objDCustomer;
  30. private $objMCustomer;
  31. private $objMUserCenterRegister;
  32. private $objDUserBindEnterprise;
  33. private $enterpriseId;//企业id
  34. private $userCenterId;
  35. public function __construct($enterpriseId, $userCenterId)
  36. {
  37. $this->enterpriseId = $enterpriseId;
  38. $this->userCenterId = $userCenterId;
  39. $this->objDUserCenter = new DUserCenter('default');
  40. $this->objDCustomer = new DCustomer('default');
  41. $this->objMCustomer = new MCustomer($enterpriseId, $this->userCenterId);
  42. $this->objMUserCenterRegister = new MUserCenterRegister();
  43. $this->objDUserBindEnterprise = new DUserBindEnterprise('default');
  44. }
  45. /**
  46. * 前台注册 userCenter添加,需要与customer关联,与企业关联
  47. * @param $params
  48. * @return ResultWrapper
  49. * @throws Exception
  50. */
  51. public function register($params)
  52. {
  53. $this->objDUserCenter->beginTransaction();
  54. $unionId = isset($params['unionId']) ? $params['unionId'] : '';
  55. $openId = isset($params['openId']) ? $params['openId'] : '';
  56. $byteOpenId = '';
  57. if($params['source'] == StatusCode::$source['ByteProgram']){
  58. $byteOpenId = $openId;
  59. $openId = '';
  60. }
  61. //如果是微信绑定 判断是否有openId 和 unionId 如果是抖音绑定 把openId赋值给byteOpenId
  62. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  63. $name = isset($params['name']) ? $params['name'] : '';
  64. $salesManId = isset($params['salesManId']) ? $params['salesManId'] : '';
  65. unset($params['salesManId']);
  66. unset($params['unionId']);
  67. unset($params['openId']);
  68. unset($params['avatar']);
  69. unset($params['name']);
  70. //判断userCenter是否已经存在
  71. $dbResult = $this->objDUserCenter->get(['mobile' => $params['mobile'], 'deleteStatus' => StatusCode::$standard]);
  72. if($dbResult === false){
  73. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  74. }
  75. $userCenterData = $dbResult;
  76. unset($dbResult);
  77. if(empty($userCenterData)){
  78. //不存在
  79. //添加userCenter
  80. $dbResult = $this->objDUserCenter->insert($params);
  81. if ($dbResult === false) {
  82. $this->objDUserCenter->rollBack();
  83. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  84. }
  85. $userCenterId = $dbResult;
  86. unset($dbResult);
  87. }else{
  88. $userCenterId = $userCenterData['id'];
  89. }
  90. //状态 为已审核
  91. $status = StatusCode::$auditStatus['auditPass'];
  92. //查询系统设置
  93. $objMBasicSetup = new MBasicSetup($this->enterpriseId);
  94. $modelResult = $objMBasicSetup->getBasicSetup();
  95. if (!$modelResult->isSuccess()) {
  96. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  97. }
  98. $system = $modelResult->getData();
  99. //判断系统设置审核
  100. if (isset($system['basicData']) && isset($system['basicData']['personnelReview']) && $system['basicData']['personnelReview'] == StatusCode::$standard) {
  101. //如果开启客户审核 把状态待补全
  102. $status = StatusCode::$auditStatus['completion'];
  103. }
  104. //查询业务员
  105. $salesManData = [];
  106. if(!empty($salesManId)){
  107. $sql = 'select id,staffCode,staffName from qianniao_staff_'.$this->enterpriseId.' where id = '.$salesManId.' and deleteStatus = '.StatusCode::$standard;
  108. $dbResult = $this->objDCustomer->query($sql);
  109. if($dbResult === false){
  110. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  111. }
  112. $salesManResult = $dbResult;
  113. unset($dbResult);
  114. if(!empty($salesManResult)){
  115. $salesManData = array_shift($salesManResult);
  116. }
  117. }
  118. $objChineseCharacter = new ChineseCharacter();
  119. $condition = $objChineseCharacter->getInitials(trim($name));
  120. $customerType = 0;
  121. $shopId = 0;
  122. //默认店铺信息
  123. $objDEnterprise = new DEnterprise();
  124. $objDEnterprise->setTable('qianniao_enterprise_1');
  125. $objDShop = new DShop();
  126. $objDShop->setTable('qianniao_shop_1');
  127. $enterpriseDate = $objDEnterprise->get(['id'=>$this->enterpriseId]);
  128. if ($enterpriseDate === false) {
  129. return ResultWrapper::fail($objDShop->error(), ErrorCode::$dberror);
  130. }
  131. // 判断是否是单店铺 单店铺
  132. if($enterpriseDate['scope'] == StatusCode::$delete){
  133. $shopDate = $objDShop->get(['enterpriseId'=>$this->enterpriseId]);
  134. if ($shopDate === false) {
  135. return ResultWrapper::fail($objDShop->error(), ErrorCode::$dberror);
  136. }
  137. $shopId = $shopDate['id'];
  138. }
  139. // 默认客户类型
  140. $objMCustomerSource = new MCustomerSource($this->enterpriseId);
  141. $defaultType = $objMCustomerSource->getCustomerSourceList();
  142. if (!$defaultType->isSuccess()) {
  143. return ResultWrapper::fail($defaultType->getData(), $defaultType->getErrorCode());
  144. }
  145. foreach ($defaultType->getData() as $typeKey => $typeVal){
  146. if($typeVal['defaultStatus'] == StatusCode::$standard){//默认客户类型
  147. $customerType = $typeVal['id'];
  148. }
  149. }
  150. //添加customer
  151. $customerData = [
  152. 'userCenterId' => $userCenterId,
  153. 'enterpriseId' => $this->enterpriseId,
  154. 'status' => $status,
  155. 'name' => $name,
  156. 'condition' => $condition,
  157. 'avatar' => $avatar,
  158. 'enableStatus' => StatusCode::$standard,//启用状态
  159. 'openId' => $openId,
  160. 'unionId' => $unionId,
  161. 'shopId' =>$shopId,
  162. 'type' =>$customerType,
  163. ];
  164. if(!empty($salesManData)){
  165. $customerData['salesManId'] = $salesManData['id'];
  166. $customerData['salesManCode'] = $salesManData['staffCode'];
  167. $customerData['salesManName'] = $salesManData['staffName'];
  168. }
  169. $byteOpenId && $customerData['byteOpenId'] = $byteOpenId;
  170. $tableName = $this->objDCustomer->getTableName($this->objDCustomer->get_Table(), $this->enterpriseId, 1);
  171. $this->objDCustomer->setTable($tableName);
  172. $customerId = $this->objDCustomer->insert($customerData);
  173. if ($customerId === false) {
  174. $this->objDUserCenter->rollBack();
  175. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  176. }
  177. //添加企业和userCenter的关联关系
  178. $userBindEnterpriseData = [
  179. 'userCenterId' => $userCenterId,
  180. 'roleType' => StatusCode::$roleType['customer'],
  181. 'enterpriseId' => $this->enterpriseId,
  182. 'updateTime' => time(),
  183. 'createTime' => time(),
  184. ];
  185. $dbResult = $this->objDUserBindEnterprise->insert($userBindEnterpriseData);
  186. if ($dbResult === false) {
  187. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  188. }
  189. $this->objDUserCenter->commit();
  190. parent::coupon([
  191. 'userCenterId' => $userCenterId,
  192. 'customerId' => $customerId,
  193. 'enterpriseId' => $this->enterpriseId,
  194. ], 'MRegisterCoupon');
  195. parent::saveLoginLog([
  196. 'userCenterId' => $userCenterId,
  197. 'enterpriseId' => $this->enterpriseId,
  198. 'mobile' => $params['mobile'],
  199. 'createTime' => time(),
  200. 'source' => self::getSource($params['source']), // 来源:
  201. 'actionType' => '注册', // 操作类型:登录/注册
  202. 'operationData' => $params
  203. ]);
  204. //$this->objMCustomer->updateEsData($customerId);
  205. return ResultWrapper::success($userCenterId);
  206. }
  207. /**
  208. * 短信验证码登录
  209. * @param $params
  210. * @return ResultWrapper
  211. * @throws Exception
  212. */
  213. public function smsLogin($params)
  214. {
  215. //查询在该企业是否有绑定数据
  216. $sql = "SELECT u.id,u.mobile FROM " . $this->objDUserCenter->get_Table() . " AS u JOIN " . $this->objDUserBindEnterprise->get_Table() . " AS e ON u.Id=e.userCenterId WHERE u.mobile=" . $params['mobile'] . " AND e.roleType IN (" . StatusCode::$roleType['customer'] . ") AND e.enterpriseId=" . $this->enterpriseId;
  217. $result = $this->objDUserCenter->query($sql);
  218. if ($result === false) {
  219. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  220. }
  221. $userInfo = [];
  222. if(!empty($result)){
  223. $userInfo = array_shift($result);
  224. if(empty($userInfo)) $result = [];
  225. }
  226. //判断绑定数据
  227. if(!empty($result)){
  228. //存在
  229. $tableName = $this->objDCustomer->getTableName($this->objDCustomer->get_Table(), $this->enterpriseId, 1);
  230. $this->objDCustomer->setTable($tableName);
  231. $statusResult = $this->objDCustomer->get(['userCenterId' => $userInfo['id']]);
  232. if($statusResult === false){
  233. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  234. }
  235. if(empty($statusResult)){
  236. $result = [];
  237. }else{
  238. if (isset($statusResult['enableStatus']) && (int)$statusResult['enableStatus'] != StatusCode::$standard) {
  239. return ResultWrapper::fail('账号被禁用', ErrorCode::$accountfail);
  240. }
  241. }
  242. }
  243. //没有客户数据
  244. if(empty($result)){
  245. //不存在
  246. //去注册
  247. $insertData = [
  248. 'mobile' => $params['mobile'],
  249. 'source' => $params['source'],
  250. 'salesManId' => $params['salesManId'],
  251. 'isCustomer' => StatusCode::$customerType['customer'],
  252. 'createTime' => time(),
  253. 'updateTime' => time(),
  254. ];
  255. $modelResult = self::register($insertData);
  256. if(!$modelResult->isSuccess()){
  257. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  258. }
  259. $userInfo = [
  260. 'id' => $modelResult->getData(),
  261. 'mobile' => $params['mobile'],
  262. ];
  263. }
  264. parent::saveLoginLog([
  265. 'userCenterId' => $userInfo['id'],
  266. 'enterpriseId' => $this->enterpriseId,
  267. 'mobile' => $userInfo['mobile'],
  268. 'createTime' => time(),
  269. 'source' => self::getSource($params['source']), // 来源:
  270. 'actionType' => '登录', // 操作类型:登录/注册
  271. 'operationData' => $params
  272. ]);
  273. parent::coupon([
  274. 'userCenterId' => $userInfo['id'],
  275. 'customerId' => isset($statusResult['id']) ? $statusResult['id'] : '',
  276. 'enterpriseId' => $this->enterpriseId,
  277. ], 'MGrantCoupon');
  278. $returnData = self::createToken($userInfo);
  279. return ResultWrapper::success($returnData);
  280. }
  281. /**
  282. * 前台登陆 判断用户是否在当前企业已注册
  283. * @param $params
  284. * @return ResultWrapper
  285. * @throws Exception
  286. */
  287. public function login($params)
  288. {
  289. $sql = "SELECT u.id,u.mobile,u.password FROM " . $this->objDUserCenter->get_Table() . " AS u JOIN " . $this->objDUserBindEnterprise->get_Table() . " AS e ON u.Id=e.userCenterId WHERE u.mobile=" . $params['mobile'] . " AND e.roleType IN (" . StatusCode::$roleType['customer'] . ") AND e.enterpriseId=" . $this->enterpriseId;
  290. $result = $this->objDUserCenter->query($sql);
  291. if ($result === false) {
  292. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  293. }
  294. if (empty($result)) {
  295. return ResultWrapper::fail('账号不存在', ErrorCode::$userNotFount);
  296. }
  297. $userInfo = array_shift($result);
  298. if (password_verify($params['password'], $userInfo['password']) === false) {
  299. return ResultWrapper::fail('账号密码错误', ErrorCode::$accountfail);
  300. }
  301. //判断该账号是否是启用状态
  302. $tableName = $this->objDCustomer->getTableName($this->objDCustomer->get_Table(), $this->enterpriseId, 1);
  303. $this->objDCustomer->setTable($tableName);
  304. $statusResult = $this->objDCustomer->get_by('userCenterId', $userInfo['id']);
  305. if (isset($statusResult['enableStatus']) && (int)$statusResult['enableStatus'] != StatusCode::$standard) {
  306. return ResultWrapper::fail('账号被禁用', ErrorCode::$accountfail);
  307. }
  308. parent::saveLoginLog([
  309. 'userCenterId' => $userInfo['id'],
  310. 'enterpriseId' => $this->enterpriseId,
  311. 'mobile' => $userInfo['mobile'],
  312. 'createTime' => time(),
  313. 'source' => self::getSource($params['source']), // 来源:
  314. 'actionType' => '登录', // 操作类型:登录/注册
  315. 'operationData' => $params
  316. ]);
  317. parent::coupon([
  318. 'userCenterId' => $userInfo['id'],
  319. 'customerId' => isset($statusResult['id']) ? $statusResult['id'] : '',
  320. 'enterpriseId' => $this->enterpriseId,
  321. ], 'MGrantCoupon');
  322. $returnData = self::createToken($userInfo);
  323. return ResultWrapper::success($returnData);
  324. }
  325. public function getSource($sourceValue)
  326. {
  327. foreach (StatusCode::$source as $key => $source) {
  328. if ($sourceValue == $source) {
  329. return $key;
  330. }
  331. }
  332. }
  333. /**
  334. * 修改用户在当前企业的密码
  335. * @param $params
  336. * @param $where
  337. * @return ResultWrapper
  338. */
  339. public function changePassword($params, $where)
  340. {
  341. $dbResult = $this->objDUserCenter->update($params, $where);
  342. if ($dbResult === false) {
  343. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  344. } else {
  345. return ResultWrapper::success($dbResult);
  346. }
  347. }
  348. /**
  349. * 创建token
  350. * @param $userInfo
  351. * @return array
  352. */
  353. public function createToken($userInfo)
  354. {
  355. //jwt过期时间
  356. $expireTime = time() + 7200;
  357. $payload = array(
  358. 'uid' => $userInfo['id'],
  359. 'expireTime' => $expireTime,
  360. );
  361. $encoded = JWT::encode($payload, SITE_SECRET);
  362. return [
  363. 'expireTime' => $expireTime,
  364. 'token' => $encoded,
  365. ];
  366. }
  367. /**
  368. * 检查是否绑定
  369. * @param $postData
  370. * @return ResultWrapper
  371. */
  372. public function checkBind($postData)
  373. {
  374. $source = $postData['source'];
  375. unset($postData['source']);
  376. $userCenterId = $this->objMCustomer->getUserCenterIdByOpenIdOrUnionId($postData,$source);
  377. if(!$userCenterId->isSuccess()){
  378. return ResultWrapper::fail($userCenterId->getData(), $userCenterId->getErrorCode());
  379. }
  380. //返回绑定数据
  381. $userCenterData = $userCenterId->getData();
  382. if(!empty($userCenterData)){
  383. $userData = array_shift($userCenterData);
  384. //有绑定数据
  385. $updateData = [];
  386. if($source == StatusCode::$source['miniProgram']){
  387. //如果是小程序检查是否绑定 不需要修改openId
  388. //判断openid是否为空 如果为空 覆盖
  389. if(isset($userData['openId']) && !empty($postData['openId'])){
  390. $updateData['openId'] = $postData['openId'];
  391. }
  392. //判断unionId是否为空 如果为空 覆盖
  393. if(isset($userData['unionId']) && !empty($postData['unionId'])){
  394. $updateData['unionId'] = $postData['unionId'];
  395. }
  396. if(!empty($updateData)){
  397. $modelResult = $this->objMCustomer->setCustomerData(['id' => $userData['id']], $updateData);
  398. if(!$modelResult->isSuccess()){
  399. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  400. }
  401. }
  402. }
  403. $userCenterId = $userData['userCenterId'];
  404. }else{
  405. //没有绑定
  406. return ResultWrapper::success(['status' => false]);
  407. }
  408. $returnData = self::createToken(['id' => $userCenterId]);
  409. return ResultWrapper::success($returnData);
  410. }
  411. /**
  412. * 绑定openid和mobile
  413. * @param $source
  414. * @param $mobile
  415. * @param $openId
  416. * @param $unionId
  417. * @param $enterpriseId
  418. * @param string $avatar
  419. * @param string $name
  420. * @return ResultWrapper
  421. */
  422. public function bindMobileAndOpenId($source, $mobile, $openId, $unionId,$enterpriseId, $avatar = '', $name = '')
  423. {
  424. //查询手机号是否注册过
  425. $userCenterId = $this->objMUserCenterRegister->mobileIsRegister($mobile, $enterpriseId);
  426. if(!$userCenterId){
  427. //未注册
  428. return ResultWrapper::fail('手机号未注册', ErrorCode::$paramError);
  429. }
  430. //手机号已注册,则绑定完openid返回token
  431. $modelResult = $this->objMCustomer->bindOpenIdAndMobile($source, $userCenterId, $openId, $unionId, $avatar, $name);
  432. if(!$modelResult->isSuccess()){
  433. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  434. }
  435. $returnData = self::createToken(['id' => $userCenterId]);
  436. return ResultWrapper::success($returnData);
  437. }
  438. }