MemberService.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\LiveModel;
  16. use App\Models\MemberBankModel;
  17. use App\Models\MemberLevelModel;
  18. use App\Models\MemberModel;
  19. use App\Models\MerchantModel;
  20. use App\Models\VideoModel;
  21. use App\Services\BaseService;
  22. use App\Services\CityService;
  23. use App\Services\ConfigService;
  24. use App\Services\EmailService;
  25. use App\Services\RedisService;
  26. use App\Services\SmsService;
  27. use App\Services\ToolService;
  28. use App\Services\UdunpayService;
  29. use App\Services\WalletService;
  30. use Illuminate\Support\Facades\DB;
  31. use phpQrcode\QRcode;
  32. /**
  33. * 会员管理-服务类
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * Class MemberService
  37. * @package App\Services\Api
  38. */
  39. class MemberService extends BaseService
  40. {
  41. // 静态对象
  42. protected static $instance = null;
  43. /**
  44. * 构造函数
  45. * @author laravel开发员
  46. * @since 2020/11/11
  47. * MemberService constructor.
  48. */
  49. public function __construct()
  50. {
  51. $this->model = new MemberModel();
  52. }
  53. /**
  54. * 静态入口
  55. * @return MemberService|static|null
  56. */
  57. public static function make()
  58. {
  59. if (!self::$instance) {
  60. self::$instance = new static();
  61. }
  62. return self::$instance;
  63. }
  64. /**
  65. * 获取资料详情
  66. * @param $where
  67. * @param array $field
  68. */
  69. public function getInfo($where, $type = 'detail', array $field = [], $userId=0)
  70. {
  71. $defaultField = ['id', 'username','email', 'realname', 'mobile','gender','member_level','box_num', 'nickname', 'code','supper_point', 'parent_id','birthday','city_id','signature','power_num','trade_state','trc_url','recharge_trc_url', 'point_id', 'balance','trx','usdt','wait_score','intro', 'score','pay_password','realname','idcard_check', 'member_level', 'status', 'avatar'];
  72. $withs = ['merchant','acceptor','parent','point','level'];
  73. if ($type == 'team') {
  74. $defaultField = ['id', 'username','email','member_level', 'nickname', 'code', 'balance', 'score','point_id','power_num','parent_id', 'status', 'avatar'];
  75. }else if($type == 'share'){
  76. $withs = ['parent','level'];
  77. $defaultField = ['id', 'username', 'nickname','member_level', 'code', 'balance','usdt','trx','trc_url', 'score', 'status', 'avatar'];
  78. }else if($type == 'check'){
  79. $withs = ['parent','level'];
  80. $defaultField = ['id', 'username', 'nickname','member_level', 'code', 'balance','usdt','trx','trc_url','wait_score', 'score', 'status', 'avatar'];
  81. }else if ($type == 'info'){
  82. $withs = ['parent','level'];
  83. }
  84. $field = $field ? $field : $defaultField;
  85. if (is_array($where)) {
  86. $info = $this->model->with($withs)->where('mark',1)->where($where)->select($field)->first();
  87. } else {
  88. $info = $this->model->with($withs)->where('mark',1)->where(['id' => (int)$where])->select($field)->first();
  89. }
  90. $info = $info ? $info->toArray() : [];
  91. if ($info && !in_array($type, ['auth', 'check','login'])) {
  92. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  93. $info['balance'] = moneyFormat($info['balance'], 2);
  94. $info['usdt'] = moneyFormat($info['usdt'], 2);
  95. $info['score'] = moneyFormat($info['score'], 2);
  96. $info['merchant'] = isset($info['merchant']) && $info['merchant']? $info['merchant'] : ['id'=>0];
  97. $info['acceptor'] = isset($info['acceptor']) && $info['acceptor']? $info['acceptor'] : ['id'=>0];
  98. $info['level'] = isset($info['level']) && $info['level']? $info['level'] : ['id'=>0,'name'=>'普通用户'];
  99. $info['custom_phone'] = ConfigService::make()->getConfigByCode('custom_phone');
  100. $info['custom_telegram'] = ConfigService::make()->getConfigByCode('custom_telegram');
  101. $info['custom_email'] = ConfigService::make()->getConfigByCode('custom_email');
  102. if(isset($info['birthday'])){
  103. $info['birthday_text'] = $info['birthday']? date('Y-m-d',$info['birthday']) : '';
  104. }
  105. // 二维码
  106. if (in_array($type, ['detail'])) {
  107. $info['parent_code'] = '';
  108. $info['parent'] = $info['parent']? $info['parent'] : ['id'=>0,'nickname' => '无', 'code' => '无'];
  109. if($info['parent']['id']>0){
  110. $info['parent_code'] = isset($info['parent']['code'])? $info['parent']['code'] :'';
  111. }
  112. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  113. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  114. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  115. if($userId != $info['id']){
  116. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  117. }else{
  118. $info['is_follow'] = 1;
  119. }
  120. }
  121. else if ($type == 'info') {
  122. $liveOpenLevel = ConfigService::make()->getConfigByCode('live_open_level',0);
  123. $liveOpenLevel = $liveOpenLevel>0? $liveOpenLevel : 0;
  124. $info['live_open'] = $info['member_level']>=$liveOpenLevel? 1 : 0;
  125. }else if($type == 'upgrade'){
  126. $nextLevel = MemberLevelModel::where('level','>', $info['member_level'])->orderBy('id','asc')->first();
  127. $info['next_level'] = isset($nextLevel['id']) && $nextLevel['id']? $nextLevel['id'] : $info['member_level'];
  128. $info['next_level_profit'] = isset($nextLevel['upgrade_usdt_profit']) && $nextLevel['upgrade_usdt_profit']? $nextLevel['upgrade_usdt_profit'] : (isset($info['level']['upgrade_usdt_profit'])? $info['level']['upgrade_usdt_profit']:0);
  129. $info['next_level_usdt'] = isset($nextLevel['upgrade_usdt']) && $nextLevel['upgrade_usdt']? $nextLevel['upgrade_usdt'] : (isset($info['level']['upgrade_usdt'])? $info['level']['upgrade_usdt']:0);
  130. $info['next_level_name'] = isset($nextLevel['name'])? $nextLevel['name'] : (isset($info['level']['name'])? $info['level']['name']:'');
  131. }
  132. else if ($type == 'recharge') {
  133. $wallet = WalletService::make()->getWallet(1);
  134. $info['recharge_trc_url'] = isset($wallet['address'])? $wallet['address'] : '';
  135. $info['xd_price'] = ConfigService::make()->getConfigByCode('xd_price',100);
  136. if($info['recharge_trc_url']){
  137. $qrcode = $this->makeQrcode($info['recharge_trc_url']);
  138. $info['trc_qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  139. }else{
  140. $info['trc_qrcode'] = '';
  141. }
  142. }
  143. else if ($type == 'team' || $type == 'share') {
  144. $inviteUrl = get_web_url('/#/pages/register/index?code=' . $info['code']);
  145. $qrcode = $this->makeQrcode($inviteUrl);
  146. $info['qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  147. $info['invite_url'] = $inviteUrl;
  148. $info['qrcode_base64'] = $qrcode?getImageContent($qrcode):'';
  149. $info['team_count1'] = $this->getUserCountByType($info['id'], 1, 0); // 直推人数
  150. $info['team_count2'] = $this->getUserCountByType($info['id'], 3, 0); // 滑落人数
  151. $info['team_count'] = $this->getUserCountByType($info['id'], 2, 0); // 团队人数
  152. }else {
  153. // 默认地址
  154. $info['addressData'] = MemberAddressService::make()->getBindInfo($info['id']);
  155. }
  156. }
  157. return $info;
  158. }
  159. /**
  160. * 获取主页信息
  161. * @param $where
  162. * @param array $field
  163. */
  164. public function getHomeInfo($id, $userId=0)
  165. {
  166. $field = ['id', 'username','email', 'realname', 'mobile','gender', 'nickname', 'code','supper_point', 'parent_id','province_id','city_id','signature','power_num','trade_state', 'point_id', 'balance','trx','usdt','wait_score', 'score','realname', 'member_level', 'status', 'avatar'];
  167. $info = $this->model->where('mark',1)->where(['id' => $id])->select($field)->first();
  168. $info = $info ? $info->toArray() : [];
  169. if ($info) {
  170. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  171. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  172. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  173. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  174. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  175. $info['is_fans'] = (int)MemberCollectService::make()->checkCollect($info['id'], $userId,1); // 是否我的粉丝
  176. $info['live_id'] = (int)LiveModel::where(['user_id'=> $info['id'],'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id'); // 是否正在直播
  177. }
  178. return $info;
  179. }
  180. /**
  181. * 获取用户缓存信息
  182. * @param $where
  183. * @param array $field
  184. * @param int $expired
  185. * @return array|mixed
  186. */
  187. public function getCacheInfo($where, $field = [], $expired = 0)
  188. {
  189. $cacheKey = "caches:member:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  190. $info = RedisService::get($cacheKey);
  191. if ($info) {
  192. return $info;
  193. }
  194. $defaultField = ['id', 'username', 'realname', 'mobile', 'nickname', 'code', 'parent_id', 'balance','usdt', 'score', 'member_level', 'status', 'avatar'];
  195. $field = $field ? $field : $defaultField;
  196. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  197. $info = $info ? $info->toArray() : [];
  198. if ($info) {
  199. if (isset($info['avatar'])) {
  200. $info['avatar'] = $info['avatar']? $info['avatar'] : '/images/member/logo.png';
  201. $info['avatar_preview'] = $info['avatar']? get_image_url($info['avatar']) : '';
  202. }
  203. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  204. }
  205. return $info;
  206. }
  207. /**
  208. * 生成普通参数二维码
  209. * @param $str 参数
  210. * @param bool $refresh 是否重新生成
  211. * @return bool
  212. */
  213. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  214. {
  215. $basePath = base_path() . '/public';
  216. $qrFile = '/images/qrcode/';
  217. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  218. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  219. }
  220. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  221. $qrFile = $qrFile . "C_{$key}.png";
  222. $cacheKey = "caches:qrcodes:member_" . $key;
  223. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  224. return $qrFile;
  225. }
  226. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  227. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  228. return false;
  229. }
  230. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  231. return $qrFile;
  232. }
  233. /**
  234. * 获取团队用户数量
  235. * @param $userId 当前用户ID
  236. * @param int $type 层级:1-直推,2-团队
  237. * @param int $isPoint 节点:1-滑落推荐,0-直接推荐
  238. * @return array|mixed
  239. */
  240. public function getUserCountByType($userId, $type = 1, $isPoint=0)
  241. {
  242. $cacheKey = "caches:teams:count_{$userId}_{$type}_{$isPoint}";
  243. $data = RedisService::get($cacheKey);
  244. if ($data) {
  245. return $data;
  246. }
  247. $where = ['status' => 1, 'mark' => 1];
  248. $data = $this->model->where($where)
  249. ->where(function ($query) use ($type, $isPoint, $userId) {
  250. if($isPoint){
  251. if ($type == 1) { // 滑落推荐
  252. $query->where('point_id', $userId);
  253. } else if($type == 2){ // 滑落团队人数
  254. $query->whereRaw('FIND_IN_SET(?,points)', $userId);
  255. }
  256. }else{
  257. if ($type == 1) { // 直推人数
  258. $query->where('parent_id', $userId);
  259. } else if($type == 2){ // 团队人数
  260. $query->whereRaw("FIND_IN_SET({$userId},parents)");
  261. } else if($type == 3){ // 滑落人数
  262. $query->where('parent_id', $userId)->where('point_id','!=', $userId);
  263. }
  264. }
  265. })->count('id');
  266. if ($data) {
  267. RedisService::set($cacheKey, $data, rand(3, 5));
  268. }
  269. return $data;
  270. }
  271. /**
  272. * 分销用户列表
  273. * @param $userId
  274. * @param $params
  275. * @param int $pageSize
  276. * @return array
  277. */
  278. public function getTeamList($userId, $params, $pageSize=15)
  279. {
  280. $page = request()->post('page',1);
  281. $cacheKey = "caches:teams:list_{$userId}_{$page}";
  282. $datas = RedisService::get($cacheKey);
  283. if ($datas) {
  284. return [
  285. 'pageSize' => $pageSize,
  286. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  287. 'list' => isset($datas['data']) ? $datas['data'] : []
  288. ];
  289. }
  290. $where = ['status' => 1, 'mark' => 1];
  291. $field = ['id','avatar', 'nickname', 'username', 'email', 'balance', 'parent_id','power_num','point_id','create_time', 'status'];
  292. $datas = $this->model->with(['level'])->where($where)
  293. ->where(function ($query) use ($userId) {
  294. $query->where('parent_id', $userId);
  295. })
  296. ->where(function($query) use($params){
  297. $kw = isset($params['kw'])? trim($params['kw']) : '';
  298. if($kw){
  299. $query->where('id','=', "%{$kw}%")->orWhere('email','like',"%{$kw}%")->orWhere('username','like',"%{$kw}%");
  300. }
  301. })
  302. ->select($field)
  303. ->orderBy('create_time', 'desc')
  304. ->orderBy('id', 'desc')
  305. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  306. $datas = $datas ? $datas->toArray() : [];
  307. if ($datas) {
  308. foreach ($datas['data'] as &$item) {
  309. $item['avatar'] = $item['avatar']? $item['avatar'] : '/images/member/logo.png';
  310. $item['avatar'] = get_image_url($item['avatar']);
  311. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'y-m-d H:i') : '';
  312. $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
  313. }
  314. unset($item);
  315. RedisService::set($cacheKey, $datas, rand(5, 10));
  316. }
  317. return [
  318. 'pageSize' => $pageSize,
  319. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  320. 'list' => isset($datas['data']) ? $datas['data'] : []
  321. ];
  322. }
  323. /**
  324. * 手机号是否已注册
  325. * @param $mobile
  326. * @return mixed
  327. */
  328. public function checkPhoneExists($mobile)
  329. {
  330. return parent::checkExists('mobile', $mobile); // TODO: Change the autogenerated stub
  331. }
  332. /**
  333. * 账号注册
  334. * @param $params
  335. * @return array|false
  336. */
  337. public function register($params)
  338. {
  339. // 授权
  340. $email = isset($params['email']) ? trim($params['email']) : '';
  341. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  342. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  343. $code = isset($params['code']) ? trim($params['code']) : '';
  344. $scene = isset($params['scene']) ? $params['scene'] : '';
  345. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  346. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  347. if (empty($params) || empty($email) || $code <= 0) {
  348. $this->error = 2014;
  349. return false;
  350. }
  351. // 验证码
  352. if (!EmailService::make()->check($email, $code, $scene)) {
  353. $this->error = EmailService::make()->getError();
  354. return false;
  355. }
  356. // 验证是否注册
  357. if($this->model->where(['email'=> $email,'mark'=>1])->value('id')){
  358. $this->error = 2009;
  359. return false;
  360. }
  361. // 推荐人
  362. $parentId = 0;
  363. $pointId = 0;
  364. $parents = '';
  365. $points = '';
  366. if ($inviteCode) {
  367. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  368. ->select(['id', 'parents', 'parent_id'])
  369. ->first();
  370. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  371. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  372. // 挂靠节点
  373. if($parentId>0){
  374. $pointId = $this->getPointParentId($parentId);
  375. if($pointId){
  376. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  377. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  378. }
  379. }
  380. }
  381. // 注册
  382. $id = $this->model->max('id') + 1;
  383. $system = isset($params['system'])? $params['system'] : [];
  384. $ip = get_client_ip();
  385. $ipData = ToolService::make()->getIpAddress($ip,'');
  386. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  387. $city = isset($ipData['city'])? $ipData['city'] : '';
  388. $emailName = explode('@', $email)[0];
  389. // 钱包
  390. $data = [
  391. 'parent_id' => $parentId,
  392. 'point_id' => $pointId,
  393. 'points' => $points,
  394. 'openid' => '',
  395. 'avatar' => '',
  396. 'nickname' => $nickname ? $nickname : '用户'.substr($emailName,-8,8),
  397. 'username' => $email,
  398. 'password' => get_password('xl123456'),
  399. 'code' => strtoupper(get_random_code(9, 'X', "{$id}")),
  400. 'email' => $email,
  401. 'mobile' => $mobile,
  402. 'recharge_trc_url' => '',
  403. 'parents' => $parents,
  404. 'province_id' => CityService::make()->getFieldByName($province),
  405. 'city_id' => CityService::make()->getFieldByName($city),
  406. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  407. 'device' => $appSources == 'android' ? 2 : 1,
  408. 'status' => 1,
  409. 'balance' => 0,
  410. 'score' => 0,
  411. 'mark' => 1,
  412. 'login_ip' => $ip,
  413. 'create_time' => time(),
  414. ];
  415. $userId = $this->model->insertGetId($data);
  416. $this->error = 2008;
  417. return [
  418. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  419. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  420. ];
  421. }
  422. /**
  423. * 邮箱登录
  424. * @param $params
  425. * @return array|false
  426. */
  427. public function loginByEmail($params)
  428. {
  429. // 授权
  430. $email = isset($params['email']) ? $params['email'] : '';
  431. $code = isset($params['code']) ? $params['code'] : '';
  432. $scene = isset($params['scene']) ? $params['scene'] : '';
  433. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  434. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  435. $this->error = 2014;
  436. return false;
  437. }
  438. // 验证码
  439. if (!EmailService::make()->check($email, $code, $scene)) {
  440. $this->error = EmailService::make()->getError();
  441. return false;
  442. }
  443. // 验证是否注册,没有则注册
  444. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  445. $data = $data ? $data->toArray() : [];
  446. $userId = isset($data['id']) ? $data['id'] : 0;
  447. $status = isset($data['status']) ? $data['status'] : 0;
  448. if(empty($data) || $userId<=0){
  449. $this->error = 2001;
  450. return false;
  451. }
  452. if ($status != 1) {
  453. $this->error = 2015;
  454. return false;
  455. }
  456. // 获取登录授权token
  457. $jwt = new Jwt('jwt_dys_app');
  458. $token = $jwt->getToken($userId);
  459. // 结果返回
  460. $result = [
  461. 'access_token' => $token,
  462. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  463. ];
  464. // 用户信息
  465. $ip = get_client_ip();
  466. $updateData = [
  467. 'login_time' => time(),
  468. 'login_ip' => $ip,
  469. ];
  470. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  471. $ipData = ToolService::make()->getIpAddress($ip,'');
  472. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  473. $city = isset($ipData['city'])? $ipData['city'] : '';
  474. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  475. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  476. }
  477. $this->model->where(['id'=> $userId])->update($updateData);
  478. $this->error = 2004;
  479. $data['token'] = $token;
  480. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  481. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  482. return $result;
  483. }
  484. /**
  485. * 手机号登录
  486. * @param $params
  487. * @return array|false
  488. */
  489. public function loginByMobile($params)
  490. {
  491. // 授权
  492. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  493. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  494. $smsType = isset($params['sms_type']) ? $params['sms_type'] : '';
  495. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  496. $openid = isset($params['openid']) ? $params['openid'] : '';
  497. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  498. $regType = isset($params['type']) ? $params['type'] : 'app';
  499. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  500. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  501. if (empty($params) || empty($mobile) || $code <= 0) {
  502. $this->error = 2014;
  503. return false;
  504. }
  505. // 验证码
  506. if (!SmsService::make()->check($mobile, $code, $smsType?$smsType:'login')) {
  507. $this->error = SmsService::make()->getError();
  508. return false;
  509. }
  510. // 推荐人
  511. $parentId = 0;
  512. $parents = '';
  513. if ($inviteCode) {
  514. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  515. ->select(['id', 'parents', 'parent_id'])
  516. ->first();
  517. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  518. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] . $parentId . ',' : ($parentId ? $parentId . ',' : '');
  519. }
  520. // 验证是否注册,没有则注册
  521. $data = $this->model->with(['merchant','mechanic'])->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'openid', 'username', 'parent_id', 'code', 'push_cid', 'status'])->first();
  522. $data = $data ? $data->toArray() : [];
  523. $userId = isset($data['id']) ? $data['id'] : 0;
  524. $status = isset($data['status']) ? $data['status'] : 0;
  525. if ($userId && $status != 1) {
  526. $this->error = 2015;
  527. return false;
  528. }
  529. // 是否是商家或技师,有订单提醒
  530. $noticeUser = ['id'=> 0,'uid'=>$userId,'type'=>1];
  531. $merchant = isset($data['merchant'])? $data['merchant'] : [];
  532. $merchantStatus = isset($merchant['status'])? $merchant['status'] : 0;
  533. $mechanic = isset($data['mechanic'])? $data['mechanic'] : [];
  534. $mechanicStatus = isset($mechanic['status'])? $mechanic['status'] : 0;
  535. if($merchantStatus==2){
  536. $uid = isset($merchant['id'])? $merchant['id'] : 0;
  537. if($uid>0){
  538. $noticeUser = ['id'=>$uid,'type'=>2,'uid'=> $userId];
  539. }
  540. }else if($mechanicStatus == 2){
  541. $uid = isset($mechanic['id'])? $mechanic['id'] : 0;
  542. if($uid>0){
  543. $noticeUser = ['id'=>$uid,'type'=>3,'uid'=>$userId];
  544. }
  545. }
  546. $pushCid = isset($params['push_cid']) ? $params['push_cid'] : '';
  547. if (!$userId) {
  548. $id = $this->model->max('id') + 1;
  549. $data = [
  550. 'parent_id' => $parentId,
  551. 'openid' => $openid,
  552. 'avatar' => $avatar,
  553. 'nickname' => $nickname ? $nickname : get_random_code(4, '用户u').substr($mobile,-4,4),
  554. 'username' => get_username($id),
  555. 'password' => get_password('123456'),
  556. 'code' => strtoupper(get_random_code(8, 'D', "{$id}")),
  557. 'mobile' => $mobile,
  558. 'parents' => $parents,
  559. 'push_cid' => isset($params['push_cid']) ? $params['push_cid'] : '',
  560. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  561. 'device' => $appSources == 'android' ? 2 : 1,
  562. 'status' => 1,
  563. 'balance' => 0,
  564. 'score' => 0,
  565. 'mark' => 1,
  566. 'login_time' => time(),
  567. 'login_ip' => get_client_ip(),
  568. 'create_time' => time(),
  569. ];
  570. $userId = $this->model->insertGetId($data);
  571. } elseif ($regType != 'h5') {
  572. // 登录更新
  573. $updateData = [
  574. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  575. 'login_count' => DB::raw("login_count+1"),
  576. 'login_time' => time(),
  577. 'login_ip' => get_client_ip(),
  578. 'update_time' => time(),
  579. ];
  580. if ($pushCid) {
  581. $updateData['push_cid'] = $pushCid;
  582. }
  583. $this->model->where(['id' => $userId])->update($updateData);
  584. }
  585. // H5 邀请注册,无需登录
  586. if ($regType == 'h5') {
  587. $this->error = $status ? 2023 : 2022;
  588. return [
  589. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  590. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'username' => $data['username']],
  591. ];
  592. }
  593. // 获取登录授权token
  594. $jwt = new Jwt('jwt_dys_app');
  595. $token = $jwt->getToken($userId);
  596. // 结果返回
  597. $result = [
  598. 'access_token' => $token,
  599. 'info' => ['uid' => $userId,'notice_user'=>$noticeUser, 'openid' => $data['openid'], 'username' => $data['username']],
  600. ];
  601. // 用户信息
  602. $this->error = 2004;
  603. $data['token'] = $token;
  604. unset($data['password']);
  605. // 实现初始化账单明细表
  606. AccountLogModel::where('id','<=',1)->value('id');
  607. RedisService::clear( "caches:map:{$userId}");
  608. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  609. return $result;
  610. }
  611. /**
  612. * 修改头像/商家logo/技师头像
  613. * @param $userId
  614. * @param $avatar
  615. * @return mixed
  616. */
  617. public function saveAvatar($userId, $avatar)
  618. {
  619. $type = request()->post('type', 1);
  620. if($type == 1){
  621. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  622. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  623. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  624. @unlink(ATTACHMENT_PATH . $oldAvatar);
  625. }
  626. return true;
  627. }
  628. }else if($type == 2){
  629. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  630. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $avatar, 'update_time' => time()])) {
  631. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  632. @unlink(ATTACHMENT_PATH . $oldAvatar);
  633. }
  634. return true;
  635. }
  636. }
  637. return false;
  638. }
  639. /**
  640. * 修改账号
  641. * @param $userId
  642. * @param $params
  643. * @return bool
  644. */
  645. public function modify($userId, $params)
  646. {
  647. $email = isset($params['email']) ? $params['email'] : '';
  648. $password = isset($params['password']) ? $params['password'] : '';
  649. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  650. $scene = isset($params['scene'])? trim($params['scene']) : '';
  651. // 用户验证
  652. $info = $this->model->with(['parent'])->where(['id' => $userId, 'mark' => 1])->select(['id','pay_password','parent_id','code', 'status'])->first();
  653. if (!$info) {
  654. $this->error = 2001;
  655. return false;
  656. }
  657. // 使用状态校验
  658. if ($info['status'] != 1) {
  659. $this->error = 2009;
  660. return false;
  661. }
  662. // 密码校验
  663. $data = ['update_time' => time()];
  664. // 验证交易密码
  665. if(isset($params['pay_password']) && $scene != 'trade'){
  666. $userPayPassword = isset($info['pay_password'])? $info['pay_password'] : '';
  667. if(empty($userPayPassword)){
  668. $this->error = 1040;
  669. return false;
  670. }
  671. if($userPayPassword != get_password($payPassword)){
  672. $this->error = 2038;
  673. return false;
  674. }
  675. }
  676. // 邀请人绑定
  677. $parent = isset($info['parent'])? $info['parent'] : [];
  678. $userParentCode = isset($parent['code'])? $parent['code'] : '';
  679. $userCode = isset($info['code'])? $info['code'] : '';
  680. $parentCode = isset($params['parent_code'])? trim($params['parent_code']) : '';
  681. if(isset($params['parent_code']) && $parentCode){
  682. //
  683. if($userParentCode){
  684. $this->error = 2451;
  685. return false;
  686. }
  687. if(($userCode == $parentCode)){
  688. $this->error = 2452;
  689. return false;
  690. }
  691. // 推荐人验证
  692. $inviteInfo = $this->model->where(['code' => $parentCode, 'mark' => 1])
  693. ->select(['id', 'parents', 'parent_id'])
  694. ->first();
  695. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  696. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  697. if(!$parentId){
  698. $this->error = 2453;
  699. return false;
  700. }
  701. // 验证是否绑定用户为自己线下用户
  702. if(preg_match("/{$userId},/", $parents)){
  703. $this->error = 2454;
  704. return false;
  705. }
  706. // 推荐人
  707. $data['parent_id'] = $parentId;
  708. $data['parents'] = $parents;
  709. // 挂靠节点
  710. if($parentId>0){
  711. $pointId = $this->getPointParentId($parentId);
  712. if($pointId){
  713. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  714. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  715. $data['point_id'] = $pointId;
  716. $data['points'] = $points;
  717. }
  718. }
  719. }
  720. // 邮箱验证
  721. if (isset($params['email']) && $email) {
  722. // 修改邮箱
  723. $newEmail = isset($params['new_email'])? trim($params['new_email']) : '';
  724. if(isset($params['new_email']) && $newEmail){
  725. $data['email'] = $newEmail;
  726. $data['username'] = $newEmail;
  727. $checkInfo = $this->model->where(['username' => $newEmail, 'mark' => 1])->select(['id', 'status'])->first();
  728. if ($checkInfo && $checkInfo['id'] != $userId) {
  729. $this->error = 2005;
  730. return false;
  731. }
  732. }
  733. // 邮箱验证码
  734. $code = isset($params['code'])? trim($params['code']) : '';
  735. $checkEmail = $scene == 'modify_email'? $newEmail : $email;
  736. if (!EmailService::make()->check($checkEmail, $code, $scene)) {
  737. $this->error = EmailService::make()->getError();
  738. return false;
  739. }
  740. }
  741. // 修改数据
  742. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  743. if (isset($params['nickname']) && $nickname) {
  744. $data['nickname'] = $nickname;
  745. }
  746. $gender = isset($params['gender']) ? intval($params['gender']) : 3;
  747. if (isset($params['gender']) && $gender) {
  748. $data['gender'] = $gender;
  749. }
  750. $intro = isset($params['intro']) ? $params['intro'] : '';
  751. if (isset($params['intro']) && $intro) {
  752. $data['intro'] = $intro;
  753. }
  754. $birthday = isset($params['birthday_text']) ? $params['birthday_text'] : '';
  755. if (isset($params['birthday_text']) && $birthday) {
  756. $data['birthday'] = strtotime($birthday);
  757. }
  758. if (isset($params['pay_password']) && $payPassword) {
  759. $payPassword = get_password($payPassword);
  760. $data['pay_password'] = $payPassword;
  761. }
  762. if($this->model->where(['id' => $userId])->update($data)){
  763. // 更新绑定上级后,团队用户全部更新
  764. if(isset($data['parent_id']) && $data['parent_id']){
  765. $updateData = ['parents'=>DB::raw("concat(parents,'{$data['parent_id']},')"),'update_time'=>time()];
  766. $this->model->where('parents','like',"%{$userId},")->update($updateData);
  767. }
  768. // 更新团队节点关系
  769. if(isset($data['point_id']) && $data['point_id']){
  770. $updateData = ['points'=>DB::raw("concat(points,'{$data['point_id']},')"),'update_time'=>time()];
  771. $this->model->where('points','like',"%{$userId},")->update($updateData);
  772. }
  773. $this->error = 1008;
  774. return true;
  775. }
  776. $this->error = 1008;
  777. return true;
  778. }
  779. /**
  780. * 身份认证
  781. * @param $userId
  782. * @param $params
  783. * @return bool
  784. */
  785. public function auth($userId, $params)
  786. {
  787. // 用户验证
  788. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  789. if (!$info) {
  790. $this->error = 2001;
  791. return false;
  792. }
  793. // 使用状态校验
  794. if ($info['status'] != 1) {
  795. $this->error = 2009;
  796. return false;
  797. }
  798. // 密码校验
  799. $data = ['update_time' => time(),'idcard_check'=>1];
  800. if (isset($params['realname']) && $params['realname']) {
  801. $data['realname'] = trim($params['realname']);
  802. }
  803. $idcard = isset($params['idcard']) && $params['idcard'];
  804. if ($idcard) {
  805. $data['idcard'] = trim($params['idcard']);
  806. }
  807. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  808. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  809. }
  810. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  811. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  812. }
  813. $this->model->where(['id' => $userId])->update($data);
  814. // 发送消息
  815. $this->error = 2036;
  816. return true;
  817. }
  818. /**
  819. * 转账
  820. * @param $userId
  821. * @param $params
  822. * @return bool
  823. */
  824. public function transfer($userId, $params)
  825. {
  826. $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
  827. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  828. $money = isset($params['money'])? floatval($params['money']) : 0;
  829. if(empty($toUserId) || $money<=0){
  830. $this->error = 2420;
  831. return false;
  832. }
  833. $cacheKey = "caches:members:transfer:{$userId}";
  834. if(RedisService::get($cacheKey)){
  835. $this->error = 1034;
  836. return false;
  837. }
  838. // 验证账户
  839. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  840. ->select(['id','nickname','username','usdt','pay_password','balance','status'])
  841. ->first();
  842. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  843. $usdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  844. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  845. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  846. if(empty($userInfo) || $status != 1){
  847. $this->error = 2024;
  848. return false;
  849. }
  850. // 余额支付支付密码验证
  851. if(empty($userPayPassword)){
  852. $this->error = 1040;
  853. return false;
  854. }
  855. $payPassword = get_password($payPassword);
  856. if($payPassword != $userPayPassword){
  857. $this->error = 2038;
  858. return false;
  859. }
  860. // 验证账户
  861. $accountInfo = $this->model->where(['id'=> $toUserId,'mark'=>1])
  862. ->select(['id','nickname','usdt','username','balance','status'])
  863. ->first();
  864. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  865. $accountUsdt = isset($accountInfo['usdt'])? $accountInfo['usdt'] : 0;
  866. $toNickname = isset($accountInfo['nickname'])? $accountInfo['nickname'] : '';
  867. if(empty($accountInfo) || $status != 1){
  868. $this->error = 2421;
  869. return false;
  870. }
  871. if($money > $usdt){
  872. $this->error = 2422;
  873. return false;
  874. }
  875. // 用户余额转账到用户
  876. DB::beginTransaction();
  877. RedisService::set($cacheKey, $params, rand(2,5));
  878. $updateData = ['usdt'=> DB::raw("usdt - {$money}"),'update_time'=> time()];
  879. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  880. DB::rollBack();
  881. $this->error = 2028;
  882. RedisService::clear($cacheKey);
  883. return false;
  884. }
  885. // 明细
  886. $orderNo = get_order_num('TR');
  887. $log = [
  888. 'user_id' => $userId,
  889. 'source_id' => $toUserId,
  890. 'source_order_no' => $orderNo,
  891. 'type' => 6,
  892. 'coin_type' => 1,
  893. 'user_type'=> 1,
  894. 'money' => -$money,
  895. 'actual_money' => -$money,
  896. 'balance' => $usdt,
  897. 'create_time' => time(),
  898. 'update_time' => time(),
  899. 'remark' => "USDT转账",
  900. 'status' => 1,
  901. 'mark' => 1,
  902. ];
  903. if(!AccountLogModel::insertGetId($log)){
  904. DB::rollBack();
  905. $this->error = 2423;
  906. RedisService::clear($cacheKey);
  907. return false;
  908. }
  909. // 到账
  910. $fee = ConfigService::make()->getConfigByCode('transfer_fee_rate',5);
  911. $fee = $fee>0 && $fee<100? moneyFormat($fee/100, 2) : 0;
  912. $feeUsdt = round($money * $fee,2);
  913. $realUsdt = moneyFormat($money - $feeUsdt,2);
  914. $updateData = ['usdt'=> DB::raw("usdt + {$realUsdt}"),'update_time'=> time()];
  915. if(!$this->model->where(['id'=> $toUserId,'mark'=>1])->update($updateData)){
  916. DB::rollBack();
  917. $this->error = 2424;
  918. RedisService::clear($cacheKey);
  919. return false;
  920. }
  921. // 明细
  922. $log = [
  923. 'user_id' => $toUserId,
  924. 'source_id' => $userId,
  925. 'source_order_no' => $orderNo,
  926. 'type' => 6,
  927. 'coin_type' => 1,
  928. 'user_type'=> 1,
  929. 'money' => $realUsdt,
  930. 'actual_money' => $realUsdt,
  931. 'balance' => $accountUsdt,
  932. 'create_time' => time(),
  933. 'update_time' => time(),
  934. 'remark' => "USDT转账",
  935. 'status' => 1,
  936. 'mark' => 1,
  937. ];
  938. if(!AccountLogModel::insertGetId($log)){
  939. DB::rollBack();
  940. $this->error = 2029;
  941. RedisService::clear($cacheKey);
  942. return false;
  943. }
  944. // 平台明细
  945. $log = [
  946. 'user_id' => 0,
  947. 'source_id' => $userId,
  948. 'source_order_no' => $orderNo,
  949. 'type' => 6,
  950. 'coin_type' => 1,
  951. 'user_type'=> 4,
  952. 'money' => $feeUsdt,
  953. 'actual_money' => $feeUsdt,
  954. 'balance' => 0,
  955. 'create_time' => time(),
  956. 'update_time' => time(),
  957. 'remark' => "USDT转账",
  958. 'status' => 1,
  959. 'mark' => 1,
  960. ];
  961. if(!AccountLogModel::insertGetId($log)){
  962. DB::rollBack();
  963. $this->error = 2029;
  964. RedisService::clear($cacheKey);
  965. return false;
  966. }
  967. // 平台
  968. FinanceService::make()->saveLog(0, $feeUsdt,1);
  969. // 消息
  970. $dateTime = date('Y-m-d H:i:s');
  971. MessageService::make()->pushMessage($userId,'USDT转账成功',"您在{$dateTime}(UTC+8)成功支付{$money}USDT转账给用户【{$toUserId}】{$toNickname}",3);
  972. $dateTime = date('Y-m-d H:i:s');
  973. MessageService::make()->pushMessage($toUserId,'USDT转账成功',"您在{$dateTime}(UTC+8)收到用户【{$userId}】{$nickname}的{$realUsdt}USDT转账",3);
  974. DB::commit();
  975. RedisService::clear($cacheKey);
  976. $this->error = 2425;
  977. return true;
  978. }
  979. /**
  980. * 余额充值
  981. * @param $userId
  982. * @param $params
  983. * @return array|false
  984. */
  985. public function recharge($userId, $params)
  986. {
  987. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  988. $usdt = isset($params['usdt'])? moneyFormat($params['usdt'], 2) : 0;
  989. $coinType = isset($params['coin_type']) && $params['coin_type']? intval($params['coin_type']) : 2;
  990. $payType = 10;
  991. if($usdt<=0){
  992. $this->error = 2031;
  993. return false;
  994. }
  995. $cacheKey = "caches:member:recharge:lock_{$userId}";
  996. if(RedisService::get($cacheKey)){
  997. $this->error = 1034;
  998. return false;
  999. }
  1000. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  1001. ->select(['id','nickname','usdt','pay_password','balance','status'])
  1002. ->first();
  1003. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1004. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1005. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1006. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1007. if(empty($userInfo) || $status != 1){
  1008. $this->error = 2024;
  1009. return false;
  1010. }
  1011. if($userPayPassword != get_password($payPassword)){
  1012. $this->error = 2038;
  1013. return false;
  1014. }
  1015. // 充值订单
  1016. $orderNo = get_order_num('DR');
  1017. $money = $usdt;
  1018. // 星豆
  1019. DB::beginTransaction();
  1020. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1021. if($coinType == 2){
  1022. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1023. $xdPrice = $xdPrice>0 && $xdPrice <=10000? $xdPrice : 100;
  1024. $money = round($xdPrice * $usdt,2);
  1025. if($usdt > $userUsdt){
  1026. $this->error = 2035;
  1027. RedisService::clear($cacheKey);
  1028. return false;
  1029. }
  1030. // 扣除usdt余额
  1031. $updateData = ['usdt'=>DB::raw("usdt - {$usdt}"),'balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  1032. if(!$this->model->where(['id'=> $userId])->update($updateData)){
  1033. RedisService::clear($cacheKey);
  1034. $this->error = 2036;
  1035. DB::rollBack();
  1036. return false;
  1037. }
  1038. }
  1039. $data = [
  1040. 'order_no'=> $orderNo,
  1041. 'user_id'=> $userId,
  1042. 'type'=> 1,
  1043. 'user_type'=> 1,
  1044. 'coin_type'=> 2,
  1045. 'money'=> $usdt,
  1046. 'actual_money'=> $money,
  1047. 'pay_type'=> $payType,
  1048. 'pay_status'=> 20,
  1049. 'pay_at'=> date('Y-m-d H:i:s'),
  1050. 'create_time'=> time(),
  1051. 'update_time'=> time(),
  1052. 'status'=> 2,
  1053. 'mark'=> 1,
  1054. ];
  1055. if(!$orderId = BalanceLogModel::insertGetId($data)){
  1056. RedisService::clear($cacheKey);
  1057. $this->error = 2033;
  1058. DB::rollBack();
  1059. return false;
  1060. }
  1061. // 扣除USDT
  1062. $log = [
  1063. 'user_id'=> $userId,
  1064. 'source_order_no'=> $data['order_no'],
  1065. 'user_type'=> 1,
  1066. 'type'=> 5,
  1067. 'coin_type'=> 1,
  1068. 'money'=> -$usdt,
  1069. 'actual_money'=> -$usdt,
  1070. 'balance'=> $userUsdt,
  1071. 'date'=> date('Y-m-d'),
  1072. 'create_time'=> time(),
  1073. 'remark'=> '星豆余额充值扣除',
  1074. 'status'=>1,
  1075. 'mark'=>1
  1076. ];
  1077. if(!AccountLogModel::insertGetId($log)){
  1078. RedisService::clear($cacheKey);
  1079. $this->error = 2029;
  1080. DB::rollBack();
  1081. return false;
  1082. }
  1083. // 增加星豆余额
  1084. $log = [
  1085. 'user_id'=> $userId,
  1086. 'source_order_no'=> $data['order_no'],
  1087. 'user_type'=> 1,
  1088. 'type'=> 5,
  1089. 'coin_type'=> 2,
  1090. 'money'=> $usdt,
  1091. 'actual_money'=> $money,
  1092. 'balance'=> $balance,
  1093. 'date'=> date('Y-m-d'),
  1094. 'create_time'=> time(),
  1095. 'remark'=> '星豆余额充值',
  1096. 'status'=>1,
  1097. 'mark'=>1
  1098. ];
  1099. if(!AccountLogModel::insertGetId($log)){
  1100. RedisService::clear($cacheKey);
  1101. $this->error = 2029;
  1102. DB::rollBack();
  1103. return false;
  1104. }
  1105. // 消息
  1106. $dateTime = date('Y-m-d H:i:s');
  1107. MessageService::make()->pushMessage($userId,'星豆余额充值成功',"您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}星豆",3);
  1108. DB::commit();
  1109. // 充值星豆任务
  1110. TaskService::make()->updateTask($userId,9, 0);
  1111. $this->error = 2037;
  1112. RedisService::clear($cacheKey);
  1113. return ['order_id'=> $orderId,'balance'=> moneyFormat($balance + $money,2),'usdt'=> moneyFormat($userUsdt-$usdt, 2)];
  1114. }
  1115. /**
  1116. * 余额提现
  1117. * @param $userId
  1118. * @param $params
  1119. * @return array|false
  1120. */
  1121. public function withdraw($userId, $params)
  1122. {
  1123. $money = isset($params['money'])? floatval($params['money']) : 0;
  1124. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1125. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  1126. if($money<=0){
  1127. $this->error = 2401;
  1128. return false;
  1129. }
  1130. if(!in_array($userType,[1,2])){
  1131. $this->error = 2404;
  1132. return false;
  1133. }
  1134. $userInfo = $this->model->with(['merchant'])
  1135. ->where(['id'=> $userId,'mark'=>1])
  1136. ->select(['id','nickname','usdt','trc_url','pay_password','status'])
  1137. ->first();
  1138. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1139. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1140. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1141. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1142. $merchantUsdt = isset($merchant['usdt'])? $merchant['usdt'] : 0;
  1143. $merchantId = isset($merchant['id'])? $merchant['id'] : 0;
  1144. $merchantTradeStatus = isset($merchant['trade_status'])? $merchant['trade_status'] : 0;
  1145. if(empty($userInfo) || $status != 1){
  1146. $this->error = 2024;
  1147. return false;
  1148. }
  1149. if($userType == 2 && (empty($merchant) || !in_array($merchantTradeStatus,[1,2]))){
  1150. $this->error = 2024;
  1151. return false;
  1152. }
  1153. // 提现金额验证
  1154. $accountUsdt = $userType==1? $userUsdt : $merchantUsdt;
  1155. if ($money > $accountUsdt) {
  1156. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  1157. return false;
  1158. }
  1159. // 提现账户
  1160. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  1161. if (empty($trcUrl)) {
  1162. $this->error = 2403;
  1163. return false;
  1164. }
  1165. if($userPayPassword != get_password($payPassword)){
  1166. $this->error = 2038;
  1167. return false;
  1168. }
  1169. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  1170. if(RedisService::get($cacheKey)){
  1171. $this->error = 1034;
  1172. return false;
  1173. }
  1174. DB::beginTransaction();
  1175. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1176. // 提现记录
  1177. $orderNo = get_order_num('DW');
  1178. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
  1179. $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
  1180. $fee = round($money*$feeRate, 2);
  1181. $realUsdt = moneyFormat($money - $fee, 2);
  1182. $data = [
  1183. 'order_no'=> $orderNo,
  1184. 'user_id'=> $userType==1? $userId : $merchantId,
  1185. 'type'=> 2,
  1186. 'user_type'=> $userType,
  1187. 'coin_type'=> 1,
  1188. 'money'=> $money,
  1189. 'actual_money'=> $realUsdt,
  1190. 'fee'=> $fee,
  1191. 'trc_url'=> $trcUrl,
  1192. 'pay_type'=> 20,
  1193. 'date'=> date('Y-m-d'),
  1194. 'create_time'=> time(),
  1195. 'update_time'=> time(),
  1196. 'status'=> 1,
  1197. 'mark'=> 1,
  1198. ];
  1199. if(!$id = BalanceLogModel::insertGetId($data)){
  1200. DB::rollBack();
  1201. $this->error = 2405;
  1202. RedisService::clear($cacheKey);
  1203. return false;
  1204. }
  1205. // 商户扣款
  1206. if($userType == 2){
  1207. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1208. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1209. DB::rollBack();
  1210. $this->error = 2406;
  1211. RedisService::clear($cacheKey);
  1212. return false;
  1213. }
  1214. // 明细
  1215. $log = [
  1216. 'user_id' => $merchantId,
  1217. 'source_id' => $userId,
  1218. 'source_order_no' => $orderNo,
  1219. 'type' => 5,
  1220. 'coin_type' => 1,
  1221. 'user_type'=> 2,
  1222. 'money' => -$money,
  1223. 'actual_money' => -$money,
  1224. 'balance' => $merchantUsdt,
  1225. 'create_time' => time(),
  1226. 'update_time' => time(),
  1227. 'remark' => "商户账户余额提现",
  1228. 'status' => 1,
  1229. 'mark' => 1,
  1230. ];
  1231. if (!AccountLogModel::insertGetId($log)) {
  1232. $this->error = 2407;
  1233. DB::rollBack();
  1234. RedisService::clear($cacheKey);
  1235. return false;
  1236. }
  1237. }
  1238. // 用户扣款
  1239. else{
  1240. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1241. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1242. DB::rollBack();
  1243. $this->error = 2406;
  1244. RedisService::clear($cacheKey);
  1245. return false;
  1246. }
  1247. // 明细
  1248. $log = [
  1249. 'user_id' => $userId,
  1250. 'source_id' => 0,
  1251. 'source_order_no' => $orderNo,
  1252. 'type' => 5,
  1253. 'coin_type' => 1,
  1254. 'user_type'=> 1,
  1255. 'money' => -$money,
  1256. 'actual_money' => -$money,
  1257. 'balance' => $userUsdt,
  1258. 'create_time' => time(),
  1259. 'update_time' => time(),
  1260. 'remark' => "USDT余额提现",
  1261. 'status' => 1,
  1262. 'mark' => 1,
  1263. ];
  1264. if (!AccountLogModel::insertGetId($log)) {
  1265. $this->error = 2407;
  1266. DB::rollBack();
  1267. RedisService::clear($cacheKey);
  1268. return false;
  1269. }
  1270. }
  1271. DB::commit();
  1272. // 站内消息
  1273. $dateTime = date('Y-m-d H:i:s');
  1274. $title = $userType == 1 ? 'USDT余额提现申请成功' : '商户余额提现申请成功';
  1275. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额成功,请耐心等候审核!!!";
  1276. MessageService::make()->pushMessage($userId, $title, $message);
  1277. // 提现自动审核,低于该金额自动审核
  1278. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  1279. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  1280. if ($money <= $autoCheckUsdt) {
  1281. // 打款处理
  1282. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  1283. $txID = isset($result['txId']) ? $result['txId'] : '';
  1284. $payAddress = isset($result['address']) ? $result['address'] : '';
  1285. if ($txID && $payAddress) {
  1286. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  1287. if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
  1288. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  1289. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  1290. MessageService::make()->pushMessage($userId, $title, $message);
  1291. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  1292. // 平台明细
  1293. $log = [
  1294. 'user_id' => 0,
  1295. 'source_id' => $userId,
  1296. 'source_order_no' => $orderNo,
  1297. 'type' => 5,
  1298. 'coin_type' => 1,
  1299. 'user_type'=> 4,
  1300. 'money' => $fee,
  1301. 'actual_money' => $fee,
  1302. 'balance' => 0,
  1303. 'create_time' => time(),
  1304. 'update_time' => time(),
  1305. 'hash' => $txID,
  1306. 'remark' => "USDT余额提现",
  1307. 'status' => 1,
  1308. 'mark' => 1,
  1309. ];
  1310. AccountLogModel::insertGetId($log);
  1311. // 平台流水
  1312. FinanceService::make()->saveLog(0, $fee, 1);
  1313. }
  1314. }
  1315. }
  1316. $this->error = $title;
  1317. RedisService::clear($cacheKey);
  1318. return [
  1319. 'id'=> $id,
  1320. 'money'=> $money,
  1321. 'user_type'=> $userType,
  1322. ];
  1323. }
  1324. /**
  1325. * 获取点对点上级节点
  1326. * @param $userId 推荐人ID
  1327. * @return int|mixed
  1328. */
  1329. public function getPointParentId($userId)
  1330. {
  1331. if($userId<=0){
  1332. return 0;
  1333. }
  1334. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1335. $userList = $userList? $userList->toArray() : [];
  1336. if(count($userList) < 3){
  1337. return $userId;
  1338. }
  1339. $users = [];
  1340. foreach($userList as $item){
  1341. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1342. $points = array_filter($points);
  1343. $users[$item['id']]['pointLen'] = count($points);
  1344. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1345. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1346. }
  1347. //var_dump($users);
  1348. $pointId = 0;
  1349. $tempPointLen = 0;
  1350. $tempChildren = 0;
  1351. foreach ($userList as $item) {
  1352. $id = isset($item['id'])? $item['id'] : 0;
  1353. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1354. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1355. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1356. if($pointLen<=$tempPointLen || $pointId == 0){
  1357. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1358. if($children <$tempChildren || $pointId == 0){
  1359. $pointId = $id;
  1360. $tempPointLen = $pointLen;
  1361. $tempChildren = $children;
  1362. }
  1363. }
  1364. }
  1365. return $pointId;
  1366. }
  1367. public function upgrade($userId, $params)
  1368. {
  1369. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1370. $level = isset($params['level'])? intval($params['level']) : 0;
  1371. if($level<=0 || $payPassword<=0){
  1372. $this->error = 2014;
  1373. return false;
  1374. }
  1375. $cacheKey = "caches:members:upgrade:buy_{$userId}_{$level}";
  1376. if(RedisService::get($cacheKey.'_lock')){
  1377. $this->error = 1034;
  1378. return false;
  1379. }
  1380. $levelInfo = MemberLevelModel::from('member_level as a')
  1381. ->where(['a.id'=> $level,'a.status'=>1,'a.mark'=>1])
  1382. ->select(['a.*'])
  1383. ->first();
  1384. $upgradeUsdt = isset($levelInfo['upgrade_usdt'])? floatval($levelInfo['upgrade_usdt']) : 0;
  1385. $upgradeProfit = isset($levelInfo['upgrade_profit'])? floatval($levelInfo['upgrade_profit']) : 0;
  1386. $boxNum = isset($levelInfo['box_num'])? intval($levelInfo['box_num']) : 0;
  1387. $levelName = isset($levelInfo['name'])? $levelInfo['name'] : '';
  1388. if(empty($nodeInfo)){
  1389. $this->error = 2511;
  1390. return false;
  1391. }
  1392. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  1393. ->select(['id','nickname','usdt','wait_score','power_num','upgrade_profit_total','trc_url','pay_password','status'])
  1394. ->first();
  1395. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1396. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1397. $userTotalProfit = isset($userInfo['upgrade_profit_total'])? $userInfo['upgrade_profit_total'] : 0;
  1398. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1399. $userWaitScore = isset($userInfo['wait_score'])? $userInfo['wait_score'] : 0;
  1400. $userPowerNum = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
  1401. $userLevel = isset($userInfo['member_level'])? intval($userInfo['member_level']) : 0;
  1402. if(empty($userInfo) || $status != 1){
  1403. $this->error = 2024;
  1404. return false;
  1405. }
  1406. if($userLevel>= $level){
  1407. $this->error = 2512;
  1408. return false;
  1409. }
  1410. if($level >= 30){
  1411. $this->error = 2513;
  1412. return false;
  1413. }
  1414. // 收益是否满足条件
  1415. if($upgradeProfit>0 && $userTotalProfit < $upgradeProfit){
  1416. $this->error = 2514;
  1417. return false;
  1418. }
  1419. if(AccountLogModel::where(['user_id'=> $userId,'source_id'=> $level,'type'=>2,'coin_type'=>1,'status'=>1,'mark'=>1])->value('id')){
  1420. $this->error = 2512;
  1421. return false;
  1422. }
  1423. // USDT余额是否足够
  1424. if($userUsdt < $upgradeUsdt){
  1425. $this->error = 2035;
  1426. return false;
  1427. }
  1428. // 交易密码
  1429. if(empty($userPayPassword)){
  1430. $this->error = 1040;
  1431. return false;
  1432. }
  1433. if($userPayPassword != get_password($payPassword)){
  1434. $this->error = 2038;
  1435. return false;
  1436. }
  1437. // 购买明细
  1438. RedisService::set($cacheKey, $params, rand(2,3));
  1439. $orderNo = get_order_num('UP');
  1440. DB::beginTransaction();
  1441. $log = [
  1442. 'user_id' => $userId,
  1443. 'source_id' => $level,
  1444. 'source_order_no' => $orderNo,
  1445. 'type' => 2,
  1446. 'coin_type' => 1,
  1447. 'user_type'=> 1,
  1448. 'money' => -$upgradeUsdt,
  1449. 'actual_money' => -$upgradeUsdt,
  1450. 'balance' => $userUsdt,
  1451. 'create_time' => time(),
  1452. 'update_time' => time(),
  1453. 'remark' => "购买{$level}升级",
  1454. 'status' => 1,
  1455. 'mark' => 1,
  1456. ];
  1457. if(!AccountLogModel::insertGetId($log)){
  1458. DB::rollBack();
  1459. $this->error = 2029;
  1460. RedisService::clear($cacheKey);
  1461. return false;
  1462. }
  1463. // 待返积分
  1464. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1465. $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
  1466. $waitScoreRate = ConfigService::make()->getConfigByCode('upgrade_award_score_rate', 200);
  1467. $waitScoreRate = $waitScoreRate>0 && $waitScoreRate<=1000? $waitScoreRate : 0;
  1468. $waitScore = moneyFormat($upgradeUsdt * $xdPrice * $waitScoreRate/100, 2);
  1469. if($waitScore>0){
  1470. $log = [
  1471. 'user_id' => $userId,
  1472. 'source_id' => $level,
  1473. 'source_order_no' => $orderNo,
  1474. 'type' => 9,
  1475. 'coin_type' => 5,
  1476. 'user_type'=> 1,
  1477. 'money' => $waitScore,
  1478. 'actual_money' => $waitScore,
  1479. 'balance' => $userWaitScore,
  1480. 'create_time' => time(),
  1481. 'update_time' => time(),
  1482. 'remark' => "购买{$levelName}升级",
  1483. 'status' => 1,
  1484. 'mark' => 1,
  1485. ];
  1486. if(!AccountLogModel::insertGetId($log)){
  1487. DB::rollBack();
  1488. $this->error = 2029;
  1489. RedisService::clear($cacheKey);
  1490. return false;
  1491. }
  1492. }
  1493. // 算力明细
  1494. $powerNum = $upgradeUsdt;
  1495. $log = [
  1496. 'user_id' => $userId,
  1497. 'source_id' => $level,
  1498. 'source_order_no' => $orderNo,
  1499. 'type' => 9,
  1500. 'coin_type' => 3,
  1501. 'user_type'=> 1,
  1502. 'money' => $powerNum,
  1503. 'actual_money' => $powerNum,
  1504. 'balance' => $userPowerNum,
  1505. 'create_time' => time(),
  1506. 'update_time' => time(),
  1507. 'remark' => "购买{$levelName}升级",
  1508. 'status' => 1,
  1509. 'mark' => 1,
  1510. ];
  1511. if(!AccountLogModel::insertGetId($log)){
  1512. DB::rollBack();
  1513. $this->error = 2029;
  1514. RedisService::clear($cacheKey);
  1515. return false;
  1516. }
  1517. // 账户
  1518. $updateData = [
  1519. 'usdt'=> DB::raw("usdt - {$upgradeUsdt}"), // 扣除USDT
  1520. 'member_level'=> $level, // 升级等级
  1521. 'box_num'=>DB::raw("box_num + {$boxNum}"), // 奖励盲盒数量
  1522. 'wait_score'=>DB::raw("wait_score + {$waitScore}"), // 奖励盲盒数量
  1523. 'power_num'=>DB::raw("power_num + {$powerNum}"), // 奖励盲盒数量
  1524. 'update_time'=>time()
  1525. ];
  1526. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  1527. DB::rollBack();
  1528. $this->error = 2036;
  1529. RedisService::clear($cacheKey);
  1530. return false;
  1531. }
  1532. // 消息
  1533. $dateTime = date('Y-m-d H:i:s');
  1534. MessageService::make()->pushMessage($userId,"购买{$levelName}升级成功","您在{$dateTime}(UTC+8)成功支付{$upgradeUsdt}USDT购买【{$levelName}】升级成功。",3);
  1535. DB::commit();
  1536. // 结算收益奖励
  1537. FinanceService::make()->settleBonus($userId, $userInfo, $upgradeUsdt, $orderNo, "购买{$levelName}");
  1538. $this->error = 2503;
  1539. RedisService::clear($cacheKey);
  1540. return true;
  1541. }
  1542. }