MemberService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Helpers\Jwt;
  13. use App\Models\DossierModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\Common\CityService;
  17. use App\Services\ConfigService;
  18. use App\Services\EmailService;
  19. use App\Services\RedisService;
  20. use App\Services\SmsService;
  21. use App\Services\ToolService;
  22. use Illuminate\Support\Facades\DB;
  23. use phpqrcode\QRcode;
  24. /**
  25. * 会员管理-服务类
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * Class MemberService
  29. * @package App\Services\Api
  30. */
  31. class MemberService extends BaseService
  32. {
  33. public static $instance = null;
  34. /**
  35. * 构造函数
  36. * @since 2020/11/11
  37. * MemberService constructor.
  38. */
  39. public function __construct()
  40. {
  41. $this->model = new MemberModel();
  42. }
  43. /**
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = new static();
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 用户登录
  55. * @param $params
  56. * @return array|false
  57. */
  58. public function login($params)
  59. {
  60. // 授权
  61. $account = isset($params['account']) ? $params['account'] : '';
  62. $smsCode = isset($params['sms_code']) ? $params['sms_code'] : ''; // 短信验证码或邮箱验证码
  63. $type = isset($params['type']) ? $params['type'] : 1;
  64. $password = isset($params['password']) ? $params['password'] : '';
  65. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  66. $incode = isset($params['incode']) ? $params['incode'] : '';
  67. if (empty($params) || empty($account)) {
  68. $this->error = 2014;
  69. return false;
  70. }
  71. $userInfo = $this->model->where(['mark' => 1])
  72. ->where(function ($query) use ($account, $type) {
  73. if ($type == 1 || $type == 2) {
  74. $query->where(['username' => $account])->orWhere(['mobile' => $account])->orWhere(['email' => $account]);
  75. } else if ($type == 3) {
  76. $query->orWhere(['email' => $account]);
  77. }
  78. })
  79. ->select(['id', 'openid', 'username', 'email', 'mobile', 'password', 'parent_id', 'vip', 'code', 'status', 'mark'])
  80. ->first();
  81. $userInfo = $userInfo ? $userInfo->toArray() : [];
  82. $userId = isset($userInfo['id']) ? $userInfo['id'] : 0;
  83. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  84. if ($userInfo && $status != 1) {
  85. $this->error = '账号已被冻结,请联系客服';
  86. return false;
  87. }
  88. // 账号密码
  89. if ($type == 1) {
  90. // 未注册
  91. if (empty($userInfo)) {
  92. $this->error = '账号未注册,请选择其他方式登录注册';
  93. return false;
  94. }
  95. // 已注册验证密码
  96. $userPassword = isset($userInfo['password']) ? $userInfo['password'] : '';
  97. if ($userInfo && $userPassword != get_password($password)) {
  98. $this->error = '登录密码错误';
  99. return false;
  100. }
  101. } else if ($type == 2) {
  102. // 验证短信验证码
  103. if (!SmsService::make()->check($account, $smsCode, 'login')) {
  104. $this->error = SmsService::make()->getError();
  105. return false;
  106. }
  107. } else if ($type == 3) {
  108. // 已注册验证邮箱验证码
  109. if (!EmailService::make()->check($account, $smsCode, 'login')) {
  110. $this->error = EmailService::make()->getError();
  111. return false;
  112. }
  113. }
  114. // 未注册
  115. $ip = get_client_ip();
  116. $system = isset($params['system']) ? $params['system'] : [];
  117. if (empty($userInfo)) {
  118. // 推荐人
  119. $parentId = 0;
  120. $parents = '';
  121. if ($incode) {
  122. $inviteInfo = $this->model->where(['code' => $incode, 'mark' => 1])
  123. ->select(['id', 'parents', 'parent_id'])
  124. ->first();
  125. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  126. $parents = isset($inviteInfo['parents']) ? $parentId . ',' . $inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  127. if(empty($parentId)){
  128. $this->error = '邀请码错误';
  129. return false;
  130. }
  131. }
  132. // 账号注册
  133. DB::beginTransaction();
  134. $userId = $this->model->max('id') + 1;
  135. $userInfo = [
  136. 'username' => $type == 1 ? $account : '',
  137. 'mobile' => $type == 2 ? $account : '',
  138. 'email' => $type == 3 ? $account : '',
  139. 'nickname' => get_nickname('T'),
  140. 'password' => $type == 1 ? get_password($password) : '',
  141. 'member_level' => 0,
  142. 'parent_id' => $parentId,
  143. 'parents' => $parents,
  144. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  145. 'device' => $appSources == 'android' ? 2 : 1,
  146. 'source' => 1,
  147. 'status' => 1,
  148. 'login_ip' => $ip,
  149. 'code' => strtoupper(get_random_code(9, 'X', "{$userId}")),
  150. 'create_time' => time(),
  151. 'login_time' => time(),
  152. 'mark' => 1
  153. ];
  154. if (!$this->model->insert($userInfo)) {
  155. DB::rollBack();
  156. $this->error = EmailService::make()->getError();
  157. return false;
  158. }
  159. // 资料
  160. $ipData = ToolService::make()->getIpAddress($ip, '');
  161. $province = isset($ipData['regionName']) ? $ipData['regionName'] : '';
  162. $city = isset($ipData['city']) ? $ipData['city'] : '';
  163. $provinceId = CityService::make()->getFieldByName($province);
  164. $cityId = CityService::make()->getFieldByName($city);
  165. $birthday = date('Y-m-d');
  166. $starData = getStar($birthday);
  167. $dossier = [
  168. 'user_id'=> $userId,
  169. 'nickname'=> '自己',
  170. 'type'=>1,
  171. 'birthday'=> $birthday,
  172. 'province_id' => $provinceId,
  173. 'city_id' => $cityId,
  174. 'city' => $city,
  175. 'star'=> isset($starData['code'])? $starData['code'] : '',
  176. 'star_text'=> isset($starData['name'])? $starData['name'] : '',
  177. 'status'=>1,
  178. 'is_default'=>1,
  179. ];
  180. if (!DossierModel::insert($dossier)) {
  181. DB::rollBack();
  182. $this->error = EmailService::make()->getError();
  183. return false;
  184. }
  185. // TODO 邀请注册奖励
  186. DB::commit();
  187. unset($userInfo['password']);
  188. unset($userInfo['parents']);
  189. unset($userInfo['parents']);
  190. } else {
  191. $this->model->where(['id' => $userId])->update([
  192. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  193. 'login_ip' => $ip,
  194. 'update_time' => time(),
  195. 'login_time' => time(),
  196. ]);
  197. }
  198. // 获取登录授权token
  199. $jwt = new Jwt('jwt_star_app');
  200. $token = $jwt->getToken($userId);
  201. // 结果返回
  202. $result = [
  203. 'access_token' => $token,
  204. 'info' => ['uid' => $userId, 'parent_id' => $userInfo['parent_id'], 'account' => $account, 'code' => $userInfo['code']],
  205. ];
  206. $this->error = 2004;
  207. RedisService::set("auths:info:{$userId}", $userInfo, 2 * 24 * 3600);
  208. return $result;
  209. }
  210. /**
  211. * 邀请注册
  212. * @param $params
  213. * @return array|false
  214. */
  215. public function register($params)
  216. {
  217. // 授权
  218. $account = isset($params['account']) ? $params['account'] : '';
  219. $smsCode = isset($params['sms_code']) ? $params['sms_code'] : ''; // 短信验证码或邮箱验证码
  220. $type = isset($params['type']) ? $params['type'] : 1;
  221. $password = isset($params['password']) ? $params['password'] : '';
  222. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  223. $incode = isset($params['incode']) ? $params['incode'] : '';
  224. if (empty($params) || empty($account) || empty($incode)) {
  225. $this->error = 2014;
  226. return false;
  227. }
  228. $userInfo = $this->model->where(['mark' => 1])
  229. ->where(function ($query) use ($account, $type) {
  230. if ($type == 1 || $type == 2) {
  231. $query->where(['username' => $account])->orWhere(['mobile' => $account])->orWhere(['email' => $account]);
  232. } else if ($type == 3) {
  233. $query->orWhere(['email' => $account]);
  234. }
  235. })
  236. ->select(['id', 'openid', 'username', 'email', 'mobile', 'password', 'parent_id', 'vip', 'code', 'status', 'mark'])
  237. ->first();
  238. $userInfo = $userInfo ? $userInfo->toArray() : [];
  239. if ($userInfo) {
  240. $this->error = '账号已注册,请直接下载APP或到小程序内登录使用';
  241. return false;
  242. }
  243. // 账号密码
  244. if ($type == 2) {
  245. // 已注册验证短信验证码
  246. if (!SmsService::make()->check($account, $smsCode, 'reg')) {
  247. $this->error = SmsService::make()->getError();
  248. return false;
  249. }
  250. } else if ($type == 3) {
  251. // 已注册验证邮箱验证码
  252. if (!EmailService::make()->check($account, $smsCode, 'reg')) {
  253. $this->error = EmailService::make()->getError();
  254. return false;
  255. }
  256. }
  257. // 推荐人
  258. $parentId = 0;
  259. $parents = '';
  260. if ($incode) {
  261. $inviteInfo = $this->model->where(['code' => $incode, 'mark' => 1])
  262. ->select(['id', 'parents', 'parent_id'])
  263. ->first();
  264. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  265. $parents = isset($inviteInfo['parents']) ? $parentId . ',' . $inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  266. }
  267. if(empty($parentId)){
  268. $this->error = '邀请码错误';
  269. return false;
  270. }
  271. // 未注册
  272. $ip = get_client_ip();
  273. $ipData = ToolService::make()->getIpAddress($ip, '');
  274. $province = isset($ipData['regionName']) ? $ipData['regionName'] : '';
  275. $city = isset($ipData['city']) ? $ipData['city'] : '';
  276. $provinceId = CityService::make()->getFieldByName($province);
  277. $cityId = CityService::make()->getFieldByName($city);
  278. $system = isset($params['system']) ? $params['system'] : [];
  279. // 账号注册
  280. DB::beginTransaction();
  281. $userId = $this->model->max('id') + 1;
  282. $userInfo = [
  283. 'username' => $type == 1 ? $account : '',
  284. 'mobile' => $type == 2 ? $account : '',
  285. 'email' => $type == 3 ? $account : '',
  286. 'nickname' => get_nickname('T'),
  287. 'password' => $type == 1 ? get_password($password) : '',
  288. 'member_level' => 0,
  289. 'parent_id' => $parentId,
  290. 'parents' => $parents,
  291. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  292. 'device' => $appSources == 'android' ? 2 : 1,
  293. 'source' => 1,
  294. 'status' => 1,
  295. 'login_ip' => $ip,
  296. 'code' => strtoupper(get_random_code(9, 'X', "{$userId}")),
  297. 'create_time' => time(),
  298. 'login_time' => time(),
  299. 'mark' => 1
  300. ];
  301. if (!$this->model->insert($userInfo)) {
  302. DB::rollBack();
  303. $this->error = EmailService::make()->getError();
  304. return false;
  305. }
  306. // 资料
  307. $birthday = date('Y-m-d');
  308. $starData = getStar($birthday);
  309. $dossier = [
  310. 'user_id'=> $userId,
  311. 'nickname'=> '自己',
  312. 'type'=>1,
  313. 'birthday'=> $birthday,
  314. 'star'=> isset($starData['code'])? $starData['code'] : '',
  315. 'star_text'=> isset($starData['name'])? $starData['name'] : '',
  316. 'province_id' => $provinceId,
  317. 'city_id' => $cityId,
  318. 'city' => $city,
  319. 'status'=>1,
  320. 'is_default'=>1,
  321. ];
  322. if (!DossierModel::insert($dossier)) {
  323. DB::rollBack();
  324. $this->error = EmailService::make()->getError();
  325. return false;
  326. }
  327. // TODO 邀请注册奖励
  328. DB::commit();
  329. unset($userInfo['password']);
  330. unset($userInfo['parents']);
  331. unset($userInfo['parents']);
  332. // 结果返回
  333. $siteInfo = ConfigService::make()->getConfigByGroup(1);
  334. $result = [
  335. 'app_urls'=>[
  336. 'android'=> isset($siteInfo['android_app_url']['value'])? $siteInfo['android_app_url']['value'] : '',
  337. 'ios'=> isset($siteInfo['ios_app_url']['value'])? $siteInfo['ios_app_url']['value'] : '',
  338. ],
  339. 'info' => ['uid' => $userId, 'parent_id' => $userInfo['parent_id'], 'account' => $account, 'code' => $userInfo['code']],
  340. ];
  341. $this->error = 2004;
  342. RedisService::set("auths:info:{$userId}", $userInfo, 2 * 24 * 3600);
  343. return $result;
  344. }
  345. /**
  346. * 用户信息
  347. * @param int $userId 用户ID
  348. * @param false $refresh
  349. * @return array|mixed
  350. */
  351. public function getInfo(int $userId, $refresh = false)
  352. {
  353. $cacheKey = "caches:member:info_{$userId}";
  354. $info = RedisService::get($cacheKey);
  355. if ($info && !$refresh) {
  356. return $info;
  357. }
  358. $info = $this->model->with(['parent','profile'])
  359. ->where(['id' => $userId, 'mark' => 1])
  360. ->select(['id', 'nickname', 'avatar', 'code', 'member_level','stars', 'beans', 'vip', 'profit', 'balance', 'view_ads', 'ads_beans', 'mobile', 'parent_id', 'email', 'username', 'status'])
  361. ->first();
  362. $info = $info ? $info->toArray() : [];
  363. if (empty($info)) {
  364. $this->error = 2016;
  365. return false;
  366. }
  367. $profile = isset($info['profile'])? $info['profile'] : [];
  368. if ($info['avatar']) {
  369. $info['avatar'] = get_image_url($info['avatar']);
  370. } else {
  371. $star = isset($profile['star']) && $profile['star'] ? $profile['star'] : '';
  372. $gender = isset($profile['gender']) && $profile['gender'] ? $profile['gender'] : 0;
  373. $name = intval($info['id'] % 3) + 1;
  374. if ($star && $gender) {
  375. $name = "{$star}/" . ($gender == 1 ? 'f_' : 'm_') . $name;
  376. }
  377. $avatar = "/images/avatar/{$name}.jpeg";
  378. $info['avatar'] = get_image_url($avatar);
  379. }
  380. $info['birthday'] = isset($profile['birthday']) && $profile['birthday']? $profile['birthday'] : date('Y-m-d');
  381. $info['star'] = isset($profile['star']) && $profile['star']? $profile['star'] : '';
  382. $info['star_text'] = isset($profile['star_text']) && $profile['star_text']? $profile['star_text'] : '';
  383. if(empty($info['star'])){
  384. $starData = getStar($info['birthday']);
  385. $info['star'] = isset($starData['code']) ? $starData['code'] : '';
  386. $info['star_text'] = isset($starData['name']) ? $starData['name'] : '';
  387. }
  388. $info['share_url'] = env('WEB_URL').'#/pages/share/index?incode='.$info['code'];
  389. $info['share_qrcode'] = get_image_url($this->makeQrcode($info['share_url']));
  390. $info['orders'] = [
  391. ];
  392. RedisService::set($cacheKey, $info, rand(3, 5));
  393. return $info;
  394. }
  395. /**
  396. * @param $userId
  397. */
  398. public function getProfile($userId)
  399. {
  400. $cacheKey = "caches:profiles:info_{$userId}";
  401. $info = RedisService::get($cacheKey);
  402. if(empty($info)){
  403. $info = DossierModel::with(['dossier_type'])->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  404. ->orderBy('is_default', 'asc')
  405. ->first();
  406. $info = $info? $info->toArray() : [];
  407. if($info){
  408. RedisService::set($cacheKey, $info, rand(300, 600));
  409. }else{
  410. $birthday =date('Y-m-d');
  411. $starData = getStar($birthday);
  412. $info = [
  413. 'nickname'=> '-示例',
  414. 'gender'=> 2,
  415. 'birthday'=> $birthday,
  416. 'star'=> isset($starData['code'])? $starData['code'] : '',
  417. 'star_text'=> isset($starData['name'])? $starData['name'] : '',
  418. 'dossier_type'=> [
  419. 'id'=>1,
  420. 'name'=> '其他'
  421. ]
  422. ];
  423. }
  424. }
  425. }
  426. /**
  427. * 生成普通参数二维码
  428. * @param $str 参数
  429. * @param bool $refresh 是否重新生成
  430. * @return bool
  431. */
  432. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  433. {
  434. $basePath = base_path() . '/public';
  435. $qrFile = '/images/qrcode/';
  436. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  437. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  438. }
  439. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  440. $qrFile = $qrFile . "C_{$key}.png";
  441. $cacheKey = "caches:qrcodes:member_" . $key;
  442. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  443. return $qrFile;
  444. }
  445. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  446. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  447. return false;
  448. }
  449. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  450. return $qrFile;
  451. }
  452. }