MemberService.php 54 KB

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