MemberService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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\ActionLogModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\Common\MemberSettingService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use App\Services\UsdtWalletService;
  20. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  21. use Illuminate\Support\Facades\DB;
  22. use phpQrcode\QRcode;
  23. /**
  24. * 会员-服务类
  25. * Class MemberService
  26. * @package App\Services\Api
  27. */
  28. class MemberService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @since 2020/11/10
  35. * MemberService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new MemberModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 获取资料详情
  54. * @param $where
  55. * @param array $field
  56. */
  57. public function getInfo($where, array $field = [])
  58. {
  59. $field = $field ? $field : ['id', 'username', 'realname', 'nickname', 'openid', 'idcard','google_secret', 'password', 'trade_password', 'trc_address', 'erc_address','erc_hexaddress', 'source', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'safe_level', 'user_type', 'member_level', 'usdt_num', 'user_type', 'status', 'credit', 'avatar'];
  60. if (is_array($where)) {
  61. $info = $this->model->where($where)->select($field)->first();
  62. } else {
  63. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  64. }
  65. $info = $info ? $info->toArray() : [];
  66. if ($info) {
  67. $tradePrice = ConfigService::make()->getConfigByCode('usdt_sell_price');
  68. $tradePrice = $tradePrice? $tradePrice : 0;
  69. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  70. $info['idcard_front_img'] = $info['idcard_front_img'] ? get_image_url($info['idcard_front_img']) : '';
  71. $info['idcard_back_img'] = $info['idcard_back_img'] ? get_image_url($info['idcard_back_img']) : '';
  72. $info['usdt_num'] = isset($info['usdt_num']) ? moneyFormat($info['usdt_num'], 4) : '0.0000';
  73. $info['cny_num'] = $info['usdt_num'] && $tradePrice>0? moneyFormat($info['usdt_num']*$tradePrice, 6) : '0.0000';
  74. $info['is_collection'] = MemberPaymentService::make()->checkHasByUser($info['id']);
  75. $info['set_trade_password'] = $info['trade_password']? 1 : 0;
  76. $info['set_trade_password'] = $info['trade_password']==$info['password']? 2 : $info['set_trade_password'];
  77. unset($info['password']);
  78. unset($info['trade_password']);
  79. // 收款二维码
  80. $qrcode = $this->makeQrcode($info['trc_address']);
  81. $info['trc_qrcode'] = $qrcode? get_image_url($qrcode) : '';
  82. $ercQrcode = $this->makeQrcode($info['erc_hexaddress']);
  83. $info['erc_qrcode'] = $ercQrcode? get_image_url($ercQrcode) : '';
  84. }
  85. return $info;
  86. }
  87. /**
  88. * 用户注册
  89. * @param $params
  90. * @return bool
  91. */
  92. public function register($params)
  93. {
  94. // 检测账号是否存在
  95. if ($this->checkExists('username', $params['username'])) {
  96. $this->error = '2005';
  97. return false;
  98. }
  99. $username = isset($params['username']) ? trim($params['username']) : '';
  100. $password = isset($params['password']) ? trim($params['password']) : '123456';
  101. $avatar = isset($params['avatar']) ? trim($params['avatar']) : '';
  102. $data = [
  103. 'username' => $username,
  104. 'password' => get_password($password . md5($password.'otc')),
  105. 'trade_password' => get_password($password . md5($password.'otc')),
  106. 'mobile' => isPhone($username) ? $username : '',
  107. 'avatar' => $avatar ? $avatar : '',
  108. 'status' => 1,
  109. 'mark' => 1,
  110. 'create_time' => time(),
  111. ];
  112. // 谷歌验证码
  113. $google = GoogleAuthenticator::CreateSecret();
  114. $data['google_secret'] = isset($google['secret'])? $google['secret'] : '';
  115. // 生成trc2.0钱包地址
  116. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  117. if ($trcAddress) {
  118. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  119. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  120. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  121. } else {
  122. $this->error = 2201;
  123. return false;
  124. }
  125. // 生erc2.0钱包地址
  126. $ercAddress = UsdtWalletService::make()->getErcAddress();
  127. if ($trcAddress) {
  128. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  129. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  130. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  131. } else {
  132. $this->error = 2202;
  133. return false;
  134. }
  135. if ($id = $this->model->edit($data)) {
  136. MemberSettingService::make()->getInfo($id);
  137. return true;
  138. }
  139. $this->error = 2007;
  140. return false;
  141. }
  142. /**
  143. * 用户登录
  144. * @param $params
  145. * @return array|false
  146. */
  147. public function login($params)
  148. {
  149. $username = isset($params['username']) ? $params['username'] : '';
  150. $password = isset($params['password']) ? $params['password'] : '';
  151. if (empty($username) || empty($password)) {
  152. $this->error = 1013;
  153. return false;
  154. }
  155. // 用户验证
  156. $info = $this->model->getOne([['username', '=', $username]]);
  157. if (!$info) {
  158. $this->error = 2001;
  159. return false;
  160. }
  161. // 密码校验
  162. $password = get_password($password . md5($password.'otc'));
  163. if ($password != $info['password']) {
  164. $this->error = 2002;
  165. return false;
  166. }
  167. // 使用状态校验
  168. if ($info['status'] != 1) {
  169. $this->error = 2009;
  170. return false;
  171. }
  172. // 承兑商
  173. if ($info['user_type'] == 2) {
  174. $this->error = 1024;
  175. return false;
  176. }
  177. // 设置日志标题
  178. ActionLogModel::setTitle("会员登录APP");
  179. ActionLogModel::record($info);
  180. // JWT生成token
  181. $jwt = new Jwt('jwt_app');
  182. $token = $jwt->getToken($info['id']);
  183. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  184. // 登录
  185. $updateData = ['login_time'=>time(),'login_ip'=>get_client_ip()];
  186. if(empty($info['google_secret'])){
  187. $google = GoogleAuthenticator::CreateSecret();
  188. $updateData['google_secret'] = isset($google['secret'])? $google['secret'] : '';
  189. }
  190. $this->model->where(['id'=> $info['id']])->update($updateData);
  191. // 登录数据
  192. return [
  193. 'token' => $token,
  194. 'user_id' => $info['id'],
  195. 'user_type' => $info['user_type'],
  196. ];
  197. }
  198. /**
  199. * 身份认证
  200. * @param $userId
  201. * @param $params
  202. * @return false
  203. */
  204. public function auth($userId, $params)
  205. {
  206. $data = [
  207. 'idcard' => isset($params['idcard']) ? $params['idcard'] : '',
  208. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  209. ];
  210. if (isset($params['idcard_front_img']) && strpos($params['idcard_front_img'], 'http') === false) {
  211. $data['idcard_front_img'] = $params['idcard_front_img'];
  212. }
  213. if (isset($params['idcard_back_img']) && strpos($params['idcard_back_img'], 'http') === false) {
  214. $data['idcard_back_img'] = $params['idcard_back_img'];
  215. }
  216. if (isset($params['idcard_hand_img']) && strpos($params['idcard_hand_img'], 'http') === false) {
  217. $data['idcard_hand_img'] = $params['idcard_hand_img'];
  218. }
  219. $info = $this->getInfo($userId);
  220. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  221. if ($idcardCheck == 1) {
  222. $this->error = '2011';
  223. return false;
  224. }
  225. return $this->model->where(['id' => $userId])->update($data);
  226. }
  227. /**
  228. * 修改账号
  229. * @param $userId
  230. * @param $params
  231. * @return bool
  232. */
  233. public function modify($userId, $params)
  234. {
  235. $username = isset($params['username']) ? $params['username'] : '';
  236. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  237. $password = isset($params['password']) ? $params['password'] : '';
  238. if (empty($username) || empty($password)) {
  239. $this->error = 1013;
  240. return false;
  241. }
  242. // 用户验证
  243. $info = $this->model->getOne([['username', '=', $username]]);
  244. if (!$info || $info['id'] != $userId) {
  245. $this->error = 2001;
  246. return false;
  247. }
  248. // 使用状态校验
  249. if ($info['status'] != 1) {
  250. $this->error = 2009;
  251. return false;
  252. }
  253. // 密码校验
  254. $password = get_password($password . md5($password.'otc'));
  255. if ($password != $info['password']) {
  256. $this->error = 2002;
  257. return false;
  258. }
  259. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  260. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  261. $this->error = 2005;
  262. return false;
  263. }
  264. if(!$this->model->where(['id'=> $info['id']])->update(['username'=> $newUsername,'update_time'=> time()])){
  265. $this->error = 2021;
  266. return false;
  267. }
  268. $this->error = 2020;
  269. return true;
  270. }
  271. /**
  272. * 修改更新登录密码
  273. * @param $userId
  274. * @param $params
  275. * @return bool
  276. */
  277. public function updatePassword($userId, $params)
  278. {
  279. $username = isset($params['username']) ? $params['username'] : '';
  280. $password = isset($params['password']) ? $params['password'] : '';
  281. if (empty($username) || empty($password)) {
  282. $this->error = 1013;
  283. return false;
  284. }
  285. // 用户验证
  286. $info = $this->model->getOne([['username', '=', $username]]);
  287. if (!$info || $info['id'] != $userId) {
  288. $this->error = 2001;
  289. return false;
  290. }
  291. // 使用状态校验
  292. if ($info['status'] != 1) {
  293. $this->error = 2009;
  294. return false;
  295. }
  296. // 更新登录密码
  297. $password = get_password($password . md5($password.'otc'));
  298. if(!$this->model->where(['id'=> $info['id']])->update(['password'=> $password,'update_time'=> time()])){
  299. $this->error = 2025;
  300. return false;
  301. }
  302. $this->error = 2024;
  303. return true;
  304. }
  305. /**
  306. * 修改更新交易密码
  307. * @param $userId
  308. * @param $params
  309. * @return bool
  310. */
  311. public function updateTradePassword($userId, $params)
  312. {
  313. $username = isset($params['username']) ? $params['username'] : '';
  314. $tradePassword = isset($params['trade_password']) ? $params['trade_password'] : '';
  315. if (empty($username) || empty($tradePassword)) {
  316. $this->error = 1013;
  317. return false;
  318. }
  319. // 用户验证
  320. $info = $this->model->getOne([['username', '=', $username]]);
  321. if (!$info || $info['id'] != $userId) {
  322. $this->error = 2001;
  323. return false;
  324. }
  325. // 使用状态校验
  326. if ($info['status'] != 1) {
  327. $this->error = 2009;
  328. return false;
  329. }
  330. // 交易密码
  331. $password = get_password($tradePassword . md5($tradePassword.'otc'));
  332. if(!$this->model->where(['id'=> $info['id']])->update(['safe_level'=>2,'trade_password'=> $password,'update_time'=> time()])){
  333. $this->error = 2023;
  334. return false;
  335. }
  336. $this->error = 2022;
  337. return true;
  338. }
  339. /**
  340. * 获取钱包地址密钥参数
  341. * @param $address
  342. * @param string $type 链类型:trc-trc2.0,erc
  343. * @return mixed
  344. */
  345. public function getWallet($address, $type = 'trc')
  346. {
  347. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  348. }
  349. /**
  350. * 获取待处理用户数据
  351. * @param int $page
  352. * @param int $pageSize
  353. * @return array|mixed
  354. */
  355. public function getCatchMember($page = 1, $pageSize = 500)
  356. {
  357. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  358. $datas = RedisService::get($cacheKey);
  359. if ($datas) {
  360. return $datas;
  361. }
  362. $datas = $this->model->where(['mark' => 1])
  363. ->select(['id', 'trc_address', 'usdt_num'])
  364. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  365. $datas = $datas ? $datas->toArray() : [];
  366. $datas = isset($datas['data']) ? $datas['data'] : [];
  367. if ($datas) {
  368. RedisService::set($cacheKey, $datas, rand(120, 600));
  369. }
  370. return $datas;
  371. }
  372. /**
  373. * 匹配承兑商
  374. * @param $num 交易数量
  375. * @param int $tradeType 交易类型:1-购买,2-出售
  376. */
  377. public function getTradeMember($num, $tradeType = 1, $userId=0)
  378. {
  379. $data = $this->model->from('member as m')
  380. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  381. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  382. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  383. ->where('m.usdt_num', '>=', $num)
  384. ->where(function($query)use($userId){
  385. if($userId){
  386. $query->whereNotIn('m.id', [$userId]);
  387. }
  388. })
  389. ->where(function ($query) use ($tradeType) {
  390. $time = time();
  391. // 买单
  392. if ($tradeType == 1) {
  393. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  394. } // 卖单
  395. else {
  396. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  397. }
  398. })
  399. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  400. ->orderBy("credit",'desc')
  401. ->orderBy(DB::raw("rand()"))
  402. ->first();
  403. $data = $data? $data->toArray() : [];
  404. return $data;
  405. }
  406. /**
  407. * 匹配承兑商
  408. * @param $num 交易数量
  409. * @param int $tradeType 交易类型:1-购买,2-出售
  410. */
  411. public function getTradeMemberOptions($num, $tradeType = 1, $userId=0, $keyword='', $paseSize=50)
  412. {
  413. $cacheKey = "caches:tradeMenber:{$tradeType}_".md5($num.$userId.$keyword.$paseSize);
  414. if($datas = RedisService::get($cacheKey)){
  415. return $datas;
  416. }
  417. $datas = $this->model->from('member as m')
  418. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  419. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  420. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  421. ->where('m.usdt_num', '>=', $num)
  422. ->where(function($query) use($userId, $keyword){
  423. if($userId){
  424. $query->whereNotIn('m.id', [$userId]);
  425. }
  426. if($keyword){
  427. $query->where('m.username', 'like',"%{$keyword}%");
  428. }
  429. })
  430. ->where(function ($query) use ($tradeType) {
  431. $time = time();
  432. // 买单
  433. if ($tradeType == 1) {
  434. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  435. } // 卖单
  436. else {
  437. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  438. }
  439. })
  440. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  441. ->orderBy("credit",'desc')
  442. ->orderBy("m.safe_level",'desc')
  443. ->paginate($paseSize);
  444. $datas = $datas ? $datas->toArray() : [];
  445. $datas = isset($datas['data']) ? $datas['data'] : [];
  446. if ($datas) {
  447. RedisService::set($cacheKey, $datas, rand(10, 30));
  448. }
  449. return $datas;
  450. }
  451. /**
  452. * 生成普通参数二维码
  453. * @param $str 参数
  454. * @param bool $refresh 是否重新生成
  455. * @return bool
  456. */
  457. public function makeQrcode($str, $refresh = false, $size = 4, $margin=2, $level=2)
  458. {
  459. $qrFile = '/images/qrcode/';
  460. if (!is_dir('/uploads'.$qrFile)) {
  461. @mkdir('./uploads' . $qrFile, 0755, true);
  462. }
  463. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size.$margin.$level)) . '.png';
  464. $cacheKey = "caches:qrcodes:member_".md5($str);
  465. if(RedisService::get($cacheKey) && is_file('/uploads'.$qrFile) && !$refresh){
  466. //return $qrFile;
  467. }
  468. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  469. if(!file_exists('./uploads'.$qrFile)){
  470. return false;
  471. }
  472. RedisService::set($cacheKey, ['str'=> $str, 'qrcode'=> $qrFile,'date'=> date('Y-m-d H:i:s')], 7*24 * 3600);
  473. return $qrFile;
  474. }
  475. /**
  476. * 获取钱包归集用户列表
  477. * @param float $minUsdt
  478. * @param int $page
  479. * @param int $pageSize
  480. * @return array|mixed
  481. */
  482. public function getTriggerAddressList($minUsdt=0.2, $page=1, $pageSize = 200)
  483. {
  484. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  485. $datas = RedisService::get($cacheKey);
  486. if ($datas) {
  487. return $datas;
  488. }
  489. $datas = $this->model->where(['mark'=>1])
  490. ->select(['id','trc_usdt','trc_address','trc_wif','trc_hexaddress','erc_address','erc_wif','erc_hexaddress'])
  491. ->orderBy('trc_usdt')
  492. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  493. $datas = $datas ? $datas->toArray() : [];
  494. $datas = isset($datas['data']) ? $datas['data'] : [];
  495. if ($datas) {
  496. RedisService::set($cacheKey, $datas, rand(10, 30));
  497. }
  498. return $datas;
  499. }
  500. }