MemberService.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  357. $code = isset($params['code']) ? trim($params['code']) : '';
  358. $scene = isset($params['scene']) ? $params['scene'] : '';
  359. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  360. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  361. if (empty($params) || empty($email) || $code <= 0) {
  362. $this->error = 2014;
  363. return false;
  364. }
  365. // 验证码
  366. if (!EmailService::make()->check($email, $code, $scene)) {
  367. $this->error = EmailService::make()->getError();
  368. return false;
  369. }
  370. // 验证是否注册
  371. if($this->model->where(['email'=> $email,'mark'=>1])->value('id')){
  372. $this->error = 2009;
  373. return false;
  374. }
  375. // 推荐人
  376. $parentId = 0;
  377. $pointId = 0;
  378. $parents = '';
  379. $points = '';
  380. if ($inviteCode) {
  381. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  382. ->select(['id', 'parents', 'parent_id'])
  383. ->first();
  384. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  385. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  386. // 挂靠节点
  387. if($parentId>0){
  388. $pointId = $this->getPointParentId($parentId);
  389. if($pointId){
  390. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  391. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  392. }
  393. }
  394. }
  395. // 注册
  396. $id = $this->model->max('id') + 1;
  397. $system = isset($params['system'])? $params['system'] : [];
  398. $ip = get_client_ip();
  399. $ipData = ToolService::make()->getIpAddress($ip,'');
  400. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  401. $city = isset($ipData['city'])? $ipData['city'] : '';
  402. $emailName = explode('@', $email)[0];
  403. // 钱包
  404. $data = [
  405. 'parent_id' => $parentId,
  406. 'point_id' => $pointId,
  407. 'points' => $points,
  408. 'openid' => '',
  409. 'avatar' => '',
  410. 'nickname' => $nickname ? $nickname : '用户'.substr($emailName,-8,8),
  411. 'username' => $email,
  412. 'password' => get_password('xl123456'),
  413. 'code' => strtoupper(get_random_code(9, 'X', "{$id}")),
  414. 'email' => $email,
  415. 'mobile' => $mobile,
  416. 'recharge_trc_url' => '',
  417. 'parents' => $parents,
  418. 'province_id' => CityService::make()->getFieldByName($province),
  419. 'city_id' => CityService::make()->getFieldByName($city),
  420. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  421. 'device' => $appSources == 'android' ? 2 : 1,
  422. 'status' => 1,
  423. 'balance' => 0,
  424. 'score' => 0,
  425. 'mark' => 1,
  426. 'login_ip' => $ip,
  427. 'create_time' => time(),
  428. ];
  429. $userId = $this->model->insertGetId($data);
  430. $this->error = 2008;
  431. return [
  432. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  433. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  434. ];
  435. }
  436. /**
  437. * 邮箱登录
  438. * @param $params
  439. * @return array|false
  440. */
  441. public function loginByEmail($params)
  442. {
  443. // 授权
  444. $email = isset($params['email']) ? $params['email'] : '';
  445. $code = isset($params['code']) ? $params['code'] : '';
  446. $scene = isset($params['scene']) ? $params['scene'] : '';
  447. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  448. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  449. $this->error = 2014;
  450. return false;
  451. }
  452. // 验证码
  453. if (!EmailService::make()->check($email, $code, $scene)) {
  454. $this->error = EmailService::make()->getError();
  455. return false;
  456. }
  457. // 验证是否注册,没有则注册
  458. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  459. $data = $data ? $data->toArray() : [];
  460. $userId = isset($data['id']) ? $data['id'] : 0;
  461. $status = isset($data['status']) ? $data['status'] : 0;
  462. if(empty($data) || $userId<=0){
  463. $this->error = 2001;
  464. return false;
  465. }
  466. if ($status != 1) {
  467. $this->error = 2015;
  468. return false;
  469. }
  470. // 获取登录授权token
  471. $jwt = new Jwt('jwt_dys_app');
  472. $token = $jwt->getToken($userId);
  473. // 结果返回
  474. $result = [
  475. 'access_token' => $token,
  476. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  477. ];
  478. // 用户信息
  479. $ip = get_client_ip();
  480. $updateData = [
  481. 'login_time' => time(),
  482. 'login_ip' => $ip,
  483. ];
  484. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  485. $ipData = ToolService::make()->getIpAddress($ip,'');
  486. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  487. $city = isset($ipData['city'])? $ipData['city'] : '';
  488. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  489. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  490. }
  491. $this->model->where(['id'=> $userId])->update($updateData);
  492. $this->error = 2004;
  493. $data['token'] = $token;
  494. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  495. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  496. return $result;
  497. }
  498. /**
  499. * 手机号登录
  500. * @param $params
  501. * @return array|false
  502. */
  503. public function loginByMobile($params)
  504. {
  505. // 授权
  506. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  507. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  508. $smsType = isset($params['sms_type']) ? $params['sms_type'] : '';
  509. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  510. $openid = isset($params['openid']) ? $params['openid'] : '';
  511. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  512. $regType = isset($params['type']) ? $params['type'] : 'app';
  513. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  514. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  515. if (empty($params) || empty($mobile) || $code <= 0) {
  516. $this->error = 2014;
  517. return false;
  518. }
  519. // 验证码
  520. if (!SmsService::make()->check($mobile, $code, $smsType?$smsType:'login')) {
  521. $this->error = SmsService::make()->getError();
  522. return false;
  523. }
  524. // 推荐人
  525. $parentId = 0;
  526. $parents = '';
  527. if ($inviteCode) {
  528. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  529. ->select(['id', 'parents', 'parent_id'])
  530. ->first();
  531. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  532. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] . $parentId . ',' : ($parentId ? $parentId . ',' : '');
  533. }
  534. // 验证是否注册,没有则注册
  535. $data = $this->model->with(['merchant','mechanic'])->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'openid', 'username', 'parent_id', 'code', 'push_cid', 'status'])->first();
  536. $data = $data ? $data->toArray() : [];
  537. $userId = isset($data['id']) ? $data['id'] : 0;
  538. $status = isset($data['status']) ? $data['status'] : 0;
  539. if ($userId && $status != 1) {
  540. $this->error = 2015;
  541. return false;
  542. }
  543. // 是否是商家或技师,有订单提醒
  544. $noticeUser = ['id'=> 0,'uid'=>$userId,'type'=>1];
  545. $merchant = isset($data['merchant'])? $data['merchant'] : [];
  546. $merchantStatus = isset($merchant['status'])? $merchant['status'] : 0;
  547. $mechanic = isset($data['mechanic'])? $data['mechanic'] : [];
  548. $mechanicStatus = isset($mechanic['status'])? $mechanic['status'] : 0;
  549. if($merchantStatus==2){
  550. $uid = isset($merchant['id'])? $merchant['id'] : 0;
  551. if($uid>0){
  552. $noticeUser = ['id'=>$uid,'type'=>2,'uid'=> $userId];
  553. }
  554. }else if($mechanicStatus == 2){
  555. $uid = isset($mechanic['id'])? $mechanic['id'] : 0;
  556. if($uid>0){
  557. $noticeUser = ['id'=>$uid,'type'=>3,'uid'=>$userId];
  558. }
  559. }
  560. $pushCid = isset($params['push_cid']) ? $params['push_cid'] : '';
  561. if (!$userId) {
  562. $id = $this->model->max('id') + 1;
  563. $data = [
  564. 'parent_id' => $parentId,
  565. 'openid' => $openid,
  566. 'avatar' => $avatar,
  567. 'nickname' => $nickname ? $nickname : get_random_code(4, '用户u').substr($mobile,-4,4),
  568. 'username' => get_username($id),
  569. 'password' => get_password('123456'),
  570. 'code' => strtoupper(get_random_code(8, 'D', "{$id}")),
  571. 'mobile' => $mobile,
  572. 'parents' => $parents,
  573. 'push_cid' => isset($params['push_cid']) ? $params['push_cid'] : '',
  574. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  575. 'device' => $appSources == 'android' ? 2 : 1,
  576. 'status' => 1,
  577. 'balance' => 0,
  578. 'score' => 0,
  579. 'mark' => 1,
  580. 'login_time' => time(),
  581. 'login_ip' => get_client_ip(),
  582. 'create_time' => time(),
  583. ];
  584. $userId = $this->model->insertGetId($data);
  585. } elseif ($regType != 'h5') {
  586. // 登录更新
  587. $updateData = [
  588. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  589. 'login_count' => DB::raw("login_count+1"),
  590. 'login_time' => time(),
  591. 'login_ip' => get_client_ip(),
  592. 'update_time' => time(),
  593. ];
  594. if ($pushCid) {
  595. $updateData['push_cid'] = $pushCid;
  596. }
  597. $this->model->where(['id' => $userId])->update($updateData);
  598. }
  599. // H5 邀请注册,无需登录
  600. if ($regType == 'h5') {
  601. $this->error = $status ? 2023 : 2022;
  602. return [
  603. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  604. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'username' => $data['username']],
  605. ];
  606. }
  607. // 获取登录授权token
  608. $jwt = new Jwt('jwt_dys_app');
  609. $token = $jwt->getToken($userId);
  610. // 结果返回
  611. $result = [
  612. 'access_token' => $token,
  613. 'info' => ['uid' => $userId,'notice_user'=>$noticeUser, 'openid' => $data['openid'], 'username' => $data['username']],
  614. ];
  615. // 用户信息
  616. $this->error = 2004;
  617. $data['token'] = $token;
  618. unset($data['password']);
  619. // 实现初始化账单明细表
  620. AccountLogModel::where('id','<=',1)->value('id');
  621. RedisService::clear( "caches:map:{$userId}");
  622. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  623. return $result;
  624. }
  625. /**
  626. * 修改头像/商家logo/技师头像
  627. * @param $userId
  628. * @param $avatar
  629. * @return mixed
  630. */
  631. public function saveAvatar($userId, $avatar)
  632. {
  633. $type = request()->post('type', 1);
  634. if($type == 1){
  635. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  636. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  637. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  638. @unlink(ATTACHMENT_PATH . $oldAvatar);
  639. }
  640. return true;
  641. }
  642. }else if($type == 2){
  643. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  644. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $avatar, 'update_time' => time()])) {
  645. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  646. @unlink(ATTACHMENT_PATH . $oldAvatar);
  647. }
  648. return true;
  649. }
  650. }
  651. return false;
  652. }
  653. /**
  654. * 修改账号
  655. * @param $userId
  656. * @param $params
  657. * @return bool
  658. */
  659. public function modify($userId, $params)
  660. {
  661. $email = isset($params['email']) ? $params['email'] : '';
  662. $password = isset($params['password']) ? $params['password'] : '';
  663. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  664. $scene = isset($params['scene'])? trim($params['scene']) : '';
  665. // 用户验证
  666. $info = $this->model->with(['parent'])->where(['id' => $userId, 'mark' => 1])->select(['id','pay_password','parent_id','code', 'status'])->first();
  667. if (!$info) {
  668. $this->error = 2001;
  669. return false;
  670. }
  671. // 使用状态校验
  672. if ($info['status'] != 1) {
  673. $this->error = 2009;
  674. return false;
  675. }
  676. // 密码校验
  677. $data = ['update_time' => time()];
  678. // 验证交易密码
  679. if(isset($params['pay_password']) && $scene != 'trade'){
  680. $userPayPassword = isset($info['pay_password'])? $info['pay_password'] : '';
  681. if(empty($userPayPassword)){
  682. $this->error = 1040;
  683. return false;
  684. }
  685. if($userPayPassword != get_password($payPassword)){
  686. $this->error = 2038;
  687. return false;
  688. }
  689. }
  690. // 邀请人绑定
  691. $parent = isset($info['parent'])? $info['parent'] : [];
  692. $userParentCode = isset($parent['code'])? $parent['code'] : '';
  693. $userCode = isset($info['code'])? $info['code'] : '';
  694. $parentCode = isset($params['parent_code'])? trim($params['parent_code']) : '';
  695. if(isset($params['parent_code']) && $parentCode){
  696. //
  697. if($userParentCode){
  698. $this->error = 2451;
  699. return false;
  700. }
  701. if(($userCode == $parentCode)){
  702. $this->error = 2452;
  703. return false;
  704. }
  705. // 推荐人验证
  706. $inviteInfo = $this->model->where(['code' => $parentCode, 'mark' => 1])
  707. ->select(['id', 'parents', 'parent_id','trc_url'])
  708. ->first();
  709. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  710. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  711. if(!$parentId){
  712. $this->error = 2453;
  713. return false;
  714. }
  715. // 验证是否绑定用户为自己线下用户
  716. if(preg_match("/{$userId},/", $parents)){
  717. $this->error = 2454;
  718. return false;
  719. }
  720. // 推荐人
  721. $data['parent_id'] = $parentId;
  722. $data['parents'] = $parents;
  723. // 挂靠节点
  724. if($parentId>0){
  725. $pointId = $this->getPointParentId($parentId);
  726. if($pointId){
  727. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  728. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  729. $data['point_id'] = $pointId;
  730. $data['points'] = $points;
  731. }
  732. }
  733. }
  734. // 邮箱验证
  735. if (isset($params['email']) && $email) {
  736. // 修改邮箱
  737. $newEmail = isset($params['new_email'])? trim($params['new_email']) : '';
  738. if(isset($params['new_email']) && $newEmail){
  739. $data['email'] = $newEmail;
  740. $data['username'] = $newEmail;
  741. $checkInfo = $this->model->where(['username' => $newEmail, 'mark' => 1])->select(['id', 'status'])->first();
  742. if ($checkInfo && $checkInfo['id'] != $userId) {
  743. $this->error = 2005;
  744. return false;
  745. }
  746. }
  747. // 邮箱验证码
  748. $code = isset($params['code'])? trim($params['code']) : '';
  749. $checkEmail = $scene == 'modify_email'? $newEmail : $email;
  750. if (!EmailService::make()->check($checkEmail, $code, $scene)) {
  751. $this->error = EmailService::make()->getError();
  752. return false;
  753. }
  754. }
  755. $trcUrl = isset($params['trc_url'])? trim($params['trc_url']) : '';
  756. $userTrcUrl = isset($info['trc_url'])? trim($info['trc_url']) : '';
  757. if(isset($params['trc_url']) && $trcUrl){
  758. if($trcUrl != $userTrcUrl){
  759. if($this->model->where(['trc_url'=> $trcUrl,'mark'=>1])->whereNotIn('id',[$userId])->value('id')){
  760. $this->error = 1048;
  761. return false;
  762. }
  763. if(!WalletService::make()->checkAddress($trcUrl)){
  764. $this->error = 1049;
  765. return false;
  766. }
  767. $data['trc_url'] = $trcUrl;
  768. }
  769. }
  770. // 修改数据
  771. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  772. if (isset($params['nickname']) && $nickname) {
  773. $data['nickname'] = $nickname;
  774. }
  775. $gender = isset($params['gender']) ? intval($params['gender']) : 3;
  776. if (isset($params['gender']) && $gender) {
  777. $data['gender'] = $gender;
  778. }
  779. $intro = isset($params['intro']) ? $params['intro'] : '';
  780. if (isset($params['intro']) && $intro) {
  781. $data['intro'] = $intro;
  782. }
  783. $birthday = isset($params['birthday_text']) ? $params['birthday_text'] : '';
  784. if (isset($params['birthday_text']) && $birthday) {
  785. $data['birthday'] = strtotime($birthday);
  786. }
  787. if (isset($params['pay_password']) && $payPassword) {
  788. $payPassword = get_password($payPassword);
  789. $data['pay_password'] = $payPassword;
  790. }
  791. if($this->model->where(['id' => $userId])->update($data)){
  792. // 更新绑定上级后,团队用户全部更新
  793. if(isset($data['parent_id']) && $data['parent_id']){
  794. $updateData = ['parents'=>DB::raw("concat(parents,'{$data['parent_id']},')"),'update_time'=>time()];
  795. $this->model->where('parents','like',"%{$userId},")->update($updateData);
  796. }
  797. // 更新团队节点关系
  798. if(isset($data['point_id']) && $data['point_id']){
  799. $updateData = ['points'=>DB::raw("concat(points,'{$data['point_id']},')"),'update_time'=>time()];
  800. $this->model->where('points','like',"%{$userId},")->update($updateData);
  801. }
  802. $this->error = 1008;
  803. return true;
  804. }
  805. $this->error = 1008;
  806. return true;
  807. }
  808. /**
  809. * 身份认证
  810. * @param $userId
  811. * @param $params
  812. * @return bool
  813. */
  814. public function auth($userId, $params)
  815. {
  816. // 用户验证
  817. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  818. if (!$info) {
  819. $this->error = 2001;
  820. return false;
  821. }
  822. // 使用状态校验
  823. if ($info['status'] != 1) {
  824. $this->error = 2009;
  825. return false;
  826. }
  827. // 密码校验
  828. $data = ['update_time' => time(),'idcard_check'=>1];
  829. if (isset($params['realname']) && $params['realname']) {
  830. $data['realname'] = trim($params['realname']);
  831. }
  832. $idcard = isset($params['idcard']) && $params['idcard'];
  833. if ($idcard) {
  834. $data['idcard'] = trim($params['idcard']);
  835. }
  836. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  837. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  838. }
  839. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  840. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  841. }
  842. $this->model->where(['id' => $userId])->update($data);
  843. // 发送消息
  844. $this->error = 2036;
  845. return true;
  846. }
  847. /**
  848. * 转账
  849. * @param $userId
  850. * @param $params
  851. * @return bool
  852. */
  853. public function transfer($userId, $params)
  854. {
  855. $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
  856. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  857. $money = isset($params['money'])? floatval($params['money']) : 0;
  858. if(empty($toUserId) || $money<=0){
  859. $this->error = 2420;
  860. return false;
  861. }
  862. $cacheKey = "caches:members:transfer:{$userId}";
  863. if(RedisService::get($cacheKey)){
  864. $this->error = 1034;
  865. return false;
  866. }
  867. // 验证账户
  868. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  869. ->select(['id','nickname','username','usdt','pay_password','balance','status'])
  870. ->first();
  871. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  872. $usdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  873. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  874. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  875. if(empty($userInfo) || $status != 1){
  876. $this->error = 2024;
  877. return false;
  878. }
  879. // 余额支付支付密码验证
  880. if(empty($userPayPassword)){
  881. $this->error = 1040;
  882. return false;
  883. }
  884. $payPassword = get_password($payPassword);
  885. if($payPassword != $userPayPassword){
  886. $this->error = 2038;
  887. return false;
  888. }
  889. // 验证账户
  890. $accountInfo = $this->model->where(['id'=> $toUserId,'mark'=>1])
  891. ->select(['id','nickname','usdt','username','balance','status'])
  892. ->first();
  893. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  894. $accountUsdt = isset($accountInfo['usdt'])? $accountInfo['usdt'] : 0;
  895. $toNickname = isset($accountInfo['nickname'])? $accountInfo['nickname'] : '';
  896. if(empty($accountInfo) || $status != 1){
  897. $this->error = 2421;
  898. return false;
  899. }
  900. if($money > $usdt){
  901. $this->error = 2422;
  902. return false;
  903. }
  904. // 用户余额转账到用户
  905. DB::beginTransaction();
  906. RedisService::set($cacheKey, $params, rand(2,5));
  907. $updateData = ['usdt'=> DB::raw("usdt - {$money}"),'update_time'=> time()];
  908. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  909. DB::rollBack();
  910. $this->error = 2028;
  911. RedisService::clear($cacheKey);
  912. return false;
  913. }
  914. // 明细
  915. $orderNo = get_order_num('TR');
  916. $log = [
  917. 'user_id' => $userId,
  918. 'source_id' => $toUserId,
  919. 'source_order_no' => $orderNo,
  920. 'type' => 6,
  921. 'coin_type' => 1,
  922. 'user_type'=> 1,
  923. 'money' => -$money,
  924. 'actual_money' => -$money,
  925. 'balance' => $usdt,
  926. 'create_time' => time(),
  927. 'update_time' => time(),
  928. 'remark' => "USDT转账",
  929. 'status' => 1,
  930. 'mark' => 1,
  931. ];
  932. if(!AccountLogModel::insertGetId($log)){
  933. DB::rollBack();
  934. $this->error = 2423;
  935. RedisService::clear($cacheKey);
  936. return false;
  937. }
  938. // 到账
  939. $fee = ConfigService::make()->getConfigByCode('transfer_fee_rate',5);
  940. $fee = $fee>0 && $fee<100? moneyFormat($fee/100, 2) : 0;
  941. $feeUsdt = round($money * $fee,2);
  942. $realUsdt = moneyFormat($money - $feeUsdt,2);
  943. $updateData = ['usdt'=> DB::raw("usdt + {$realUsdt}"),'update_time'=> time()];
  944. if(!$this->model->where(['id'=> $toUserId,'mark'=>1])->update($updateData)){
  945. DB::rollBack();
  946. $this->error = 2424;
  947. RedisService::clear($cacheKey);
  948. return false;
  949. }
  950. // 明细
  951. $log = [
  952. 'user_id' => $toUserId,
  953. 'source_id' => $userId,
  954. 'source_order_no' => $orderNo,
  955. 'type' => 6,
  956. 'coin_type' => 1,
  957. 'user_type'=> 1,
  958. 'money' => $realUsdt,
  959. 'actual_money' => $realUsdt,
  960. 'balance' => $accountUsdt,
  961. 'create_time' => time(),
  962. 'update_time' => time(),
  963. 'remark' => "USDT转账",
  964. 'status' => 1,
  965. 'mark' => 1,
  966. ];
  967. if(!AccountLogModel::insertGetId($log)){
  968. DB::rollBack();
  969. $this->error = 2029;
  970. RedisService::clear($cacheKey);
  971. return false;
  972. }
  973. // 平台明细
  974. $log = [
  975. 'user_id' => 0,
  976. 'source_id' => $userId,
  977. 'source_order_no' => $orderNo,
  978. 'type' => 6,
  979. 'coin_type' => 1,
  980. 'user_type'=> 4,
  981. 'money' => $feeUsdt,
  982. 'actual_money' => $feeUsdt,
  983. 'balance' => 0,
  984. 'create_time' => time(),
  985. 'update_time' => time(),
  986. 'remark' => "USDT转账",
  987. 'status' => 1,
  988. 'mark' => 1,
  989. ];
  990. if(!AccountLogModel::insertGetId($log)){
  991. DB::rollBack();
  992. $this->error = 2029;
  993. RedisService::clear($cacheKey);
  994. return false;
  995. }
  996. // 平台
  997. FinanceService::make()->saveLog(0, $feeUsdt,1);
  998. // 消息
  999. $dateTime = date('Y-m-d H:i:s');
  1000. MessageService::make()->pushMessage($userId,'USDT转账成功',"您在{$dateTime}(UTC+8)成功支付{$money}USDT转账给用户【{$toUserId}】{$toNickname}",3);
  1001. $dateTime = date('Y-m-d H:i:s');
  1002. MessageService::make()->pushMessage($toUserId,'USDT转账成功',"您在{$dateTime}(UTC+8)收到用户【{$userId}】{$nickname}的{$realUsdt}USDT转账",3);
  1003. DB::commit();
  1004. RedisService::clear($cacheKey);
  1005. $this->error = 2425;
  1006. return true;
  1007. }
  1008. /**
  1009. * 余额充值
  1010. * @param $userId
  1011. * @param $params
  1012. * @return array|false
  1013. */
  1014. public function recharge($userId, $params)
  1015. {
  1016. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1017. $usdt = isset($params['usdt'])? moneyFormat($params['usdt'], 2) : 0;
  1018. $coinType = isset($params['coin_type']) && $params['coin_type']? intval($params['coin_type']) : 2;
  1019. $payType = 10;
  1020. if($usdt<=0){
  1021. $this->error = 2031;
  1022. return false;
  1023. }
  1024. $cacheKey = "caches:member:recharge:lock_{$userId}";
  1025. if(RedisService::get($cacheKey)){
  1026. $this->error = 1034;
  1027. return false;
  1028. }
  1029. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  1030. ->select(['id','nickname','usdt','pay_password','balance','status'])
  1031. ->first();
  1032. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1033. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1034. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1035. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1036. if(empty($userInfo) || $status != 1){
  1037. $this->error = 2024;
  1038. return false;
  1039. }
  1040. if($userPayPassword != get_password($payPassword)){
  1041. $this->error = 2038;
  1042. return false;
  1043. }
  1044. // 充值订单
  1045. $orderNo = get_order_num('DR');
  1046. $money = $usdt;
  1047. // 星豆
  1048. DB::beginTransaction();
  1049. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1050. if($coinType == 2){
  1051. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1052. $xdPrice = $xdPrice>0 && $xdPrice <=10000? $xdPrice : 100;
  1053. $money = round($xdPrice * $usdt,2);
  1054. if($usdt > $userUsdt){
  1055. $this->error = 2035;
  1056. RedisService::clear($cacheKey);
  1057. return false;
  1058. }
  1059. // 扣除usdt余额
  1060. $updateData = ['usdt'=>DB::raw("usdt - {$usdt}"),'balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  1061. if(!$this->model->where(['id'=> $userId])->update($updateData)){
  1062. RedisService::clear($cacheKey);
  1063. $this->error = 2036;
  1064. DB::rollBack();
  1065. return false;
  1066. }
  1067. }
  1068. $data = [
  1069. 'order_no'=> $orderNo,
  1070. 'user_id'=> $userId,
  1071. 'type'=> 1,
  1072. 'user_type'=> 1,
  1073. 'coin_type'=> 2,
  1074. 'money'=> $usdt,
  1075. 'actual_money'=> $money,
  1076. 'pay_type'=> $payType,
  1077. 'pay_status'=> 20,
  1078. 'pay_at'=> date('Y-m-d H:i:s'),
  1079. 'create_time'=> time(),
  1080. 'update_time'=> time(),
  1081. 'status'=> 2,
  1082. 'mark'=> 1,
  1083. ];
  1084. if(!$orderId = BalanceLogModel::insertGetId($data)){
  1085. RedisService::clear($cacheKey);
  1086. $this->error = 2033;
  1087. DB::rollBack();
  1088. return false;
  1089. }
  1090. // 扣除USDT
  1091. $log = [
  1092. 'user_id'=> $userId,
  1093. 'source_order_no'=> $data['order_no'],
  1094. 'user_type'=> 1,
  1095. 'type'=> 5,
  1096. 'coin_type'=> 1,
  1097. 'money'=> -$usdt,
  1098. 'actual_money'=> -$usdt,
  1099. 'balance'=> $userUsdt,
  1100. 'date'=> date('Y-m-d'),
  1101. 'create_time'=> time(),
  1102. 'remark'=> '星豆余额充值扣除',
  1103. 'status'=>1,
  1104. 'mark'=>1
  1105. ];
  1106. if(!AccountLogModel::insertGetId($log)){
  1107. RedisService::clear($cacheKey);
  1108. $this->error = 2029;
  1109. DB::rollBack();
  1110. return false;
  1111. }
  1112. // 增加星豆余额
  1113. $log = [
  1114. 'user_id'=> $userId,
  1115. 'source_order_no'=> $data['order_no'],
  1116. 'user_type'=> 1,
  1117. 'type'=> 5,
  1118. 'coin_type'=> 2,
  1119. 'money'=> $usdt,
  1120. 'actual_money'=> $money,
  1121. 'balance'=> $balance,
  1122. 'date'=> date('Y-m-d'),
  1123. 'create_time'=> time(),
  1124. 'remark'=> '星豆余额充值',
  1125. 'status'=>1,
  1126. 'mark'=>1
  1127. ];
  1128. if(!AccountLogModel::insertGetId($log)){
  1129. RedisService::clear($cacheKey);
  1130. $this->error = 2029;
  1131. DB::rollBack();
  1132. return false;
  1133. }
  1134. // 消息
  1135. $dateTime = date('Y-m-d H:i:s');
  1136. MessageService::make()->pushMessage($userId,'星豆余额充值成功',"您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}星豆",3);
  1137. DB::commit();
  1138. // 充值星豆任务
  1139. TaskService::make()->updateTask($userId,9, 0);
  1140. $this->error = 2037;
  1141. RedisService::clear($cacheKey);
  1142. return ['order_id'=> $orderId,'balance'=> moneyFormat($balance + $money,2),'usdt'=> moneyFormat($userUsdt-$usdt, 2)];
  1143. }
  1144. /**
  1145. * 余额提现
  1146. * @param $userId
  1147. * @param $params
  1148. * @return array|false
  1149. */
  1150. public function withdraw($userId, $params)
  1151. {
  1152. $money = isset($params['money'])? floatval($params['money']) : 0;
  1153. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1154. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  1155. if($money<=0){
  1156. $this->error = 2401;
  1157. return false;
  1158. }
  1159. if(!in_array($userType,[1,2])){
  1160. $this->error = 2404;
  1161. return false;
  1162. }
  1163. $userInfo = $this->model->with(['merchant'])
  1164. ->where(['id'=> $userId,'mark'=>1])
  1165. ->select(['id','nickname','usdt','trc_url','pay_password','status'])
  1166. ->first();
  1167. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1168. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1169. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1170. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1171. $merchantUsdt = isset($merchant['usdt'])? $merchant['usdt'] : 0;
  1172. $merchantId = isset($merchant['id'])? $merchant['id'] : 0;
  1173. $merchantTradeStatus = isset($merchant['trade_status'])? $merchant['trade_status'] : 0;
  1174. if(empty($userInfo) || $status != 1){
  1175. $this->error = 2024;
  1176. return false;
  1177. }
  1178. if($userType == 2 && (empty($merchant) || !in_array($merchantTradeStatus,[1,2]))){
  1179. $this->error = 2024;
  1180. return false;
  1181. }
  1182. // 提现金额验证
  1183. $accountUsdt = $userType==1? $userUsdt : $merchantUsdt;
  1184. if ($money > $accountUsdt) {
  1185. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  1186. return false;
  1187. }
  1188. // 提现账户
  1189. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  1190. if (empty($trcUrl)) {
  1191. $this->error = 2403;
  1192. return false;
  1193. }
  1194. if($userPayPassword != get_password($payPassword)){
  1195. $this->error = 2038;
  1196. return false;
  1197. }
  1198. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  1199. if(RedisService::get($cacheKey)){
  1200. $this->error = 1034;
  1201. return false;
  1202. }
  1203. DB::beginTransaction();
  1204. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1205. // 提现记录
  1206. $orderNo = get_order_num('DW');
  1207. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
  1208. $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
  1209. $fee = round($money*$feeRate, 2);
  1210. $realUsdt = moneyFormat($money - $fee, 2);
  1211. $data = [
  1212. 'order_no'=> $orderNo,
  1213. 'user_id'=> $userType==1? $userId : $merchantId,
  1214. 'type'=> 2,
  1215. 'user_type'=> $userType,
  1216. 'coin_type'=> 1,
  1217. 'money'=> $money,
  1218. 'actual_money'=> $realUsdt,
  1219. 'fee'=> $fee,
  1220. 'trc_url'=> $trcUrl,
  1221. 'pay_type'=> 20,
  1222. 'date'=> date('Y-m-d'),
  1223. 'create_time'=> time(),
  1224. 'update_time'=> time(),
  1225. 'status'=> 1,
  1226. 'mark'=> 1,
  1227. ];
  1228. if(!$id = BalanceLogModel::insertGetId($data)){
  1229. DB::rollBack();
  1230. $this->error = 2405;
  1231. RedisService::clear($cacheKey);
  1232. return false;
  1233. }
  1234. // 商户扣款
  1235. if($userType == 2){
  1236. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1237. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1238. DB::rollBack();
  1239. $this->error = 2406;
  1240. RedisService::clear($cacheKey);
  1241. return false;
  1242. }
  1243. // 明细
  1244. $log = [
  1245. 'user_id' => $merchantId,
  1246. 'source_id' => $userId,
  1247. 'source_order_no' => $orderNo,
  1248. 'type' => 5,
  1249. 'coin_type' => 1,
  1250. 'user_type'=> 2,
  1251. 'money' => -$money,
  1252. 'actual_money' => -$money,
  1253. 'balance' => $merchantUsdt,
  1254. 'create_time' => time(),
  1255. 'update_time' => time(),
  1256. 'remark' => "商户账户余额提现",
  1257. 'status' => 1,
  1258. 'mark' => 1,
  1259. ];
  1260. if (!AccountLogModel::insertGetId($log)) {
  1261. $this->error = 2407;
  1262. DB::rollBack();
  1263. RedisService::clear($cacheKey);
  1264. return false;
  1265. }
  1266. }
  1267. // 用户扣款
  1268. else{
  1269. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1270. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1271. DB::rollBack();
  1272. $this->error = 2406;
  1273. RedisService::clear($cacheKey);
  1274. return false;
  1275. }
  1276. // 明细
  1277. $log = [
  1278. 'user_id' => $userId,
  1279. 'source_id' => 0,
  1280. 'source_order_no' => $orderNo,
  1281. 'type' => 5,
  1282. 'coin_type' => 1,
  1283. 'user_type'=> 1,
  1284. 'money' => -$money,
  1285. 'actual_money' => -$money,
  1286. 'balance' => $userUsdt,
  1287. 'create_time' => time(),
  1288. 'update_time' => time(),
  1289. 'remark' => "USDT余额提现",
  1290. 'status' => 1,
  1291. 'mark' => 1,
  1292. ];
  1293. if (!AccountLogModel::insertGetId($log)) {
  1294. $this->error = 2407;
  1295. DB::rollBack();
  1296. RedisService::clear($cacheKey);
  1297. return false;
  1298. }
  1299. }
  1300. DB::commit();
  1301. // 站内消息
  1302. $dateTime = date('Y-m-d H:i:s');
  1303. $title = $userType == 1 ? 'USDT余额提现申请成功' : '商户余额提现申请成功';
  1304. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额成功,请耐心等候审核!!!";
  1305. MessageService::make()->pushMessage($userId, $title, $message);
  1306. // 提现自动审核,低于该金额自动审核
  1307. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  1308. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  1309. if ($money <= $autoCheckUsdt) {
  1310. // 打款处理
  1311. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  1312. $txID = isset($result['txId']) ? $result['txId'] : '';
  1313. $payAddress = isset($result['address']) ? $result['address'] : '';
  1314. if ($txID && $payAddress) {
  1315. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  1316. if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
  1317. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  1318. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  1319. MessageService::make()->pushMessage($userId, $title, $message);
  1320. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  1321. // 平台明细
  1322. $log = [
  1323. 'user_id' => 0,
  1324. 'source_id' => $userId,
  1325. 'source_order_no' => $orderNo,
  1326. 'type' => 5,
  1327. 'coin_type' => 1,
  1328. 'user_type'=> 4,
  1329. 'money' => $fee,
  1330. 'actual_money' => $fee,
  1331. 'balance' => 0,
  1332. 'create_time' => time(),
  1333. 'update_time' => time(),
  1334. 'hash' => $txID,
  1335. 'remark' => "USDT余额提现",
  1336. 'status' => 1,
  1337. 'mark' => 1,
  1338. ];
  1339. AccountLogModel::insertGetId($log);
  1340. // 平台流水
  1341. FinanceService::make()->saveLog(0, $fee, 1);
  1342. }
  1343. }
  1344. }
  1345. $this->error = $title;
  1346. RedisService::clear($cacheKey);
  1347. return [
  1348. 'id'=> $id,
  1349. 'money'=> $money,
  1350. 'user_type'=> $userType,
  1351. ];
  1352. }
  1353. /**
  1354. * 获取点对点上级节点
  1355. * @param $userId 推荐人ID
  1356. * @return int|mixed
  1357. */
  1358. public function getPointParentId($userId)
  1359. {
  1360. if($userId<=0){
  1361. return 0;
  1362. }
  1363. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1364. $userList = $userList? $userList->toArray() : [];
  1365. if(count($userList) < 3){
  1366. return $userId;
  1367. }
  1368. $users = [];
  1369. foreach($userList as $item){
  1370. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1371. $points = array_filter($points);
  1372. $users[$item['id']]['pointLen'] = count($points);
  1373. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1374. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1375. }
  1376. //var_dump($users);
  1377. $pointId = 0;
  1378. $tempPointLen = 0;
  1379. $tempChildren = 0;
  1380. foreach ($userList as $item) {
  1381. $id = isset($item['id'])? $item['id'] : 0;
  1382. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1383. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1384. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1385. if($pointLen<=$tempPointLen || $pointId == 0){
  1386. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1387. if($children <$tempChildren || $pointId == 0){
  1388. $pointId = $id;
  1389. $tempPointLen = $pointLen;
  1390. $tempChildren = $children;
  1391. }
  1392. }
  1393. }
  1394. return $pointId;
  1395. }
  1396. /**
  1397. * DAO升级购买
  1398. * @param $userId 用户ID
  1399. * @param $params 参数:level-等级,pay_password-交易密码
  1400. * @return bool
  1401. */
  1402. public function upgrade($userId, $params)
  1403. {
  1404. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1405. $level = isset($params['level'])? intval($params['level']) : 0;
  1406. if($level<=0 || empty($payPassword)){
  1407. $this->error = 2014;
  1408. return false;
  1409. }
  1410. $cacheKey = "caches:members:upgrade:buy_{$userId}_{$level}";
  1411. if(RedisService::get($cacheKey.'_lock')){
  1412. $this->error = 1034;
  1413. return false;
  1414. }
  1415. $levelInfo = MemberLevelModel::from('member_level as a')
  1416. ->where(['a.id'=> $level,'a.status'=>1,'a.mark'=>1])
  1417. ->select(['a.*'])
  1418. ->first();
  1419. $upgradeUsdt = isset($levelInfo['upgrade_usdt'])? floatval($levelInfo['upgrade_usdt']) : 0;
  1420. $upgradeProfit = isset($levelInfo['upgrade_profit'])? floatval($levelInfo['upgrade_profit']) : 0;
  1421. $boxNum = isset($levelInfo['box_num'])? intval($levelInfo['box_num']) : 0;
  1422. $levelName = isset($levelInfo['name'])? $levelInfo['name'] : '';
  1423. if(empty($levelInfo)){
  1424. $this->error = 2511;
  1425. return false;
  1426. }
  1427. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  1428. ->select(['id','nickname','usdt','wait_score','parents','points','parent_id','member_level','power_num','trc_url','pay_password','status'])
  1429. ->first();
  1430. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1431. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1432. $userTotalProfit = isset($userInfo['upgrade_profit_total'])? $userInfo['upgrade_profit_total'] : 0;
  1433. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1434. $userWaitScore = isset($userInfo['wait_score'])? $userInfo['wait_score'] : 0;
  1435. $userPowerNum = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
  1436. $userLevel = isset($userInfo['member_level'])? intval($userInfo['member_level']) : 0;
  1437. if(empty($userInfo) || $status != 1){
  1438. $this->error = 2024;
  1439. return false;
  1440. }
  1441. if($userLevel>= $level){
  1442. $this->error = 2512;
  1443. return false;
  1444. }
  1445. if($level >= 30){
  1446. $this->error = 2513;
  1447. return false;
  1448. }
  1449. // 收益是否满足条件
  1450. if($upgradeProfit>0 && $userTotalProfit < $upgradeProfit){
  1451. $this->error = 2514;
  1452. return false;
  1453. }
  1454. if(AccountLogModel::where(['user_id'=> $userId,'source_id'=> $level,'type'=>2,'coin_type'=>1,'status'=>1,'mark'=>1])->value('id')){
  1455. $this->error = 2512;
  1456. return false;
  1457. }
  1458. // USDT余额是否足够
  1459. if($userUsdt < $upgradeUsdt){
  1460. $this->error = 2035;
  1461. return false;
  1462. }
  1463. // 交易密码
  1464. if(empty($userPayPassword)){
  1465. $this->error = 1040;
  1466. return false;
  1467. }
  1468. if($userPayPassword != get_password($payPassword)){
  1469. $this->error = 2038;
  1470. return false;
  1471. }
  1472. // 购买明细
  1473. RedisService::set($cacheKey, $params, rand(2,3));
  1474. $orderNo = get_order_num('UP');
  1475. DB::beginTransaction();
  1476. $log = [
  1477. 'user_id' => $userId,
  1478. 'source_id' => $level,
  1479. 'source_order_no' => $orderNo,
  1480. 'type' => 2,
  1481. 'coin_type' => 1,
  1482. 'user_type'=> 1,
  1483. 'money' => -$upgradeUsdt,
  1484. 'actual_money' => -$upgradeUsdt,
  1485. 'balance' => $userUsdt,
  1486. 'create_time' => time(),
  1487. 'update_time' => time(),
  1488. 'remark' => "购买{$levelName}升级",
  1489. 'status' => 1,
  1490. 'mark' => 1,
  1491. ];
  1492. if(!AccountLogModel::insertGetId($log)){
  1493. DB::rollBack();
  1494. $this->error = 2029;
  1495. RedisService::clear($cacheKey);
  1496. return false;
  1497. }
  1498. // 待返积分
  1499. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1500. $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
  1501. $waitScoreRate = ConfigService::make()->getConfigByCode('upgrade_award_score_rate', 200);
  1502. $waitScoreRate = $waitScoreRate>0 && $waitScoreRate<=1000? $waitScoreRate : 0;
  1503. $waitScore = moneyFormat($upgradeUsdt * $xdPrice * $waitScoreRate/100, 2);
  1504. if($waitScore>0){
  1505. $log = [
  1506. 'user_id' => $userId,
  1507. 'source_id' => $level,
  1508. 'source_order_no' => $orderNo,
  1509. 'type' => 9,
  1510. 'coin_type' => 5,
  1511. 'user_type'=> 1,
  1512. 'money' => $waitScore,
  1513. 'actual_money' => $waitScore,
  1514. 'balance' => $userWaitScore,
  1515. 'create_time' => time(),
  1516. 'update_time' => time(),
  1517. 'remark' => "购买{$levelName}升级",
  1518. 'status' => 1,
  1519. 'mark' => 1,
  1520. ];
  1521. if(!AccountLogModel::insertGetId($log)){
  1522. DB::rollBack();
  1523. $this->error = 2029;
  1524. RedisService::clear($cacheKey);
  1525. return false;
  1526. }
  1527. }
  1528. // 算力明细
  1529. $powerNum = $upgradeUsdt;
  1530. $log = [
  1531. 'user_id' => $userId,
  1532. 'source_id' => $level,
  1533. 'source_order_no' => $orderNo,
  1534. 'type' => 9,
  1535. 'coin_type' => 3,
  1536. 'user_type'=> 1,
  1537. 'money' => $powerNum,
  1538. 'actual_money' => $powerNum,
  1539. 'balance' => $userPowerNum,
  1540. 'create_time' => time(),
  1541. 'update_time' => time(),
  1542. 'remark' => "购买{$levelName}升级",
  1543. 'status' => 1,
  1544. 'mark' => 1,
  1545. ];
  1546. if(!AccountLogModel::insertGetId($log)){
  1547. DB::rollBack();
  1548. $this->error = 2029;
  1549. RedisService::clear($cacheKey);
  1550. return false;
  1551. }
  1552. // 账户
  1553. $updateData = [
  1554. 'usdt'=> DB::raw("usdt - {$upgradeUsdt}"), // 扣除USDT
  1555. 'member_level'=> $level, // 升级等级
  1556. 'box_num'=>DB::raw("box_num + {$boxNum}"), // 奖励盲盒数量
  1557. 'wait_score'=>DB::raw("wait_score + {$waitScore}"), // 待返积分奖励
  1558. 'power_num'=>DB::raw("power_num + {$powerNum}"), // 算力奖励
  1559. 'update_time'=>time()
  1560. ];
  1561. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  1562. DB::rollBack();
  1563. $this->error = 2036;
  1564. RedisService::clear($cacheKey);
  1565. return false;
  1566. }
  1567. // 消息
  1568. $dateTime = date('Y-m-d H:i:s');
  1569. MessageService::make()->pushMessage($userId,"购买{$levelName}升级成功","您在{$dateTime}(UTC+8)成功支付{$upgradeUsdt}USDT购买【{$levelName}】升级成功。",3);
  1570. DB::commit();
  1571. // 算力统计
  1572. AccountLogService::make()->saveCount($userId, $powerNum, 1);
  1573. // 业绩统计
  1574. AccountLogService::make()->saveCount($userId, $upgradeUsdt, 2);
  1575. // 结算收益奖励
  1576. FinanceService::make()->settleBonus($userId, $userInfo, $upgradeUsdt, $orderNo, "购买{$levelName}");
  1577. $this->error = 2503;
  1578. RedisService::clear($cacheKey);
  1579. return true;
  1580. }
  1581. }