MemberService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. $info = $this->getInfo($userId);
  217. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  218. if ($idcardCheck == 1) {
  219. $this->error = '2011';
  220. return false;
  221. }
  222. return $this->model->where(['id' => $userId])->update($data);
  223. }
  224. /**
  225. * 修改账号
  226. * @param $userId
  227. * @param $params
  228. * @return bool
  229. */
  230. public function modify($userId, $params)
  231. {
  232. $username = isset($params['username']) ? $params['username'] : '';
  233. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  234. $password = isset($params['password']) ? $params['password'] : '';
  235. if (empty($username) || empty($password)) {
  236. $this->error = 1013;
  237. return false;
  238. }
  239. // 用户验证
  240. $info = $this->model->getOne([['username', '=', $username]]);
  241. if (!$info || $info['id'] != $userId) {
  242. $this->error = 2001;
  243. return false;
  244. }
  245. // 使用状态校验
  246. if ($info['status'] != 1) {
  247. $this->error = 2009;
  248. return false;
  249. }
  250. // 密码校验
  251. $password = get_password($password . md5($password.'otc'));
  252. if ($password != $info['password']) {
  253. $this->error = 2002;
  254. return false;
  255. }
  256. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  257. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  258. $this->error = 2005;
  259. return false;
  260. }
  261. if(!$this->model->where(['id'=> $info['id']])->update(['username'=> $newUsername,'update_time'=> time()])){
  262. $this->error = 2021;
  263. return false;
  264. }
  265. $this->error = 2020;
  266. return true;
  267. }
  268. /**
  269. * 修改更新登录密码
  270. * @param $userId
  271. * @param $params
  272. * @return bool
  273. */
  274. public function updatePassword($userId, $params)
  275. {
  276. $username = isset($params['username']) ? $params['username'] : '';
  277. $password = isset($params['password']) ? $params['password'] : '';
  278. if (empty($username) || empty($password)) {
  279. $this->error = 1013;
  280. return false;
  281. }
  282. // 用户验证
  283. $info = $this->model->getOne([['username', '=', $username]]);
  284. if (!$info || $info['id'] != $userId) {
  285. $this->error = 2001;
  286. return false;
  287. }
  288. // 使用状态校验
  289. if ($info['status'] != 1) {
  290. $this->error = 2009;
  291. return false;
  292. }
  293. // 更新登录密码
  294. $password = get_password($password . md5($password.'otc'));
  295. if(!$this->model->where(['id'=> $info['id']])->update(['password'=> $password,'update_time'=> time()])){
  296. $this->error = 2025;
  297. return false;
  298. }
  299. $this->error = 2024;
  300. return true;
  301. }
  302. /**
  303. * 修改更新交易密码
  304. * @param $userId
  305. * @param $params
  306. * @return bool
  307. */
  308. public function updateTradePassword($userId, $params)
  309. {
  310. $username = isset($params['username']) ? $params['username'] : '';
  311. $tradePassword = isset($params['trade_password']) ? $params['trade_password'] : '';
  312. if (empty($username) || empty($tradePassword)) {
  313. $this->error = 1013;
  314. return false;
  315. }
  316. // 用户验证
  317. $info = $this->model->getOne([['username', '=', $username]]);
  318. if (!$info || $info['id'] != $userId) {
  319. $this->error = 2001;
  320. return false;
  321. }
  322. // 使用状态校验
  323. if ($info['status'] != 1) {
  324. $this->error = 2009;
  325. return false;
  326. }
  327. // 交易密码
  328. $password = get_password($tradePassword . md5($tradePassword.'otc'));
  329. if(!$this->model->where(['id'=> $info['id']])->update(['safe_level'=>2,'trade_password'=> $password,'update_time'=> time()])){
  330. $this->error = 2023;
  331. return false;
  332. }
  333. $this->error = 2022;
  334. return true;
  335. }
  336. /**
  337. * 获取钱包地址密钥参数
  338. * @param $address
  339. * @param string $type 链类型:trc-trc2.0,erc
  340. * @return mixed
  341. */
  342. public function getWallet($address, $type = 'trc')
  343. {
  344. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  345. }
  346. /**
  347. * 获取待处理用户数据
  348. * @param int $page
  349. * @param int $pageSize
  350. * @return array|mixed
  351. */
  352. public function getCatchMember($page = 1, $pageSize = 500)
  353. {
  354. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  355. $datas = RedisService::get($cacheKey);
  356. if ($datas) {
  357. return $datas;
  358. }
  359. $datas = $this->model->where(['mark' => 1])
  360. ->select(['id', 'trc_address', 'usdt_num'])
  361. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  362. $datas = $datas ? $datas->toArray() : [];
  363. $datas = isset($datas['data']) ? $datas['data'] : [];
  364. if ($datas) {
  365. RedisService::set($cacheKey, $datas, rand(120, 600));
  366. }
  367. return $datas;
  368. }
  369. /**
  370. * 匹配承兑商
  371. * @param $num 交易数量
  372. * @param int $tradeType 交易类型:1-购买,2-出售
  373. */
  374. public function getTradeMember($num, $tradeType = 1, $userId=0)
  375. {
  376. $data = $this->model->from('member as m')
  377. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  378. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  379. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  380. ->where('m.usdt_num', '>=', $num)
  381. ->where(function($query)use($userId){
  382. if($userId){
  383. $query->whereNotIn('m.id', [$userId]);
  384. }
  385. })
  386. ->where(function ($query) use ($tradeType) {
  387. $time = time();
  388. // 买单
  389. if ($tradeType == 1) {
  390. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  391. } // 卖单
  392. else {
  393. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  394. }
  395. })
  396. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  397. ->orderBy("credit",'desc')
  398. ->orderBy(DB::raw("rand()"))
  399. ->first();
  400. $data = $data? $data->toArray() : [];
  401. return $data;
  402. }
  403. /**
  404. * 匹配承兑商
  405. * @param $num 交易数量
  406. * @param int $tradeType 交易类型:1-购买,2-出售
  407. */
  408. public function getTradeMemberOptions($num, $tradeType = 1, $userId=0, $keyword='', $paseSize=50)
  409. {
  410. $cacheKey = "caches:tradeMenber:{$tradeType}_".md5($num.$userId.$keyword.$paseSize);
  411. if($datas = RedisService::get($cacheKey)){
  412. return $datas;
  413. }
  414. $datas = $this->model->from('member as m')
  415. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  416. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  417. ->where(['m.status' => 1,'m.user_type'=>2,'u.status'=>1, 'm.mark' => 1])
  418. ->where('m.usdt_num', '>=', $num)
  419. ->where(function($query) use($userId, $keyword){
  420. if($userId){
  421. $query->whereNotIn('m.id', [$userId]);
  422. }
  423. if($keyword){
  424. $query->where('m.username', 'like',"%{$keyword}%");
  425. }
  426. })
  427. ->where(function ($query) use ($tradeType) {
  428. $time = time();
  429. // 买单
  430. if ($tradeType == 1) {
  431. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  432. } // 卖单
  433. else {
  434. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  435. }
  436. })
  437. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  438. ->orderBy("credit",'desc')
  439. ->orderBy("m.safe_level",'desc')
  440. ->paginate($paseSize);
  441. $datas = $datas ? $datas->toArray() : [];
  442. $datas = isset($datas['data']) ? $datas['data'] : [];
  443. if ($datas) {
  444. RedisService::set($cacheKey, $datas, rand(10, 30));
  445. }
  446. return $datas;
  447. }
  448. /**
  449. * 生成普通参数二维码
  450. * @param $str 参数
  451. * @param bool $refresh 是否重新生成
  452. * @return bool
  453. */
  454. public function makeQrcode($str, $refresh = false, $size = 4, $margin=2, $level=2)
  455. {
  456. $qrFile = '/images/qrcode/';
  457. if (!is_dir('/uploads'.$qrFile)) {
  458. @mkdir('./uploads' . $qrFile, 0755, true);
  459. }
  460. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size.$margin.$level)) . '.png';
  461. $cacheKey = "caches:qrcodes:member_".md5($str);
  462. if(RedisService::get($cacheKey) && is_file('/uploads'.$qrFile) && !$refresh){
  463. //return $qrFile;
  464. }
  465. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  466. if(!file_exists('./uploads'.$qrFile)){
  467. return false;
  468. }
  469. RedisService::set($cacheKey, ['str'=> $str, 'qrcode'=> $qrFile,'date'=> date('Y-m-d H:i:s')], 7*24 * 3600);
  470. return $qrFile;
  471. }
  472. /**
  473. * 获取钱包归集用户列表
  474. * @param float $minUsdt
  475. * @param int $page
  476. * @param int $pageSize
  477. * @return array|mixed
  478. */
  479. public function getTriggerAddressList($minUsdt=0.2, $page=1, $pageSize = 200)
  480. {
  481. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  482. $datas = RedisService::get($cacheKey);
  483. if ($datas) {
  484. return $datas;
  485. }
  486. $datas = $this->model->where(['mark'=>1])
  487. ->select(['id','trc_usdt','trc_address','trc_wif','trc_hexaddress','erc_address','erc_wif','erc_hexaddress'])
  488. ->orderBy('trc_usdt')
  489. ->paginate($pageSize > 0 ? $pageSize : 9999999,[],'page', $page);
  490. $datas = $datas ? $datas->toArray() : [];
  491. $datas = isset($datas['data']) ? $datas['data'] : [];
  492. if ($datas) {
  493. RedisService::set($cacheKey, $datas, rand(10, 30));
  494. }
  495. return $datas;
  496. }
  497. }