MemberService.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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\MemberModel;
  18. use App\Models\MerchantModel;
  19. use App\Models\VideoModel;
  20. use App\Services\BaseService;
  21. use App\Services\CityService;
  22. use App\Services\ConfigService;
  23. use App\Services\EmailService;
  24. use App\Services\RedisService;
  25. use App\Services\SmsService;
  26. use App\Services\ToolService;
  27. use App\Services\UdunpayService;
  28. use Illuminate\Support\Facades\DB;
  29. use phpQrcode\QRcode;
  30. /**
  31. * 会员管理-服务类
  32. * @author laravel开发员
  33. * @since 2020/11/11
  34. * Class MemberService
  35. * @package App\Services\Api
  36. */
  37. class MemberService extends BaseService
  38. {
  39. // 静态对象
  40. protected static $instance = null;
  41. /**
  42. * 构造函数
  43. * @author laravel开发员
  44. * @since 2020/11/11
  45. * MemberService constructor.
  46. */
  47. public function __construct()
  48. {
  49. $this->model = new MemberModel();
  50. }
  51. /**
  52. * 静态入口
  53. * @return MemberService|static|null
  54. */
  55. public static function make()
  56. {
  57. if (!self::$instance) {
  58. self::$instance = new static();
  59. }
  60. return self::$instance;
  61. }
  62. /**
  63. * 获取资料详情
  64. * @param $where
  65. * @param array $field
  66. */
  67. public function getInfo($where, $type = 'detail', array $field = [])
  68. {
  69. $defaultField = ['id', 'username','email', 'realname', 'mobile','gender', 'nickname', 'code','supper_point', 'parent_id','province_id','city_id','signature','power_num','trade_state','trc_url','recharge_trc_url', 'point_id', 'balance','trx','usdt','wait_score', 'score','pay_password','realname','idcard_check', 'member_level', 'status', 'avatar'];
  70. if ($type == 'team') {
  71. $defaultField = ['id', 'username','email', 'nickname', 'code', 'balance', 'score','point_id','power_num','parent_id', 'status', 'avatar'];
  72. }else if($type == 'share'){
  73. $defaultField = ['id', 'username', 'nickname', 'code', 'balance','usdt','trx','trc_url', 'score', 'status', 'avatar'];
  74. }else if($type == 'check'){
  75. $defaultField = ['id', 'username', 'nickname', 'code', 'balance','usdt','trx','trc_url','wait_score', 'score', 'status', 'avatar'];
  76. }
  77. $field = $field ? $field : $defaultField;
  78. if (is_array($where)) {
  79. $info = $this->model->with(['merchant','acceptor','parent','point','level'])->where('mark',1)->where($where)->select($field)->first();
  80. } else {
  81. $info = $this->model->with(['merchant','acceptor','parent','point','level'])->where('mark',1)->where(['id' => (int)$where])->select($field)->first();
  82. }
  83. $info = $info ? $info->toArray() : [];
  84. if ($info && !in_array($type, ['auth', 'check','login'])) {
  85. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  86. // 二维码
  87. if (in_array($type, ['detail','share'])) {
  88. if (isset($info['wxpay_qrcode'])) {
  89. $info['wxpay_qrcode'] = $info['wxpay_qrcode'] ? get_image_url($info['wxpay_qrcode']) : '';
  90. }
  91. if (isset($info['alipay_qrcode'])) {
  92. $info['alipay_qrcode'] = $info['alipay_qrcode'] ? get_image_url($info['alipay_qrcode']) : '';
  93. }
  94. $inviteUrl = get_web_url('/#/pages/register/index?code=' . $info['code']);
  95. $qrcode = $this->makeQrcode($inviteUrl);
  96. $info['qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  97. $info['invite_url'] = $inviteUrl;
  98. $info['qrcode_base64'] = $qrcode?getImageContent($qrcode):'';
  99. $info['parent'] = $info['parent']? $info['parent'] : ['id'=>0,'nickname' => '无', 'code' => '无'];
  100. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  101. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  102. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  103. }
  104. else if ($type == 'info') {
  105. $liveOpenLevel = ConfigService::make()->getConfigByCode('live_open_level',0);
  106. $liveOpenLevel = $liveOpenLevel>0? $liveOpenLevel : 0;
  107. $info['live_open'] = $info['member_level']>=$liveOpenLevel? 1 : 0;
  108. }
  109. else if ($type == 'team') {
  110. $info['team_count1'] = $this->getUserCountByType($info['id'], 1, 0); // 直推人数
  111. $info['team_count2'] = $this->getUserCountByType($info['id'], 3, 0); // 滑落人数
  112. $info['team_count'] = $this->getUserCountByType($info['id'], 1, 1); // 节点推荐人数
  113. }else {
  114. // 默认地址
  115. $info['addressData'] = MemberAddressService::make()->getBindInfo($info['id']);
  116. }
  117. }
  118. return $info;
  119. }
  120. /**
  121. * 获取主页信息
  122. * @param $where
  123. * @param array $field
  124. */
  125. public function getHomeInfo($id, $userId=0)
  126. {
  127. $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'];
  128. $info = $this->model->where('mark',1)->where(['id' => $id])->select($field)->first();
  129. $info = $info ? $info->toArray() : [];
  130. if ($info) {
  131. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  132. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  133. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  134. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  135. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  136. $info['is_fans'] = (int)MemberCollectService::make()->checkCollect($info['id'], $userId,1); // 是否我的粉丝
  137. $info['live_id'] = (int)LiveModel::where(['user_id'=> $info['id'],'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id'); // 是否正在直播
  138. }
  139. return $info;
  140. }
  141. /**
  142. * 获取用户缓存信息
  143. * @param $where
  144. * @param array $field
  145. * @param int $expired
  146. * @return array|mixed
  147. */
  148. public function getCacheInfo($where, $field = [], $expired = 0)
  149. {
  150. $cacheKey = "caches:member:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  151. $info = RedisService::get($cacheKey);
  152. if ($info) {
  153. return $info;
  154. }
  155. $defaultField = ['id', 'username', 'realname', 'mobile', 'nickname', 'code', 'parent_id', 'openid', 'balance', 'score', 'member_level', 'status', 'avatar'];
  156. $field = $field ? $field : $defaultField;
  157. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  158. $info = $info ? $info->toArray() : [];
  159. if ($info) {
  160. if (isset($info['avatar'])) {
  161. $info['avatar_preview'] = get_image_url($info['avatar']);
  162. }
  163. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  164. }
  165. return $info;
  166. }
  167. /**
  168. * 生成普通参数二维码
  169. * @param $str 参数
  170. * @param bool $refresh 是否重新生成
  171. * @return bool
  172. */
  173. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  174. {
  175. $basePath = base_path() . '/public';
  176. $qrFile = '/images/qrcode/';
  177. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  178. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  179. }
  180. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  181. $qrFile = $qrFile . "C_{$key}.png";
  182. $cacheKey = "caches:qrcodes:member_" . $key;
  183. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  184. return $qrFile;
  185. }
  186. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  187. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  188. return false;
  189. }
  190. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  191. return $qrFile;
  192. }
  193. /**
  194. * 获取团队用户数量
  195. * @param $userId 当前用户ID
  196. * @param int $type 层级:1-直推,2-团队
  197. * @param int $isPoint 节点:1-滑落推荐,0-直接推荐
  198. * @return array|mixed
  199. */
  200. public function getUserCountByType($userId, $type = 1, $isPoint=0)
  201. {
  202. $cacheKey = "caches:teams:count_{$userId}_{$type}_{$isPoint}";
  203. $data = RedisService::get($cacheKey);
  204. if ($data) {
  205. return $data;
  206. }
  207. $where = ['status' => 1, 'mark' => 1];
  208. $data = $this->model->where($where)
  209. ->where(function ($query) use ($type, $isPoint, $userId) {
  210. if($isPoint){
  211. if ($type == 1) { // 滑落推荐
  212. $query->where('point_id', $userId);
  213. } else if($type == 2){ // 滑落团队人数
  214. $query->whereRaw('FIND_IN_SET(?,points)', $userId);
  215. }
  216. }else{
  217. if ($type == 1) { // 直推人数
  218. $query->where('parent_id', $userId);
  219. } else if($type == 2){ // 团队人数
  220. $query->whereRaw('FIND_IN_SET(?,parents)', $userId);
  221. } else if($type == 3){ // 滑落人数
  222. $query->where('parent_id', $userId)->where('point_id','!=', $userId);
  223. }
  224. }
  225. })->count('id');
  226. if ($data) {
  227. RedisService::set($cacheKey, $data, rand(3, 5));
  228. }
  229. return $data;
  230. }
  231. /**
  232. * 分销用户列表
  233. * @param $userId
  234. * @param $params
  235. * @param int $pageSize
  236. * @return array
  237. */
  238. public function getTeamList($userId, $params, $pageSize=15)
  239. {
  240. $page = request()->post('page',1);
  241. $cacheKey = "caches:teams:list_{$userId}_{$page}";
  242. $datas = RedisService::get($cacheKey);
  243. if ($datas) {
  244. return [
  245. 'pageSize' => $pageSize,
  246. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  247. 'list' => isset($datas['data']) ? $datas['data'] : []
  248. ];
  249. }
  250. $where = ['status' => 1, 'mark' => 1];
  251. $field = ['id', 'nickname', 'username', 'email', 'balance', 'parent_id','create_time', 'status'];
  252. $datas = $this->model->where($where)
  253. ->where(function ($query) use ($userId) {
  254. $query->where('parent_id', $userId);
  255. })
  256. ->where(function($query) use($params){
  257. $kw = isset($params['kw'])? trim($params['kw']) : '';
  258. if($kw){
  259. $query->where('id','=', "%{$kw}%")->orWhere('email','like',"%{$kw}%")->orWhere('username','like',"%{$kw}%");
  260. }
  261. })
  262. ->select($field)
  263. ->orderBy('create_time', 'desc')
  264. ->orderBy('id', 'desc')
  265. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  266. $datas = $datas ? $datas->toArray() : [];
  267. if ($datas) {
  268. foreach ($datas['data'] as &$item) {
  269. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'y-m-d H:i') : '';
  270. $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
  271. }
  272. unset($item);
  273. RedisService::set($cacheKey, $datas, rand(5, 10));
  274. }
  275. return [
  276. 'pageSize' => $pageSize,
  277. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  278. 'list' => isset($datas['data']) ? $datas['data'] : []
  279. ];
  280. }
  281. /**
  282. * 手机号是否已注册
  283. * @param $mobile
  284. * @return mixed
  285. */
  286. public function checkPhoneExists($mobile)
  287. {
  288. return parent::checkExists('mobile', $mobile); // TODO: Change the autogenerated stub
  289. }
  290. /**
  291. * 账号注册
  292. * @param $params
  293. * @return array|false
  294. */
  295. public function register($params)
  296. {
  297. // 授权
  298. $email = isset($params['email']) ? trim($params['email']) : '';
  299. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  300. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  301. $code = isset($params['code']) ? trim($params['code']) : '';
  302. $scene = isset($params['scene']) ? $params['scene'] : '';
  303. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  304. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  305. if (empty($params) || empty($email) || $code <= 0) {
  306. $this->error = 2014;
  307. return false;
  308. }
  309. // 验证码
  310. if (!EmailService::make()->check($email, $code, $scene)) {
  311. $this->error = EmailService::make()->getError();
  312. return false;
  313. }
  314. // 验证是否注册
  315. if($this->model->where(['email'=> $email,'mark'=>1])->value('id')){
  316. $this->error = 2009;
  317. return false;
  318. }
  319. // 推荐人
  320. $parentId = 0;
  321. $pointId = 0;
  322. $parents = '';
  323. $points = '';
  324. if ($inviteCode) {
  325. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  326. ->select(['id', 'parents', 'parent_id'])
  327. ->first();
  328. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  329. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  330. // 挂靠节点
  331. if($parentId>0){
  332. $pointId = $this->getPointParentId($parentId);
  333. if($pointId){
  334. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  335. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  336. }
  337. }
  338. }
  339. // 注册
  340. $id = $this->model->max('id') + 1;
  341. $system = isset($params['system'])? $params['system'] : [];
  342. $ip = get_client_ip();
  343. $ipData = ToolService::make()->getIpAddress($ip,'');
  344. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  345. $city = isset($ipData['city'])? $ipData['city'] : '';
  346. $emailName = explode('@', $email)[0];
  347. // 钱包
  348. $data = [
  349. 'parent_id' => $parentId,
  350. 'point_id' => $pointId,
  351. 'points' => $points,
  352. 'openid' => '',
  353. 'avatar' => '',
  354. 'nickname' => $nickname ? $nickname : '用户'.rand(100,999).$emailName,
  355. 'username' => $email,
  356. 'password' => get_password('xl123456'),
  357. 'code' => strtoupper(get_random_code(9, 'X', "{$id}")),
  358. 'email' => $email,
  359. 'mobile' => $mobile,
  360. 'recharge_trc_url' => UdunpayService::make()->createAddress(195),
  361. 'parents' => $parents,
  362. 'province_id' => CityService::make()->getFieldByName($province),
  363. 'city_id' => CityService::make()->getFieldByName($city),
  364. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  365. 'device' => $appSources == 'android' ? 2 : 1,
  366. 'status' => 1,
  367. 'balance' => 0,
  368. 'score' => 0,
  369. 'mark' => 1,
  370. 'login_ip' => $ip,
  371. 'create_time' => time(),
  372. ];
  373. $userId = $this->model->insertGetId($data);
  374. $this->error = 2008;
  375. return [
  376. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  377. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  378. ];
  379. }
  380. /**
  381. * 邮箱登录
  382. * @param $params
  383. * @return array|false
  384. */
  385. public function loginByEmail($params)
  386. {
  387. // 授权
  388. $email = isset($params['email']) ? $params['email'] : '';
  389. $code = isset($params['code']) ? $params['code'] : '';
  390. $scene = isset($params['scene']) ? $params['scene'] : '';
  391. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  392. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  393. $this->error = 2014;
  394. return false;
  395. }
  396. // 验证码
  397. if (!EmailService::make()->check($email, $code, $scene)) {
  398. $this->error = EmailService::make()->getError();
  399. return false;
  400. }
  401. // 验证是否注册,没有则注册
  402. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  403. $data = $data ? $data->toArray() : [];
  404. $userId = isset($data['id']) ? $data['id'] : 0;
  405. $status = isset($data['status']) ? $data['status'] : 0;
  406. if(empty($data) || $userId<=0){
  407. $this->error = 2001;
  408. return false;
  409. }
  410. if ($status != 1) {
  411. $this->error = 2015;
  412. return false;
  413. }
  414. // 获取登录授权token
  415. $jwt = new Jwt('jwt_dys_app');
  416. $token = $jwt->getToken($userId);
  417. // 结果返回
  418. $result = [
  419. 'access_token' => $token,
  420. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  421. ];
  422. // 用户信息
  423. $ip = get_client_ip();
  424. $updateData = [
  425. 'login_time' => time(),
  426. 'login_ip' => $ip,
  427. ];
  428. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  429. $ipData = ToolService::make()->getIpAddress($ip,'');
  430. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  431. $city = isset($ipData['city'])? $ipData['city'] : '';
  432. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  433. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  434. }
  435. $this->model->where(['id'=> $userId])->update($updateData);
  436. $this->error = 2004;
  437. $data['token'] = $token;
  438. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  439. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  440. return $result;
  441. }
  442. /**
  443. * 手机号登录
  444. * @param $params
  445. * @return array|false
  446. */
  447. public function loginByMobile($params)
  448. {
  449. // 授权
  450. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  451. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  452. $smsType = isset($params['sms_type']) ? $params['sms_type'] : '';
  453. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  454. $openid = isset($params['openid']) ? $params['openid'] : '';
  455. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  456. $regType = isset($params['type']) ? $params['type'] : 'app';
  457. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  458. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  459. if (empty($params) || empty($mobile) || $code <= 0) {
  460. $this->error = 2014;
  461. return false;
  462. }
  463. // 验证码
  464. if (!SmsService::make()->check($mobile, $code, $smsType?$smsType:'login')) {
  465. $this->error = SmsService::make()->getError();
  466. return false;
  467. }
  468. // 推荐人
  469. $parentId = 0;
  470. $parents = '';
  471. if ($inviteCode) {
  472. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  473. ->select(['id', 'parents', 'parent_id'])
  474. ->first();
  475. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  476. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] . $parentId . ',' : ($parentId ? $parentId . ',' : '');
  477. }
  478. // 验证是否注册,没有则注册
  479. $data = $this->model->with(['merchant','mechanic'])->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'openid', 'username', 'parent_id', 'code', 'push_cid', 'status'])->first();
  480. $data = $data ? $data->toArray() : [];
  481. $userId = isset($data['id']) ? $data['id'] : 0;
  482. $status = isset($data['status']) ? $data['status'] : 0;
  483. if ($userId && $status != 1) {
  484. $this->error = 2015;
  485. return false;
  486. }
  487. // 是否是商家或技师,有订单提醒
  488. $noticeUser = ['id'=> 0,'uid'=>$userId,'type'=>1];
  489. $merchant = isset($data['merchant'])? $data['merchant'] : [];
  490. $merchantStatus = isset($merchant['status'])? $merchant['status'] : 0;
  491. $mechanic = isset($data['mechanic'])? $data['mechanic'] : [];
  492. $mechanicStatus = isset($mechanic['status'])? $mechanic['status'] : 0;
  493. if($merchantStatus==2){
  494. $uid = isset($merchant['id'])? $merchant['id'] : 0;
  495. if($uid>0){
  496. $noticeUser = ['id'=>$uid,'type'=>2,'uid'=> $userId];
  497. }
  498. }else if($mechanicStatus == 2){
  499. $uid = isset($mechanic['id'])? $mechanic['id'] : 0;
  500. if($uid>0){
  501. $noticeUser = ['id'=>$uid,'type'=>3,'uid'=>$userId];
  502. }
  503. }
  504. $pushCid = isset($params['push_cid']) ? $params['push_cid'] : '';
  505. if (!$userId) {
  506. $id = $this->model->max('id') + 1;
  507. $data = [
  508. 'parent_id' => $parentId,
  509. 'openid' => $openid,
  510. 'avatar' => $avatar,
  511. 'nickname' => $nickname ? $nickname : get_random_code(4, '用户u').substr($mobile,-4,4),
  512. 'username' => get_username($id),
  513. 'password' => get_password('123456'),
  514. 'code' => strtoupper(get_random_code(8, 'D', "{$id}")),
  515. 'mobile' => $mobile,
  516. 'parents' => $parents,
  517. 'push_cid' => isset($params['push_cid']) ? $params['push_cid'] : '',
  518. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  519. 'device' => $appSources == 'android' ? 2 : 1,
  520. 'status' => 1,
  521. 'balance' => 0,
  522. 'score' => 0,
  523. 'mark' => 1,
  524. 'login_time' => time(),
  525. 'login_ip' => get_client_ip(),
  526. 'create_time' => time(),
  527. ];
  528. $userId = $this->model->insertGetId($data);
  529. } elseif ($regType != 'h5') {
  530. // 登录更新
  531. $updateData = [
  532. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  533. 'login_count' => DB::raw("login_count+1"),
  534. 'login_time' => time(),
  535. 'login_ip' => get_client_ip(),
  536. 'update_time' => time(),
  537. ];
  538. if ($pushCid) {
  539. $updateData['push_cid'] = $pushCid;
  540. }
  541. $this->model->where(['id' => $userId])->update($updateData);
  542. }
  543. // H5 邀请注册,无需登录
  544. if ($regType == 'h5') {
  545. $this->error = $status ? 2023 : 2022;
  546. return [
  547. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  548. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'username' => $data['username']],
  549. ];
  550. }
  551. // 获取登录授权token
  552. $jwt = new Jwt('jwt_dys_app');
  553. $token = $jwt->getToken($userId);
  554. // 结果返回
  555. $result = [
  556. 'access_token' => $token,
  557. 'info' => ['uid' => $userId,'notice_user'=>$noticeUser, 'openid' => $data['openid'], 'username' => $data['username']],
  558. ];
  559. // 用户信息
  560. $this->error = 2004;
  561. $data['token'] = $token;
  562. unset($data['password']);
  563. // 实现初始化账单明细表
  564. AccountLogModel::where('id','<=',1)->value('id');
  565. RedisService::clear( "caches:map:{$userId}");
  566. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  567. return $result;
  568. }
  569. /**
  570. * 修改头像/商家logo/技师头像
  571. * @param $userId
  572. * @param $avatar
  573. * @return mixed
  574. */
  575. public function saveAvatar($userId, $avatar)
  576. {
  577. $type = request()->post('type', 1);
  578. if($type == 1){
  579. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  580. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  581. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  582. @unlink(ATTACHMENT_PATH . $oldAvatar);
  583. }
  584. return true;
  585. }
  586. }else if($type == 2){
  587. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  588. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $avatar, 'update_time' => time()])) {
  589. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  590. @unlink(ATTACHMENT_PATH . $oldAvatar);
  591. }
  592. return true;
  593. }
  594. }else if($type == 3){
  595. $oldAvatar = MechanicModel::where(['user_id' => $userId])->value('avatar');
  596. if (MechanicModel::where(['user_id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  597. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  598. @unlink(ATTACHMENT_PATH . $oldAvatar);
  599. }
  600. return true;
  601. }
  602. }
  603. return false;
  604. }
  605. /**
  606. * 修改账号
  607. * @param $userId
  608. * @param $params
  609. * @return bool
  610. */
  611. public function modify($userId, $params)
  612. {
  613. $username = isset($params['username']) ? $params['username'] : '';
  614. $password = isset($params['password']) ? $params['password'] : '';
  615. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  616. // 用户验证
  617. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id', 'status'])->first();
  618. if (!$info) {
  619. $this->error = 2001;
  620. return false;
  621. }
  622. // 使用状态校验
  623. if ($info['status'] != 1) {
  624. $this->error = 2009;
  625. return false;
  626. }
  627. // 密码校验
  628. $data = ['update_time' => time()];
  629. if (isset($params['password']) && $password) {
  630. $password = get_password($password);
  631. $data['password'] = $password;
  632. if ($password != $info['password']) {
  633. $this->error = 2002;
  634. return false;
  635. }
  636. }
  637. // 用户账号验证
  638. if (isset($params['username']) && $username) {
  639. $data['username'] = $username;
  640. $checkInfo = $this->model->where(['username' => $username, 'mark' => 1])->select(['id', 'status'])->first();
  641. if ($checkInfo && $checkInfo['id'] != $userId) {
  642. $this->error = 2005;
  643. return false;
  644. }
  645. }
  646. // 手机号验证
  647. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  648. if (isset($params['mobile']) && $mobile) {
  649. $data['mobile'] = $mobile;
  650. $checkInfo = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'status'])->first();
  651. if ($checkInfo && $checkInfo['id'] != $userId) {
  652. $this->error = 2009;
  653. return false;
  654. }
  655. // 短信验证码
  656. $code = isset($params['code']) ? $params['code'] : '';
  657. if (empty($code)) {
  658. $this->error = 2013;
  659. return false;
  660. }
  661. if (!SmsService::make()->check($mobile, $code, 'modify')) {
  662. $this->error = SmsService::make()->getError();
  663. return false;
  664. }
  665. }
  666. // 修改数据
  667. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  668. if (isset($params['nickname']) && $nickname) {
  669. $data['nickname'] = $nickname;
  670. }
  671. $sex = isset($params['sex']) ? $params['sex'] : 0;
  672. if (isset($params['sex']) && $sex) {
  673. $data['sex'] = $sex;
  674. }
  675. $intro = isset($params['intro']) ? $params['intro'] : '';
  676. if (isset($params['intro']) && $intro) {
  677. $data['intro'] = $intro;
  678. }
  679. if (isset($params['pay_password']) && $payPassword) {
  680. $payPassword = get_password($payPassword);
  681. $data['pay_password'] = $payPassword;
  682. }
  683. $this->model->where(['id' => $userId])->update($data);
  684. $this->error = 1008;
  685. return true;
  686. }
  687. /**
  688. * 身份认证
  689. * @param $userId
  690. * @param $params
  691. * @return bool
  692. */
  693. public function auth($userId, $params)
  694. {
  695. // 用户验证
  696. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  697. if (!$info) {
  698. $this->error = 2001;
  699. return false;
  700. }
  701. // 使用状态校验
  702. if ($info['status'] != 1) {
  703. $this->error = 2009;
  704. return false;
  705. }
  706. // 密码校验
  707. $data = ['update_time' => time(),'idcard_check'=>1];
  708. if (isset($params['realname']) && $params['realname']) {
  709. $data['realname'] = trim($params['realname']);
  710. }
  711. $idcard = isset($params['idcard']) && $params['idcard'];
  712. if ($idcard) {
  713. $data['idcard'] = trim($params['idcard']);
  714. }
  715. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  716. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  717. }
  718. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  719. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  720. }
  721. $this->model->where(['id' => $userId])->update($data);
  722. // 发送消息
  723. $params = [
  724. 'title'=> "身份认证提交成功",
  725. 'body' => "您的身份认证信息已提交成功,请耐心等候审核",
  726. 'type' => 1, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  727. 'content' => [
  728. 'account' => ['name' => '账号', 'text' => $info['username']],
  729. 'pay_time' => ['name' => '申请时间', 'text' => date('Y-m-d H:i:s')],
  730. 'status' => ['name' => '状态', 'text' => '待审核'],
  731. ],
  732. 'click_type' => 'payload',
  733. 'url' => '/pages/my/profile/auth',
  734. ];
  735. PushService::make()->pushMessageByUser($userId, $params, 0);
  736. $this->error = 2036;
  737. return true;
  738. }
  739. /**
  740. * 转账
  741. * @param $userId
  742. * @param $params
  743. * @return bool
  744. */
  745. public function transfer($userId, $params)
  746. {
  747. $account = isset($params['account'])? trim($params['account']) : '';
  748. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  749. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  750. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  751. $money = isset($params['money'])? floatval($params['money']) : 0;
  752. if(empty($account) || $money<=0){
  753. $this->error = 2501;
  754. return false;
  755. }
  756. $cachekey = "caches:members:transfer:{$userId}";
  757. if(RedisService::get($cachekey)){
  758. $this->error = 1034;
  759. return false;
  760. }
  761. // 验证账户
  762. $userInfo = $this->model->with(['merchant'])
  763. ->where(['id'=> $userId,'mark'=>1])
  764. ->select(['id','nickname','username','pay_password','balance','status'])
  765. ->first();
  766. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  767. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  768. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  769. $username = isset($userInfo['username'])? $userInfo['username'] : '';
  770. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  771. $merchId = isset($merchant['id'])? $merchant['id'] : 0;
  772. $merchName = isset($merchant['name'])? $merchant['name'] : '';
  773. $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
  774. if(empty($userInfo) || $status != 1){
  775. $this->error = 2024;
  776. return false;
  777. }
  778. // 余额支付支付密码验证
  779. if(empty($userPayPassword)){
  780. $this->error ='1039';
  781. return false;
  782. }
  783. $payPassword = get_password($payPassword);
  784. if($payPassword != $userPayPassword){
  785. $this->error ='1038';
  786. return false;
  787. }
  788. // 验证账户
  789. $accountInfo = $this->model->where(['username'=> $account,'mark'=>1])
  790. ->select(['id','nickname','username','balance','status'])
  791. ->first();
  792. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  793. $accountBalance = isset($accountInfo['balance'])? $accountInfo['balance'] : 0;
  794. if(empty($accountInfo) || $status != 1){
  795. $this->error = 2025;
  796. return false;
  797. }
  798. if($accountInfo['id'] == $userId && $type == 1){
  799. $this->error = 2645;
  800. return false;
  801. }
  802. // 用户余额转账到用户
  803. DB::beginTransaction();
  804. if($type == 1){
  805. if($money>$balance){
  806. $this->error = 2026;
  807. return false;
  808. }
  809. // 扣除账户余额
  810. RedisService::set($cachekey, $params, rand(2,5));
  811. $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
  812. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  813. DB::rollBack();
  814. $this->error = 2028;
  815. RedisService::clear($cachekey);
  816. return false;
  817. }
  818. }
  819. // 商户余额转账到用户
  820. else if($type == 2){
  821. if($money>$merchBalance){
  822. $this->error = 2027;
  823. return false;
  824. }
  825. //
  826. if($merchId<=0){
  827. $this->error = 2018;
  828. return false;
  829. }
  830. // 扣除商户余额
  831. RedisService::set($cachekey, $params, rand(2,5));
  832. $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
  833. if(!MerchantModel::where(['id'=> $merchId,'mark'=>1])->update($updateData)){
  834. DB::rollBack();
  835. $this->error = 2028;
  836. RedisService::clear($cachekey);
  837. return false;
  838. }
  839. }
  840. // 到账
  841. $updateData = ['balance'=> DB::raw("balance + {$money}"),'update_time'=> time()];
  842. if(!$this->model->where(['id'=> $accountInfo['id'],'mark'=>1])->update($updateData)){
  843. DB::rollBack();
  844. $this->error = 2028;
  845. RedisService::clear($cachekey);
  846. return false;
  847. }
  848. // 明细
  849. $log = [
  850. 'user_id' => $userId,
  851. 'merch_id' => $merchId,
  852. 'source_uid' => $accountInfo['id'],
  853. 'source_order_no' => '',
  854. 'type' => 6,
  855. 'coin_type' => 4,
  856. 'user_type'=> $type==2? 2 : ($userType!=2? $userType : 1),
  857. 'money' => -$money,
  858. 'balance' => $type==2? $merchBalance : $balance,
  859. 'create_time' => time(),
  860. 'update_time' => time(),
  861. 'remark' => $type==2? "商户余额转账给【{$account}】": "账户余额转账给【{$account}】",
  862. 'status' => 1,
  863. 'mark' => 1,
  864. ];
  865. if(!AccountLogModel::insertGetId($log)){
  866. DB::rollBack();
  867. $this->error = 2029;
  868. RedisService::clear($cachekey);
  869. return false;
  870. }
  871. // 到账明细
  872. $log = [
  873. 'user_id' => $accountInfo['id'],
  874. 'merch_id' => $merchId,
  875. 'source_uid' => $userId,
  876. 'source_order_no' => '',
  877. 'type' => 6,
  878. 'coin_type' => 4,
  879. 'user_type'=> 1,
  880. 'money' => $money,
  881. 'balance' => $accountBalance,
  882. 'create_time' => time(),
  883. 'update_time' => time(),
  884. 'remark' => $type==2? "来自商家【{$merchName}】的转账": "来自".($userType==3?'技师':($userType==4?'代理':'用户'))."【{$username}】的转账",
  885. 'status' => 1,
  886. 'mark' => 1,
  887. ];
  888. if(!AccountLogModel::insertGetId($log)){
  889. DB::rollBack();
  890. $this->error = 2029;
  891. RedisService::clear($cachekey);
  892. return false;
  893. }
  894. // 发送消息
  895. $params = [
  896. 'title'=> "转账成功通知",
  897. 'body' => $type == 2? "收到一笔来自商家的转账":"收到一笔来自用户的转账",
  898. 'type' => 2, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  899. 'content' => [
  900. 'account' => ['name' => $type==2?'来自商家':'来自用户', 'text' => $type==2? $merchName : $userInfo['username']],
  901. 'pay_time' => ['name' => '到账时间', 'text' => date('Y-m-d H:i:s')],
  902. 'money' => ['name' => '到账金额', 'text' => $money],
  903. 'status' => ['name' => '状态', 'text' => '已到账'],
  904. ],
  905. 'click_type' => 'payload',
  906. 'url' => '/pages/account/index',
  907. ];
  908. if(!PushService::make()->pushMessageByUser($accountInfo['id'], $params, 0)){
  909. DB::rollBack();
  910. $this->error = PushService::make()->getError();
  911. RedisService::clear($cachekey);
  912. return false;
  913. }
  914. DB::commit();
  915. $this->error = 2030;
  916. return true;
  917. }
  918. /**
  919. * 余额充值
  920. * @param $userId
  921. * @param $params
  922. * @return array|false
  923. */
  924. public function recharge($userId, $params)
  925. {
  926. $money = isset($params['money'])? $params['money'] : 0;
  927. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  928. if($money<=0){
  929. $this->error = 2031;
  930. return false;
  931. }
  932. if(!in_array($payType, [10,20])){
  933. $this->error = 2032;
  934. return false;
  935. }
  936. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  937. ->select(['id','nickname','balance','status'])
  938. ->first();
  939. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  940. if(empty($userInfo) || $status != 1){
  941. $this->error = 2024;
  942. return false;
  943. }
  944. // 充值订单
  945. $orderNo = get_order_num('DR');
  946. $data = [
  947. 'order_no'=> $orderNo,
  948. 'user_id'=> $userId,
  949. 'type'=> 1,
  950. 'user_type'=> 1,
  951. 'money'=> $money,
  952. 'gift_money'=> 0,
  953. 'pay_type'=> $payType,
  954. 'create_time'=> time(),
  955. 'update_time'=> time(),
  956. 'status'=> 1,
  957. 'mark'=> 1,
  958. ];
  959. if(!$orderId = BalanceLogModel::insertGetId($data)){
  960. $this->error = 2033;
  961. return false;
  962. }
  963. // 支付方式
  964. $order = [
  965. 'order_no'=> $orderNo,
  966. 'type'=> 0,
  967. 'pay_type'=> $payType,
  968. 'pay_money'=> $money,
  969. 'body'=> '充值订单支付',
  970. ];
  971. switch($payType){
  972. case 20: // 支付宝
  973. $payment = PaymentService::make()->aliPay($userInfo, $order,'recharge');
  974. if(empty($payment)){
  975. DB::rollBack();
  976. $this->error = PaymentService::make()->getError();
  977. return false;
  978. }
  979. break;
  980. case 10: // 微信支付
  981. $payment = PaymentService::make()->wechatPay($userInfo, $order,'recharge');
  982. if(empty($payment)){
  983. DB::rollBack();
  984. $this->error = PaymentService::make()->getError();
  985. return false;
  986. }
  987. break;
  988. default:
  989. $this->error = 1030;
  990. return false;
  991. }
  992. $this->error = 2034;
  993. return [
  994. 'id'=> $orderId,
  995. 'payment'=> $payment,
  996. 'total'=> $money,
  997. 'order_no'=> $orderNo,
  998. 'pay_type'=> $payType,
  999. ];
  1000. }
  1001. /**
  1002. * 余额提现
  1003. * @param $userId
  1004. * @param $params
  1005. * @return array|false
  1006. */
  1007. public function withdraw($userId, $params)
  1008. {
  1009. $money = isset($params['money'])? floatval($params['money']) : 0;
  1010. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1011. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 10;
  1012. $bankId = isset($params['bank_id']) && $params['bank_id']? intval($params['bank_id']) : 0;
  1013. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  1014. if($money<=0){
  1015. $this->error = 2031;
  1016. return false;
  1017. }
  1018. if(!in_array($payType, [10,20,50])){
  1019. $this->error = 2425;
  1020. return false;
  1021. }
  1022. $userInfo = $this->model->with(['merchant'])
  1023. ->where(['id'=> $userId,'mark'=>1])
  1024. ->select(['id','nickname','balance','pay_password','wxpay_qrcode','alipay_qrcode','status'])
  1025. ->first();
  1026. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1027. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1028. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1029. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1030. $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
  1031. $merchPayPassword = isset($merchant['pay_password'])? $merchant['pay_password'] : '';
  1032. if(empty($userInfo) || $status != 1){
  1033. $this->error = 2024;
  1034. return false;
  1035. }
  1036. // 验证提现类型金额
  1037. $qrcode = '';
  1038. $account = '';
  1039. if($type == 2){
  1040. // 支付密码验证
  1041. if(empty($merchPayPassword)){
  1042. $this->error ='1042';
  1043. return false;
  1044. }
  1045. $payPassword = get_password($payPassword);
  1046. if($payPassword != $merchPayPassword){
  1047. $this->error ='1043';
  1048. return false;
  1049. }
  1050. if(empty($merchant)){
  1051. $this->error = 2646;
  1052. return false;
  1053. }
  1054. if($money>$merchBalance){
  1055. $this->error = 2647;
  1056. return false;
  1057. }
  1058. $wxpayQrcode = isset($merchant['wxpay_qrcode'])? $merchant['wxpay_qrcode'] : '';
  1059. $alipayQrcode = isset($merchant['alipay_qrcode'])? $merchant['alipay_qrcode'] : '';
  1060. if($payType == 10){
  1061. $qrcode = $wxpayQrcode;
  1062. if(empty($wxpayQrcode)){
  1063. $this->error = 2649;
  1064. return false;
  1065. }
  1066. }
  1067. if($payType == 20){
  1068. $qrcode = $alipayQrcode;
  1069. if(empty($alipayQrcode)) {
  1070. $this->error = 2649;
  1071. return false;
  1072. }
  1073. }
  1074. }else{
  1075. // 支付密码验证
  1076. if(empty($userPayPassword)){
  1077. $this->error ='1039';
  1078. return false;
  1079. }
  1080. $payPassword = get_password($payPassword);
  1081. if($payPassword != $userPayPassword){
  1082. $this->error ='1038';
  1083. return false;
  1084. }
  1085. if($money>$balance){
  1086. $this->error = 2648;
  1087. return false;
  1088. }
  1089. $wxpayQrcode = isset($userInfo['wxpay_qrcode'])? $userInfo['wxpay_qrcode'] : '';
  1090. $alipayQrcode = isset($userInfo['alipay_qrcode'])? $userInfo['alipay_qrcode'] : '';
  1091. if($payType == 10){
  1092. $qrcode = $wxpayQrcode;
  1093. if(empty($wxpayQrcode)) {
  1094. $this->error = 2420;
  1095. return false;
  1096. }
  1097. }
  1098. if($payType == 20){
  1099. $qrcode = $alipayQrcode;
  1100. if(empty($alipayQrcode)){
  1101. $this->error = 2421;
  1102. return false;
  1103. }
  1104. }
  1105. if($payType == 50){
  1106. $bankInfo = MemberBankModel::where(['id'=> $bankId,'mark'=>1])
  1107. ->select(['realname','bank_name','bank_card'])
  1108. ->first();
  1109. $bankInfo = $bankInfo? $bankInfo->toArray() : [];
  1110. $bankInfo = array_values($bankInfo);
  1111. if(empty($bankInfo)){
  1112. $this->error = 2422;
  1113. return false;
  1114. }else {
  1115. $account = $bankInfo? implode(' ', $bankInfo) : '';
  1116. }
  1117. }
  1118. }
  1119. // 提现订单
  1120. $orderNo = get_order_num('DW');
  1121. $data = [
  1122. 'order_no'=> $orderNo,
  1123. 'user_id'=> $userId,
  1124. 'type'=> 2,
  1125. 'user_type'=> $type,
  1126. 'money'=> $money,
  1127. 'actual_money'=> 0,
  1128. 'qrcode'=> $qrcode,
  1129. 'account'=> $account,
  1130. 'pay_type'=> $payType,
  1131. 'create_time'=> time(),
  1132. 'update_time'=> time(),
  1133. 'status'=> 1,
  1134. 'mark'=> 1,
  1135. ];
  1136. // 提现记录
  1137. DB::beginTransaction();
  1138. if(!$id = BalanceLogModel::insertGetId($data)){
  1139. DB::rollBack();
  1140. $this->error = 2033;
  1141. return false;
  1142. }
  1143. // 商户扣款
  1144. if($type == 2){
  1145. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=> time()];
  1146. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1147. DB::rollBack();
  1148. $this->error = 2423;
  1149. return false;
  1150. }
  1151. /*$log = [
  1152. 'user_id' => 0,
  1153. 'merch_id' => $merchant['id'],
  1154. 'source_uid' => $userId,
  1155. 'source_order_no' => $orderNo,
  1156. 'type' => 12,
  1157. 'coin_type' => 4,
  1158. 'user_type'=> 2,
  1159. 'money' => -$money,
  1160. 'balance' => $merchBalance,
  1161. 'create_time' => time(),
  1162. 'update_time' => time(),
  1163. 'remark' => "商户余额提现",
  1164. 'status' => 1,
  1165. 'mark' => 1,
  1166. ];*/
  1167. }
  1168. // 用户扣款
  1169. else{
  1170. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=> time()];
  1171. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1172. DB::rollBack();
  1173. $this->error = 2423;
  1174. return false;
  1175. }
  1176. /*$log = [
  1177. 'user_id' => $userId,
  1178. 'merch_id' => 0,
  1179. 'source_uid' => 0,
  1180. 'source_order_no' => $orderNo,
  1181. 'type' => 11,
  1182. 'coin_type' => 4,
  1183. 'user_type'=> 1,
  1184. 'money' => -$money,
  1185. 'balance' => $balance,
  1186. 'create_time' => time(),
  1187. 'update_time' => time(),
  1188. 'remark' => "账户余额提现",
  1189. 'status' => 1,
  1190. 'mark' => 1,
  1191. ];*/
  1192. }
  1193. // 推送消息
  1194. $pushData = [
  1195. 'title' => '提现申请提交成功',
  1196. 'body' => "您的提现申请已提交请耐心等候审核",
  1197. 'type' => 3, // 1-公告通知,2-订单通知,3-交易通知,4-其他
  1198. 'content' => [
  1199. 'order_no' => ['name' => '订单号', 'text' => $orderNo],
  1200. 'pay_time' => ['name' => '申请时间', 'text' => date('Y-m-d H:i:s')],
  1201. 'pay_type' => ['name' => '付款方式', 'text' => $payType==10?'微信支付':($payType==20?'支付宝':'银行卡')],
  1202. 'money' => ['name' => '提现金额', 'text' => $money],
  1203. 'status' => ['name' => '状态', 'text' => '待审核'],
  1204. ],
  1205. 'click_type' => 'payload',
  1206. 'url' => '',
  1207. ];
  1208. $merchId = isset($merchant['id'])? $merchant['id'] : 0;
  1209. if($type == 2 && $merchId){
  1210. PushService::make()->pushMessageByUser($merchId, $pushData, 2);
  1211. }else{
  1212. PushService::make()->pushMessageByUser($userId, $pushData);
  1213. }
  1214. DB::commit();
  1215. $this->error = 2424;
  1216. return [
  1217. 'id'=> $id,
  1218. 'money'=> $money,
  1219. 'pay_type'=> $payType,
  1220. ];
  1221. }
  1222. /**
  1223. * 分销佣金入账
  1224. * @param $info
  1225. * @return false
  1226. */
  1227. public function settleBonus($info, $level=1)
  1228. {
  1229. $money = isset($info['money'])? floatval($info['money']) : 0;
  1230. $merchId = isset($info['merch_id'])? intval($info['merch_id']) : 0;
  1231. $userId = isset($info['user_id'])? intval($info['user_id']) : 0;
  1232. // 分销佣金
  1233. $settleUserId = isset($info["bonus_level{$level}_id"])? $info["bonus_level{$level}_id"] : 0;
  1234. $settleMoney = isset($info["bonus_level{$level}_money"])? $info["bonus_level{$level}_money"] : 0;
  1235. if($settleUserId>0 && $settleMoney>0){
  1236. $userInfo = $this->model->where(['id'=> $settleUserId,'mark'=>1])
  1237. ->select(['id','nickname','balance'])
  1238. ->first();
  1239. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1240. if($settleUserId && $userInfo){
  1241. // 更新账户
  1242. $updateData = [
  1243. 'balance'=>DB::raw("balance + {$settleMoney}"),
  1244. 'bonus_total'=>DB::raw("bonus_total + {$settleMoney}"),
  1245. "bonus_level{$level}_total"=>DB::raw("bonus_level{$level}_total + {$settleMoney}"),
  1246. 'update_time'=> time()
  1247. ];
  1248. if(!MemberModel::where(['id'=> $settleUserId,'mark'=>1])->update($updateData)){
  1249. $this->error = lang(3413, ['level'=> $level]);
  1250. return false;
  1251. }
  1252. $log = [
  1253. 'user_id'=> $settleUserId,
  1254. 'merch_id'=> $merchId,
  1255. 'source_uid'=> $userId,
  1256. 'source_order_no'=> isset($info['order_no'])? $info['order_no'] :'',
  1257. 'type'=> 3,
  1258. 'coin_type'=> 2,
  1259. 'user_type'=> 1,
  1260. 'money'=> $settleMoney,
  1261. 'balance'=>$balance? $balance : 0.00,
  1262. 'create_time'=> time(),
  1263. 'update_time'=> time(),
  1264. 'remark'=> "服务订单[{$money}]元的{$level}级分销佣金",
  1265. 'status'=>1,
  1266. 'mark'=>1
  1267. ];
  1268. if(!AccountLogModel::insertGetId($log)){
  1269. $this->error = lang(3414, ['level'=> $level]);
  1270. return false;
  1271. }
  1272. $this->error = lang(3415, ['level'=> $level]);
  1273. return true;
  1274. }
  1275. }
  1276. $this->error = lang(3412, ['level'=> $level]);
  1277. return true;
  1278. }
  1279. /**
  1280. * 获取点对点上级节点
  1281. * @param $userId 推荐人ID
  1282. * @return int|mixed
  1283. */
  1284. public function getPointParentId($userId)
  1285. {
  1286. if($userId<=0){
  1287. return 0;
  1288. }
  1289. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1290. $userList = $userList? $userList->toArray() : [];
  1291. if(count($userList) < 3){
  1292. return $userId;
  1293. }
  1294. $users = [];
  1295. foreach($userList as $item){
  1296. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1297. $points = array_filter($points);
  1298. $users[$item['id']]['pointLen'] = count($points);
  1299. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1300. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1301. }
  1302. //var_dump($users);
  1303. $pointId = 0;
  1304. $tempPointLen = 0;
  1305. $tempChildren = 0;
  1306. foreach ($userList as $item) {
  1307. $id = isset($item['id'])? $item['id'] : 0;
  1308. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1309. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1310. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1311. if($pointLen<=$tempPointLen || $pointId == 0){
  1312. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1313. if($children <$tempChildren || $pointId == 0){
  1314. $pointId = $id;
  1315. $tempPointLen = $pointLen;
  1316. $tempChildren = $children;
  1317. }
  1318. }
  1319. }
  1320. return $pointId;
  1321. }
  1322. }