MemberService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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\Http\Validator\MemberValidator;
  14. use App\Models\ActionLogModel;
  15. use App\Models\MemberModel;
  16. use App\Services\BaseService;
  17. use App\Services\Common\ApiService;
  18. use App\Services\Common\MemberSettingService;
  19. use App\Services\ConfigService;
  20. use App\Services\EmailService;
  21. use App\Services\RedisService;
  22. use App\Services\SmsService;
  23. use App\Services\UsdtWalletService;
  24. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  25. use Illuminate\Support\Facades\DB;
  26. use phpQrcode\QRcode;
  27. /**
  28. * 会员-服务类
  29. * Class MemberService
  30. * @package App\Services\Api
  31. */
  32. class MemberService extends BaseService
  33. {
  34. // 静态对象
  35. protected static $instance = null;
  36. /**
  37. * 构造函数
  38. * @since 2020/11/10
  39. * MemberService constructor.
  40. */
  41. public function __construct()
  42. {
  43. $this->model = new MemberModel();
  44. }
  45. /**
  46. * 静态入口
  47. * @return static|null
  48. */
  49. public static function make()
  50. {
  51. if (!self::$instance) {
  52. self::$instance = (new static());
  53. }
  54. return self::$instance;
  55. }
  56. /**
  57. * 获取资料详情
  58. * @param $where
  59. * @param array $field
  60. */
  61. public function getInfo($where, array $field = [])
  62. {
  63. $field = $field ? $field : ['id', 'api_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', 'idcard_hand_img', 'bank_front_img', 'bank_back_img', 'safe_level', 'user_type', 'member_level', 'usdt_num', 'user_type', 'status', 'credit', 'avatar'];
  64. if (is_array($where)) {
  65. $info = $this->model->where($where)->select($field)->first();
  66. } else {
  67. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  68. }
  69. $info = $info ? $info->toArray() : [];
  70. if ($info) {
  71. $tradePrice = ConfigService::make()->getConfigByCode('usdt_sell_price');
  72. $tradePrice = $tradePrice ? $tradePrice : 0;
  73. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  74. $info['idcard_front_img'] = $info['idcard_front_img'] ? get_image_url($info['idcard_front_img']) : '';
  75. $info['idcard_back_img'] = $info['idcard_back_img'] ? get_image_url($info['idcard_back_img']) : '';
  76. $info['idcard_hand_img'] = $info['idcard_hand_img'] ? get_image_url($info['idcard_hand_img']) : '';
  77. $info['bank_front_img'] = $info['bank_front_img'] ? get_image_url($info['bank_front_img']) : '';
  78. $info['bank_back_img'] = $info['bank_back_img'] ? get_image_url($info['bank_back_img']) : '';
  79. $info['usdt_num'] = isset($info['usdt_num']) ? moneyFormat($info['usdt_num'], 4) : '0.0000';
  80. $info['cny_num'] = $info['usdt_num'] && $tradePrice > 0 ? moneyFormat($info['usdt_num'] * $tradePrice, 4) : '0.0000';
  81. $info['is_collection'] = MemberPaymentService::make()->checkHasByUser($info['id']);
  82. $info['set_trade_password'] = $info['trade_password'] ? 1 : 0;
  83. $info['set_trade_password'] = $info['trade_password'] == $info['password'] ? 2 : $info['set_trade_password'];
  84. unset($info['password']);
  85. unset($info['trade_password']);
  86. // 收款二维码
  87. $qrcode = $this->makeQrcode($info['trc_address']);
  88. $info['trc_qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  89. $ercQrcode = $this->makeQrcode($info['erc_hexaddress']);
  90. $info['erc_qrcode'] = $ercQrcode ? get_image_url($ercQrcode) : '';
  91. }
  92. return $info;
  93. }
  94. /**
  95. * 用户注册
  96. * @param $params
  97. * @return bool
  98. */
  99. public function register($params)
  100. {
  101. // 检测账号是否存在
  102. if ($this->checkExists('username', $params['username'])) {
  103. $this->error = '2005';
  104. return false;
  105. }
  106. $username = isset($params['username']) ? trim($params['username']) : '';
  107. $password = isset($params['password']) ? trim($params['password']) : '';
  108. $userType = isset($params['user_type']) ? intval($params['user_type']) : 1;
  109. if (!checkPassword($password)) {
  110. $this->error = '2030';
  111. return false;
  112. }
  113. $data = [
  114. 'user_type' => $userType ? $userType : 1,
  115. 'username' => $username,
  116. 'password' => get_password($password . md5($password . 'otc')),
  117. 'mobile' => isPhone($username) ? $username : '',
  118. 'status' => 1,
  119. 'mark' => 1,
  120. 'create_time' => time(),
  121. ];
  122. // 谷歌验证码
  123. $google = GoogleAuthenticator::CreateSecret();
  124. $data['google_secret'] = isset($google['secret']) ? $google['secret'] : '';
  125. // 生成trc2.0钱包地址
  126. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  127. if ($trcAddress) {
  128. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  129. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  130. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  131. } else {
  132. $this->error = 2201;
  133. return false;
  134. }
  135. // 生erc2.0钱包地址
  136. $ercAddress = UsdtWalletService::make()->getErcAddress();
  137. if ($trcAddress) {
  138. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  139. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  140. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  141. } else {
  142. $this->error = 2202;
  143. return false;
  144. }
  145. if ($id = $this->model->edit($data)) {
  146. MemberSettingService::make()->getInfo($id);
  147. return true;
  148. }
  149. $this->error = 2007;
  150. return false;
  151. }
  152. /**
  153. * 用户登录
  154. * @param $params
  155. * @return array|false
  156. */
  157. public function login($params)
  158. {
  159. $username = isset($params['username']) ? $params['username'] : '';
  160. $password = isset($params['password']) ? $params['password'] : '';
  161. if (empty($username) || empty($password)) {
  162. $this->error = 1013;
  163. return false;
  164. }
  165. // 用户验证
  166. $info = $this->model->getOne([['username', '=', $username]]);
  167. if (!$info) {
  168. $this->error = 2001;
  169. return false;
  170. }
  171. // 密码校验
  172. $password = get_password($password . md5($password . 'otc'));
  173. if ($password != $info['password']) {
  174. $this->error = 2002;
  175. return false;
  176. }
  177. // 使用状态校验
  178. if ($info['status'] != 1) {
  179. $this->error = 2009;
  180. return false;
  181. }
  182. // 承兑商
  183. if ($info['user_type'] == 2) {
  184. $this->error = 1024;
  185. return false;
  186. }
  187. // 设置日志标题
  188. ActionLogModel::setTitle("会员登录APP");
  189. ActionLogModel::record($info);
  190. // JWT生成token
  191. $jwt = new Jwt('jwt_app');
  192. $token = $jwt->getToken($info['id']);
  193. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  194. // 登录
  195. $updateData = ['login_time' => time(),'is_online'=>1,'login_count'=>$info['login_count']+1, 'login_ip' => get_client_ip()];
  196. if (empty($info['google_secret'])) {
  197. $google = GoogleAuthenticator::CreateSecret();
  198. $updateData['google_secret'] = isset($google['secret']) ? $google['secret'] : '';
  199. }
  200. $this->model->where(['id' => $info['id']])->update($updateData);
  201. // 登录数据
  202. return [
  203. 'token' => $token,
  204. 'user_id' => $info['id'],
  205. 'user_type' => $info['user_type'],
  206. ];
  207. }
  208. /**
  209. * API用户注册
  210. * @param $apiId
  211. * @param $params
  212. * @return bool
  213. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  214. * @throws \Tron\Exceptions\TronErrorException
  215. */
  216. public function apiRegister($apiId, $params)
  217. {
  218. // 检测账号是否存在
  219. if ($this->checkExists('username', $params['username'])) {
  220. $this->error = '2005';
  221. return false;
  222. }
  223. $username = isset($params['username']) ? trim($params['username']) : '';
  224. $password = isset($params['password']) ? trim($params['password']) : '';
  225. $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
  226. $apiInfo = ApiService::make()->getInfo($apiId);
  227. $limits = isset($apiInfo['user_limits']) ? $apiInfo['user_limits'] : [];
  228. if ($limits && !in_array($userType, $limits)) {
  229. $this->error = '6004';
  230. return false;
  231. }
  232. $data = [
  233. 'username' => $username,
  234. 'api_id' => $apiId,
  235. 'user_type' => $userType ? $userType : 3,
  236. 'password' => get_password($password . md5($password . 'otc')),
  237. 'trade_password' => get_password($password . md5($password . 'otc')),
  238. 'mobile' => isPhone($username) ? $username : '',
  239. 'idcard' => isset($params['idcard'])? $params['idcard'] : '',
  240. 'realname' => isset($params['realname'])? $params['realname'] : '',
  241. 'status' => 1,
  242. 'mark' => 1,
  243. 'create_time' => time(),
  244. ];
  245. // 生成trc2.0钱包地址
  246. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  247. if ($trcAddress) {
  248. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  249. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  250. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  251. } else {
  252. $this->error = 2201;
  253. return false;
  254. }
  255. // 生erc2.0钱包地址
  256. $ercAddress = UsdtWalletService::make()->getErcAddress();
  257. if ($trcAddress) {
  258. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  259. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  260. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  261. } else {
  262. $this->error = 2202;
  263. return false;
  264. }
  265. if ($id = $this->model->edit($data)) {
  266. MemberSettingService::make()->getInfo($id);
  267. return true;
  268. }
  269. $this->error = 2007;
  270. return false;
  271. }
  272. /**
  273. * API用户登录
  274. * @param $params
  275. * @return array|false
  276. */
  277. public function apiLogin($apiId, $params)
  278. {
  279. $username = isset($params['username']) ? $params['username'] : '';
  280. $password = isset($params['password']) ? $params['password'] : '';
  281. $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
  282. $apiKey = isset($params['api_key']) ? $params['api_key'] : '';
  283. if (empty($username) || empty($password) || empty($apiKey) || empty($userType)) {
  284. $this->error = 1013;
  285. return false;
  286. }
  287. // 用户验证
  288. $info = $this->model->getOne([['username', '=', $username]]);
  289. if (!$info) {
  290. // 直接注册
  291. if (!$this->apiRegister($apiId, $params)) {
  292. return false;
  293. }
  294. $info = $this->model->getOne([['username', '=', $username]]);
  295. } else {
  296. // 密码校验
  297. $password = get_password($password . md5($password . 'otc'));
  298. if ($password != $info['password']) {
  299. $this->error = 2002;
  300. return false;
  301. }
  302. // 使用状态校验
  303. if ($info['status'] != 1) {
  304. $this->error = 2009;
  305. return false;
  306. }
  307. if ($info['api_id'] != $apiId) {
  308. $this->error = 6010;
  309. return false;
  310. }
  311. // 用户类型
  312. if (!in_array($info['user_type'], [3, 4, 5])) {
  313. $this->error = 1024;
  314. return false;
  315. }
  316. }
  317. // 设置日志标题
  318. ActionLogModel::setTitle("接口平台会员授权登录");
  319. ActionLogModel::record($info);
  320. // JWT生成token
  321. $token = 'TB'.strtoupper(md5($info['id'] . $username).rand(100,999));
  322. RedisService::set("apis:auths:info:{$info['id']}", $info, rand(5, 10));
  323. RedisService::set("apis:tokens:{$token}", ['id' => $info['id'], 'info' => $info, 'token' => $token], 48 * 3600);
  324. // 登录
  325. $updateData = ['login_time' => time(), 'login_ip' => get_client_ip()];
  326. $this->model->where(['id' => $info['id']])->update($updateData);
  327. // 登录数据
  328. if ($info['idcard_check'] != 1) {
  329. $appUrl = ConfigService::make()->getConfigByCode('app_download_url');
  330. return [
  331. 'app_url' => $appUrl,
  332. ];
  333. } else {
  334. return [
  335. 'token' => $token,
  336. 'user_id' => $info['id'],
  337. ];
  338. }
  339. }
  340. /**
  341. * 身份认证
  342. * @param $userId
  343. * @param $params
  344. * @return false
  345. */
  346. public function auth($userId, $params)
  347. {
  348. $data = [
  349. 'idcard' => isset($params['idcard']) ? $params['idcard'] : '',
  350. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  351. ];
  352. if (isset($params['idcard_front_img']) && strpos($params['idcard_front_img'], 'http') === false) {
  353. $data['idcard_front_img'] = $params['idcard_front_img'];
  354. }
  355. if (isset($params['idcard_back_img']) && strpos($params['idcard_back_img'], 'http') === false) {
  356. $data['idcard_back_img'] = $params['idcard_back_img'];
  357. }
  358. if (isset($params['idcard_hand_img']) && strpos($params['idcard_hand_img'], 'http') === false) {
  359. $data['idcard_hand_img'] = $params['idcard_hand_img'];
  360. }
  361. if (isset($params['bank_front_img']) && strpos($params['bank_front_img'], 'http') === false) {
  362. $data['bank_front_img'] = $params['bank_front_img'];
  363. }
  364. if (isset($params['bank_back_img']) && strpos($params['bank_back_img'], 'http') === false) {
  365. $data['bank_back_img'] = $params['bank_back_img'];
  366. }
  367. $info = $this->getInfo($userId);
  368. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  369. if ($idcardCheck == 1) {
  370. $this->error = '2011';
  371. return false;
  372. }
  373. return $this->model->where(['id' => $userId])->update($data);
  374. }
  375. /**
  376. * 修改账号
  377. * @param $userId
  378. * @param $params
  379. * @return bool
  380. */
  381. public function modify($userId, $params)
  382. {
  383. $username = isset($params['username']) ? $params['username'] : '';
  384. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  385. $password = isset($params['password']) ? $params['password'] : '';
  386. if (empty($username) || empty($password)) {
  387. $this->error = 1013;
  388. return false;
  389. }
  390. // 用户验证
  391. $info = $this->model->getOne([['username', '=', $username]]);
  392. if (!$info || $info['id'] != $userId) {
  393. $this->error = 2001;
  394. return false;
  395. }
  396. // 使用状态校验
  397. if ($info['status'] != 1) {
  398. $this->error = 2009;
  399. return false;
  400. }
  401. // 密码校验
  402. $password = get_password($password . md5($password . 'otc'));
  403. if ($password != $info['password']) {
  404. $this->error = 2002;
  405. return false;
  406. }
  407. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  408. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  409. $this->error = 2005;
  410. return false;
  411. }
  412. if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) {
  413. $this->error = 2021;
  414. return false;
  415. }
  416. $this->error = 2020;
  417. return true;
  418. }
  419. /**
  420. * 修改更新登录密码
  421. * @param $userId
  422. * @param $params
  423. * @return bool
  424. */
  425. public function updatePassword($userId, $params)
  426. {
  427. $username = isset($params['username']) ? $params['username'] : '';
  428. $password = isset($params['password']) ? $params['password'] : '';
  429. if (empty($username) || empty($password)) {
  430. $this->error = 1013;
  431. return false;
  432. }
  433. // 用户验证
  434. $info = $this->model->getOne([['username', '=', $username]]);
  435. if (!$info || $info['id'] != $userId) {
  436. $this->error = 2001;
  437. return false;
  438. }
  439. // 使用状态校验
  440. if ($info['status'] != 1) {
  441. $this->error = 2009;
  442. return false;
  443. }
  444. // 更新登录密码
  445. $password = get_password($password . md5($password . 'otc'));
  446. if (!$this->model->where(['id' => $info['id']])->update(['password' => $password, 'update_time' => time()])) {
  447. $this->error = 2025;
  448. return false;
  449. }
  450. $this->error = 2024;
  451. return true;
  452. }
  453. /**
  454. * 修改更新交易密码
  455. * @param $userId
  456. * @param $params
  457. * @return bool
  458. */
  459. public function updateTradePassword($userId, $params)
  460. {
  461. $username = isset($params['username']) ? $params['username'] : '';
  462. $tradePassword = isset($params['trade_password']) ? $params['trade_password'] : '';
  463. if (empty($username) || empty($tradePassword)) {
  464. $this->error = 1013;
  465. return false;
  466. }
  467. // 用户验证
  468. $info = $this->model->getOne([['username', '=', $username]]);
  469. if (!$info || $info['id'] != $userId) {
  470. $this->error = 2001;
  471. return false;
  472. }
  473. // 使用状态校验
  474. if ($info['status'] != 1) {
  475. $this->error = 2009;
  476. return false;
  477. }
  478. // 交易密码
  479. $password = get_password($tradePassword . md5($tradePassword . 'otc'));
  480. if (!$this->model->where(['id' => $info['id']])->update(['safe_level' => 2, 'trade_password' => $password, 'update_time' => time()])) {
  481. $this->error = 2023;
  482. return false;
  483. }
  484. $this->error = 2022;
  485. return true;
  486. }
  487. /**
  488. * 获取钱包地址密钥参数
  489. * @param $address
  490. * @param string $type 链类型:trc-trc2.0,erc
  491. * @return mixed
  492. */
  493. public function getWallet($address, $type = 'trc')
  494. {
  495. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  496. }
  497. /**
  498. * 获取待处理用户数据
  499. * @param int $page
  500. * @param int $pageSize
  501. * @return array|mixed
  502. */
  503. public function getCatchMember($page = 1, $pageSize = 500)
  504. {
  505. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  506. $datas = RedisService::get($cacheKey);
  507. if ($datas) {
  508. return $datas;
  509. }
  510. $datas = $this->model->where(['mark' => 1])
  511. ->select(['id', 'trc_address', 'usdt_num'])
  512. ->paginate($pageSize > 0 ? $pageSize : 9999999, [], 'page', $page);
  513. $datas = $datas ? $datas->toArray() : [];
  514. $datas = isset($datas['data']) ? $datas['data'] : [];
  515. if ($datas) {
  516. RedisService::set($cacheKey, $datas, rand(120, 600));
  517. }
  518. return $datas;
  519. }
  520. /**
  521. * 匹配承兑商
  522. * @param $num 交易数量
  523. * @param int $tradeType 交易类型:1-购买,2-出售
  524. */
  525. public function getTradeMember($num, $tradeType = 1, $userId = 0)
  526. {
  527. $data = $this->model->from('member as m')
  528. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  529. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  530. ->where(['m.status' => 1,'m.idcard_check'=>1, 'm.user_type' => 2, 'u.status' => 1, 'm.mark' => 1])
  531. ->where('m.usdt_num', '>=', $num)
  532. ->where(function ($query) use ($userId) {
  533. if ($userId) {
  534. $query->whereNotIn('m.id', [$userId]);
  535. }
  536. })
  537. ->where(function ($query) use ($tradeType) {
  538. $time = time();
  539. // 买单
  540. if ($tradeType == 1) {
  541. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  542. } // 卖单
  543. else {
  544. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  545. }
  546. })
  547. ->select(['m.id', 'm.usdt_num', 'm.username', 'm.credit'])
  548. ->orderBy("credit", 'desc')
  549. ->orderBy(DB::raw("rand()"))
  550. ->first();
  551. $data = $data ? $data->toArray() : [];
  552. return $data;
  553. }
  554. /**
  555. * 匹配承兑商
  556. * @param $num 交易数量
  557. * @param int $tradeType 交易类型:1-购买,2-出售
  558. */
  559. public function getTradeMemberOptions($num, $tradeType = 1, $userId = 0, $keyword = '', $paseSize = 50)
  560. {
  561. $cacheKey = "caches:tradeMenber:{$tradeType}_" . md5($num . $userId . $keyword . $paseSize);
  562. if ($datas = RedisService::get($cacheKey)) {
  563. return $datas;
  564. }
  565. $datas = $this->model->from('member as m')
  566. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  567. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  568. ->where(['m.status' => 1, 'm.user_type' => 2, 'u.status' => 1, 'm.mark' => 1])
  569. ->where('m.usdt_num', '>=', $num)
  570. ->where(function ($query) use ($userId, $keyword) {
  571. if ($userId) {
  572. $query->whereNotIn('m.id', [$userId]);
  573. }
  574. if ($keyword) {
  575. $query->where('m.username', 'like', "%{$keyword}%");
  576. }
  577. })
  578. ->where(function ($query) use ($tradeType) {
  579. $time = time();
  580. // 买单
  581. if ($tradeType == 1) {
  582. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  583. } // 卖单
  584. else {
  585. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  586. }
  587. })
  588. ->select(['m.id', 'm.usdt_num', 'm.username', 'm.credit'])
  589. ->orderBy("credit", 'desc')
  590. ->orderBy("m.safe_level", 'desc')
  591. ->paginate($paseSize);
  592. $datas = $datas ? $datas->toArray() : [];
  593. $datas = isset($datas['data']) ? $datas['data'] : [];
  594. if ($datas) {
  595. RedisService::set($cacheKey, $datas, rand(10, 30));
  596. }
  597. return $datas;
  598. }
  599. /**
  600. * 生成普通参数二维码
  601. * @param $str 参数
  602. * @param bool $refresh 是否重新生成
  603. * @return bool
  604. */
  605. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  606. {
  607. $qrFile = '/images/qrcode/';
  608. if (!is_dir('/uploads' . $qrFile)) {
  609. @mkdir('./uploads' . $qrFile, 0755, true);
  610. }
  611. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size . $margin . $level)) . '.png';
  612. $cacheKey = "caches:qrcodes:member_" . md5($str);
  613. if (RedisService::get($cacheKey) && is_file('/uploads' . $qrFile) && !$refresh) {
  614. //return $qrFile;
  615. }
  616. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  617. if (!file_exists('./uploads' . $qrFile)) {
  618. return false;
  619. }
  620. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  621. return $qrFile;
  622. }
  623. /**
  624. * 获取钱包归集用户列表
  625. * @param float $minUsdt
  626. * @param int $page
  627. * @param int $pageSize
  628. * @return array|mixed
  629. */
  630. public function getTriggerAddressList($minUsdt = 0.2, $page = 1, $pageSize = 200)
  631. {
  632. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  633. $datas = RedisService::get($cacheKey);
  634. if ($datas) {
  635. return $datas;
  636. }
  637. $datas = $this->model->where(['mark' => 1])
  638. ->select(['id', 'trc_usdt', 'trc_address', 'trc_wif', 'trc_hexaddress', 'erc_address', 'erc_wif', 'erc_hexaddress'])
  639. ->orderBy('trc_usdt')
  640. ->paginate($pageSize > 0 ? $pageSize : 9999999, [], 'page', $page);
  641. $datas = $datas ? $datas->toArray() : [];
  642. $datas = isset($datas['data']) ? $datas['data'] : [];
  643. if ($datas) {
  644. RedisService::set($cacheKey, $datas, rand(10, 30));
  645. }
  646. return $datas;
  647. }
  648. /**
  649. * 检测验证码
  650. * @param $username 账号
  651. * @param $code 验证码
  652. * @param $scene 验证场景:reg-注册,forget-找回密码
  653. * @return bool
  654. */
  655. public function checkCode($username, $code, $scene)
  656. {
  657. $validator = new MemberValidator();
  658. if(!is_array($validator->check(['mobile'=> $username],'mobile'))){
  659. if(!EmailService::make()->check($username, $code, $scene)){
  660. $this->error = EmailService::make()->getError();
  661. return false;
  662. }
  663. return true;
  664. }else{
  665. if(!SmsService::make()->check($username, $code, $scene)){
  666. $this->error = SmsService::make()->getError();
  667. return false;
  668. }
  669. return true;
  670. }
  671. }
  672. }