MemberService.php 65 KB

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