MemberService.php 55 KB

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