MemberService.php 66 KB

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