UserCenterService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. <?php
  2. namespace App\Common\Services;
  3. use App\Exceptions\ApiException;
  4. use Illuminate\Support\Facades\Log;
  5. class UserCenterService
  6. {
  7. private $appCode;
  8. private $appKey;
  9. private $verKey;
  10. private $operationKey;
  11. private $url;
  12. public function __construct()
  13. {
  14. $config = config('api.user_center_config');
  15. $this->appCode = $config['app_code'];
  16. $this->appKey = $config['app_key'];
  17. $this->verKey = $config['ver_key'];
  18. $this->url = $config['url'];
  19. $this->operationKey = $config['operation_key'];
  20. //Log::info(json_encode($config));
  21. }
  22. /**
  23. * http请求
  24. * @param $method 请求方式 GET|POST|PUT|DELETE
  25. * @param $url 请求地址
  26. * @param array $params 请求参数
  27. * @param array $headers 请求带的header头
  28. * @return string 返回数据
  29. * @throws ApiException
  30. */
  31. public function request($method, $url, $params = [], $headers = [])
  32. {
  33. $headers['X-Requested-With'] = 'XMLHttpRequest';
  34. return httpClient($method, $url, $params, $headers);
  35. }
  36. /**
  37. * 登录
  38. * @param $username
  39. * @param $password
  40. * @param string $redirect
  41. * @param string $checkCode
  42. * @return array
  43. * @throws ApiException
  44. */
  45. public function pwdLogin($username, $password)
  46. {
  47. $ip = getClientIp(0, true);
  48. $res = $this->request('post', $this->url . 'app/user/login', [
  49. 'user_name' => $username,
  50. 'password' => $password,
  51. 'app_code' => $this->appCode,
  52. 'app_key' => $this->appKey,
  53. 'ip' => $ip
  54. ]);
  55. $resData = json_decode($res, true);
  56. if ($this->isSuccess($resData)) {
  57. return $resData['data'];
  58. } else {
  59. $message = empty($resData['message']) ? '' : $resData['message'];
  60. throw new ApiException(1004,['msg'=> $message]);
  61. }
  62. }
  63. /**
  64. * 是否返回成功
  65. * @param $res
  66. * @return bool
  67. */
  68. private function isSuccess($res)
  69. {
  70. if (!empty($res) && is_array($res) && $res['code'] == 0) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * 登出
  77. * @param $apiToken
  78. * @return boolean
  79. * @throws ApiException
  80. */
  81. public function logout($apiToken)
  82. {
  83. $res = $this->request('post', $this->url . 'app/user/logout', ['api_token' => $apiToken], ['api-token' => $apiToken]);
  84. $resData = json_decode($res, true);
  85. if ($this->isSuccess($resData)) {
  86. return $resData['data'];
  87. } else {
  88. $message = empty($resData['message']) ? '' : $resData['message'];
  89. throw new ApiException(1004,['msg'=> $message]);
  90. }
  91. }
  92. /**
  93. * 获取主帐号详情
  94. * @return mixed|string
  95. * @throws ApiException
  96. */
  97. public function getMasterSysUserInfo($apiToken)
  98. {
  99. $res = $this->request('post', $this->url . 'app/user/master-user-info', [], ['api-token' => $apiToken]);
  100. $resData = json_decode($res, true);
  101. if ($this->isSuccess($resData)) {
  102. return $resData['data'];
  103. } else {
  104. $message = empty($resData['message']) ? '' : $resData['message'];
  105. throw new ApiException(1004,['msg'=> $message]);
  106. }
  107. }
  108. /**
  109. * 获取帐号详情
  110. * @return mixed|string
  111. * @throws ApiException
  112. */
  113. public function getSysUserInfo($apiToken)
  114. {
  115. $res = $this->request('post', $this->url . 'app/user/base-info', [
  116. 'app_code' => $this->appCode
  117. ], ['api-token' => $apiToken]);
  118. $resData = json_decode($res, true);
  119. if ($this->isSuccess($resData)) {
  120. return $resData['data'];
  121. } else {
  122. $message = empty($resData['message']) ? '' : $resData['message'];
  123. throw new ApiException(1004,['msg'=> $message]);
  124. }
  125. }
  126. /**
  127. * 授权接口
  128. * @param $token
  129. * @return array
  130. * @throws ApiException
  131. */
  132. public function getAuth($apiToken)
  133. {
  134. $res = $this->request('post', $this->url . 'app/user/get-auth', [
  135. 'app_code' => $this->appCode,
  136. 'app_key' => $this->appKey
  137. ], ['api-token' => $apiToken]);
  138. $resData = json_decode($res, true);
  139. if ($this->isSuccess($resData)) {
  140. return $resData['data'];
  141. } else {
  142. $message = empty($resData['message']) ? '' : $resData['message'];
  143. throw new ApiException(1004,['msg'=> $message]);
  144. }
  145. }
  146. /**
  147. * 心跳接口
  148. * @param $apiToken
  149. * @return mixed|string
  150. * @throws ApiException
  151. */
  152. public function heartBeat($apiToken, $throwError = false)
  153. {
  154. $res = $this->request('post', $this->url . 'app/user/heart-beat', [], ['api-token' => $apiToken]);
  155. //file_put_contents(storage_path('logs/hearBeat.log'), $res."\r\n", FILE_APPEND);
  156. $resData = json_decode($res, true);
  157. if ($this->isSuccess($resData)) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. /**
  164. * 修改密码
  165. * @param integer $type 0 根据帐号 1根据手机 2根据旧邮箱
  166. * @param string $password 密码
  167. * @param string $confirm_password 确认密码
  168. * @param string $oldPassword 旧密码 类型(0)必填
  169. * @param string $validCode 验证码 类型(1、2)必填
  170. * @return mixed|string|array
  171. * @throws ApiException
  172. */
  173. public function updatePwd($type, $password, $confirm_password, $oldPassword = '', $validCode = '', $apiToken)
  174. {
  175. $res = $this->request('post', $this->url . 'app/user/update-pwd', [
  176. 'type' => $type,
  177. 'password' => $password,
  178. 'confirm_password' => $confirm_password,
  179. 'old_password' => $oldPassword,
  180. 'valid_code' => $validCode,
  181. ], ['api-token' => $apiToken]);
  182. $resData = json_decode($res, true);
  183. if ($this->isSuccess($resData)) {
  184. return $resData['data'];
  185. } else {
  186. $message = empty($resData['message']) ? '' : $resData['message'];
  187. throw new ApiException(1004,['msg'=> $message]);
  188. }
  189. }
  190. /**
  191. * 手机号查看用户是否存在
  192. * @param $phone
  193. * @return mixed
  194. * @throws ApiException
  195. */
  196. public function checkSysUserPhoneUnique($phone, $throwError = true)
  197. {
  198. $res = $this->request('post', $this->url . 'app/user/check-client-phone-unique', [
  199. 'phone' => $phone
  200. ]);
  201. $resData = json_decode($res, true);
  202. if ($this->isSuccess($resData)) {
  203. return $resData['data'];
  204. } else {
  205. if ($throwError) {
  206. $message = empty($resData['message']) ? '' : $resData['message'];
  207. throw new ApiException(1004,['msg'=> $message]);
  208. } else {
  209. //10012 手机号已被注册
  210. return $resData;
  211. }
  212. }
  213. }
  214. /**
  215. * 检查注册用户名是否存在 暂时不开发用户名注册
  216. * @param $phone
  217. * @return mixed
  218. * @throws ApiException
  219. */
  220. public function checkSysUserUserNameUnique($userName, $throwError = true)
  221. {
  222. $res = $this->request('post', $this->url . 'app/user/check-client-user-name-unique', [
  223. 'user_name' => $userName
  224. ]);
  225. $resData = json_decode($res, true);
  226. if ($this->isSuccess($resData)) {
  227. return $resData['data'];
  228. } else {
  229. if ($throwError) {
  230. $message = empty($resData['message']) ? '' : $resData['message'];
  231. throw new ApiException(1004,['msg'=> $message]);
  232. } else {
  233. return $resData;
  234. }
  235. }
  236. }
  237. /**
  238. * 检查注册邮箱是否存在
  239. * @param $phone
  240. * @return mixed
  241. * @throws ApiException
  242. */
  243. public function checkSysUserEmailUnique($email, $throwError = true)
  244. {
  245. $res = $this->request('post', $this->url . 'app/user/check-client-email-unique', [
  246. 'email' => $email
  247. ]);
  248. $resData = json_decode($res, true);
  249. if ($this->isSuccess($resData)) {
  250. return $resData['data'];
  251. } else {
  252. if ($throwError) {
  253. $message = empty($resData['message']) ? '' : $resData['message'];
  254. throw new ApiException(1004,['msg'=> $message]);
  255. } else {
  256. //10014 邮箱已被注册
  257. return $resData;
  258. }
  259. }
  260. }
  261. /**
  262. * 检查帐号是否为主帐号
  263. * @param $phone
  264. * @return mixed
  265. * @throws ApiException
  266. */
  267. public function checkSysUserIsMaster($params, $throwError = true)
  268. {
  269. $checkData = [];
  270. if (!empty($params['user_name'])) {
  271. $checkData['user_name'] = $params['user_name'];
  272. }
  273. if (!empty($params['email'])) {
  274. $checkData['email'] = $params['email'];
  275. }
  276. if (!empty($params['phone'])) {
  277. $checkData['phone'] = $params['phone'];
  278. }
  279. $res = $this->request('post', $this->url . 'app/user/check-client-user-is-master', $checkData);
  280. $resData = json_decode($res, true);
  281. if ($this->isSuccess($resData)) {
  282. return $resData['data'];
  283. } else {
  284. if ($throwError) {
  285. $message = empty($resData['message']) ? '' : $resData['message'];
  286. throw new ApiException(1004,['msg'=> $message]);
  287. } else {
  288. return $resData;
  289. }
  290. }
  291. }
  292. /**
  293. * 获取短信验证码
  294. * @param $phone
  295. * @param $type
  296. * @return mixed
  297. * @throws ApiException
  298. */
  299. public function getSmsCode($phone,$countryCode, $type = 0)
  300. {
  301. $res = $this->request('get', $this->url . 'common/get-sms-code', [
  302. 'phone' => $phone,
  303. 'type' => $type,
  304. 'country_code' => $countryCode,
  305. ]);
  306. // $res = '{"code":0,"message":"","data":1}';
  307. $resData = json_decode($res, true);
  308. if ($this->isSuccess($resData)) {
  309. return $resData['data'];
  310. } else {
  311. $message = empty($resData['message']) ? '' : $resData['message'];
  312. throw new ApiException(1004,['msg'=> $message]);
  313. }
  314. }
  315. /**
  316. * 获取短信验证码
  317. * @param $email
  318. * @return mixed
  319. * @throws ApiException
  320. */
  321. public function getEmailCode($email,$language='zh-cn',$footer='')
  322. {
  323. $res = $this->request('get', $this->url . 'common/get-email-code', [
  324. 'email' => $email,
  325. 'language' => $language,
  326. 'footer' => $footer,
  327. ]);
  328. $resData = json_decode($res, true);
  329. if ($this->isSuccess($resData)) {
  330. return $resData['data'];
  331. } else {
  332. if($resData['code']==10021) {
  333. throw new ApiException('common.email_empty', '邮箱不能为空');
  334. }
  335. $message = empty($resData['message']) ? '' : $resData['message'];
  336. throw new ApiException(1004,['msg'=> $message]);
  337. }
  338. }
  339. /**
  340. * 登录
  341. * @param $username
  342. * @param $password
  343. * @param string $redirect
  344. * @param string $checkCode
  345. * @return array ['status'=>1,'redirect'=>'']
  346. * @throws ApiException
  347. */
  348. public function phoneLogin($phone, $validCode,$countryCode=86)
  349. {
  350. $ip = getClientIp(0, true);
  351. $res = $this->request('post', $this->url . 'app/user/phone-login', [
  352. 'phone' => $phone,
  353. 'valid_code' => $validCode,
  354. 'country_code' => $countryCode,
  355. 'app_code' => $this->appCode,
  356. 'app_key' => $this->appKey,
  357. 'ip' => $ip
  358. ]);
  359. // $res = '{"code":0,"message":"","data":{"api_token":"d3506997fe83325eb0cca61de31737d8","is_admin":1,"expire":604800,"nick_name":"李肖明","user_name":"lixming","real_name":"","company_id":10327,"invalid_time":"2028-04-15 23:59:59"}}';
  360. $resData = json_decode($res, true);
  361. if ($this->isSuccess($resData)) {
  362. return $resData['data'];
  363. } else {
  364. $message = empty($resData['message']) ? '' : $resData['message'];
  365. throw new ApiException(1004,['msg'=> $message]);
  366. }
  367. }
  368. /**
  369. * 获取子帐号
  370. * @param $companyId
  371. * @return array
  372. * @throws ApiException
  373. */
  374. public function getSysUserSubUser($keyword, $apiToken)
  375. {
  376. $res = $this->request('post', $this->url . 'app/user/get-client-sub-user', ['keyword' => $keyword], ['api-token' => $apiToken]);
  377. //file_put_contents(storage_path('logs/aaa.log'), $res."\r\n", FILE_APPEND);
  378. $resData = json_decode($res, true);
  379. if ($this->isSuccess($resData)) {
  380. return $resData['data'];
  381. } else {
  382. $message = empty($resData['message']) ? '' : $resData['message'];
  383. throw new ApiException(1004,['msg'=> $message]);
  384. }
  385. }
  386. /**
  387. * 修改子帐号
  388. * @param array $params id nick_name password real_name status
  389. * @param $apiToken
  390. * @return array
  391. * @throws ApiException
  392. */
  393. public function updateSubAccount($params = [], $apiToken)
  394. {
  395. $res = $this->request('post', $this->url . 'app/user/update-sub-account', $params, ['api-token' => $apiToken]);
  396. //file_put_contents(storage_path('logs/aaa.log'), $res."\r\n", FILE_APPEND);
  397. $resData = json_decode($res, true);
  398. if ($this->isSuccess($resData)) {
  399. return $resData['data'];
  400. } else {
  401. $message = empty($resData['message']) ? '' : $resData['message'];
  402. throw new ApiException(1004,['msg'=> $message]);
  403. }
  404. }
  405. /**
  406. * 删除
  407. * @param integer $sysUserId 用户中心id
  408. * @return array
  409. * @throws ApiException
  410. */
  411. public function delSubAccount($sysUserId, $apiToken)
  412. {
  413. $res = $this->request('post', $this->url . 'app/user/del-sub-account', ['id' => $sysUserId], ['api-token' => $apiToken]);
  414. //file_put_contents(storage_path('logs/aaa.log'), $res."\r\n", FILE_APPEND);
  415. $resData = json_decode($res, true);
  416. if ($this->isSuccess($resData)) {
  417. return $resData['data'];
  418. } else {
  419. $message = empty($resData['message']) ? '' : $resData['message'];
  420. throw new ApiException(1004,['msg'=> $message]);
  421. }
  422. }
  423. /**
  424. * 添加子账号
  425. * @param array params 子帐号参数
  426. * @param array params.user_name 帐号
  427. * @param array params.nick_name 昵称
  428. * @param array params.password 密码
  429. * @return array
  430. * @throws ApiException
  431. */
  432. public function addSubAccount($params = [], $apiToken)
  433. {
  434. $subAccountData = [];
  435. $subAccountData['user_name'] = $params['user_name'];
  436. $subAccountData['nick_name'] = !empty($params['nick_name']) ? $params['nick_name'] : '';
  437. $subAccountData['password'] = $params['password'];
  438. $res = $this->request('post', $this->url . 'app/user/add-sub-account', $subAccountData, ['api-token' => $apiToken]);
  439. $resData = json_decode($res, true);
  440. if ($this->isSuccess($resData)) {
  441. return $resData['data'];
  442. } else {
  443. $message = empty($resData['message']) ? '' : $resData['message'];
  444. throw new ApiException(1004,['msg'=> $message]);
  445. }
  446. }
  447. /**
  448. * 注册用户
  449. * @param array params 注册用户参数
  450. * @param string params.password 密码
  451. * @param string params.confirm_password 确认密码
  452. * @param string params.phone 手机
  453. * @param string params.email 邮箱
  454. * @param string params.valid_code 验证码
  455. * @param string params.nick_name 昵称
  456. * @param string params.app_code 应用code
  457. * @param string params.app_key 应用key
  458. * @param string params.ver_Key 应用版本
  459. * @param integer params.auto_login 自动登录
  460. * */
  461. public function registerSysUser($params)
  462. {
  463. $nowTime = nowTime();
  464. $registerData = [];
  465. if (!empty($params['password'])) {
  466. $registerData['password'] = $params['password'];
  467. }
  468. if (!empty($params['confirm_password'])) {
  469. $registerData['confirm_password'] = $params['confirm_password'];
  470. }
  471. if (!empty($params['phone'])) {
  472. $registerData['phone'] = $params['phone'];
  473. }
  474. if (!empty($params['email'])) {
  475. $registerData['email'] = $params['email'];
  476. }
  477. if (!empty($params['valid_code'])) {
  478. $registerData['valid_code'] = $params['valid_code'];
  479. }
  480. if (!empty($params['nick_name'])) {
  481. $registerData['nick_name'] = $params['nick_name'];
  482. }
  483. if (isset($params['auto_login'])) {
  484. $registerData['auto_login'] = empty($params['auto_login']) ? 0 : $params['auto_login'];
  485. }
  486. $registerData['app_code'] = $this->appCode;
  487. $registerData['app_key'] = $this->appKey;
  488. $registerData['ver_Key'] = $this->verKey;
  489. $registerData['start_time'] = $nowTime;
  490. $registerData['end_time'] = '2099-12-31 23:59:59';
  491. $res = $this->request('post', $this->url . 'app/user/register', $registerData);
  492. $resData = json_decode($res, true);
  493. if ($this->isSuccess($resData)) {
  494. return $resData['data'];
  495. } else {
  496. $message = empty($resData['message']) ? '' : $resData['message'];
  497. throw new ApiException(1004,['msg'=> $message]);
  498. }
  499. }
  500. /**
  501. * 忘记密码 获取重置密码的token
  502. * @param array params 获取重置密码的token
  503. * @param integer params.type 类型 1手机 2邮箱
  504. * @param integer params.valid_code 验证码
  505. * @param string params.email 邮箱
  506. * @param string params.phone 手机
  507. * */
  508. public function getResetPwdToken($params)
  509. {
  510. $resetParams = [];
  511. $resetParams['type'] = $params['type'];
  512. $resetParams['valid_code'] = $params['valid_code'];
  513. if (!empty($params['email'])) {
  514. $resetParams['email'] = $params['email'];
  515. }
  516. if (!empty($params['phone'])) {
  517. $resetParams['phone'] = $params['phone'];
  518. }
  519. $res = $this->request('post', $this->url . 'app/user/reset-pwd-validate', $resetParams);
  520. $resData = json_decode($res, true);
  521. if ($this->isSuccess($resData)) {
  522. return $resData['data'];
  523. } else {
  524. $message = empty($resData['message']) ? '' : $resData['message'];
  525. throw new ApiException(1004,['msg'=> $message]);
  526. }
  527. }
  528. /**
  529. * 根据reset_token 重置密码
  530. * @param string password 密码
  531. * @param string confirm_password 确认密码
  532. * @param string reset_token 重置tokem
  533. * */
  534. public function resetPasswordByToken($params)
  535. {
  536. $resetParams = [];
  537. $resetParams['password'] = $params['password'];
  538. $resetParams['confirm_password'] = $params['confirm_password'];
  539. $resetParams['reset_token'] = $params['reset_token'];
  540. $res = $this->request('post', $this->url . 'app/user/reset-pwd', $resetParams);
  541. $resData = json_decode($res, true);
  542. if ($this->isSuccess($resData)) {
  543. return $resData['data'];
  544. } else {
  545. $message = empty($resData['message']) ? '' : $resData['message'];
  546. throw new ApiException(1004,['msg'=> $message]);
  547. }
  548. }
  549. /**
  550. * 绑定应用
  551. * @param string apiToken
  552. * */
  553. public function appBind($apiToken)
  554. {
  555. $nowTime = nowTime();
  556. $bindData = [];
  557. $bindData['app_code'] = $this->appCode;
  558. $bindData['app_key'] = $this->appKey;
  559. $bindData['ver_key'] = $this->verKey;
  560. $bindData['start_time'] = $nowTime;
  561. $bindData['end_time'] = '2099-12-31 23:59:59';
  562. $res = $this->request('post', $this->url . 'app/user/app-bind', $bindData, ['api-token' => $apiToken]);
  563. $resData = json_decode($res, true);
  564. if ($this->isSuccess($resData)) {
  565. return $resData['data'];
  566. } else {
  567. $message = empty($resData['message']) ? '' : $resData['message'];
  568. throw new ApiException(1004,['msg'=> $message]);
  569. }
  570. }
  571. /**
  572. * 绑定应用
  573. * @param string apiToken
  574. * */
  575. public function appBindById($params)
  576. {
  577. $bindData = [];
  578. $bindData['app_code'] = $params['app_code'];
  579. $bindData['app_key'] = $params['app_key'];
  580. $bindData['ver_key'] = $params['ver_key'];
  581. $bindData['start_time'] = $params['start_time'];;
  582. $bindData['end_time'] = $params['end_time'];;
  583. $bindData['user_id'] = $params['user_id'];;
  584. $bindData['operation_key'] = $this->operationKey;
  585. $res = $this->request('post', $this->url . 'app/user/app-bind-by-id', $bindData);
  586. $resData = json_decode($res, true);
  587. if ($this->isSuccess($resData)) {
  588. return $resData['data'];
  589. } else {
  590. $message = empty($resData['message']) ? '' : $resData['message'];
  591. throw new ApiException(1004,['msg'=> $message]);
  592. }
  593. }
  594. /**
  595. * 绑定应用
  596. * @param string apiToken
  597. * */
  598. public function appBindUp($params,$throw=true)
  599. {
  600. $bindData = [];
  601. $bindData['app_code'] = $this->appCode;
  602. $bindData['app_key'] = $this->appKey;
  603. $verKey=$params['ver_key'];
  604. $bindData['ver_key'] = $verKey;
  605. $bindData['bind_type'] =$params['bind_type'];//bind_type 绑定类型 0 新增 1续费 2升级 3降级
  606. $bindData['user_id'] =$params['user_id'];//bind_type 绑定类型 0 新增 1续费 2升级 3降级
  607. $bindData['operation_key'] = $this->operationKey;
  608. if(isset($params['start_time'])){
  609. $bindData['start_time'] = $params['start_time'];
  610. }
  611. if(isset($params['end_time'])){
  612. $bindData['end_time'] = $params['end_time'];
  613. }
  614. $res = $this->request('post', $this->url . 'app/user/manage-up-app-bind', $bindData);
  615. $resData = json_decode($res, true);
  616. if ($this->isSuccess($resData)) {
  617. return $resData['data'];
  618. } else {
  619. if($throw){
  620. $message = empty($resData['message']) ? '' : $resData['message'];
  621. throw new ApiException(1004,['msg'=> $message]);
  622. }else{
  623. return false;
  624. }
  625. }
  626. }
  627. /**
  628. * 绑定应用
  629. * @param string apiToken
  630. * */
  631. public function addExpandPacket($params)
  632. {
  633. $bindData = [];
  634. $bindData['app_code'] = $this->appCode;
  635. $bindData['app_key'] = $this->appKey;
  636. $bindData['packet_key'] = $params['packet_key'];
  637. $bindData['start_time'] =$params['start_time'];
  638. $bindData['user_id'] =$params['user_id'];
  639. $bindData['operation_key'] = $this->operationKey;
  640. $res = $this->request('post', $this->url . 'app/user/add-expand-packet', $bindData);
  641. $resData = json_decode($res, true);
  642. if ($this->isSuccess($resData)) {
  643. return $resData['data'];
  644. } else {
  645. $message = empty($resData['message']) ? '' : $resData['message'];
  646. throw new ApiException(1004,['msg'=> $message]);
  647. }
  648. }
  649. /**
  650. * 变更绑定手机获取变更绑定的reset_token
  651. * @param array params 获取重置密码的token
  652. * @param integer params.type 类型 1当前手机 2邮箱
  653. * @param string params.valid_code 验证码
  654. * */
  655. public function getBindPhoneResetToken($params, $apiToken)
  656. {
  657. $resetParams = [];
  658. $resetParams['type'] = $params['type'];
  659. $resetParams['valid_code'] = $params['valid_code'];
  660. $res = $this->request('post', $this->url . 'app/user/get-bind-phone-reset-token', $resetParams, ['api-token' => $apiToken]);
  661. $resData = json_decode($res, true);
  662. if ($this->isSuccess($resData)) {
  663. return $resData['data'];
  664. } else {
  665. $message = empty($resData['message']) ? '' : $resData['message'];
  666. throw new ApiException(1004,['msg'=> $message]);
  667. }
  668. }
  669. /**
  670. * 绑定手机
  671. * @param array params 获取重置密码的token
  672. * @param integer params.type 类型 1手机 2邮箱
  673. * @param string params.valid_code 验证码
  674. * */
  675. public function userPhoneBind($params, $apiToken)
  676. {
  677. $resetParams = [];
  678. $resetParams['phone'] = $params['phone'];
  679. $resetParams['valid_code'] = $params['valid_code'];
  680. if (isset($params['reset_token'])) {
  681. $resetParams['reset_token'] = $params['reset_token'];
  682. }
  683. $res = $this->request('post', $this->url . 'app/user/bind-phone', $resetParams, ['api-token' => $apiToken]);
  684. $resData = json_decode($res, true);
  685. if ($this->isSuccess($resData)) {
  686. return $resData['data'];
  687. } else {
  688. $message = empty($resData['message']) ? '' : $resData['message'];
  689. throw new ApiException(1004,['msg'=> $message]);
  690. }
  691. }
  692. /**
  693. * 变更绑定邮箱获取变更绑定的reset_token
  694. * @param array params 获取重置密码的token
  695. * @param integer params.type 类型 1当前手机 2邮箱
  696. * @param string params.valid_code 验证码
  697. * */
  698. public function getBindEmailResetToken($params, $apiToken)
  699. {
  700. $resetParams = [];
  701. $resetParams['type'] = $params['type'];
  702. $resetParams['valid_code'] = $params['valid_code'];
  703. $res = $this->request('post', $this->url . 'app/user/get-bind-email-reset-token', $resetParams, ['api-token' => $apiToken]);
  704. $resData = json_decode($res, true);
  705. if ($this->isSuccess($resData)) {
  706. return $resData['data'];
  707. } else {
  708. $message = empty($resData['message']) ? '' : $resData['message'];
  709. throw new ApiException(1004,['msg'=> $message]);
  710. }
  711. }
  712. /**
  713. * 绑定邮箱
  714. * @param array params 获取重置密码的token
  715. * @param integer params.type 类型 1当前手机 2邮箱
  716. * @param string params.valid_code 验证码
  717. * */
  718. public function userEmailBind($params, $apiToken)
  719. {
  720. $resetParams = [];
  721. $resetParams['email'] = $params['email'];
  722. $resetParams['valid_code'] = $params['valid_code'];
  723. if (isset($params['reset_token'])) {
  724. $resetParams['reset_token'] = $params['reset_token'];
  725. }
  726. $res = $this->request('post', $this->url . 'app/user/bind-email', $resetParams, ['api-token' => $apiToken]);
  727. $resData = json_decode($res, true);
  728. if ($this->isSuccess($resData)) {
  729. return $resData['data'];
  730. } else {
  731. $message = empty($resData['message']) ? '' : $resData['message'];
  732. throw new ApiException(1004,['msg'=> $message]);
  733. }
  734. }
  735. /**
  736. * 子帐号修改密码
  737. * */
  738. public function subAccountUpdatePwd($params, $apiToken)
  739. {
  740. $updateParams = [];
  741. $updateParams['password'] = $params['password'];
  742. $updateParams['confirm_password'] = $params['confirm_password'];
  743. $updateParams['old_password'] = $params['old_password'];
  744. $res = $this->request('post', $this->url . 'app/user/sub-account-update-pwd', $updateParams, ['api-token' => $apiToken]);
  745. $resData = json_decode($res, true);
  746. if ($this->isSuccess($resData)) {
  747. return $resData['data'];
  748. } else {
  749. $message = empty($resData['message']) ? '' : $resData['message'];
  750. throw new ApiException(1004,['msg'=> $message]);
  751. }
  752. }
  753. /**
  754. * 注册用户
  755. * @param array params 注册用户参数
  756. * @param string params.password 密码
  757. * @param string params.confirm_password 确认密码
  758. * @param string params.phone 手机
  759. * @param string params.email 邮箱
  760. * @param string params.valid_code 验证码
  761. * @param string params.nick_name 昵称
  762. * @param string params.app_code 应用code
  763. * @param string params.app_key 应用key
  764. * @param string params.ver_Key 应用版本
  765. * @param integer params.auto_login 自动登录
  766. * */
  767. public function preRegister($params)
  768. {
  769. $nowTime = nowTime();
  770. $registerData = [];
  771. if (!empty($params['phone'])) {
  772. $registerData['phone'] = $params['phone'];
  773. $registerData['country_code'] = empty($params['country_code'])?'86':$params['country_code'];
  774. }
  775. if (!empty($params['email'])) {
  776. $registerData['email'] = $params['email'];
  777. }
  778. if (!empty($params['valid_code'])) {
  779. $registerData['valid_code'] = $params['valid_code'];
  780. }
  781. $registerData['app_code'] = $this->appCode;
  782. $registerData['app_key'] = $this->appKey;
  783. $registerData['ver_Key'] = $this->verKey;
  784. $registerData['start_time'] = $nowTime;
  785. $registerData['end_time'] = '2099-12-31 23:59:59';
  786. $res = $this->request('post', $this->url . 'app/user/pre-register', $registerData);
  787. $resData = json_decode($res, true);
  788. if ($this->isSuccess($resData)) {
  789. return $resData['data'];
  790. } else {
  791. $message = empty($resData['message']) ? '' : $resData['message'];
  792. throw new ApiException(1004,['msg'=> $message]);
  793. }
  794. }
  795. /**
  796. * 确认注册
  797. * */
  798. public function confirmRegister($params){
  799. $confirmParams = [];
  800. $confirmParams['password'] = $params['password'];
  801. $confirmParams['confirm_password'] = $params['confirm_password'];
  802. $confirmParams['pre_register_key'] = $params['pre_register_key'];
  803. if (!empty($params['auto_login'])) {
  804. //是否自动登陆
  805. $confirmParams['auto_login'] = $params['auto_login'];
  806. }
  807. $res = $this->request('post', $this->url . 'app/user/confirm-register', $confirmParams);
  808. $resData = json_decode($res, true);
  809. if ($this->isSuccess($resData)) {
  810. return $resData['data'];
  811. } else {
  812. $message = empty($resData['message']) ? '' : $resData['message'];
  813. throw new ApiException(1004,['msg'=> $message]);
  814. }
  815. }
  816. /**
  817. * 更新用户数值权限使用量
  818. * @return mixed
  819. * @throws ApiException
  820. * */
  821. public function updateBindUse($params){
  822. $updateBindData = [];
  823. $updateBindData['app_code'] = $this->appCode;
  824. $updateBindData['app_key'] = $this->appKey;
  825. $updateBindData['operation_key'] = $this->operationKey;
  826. $updateBindData['type_numerical_key'] = $params['type_numerical_key'];
  827. $updateBindData['user_id'] = $params['user_id'];
  828. $updateBindData['update_value'] = $params['update_value'];
  829. $res = $this->request('post', $this->url . 'app/user/update-bind-use', $updateBindData);
  830. $resData = json_decode($res, true);
  831. if ($this->isSuccess($resData)) {
  832. return $resData['data'];
  833. } else {
  834. $message = empty($resData['message']) ? '' : $resData['message'];
  835. throw new ApiException(1004,['msg'=> $message]);
  836. }
  837. }
  838. /**
  839. * 获取用户数值权限使用量
  840. * */
  841. public function getBindUse($params){
  842. $updateBindData = [];
  843. $updateBindData['app_code'] = $this->appCode;
  844. $updateBindData['app_key'] = $this->appKey;
  845. $updateBindData['operation_key'] = $this->operationKey;
  846. $updateBindData['user_id'] = $params['user_id'];
  847. $updateBindData['type_numerical_key'] = empty($params['type_numerical_key'])?'':$params['type_numerical_key'];
  848. $res = $this->request('post', $this->url . 'app/user/get-bind-use', $updateBindData);
  849. $resData = json_decode($res, true);
  850. if ($this->isSuccess($resData)) {
  851. return $resData['data'];
  852. } else {
  853. $message = empty($resData['message']) ? '' : $resData['message'];
  854. throw new ApiException(1004,['msg'=> $message]);
  855. }
  856. }
  857. /**
  858. * 获取用户已绑定(开通)应用
  859. * @param string apiToken
  860. * */
  861. public function getBindAppList($userId)
  862. {
  863. $bindData = [];
  864. $bindData['app_code'] = $this->appCode;
  865. $bindData['user_id'] =$userId;
  866. $bindData['operation_key'] = $this->operationKey;
  867. $res = $this->request('post', $this->url . 'app/user/get-bind-app-list', $bindData);
  868. $resData = json_decode($res, true);
  869. if ($this->isSuccess($resData)) {
  870. return $resData['data'];
  871. } else {
  872. $message = empty($resData['message']) ? '' : $resData['message'];
  873. throw new ApiException(1004,['msg'=> $message]);
  874. }
  875. }
  876. /**
  877. * 获取应用的版本列表
  878. * */
  879. public function getAppVerList()
  880. {
  881. $bindData = [];
  882. $bindData['app_code'] = $this->appCode;
  883. $bindData['operation_key'] = $this->operationKey;
  884. $res = $this->request('post', $this->url . 'app/user/get-app-ver-list', $bindData);
  885. $resData = json_decode($res, true);
  886. if ($this->isSuccess($resData)) {
  887. return $resData['data'];
  888. } else {
  889. $message = empty($resData['message']) ? '' : $resData['message'];
  890. throw new ApiException(1004,['msg'=> $message]);
  891. }
  892. }
  893. }