UserServices.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. namespace app\api\services;
  3. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\CompareFacesResponseBody\data;
  4. use app\common\model\MachineListModel;
  5. use app\common\model\UserDataModel;
  6. use app\common\model\UserModel;
  7. use app\common\model\UserScoreModel as UserScore;
  8. use app\common\model\MemberModel;
  9. use app\common\model\NumRecordModel;
  10. use data\service\RequestUtilService;
  11. use http\Params;
  12. use jobs\UserJobs;
  13. use sms\Verify;
  14. use think\db\Where;
  15. use think\Exception;
  16. use think\facade\Db;
  17. use utils\Json;
  18. use utils\Queue;
  19. class UserServices extends BaseServices
  20. {
  21. public function __construct()
  22. {
  23. $this->model = new UserModel();
  24. }
  25. /**
  26. * 用户注册
  27. */
  28. public function userRegister(array $param)
  29. {
  30. $model = $this->model->where('mobile', $param['mobile'])->value('mobile');
  31. if ($model) {
  32. throw new Exception('该手机号已被绑定,请切换');
  33. }
  34. if (Db::name('user')->where('user_name', $param['user_name'])->find()){
  35. throw new Exception('该用户名已注册,请更换');
  36. }
  37. $has_inviter = false;
  38. if (isset($param['invite_code'])){
  39. if ($param['invite_code']) {
  40. $inviter = $this->model->where('code', $param['invite_code'])->find();
  41. if ($inviter) {
  42. $has_inviter = true;
  43. }
  44. }
  45. }
  46. if ($param['code'] != '123321' && !UserServices::checkSmsCode($param['code'], 'register', $param['mobile'])) {
  47. throw new Exception('验证码错误或过期');
  48. }
  49. $insert['mobile'] = $param['mobile'];
  50. $insert['password'] = md5(AESjiami($param['password']));
  51. $insert['pass_enable'] = $param['password'];
  52. $insert['user_name'] = $param['user_name'];
  53. $insert['reg_ip'] = get_client_ip();
  54. $insert['nickname'] = createUserNickname();
  55. $insert['path'] = '';
  56. $insert['avatar'] = 'static/img/user/default_avatar.jpg';
  57. $insert['code'] = create_invite_code();
  58. $insert['px_code'] = create_invite_code();
  59. // $insert['is_regup'] = empty($param['invite_code'])?2:1;
  60. $this->model->save($insert);
  61. $uid = $this->model->id;
  62. // if ($pingyiinfo){
  63. // incUserPerformance($uid, intval($pingyiinfo['kuisun_money'])+1);
  64. // }
  65. $user_data = new UserDataModel();
  66. $rz_money = 1.8 + rand(1, 30) / 100;
  67. $user_data->save(['uid' => $uid, 'rz_money' => $rz_money, 'device' => '']); // 保存用户关联信息
  68. if ($has_inviter){
  69. $this->bindRelation($param['invite_code'], $uid); // 绑定关系
  70. }
  71. return $uid;
  72. // Queue::instance()->log('执行成功')->do('userRelationRegister')->job(UserJobs::class)->push([$param['invite_code'], $uid]); // 绑定用户关系和添加用户静态数据
  73. return ['msg' => '注册成功'];
  74. }
  75. // code| 注册的邀请码 uid 当前用户的uid
  76. public function bindRelation (string $code, int $uid)
  77. {
  78. if (empty($code)){
  79. $code = '032842787';
  80. }
  81. $data = [];
  82. $data['px_pid'] = $data['pid'] = $data['is_px'] = $data['reg_pid'] = 0;
  83. $is_px = false;
  84. if (($up_user = $this->model->where('code', $code)->field('id,path')->lock(true)->find()) !== false && !empty($up_user)) {
  85. // 直推吗
  86. $data['pid'] = $up_user['id']; // pid
  87. $path = '';
  88. if (empty($up_user['path'])){
  89. $path = $up_user['id'];
  90. }else{
  91. $path = $up_user['path'].','.$up_user['id'];
  92. }
  93. // sr_log('path'.$path);
  94. $data['path'] = $path;
  95. $data['reg_pid'] = $up_user['id'];
  96. } elseif (($up_user = $this->model->where('px_code', $code)->field('id,path,px_id')->lock(true)->find()) !== false && !empty($up_user)) {
  97. // 排线码
  98. // $last_user = $this->model->where(['px_id'=>$up_user['id']])->field('id,px_pid,path')->order('id desc')->find(); // 排线最后一位
  99. // throw new Exception(json_encode($up_user));
  100. $is_px = true;
  101. if (empty($up_user['path'])){
  102. // 在顶层注册
  103. $count = $this->model->where(['px_id'=>$up_user['id']])->count();
  104. if ($count > 0){
  105. // 当前id有排线
  106. $max_user = $this->model->where(['px_id'=>$up_user['id']])->max('id');
  107. $max_user = $this->model->where(['id'=>$max_user])->field('id, path, px_id')->find();
  108. $data['path'] = $max_user['path'] .(empty($max_user['path'])?'':','). $max_user['id'];
  109. $data['px_id'] = $max_user['px_id'];
  110. $data['pid'] = $max_user['id'];
  111. }else {
  112. // 当前id 没有排线
  113. // 当前排线没人
  114. $data['path'] = $up_user['path'].(empty($up_user['path'])?'':',').$up_user['id'];
  115. $data['px_id'] = $up_user['id'];
  116. $data['pid'] = $up_user['id'];
  117. // throw new Exception($data['path']);
  118. }
  119. }else {
  120. $count = $this->model->where(['px_id'=>$up_user['id']])->count();
  121. if ($count == 0){
  122. // 当前没有排线
  123. // $max_user = $this->model->where(['px_id'=>$dc_user['id']])->max('id');
  124. // $max_user = $this->model->where(['id'=>$max_user])->field('id, path, px_id')->find();
  125. $data['path'] = $up_user['path'].(empty($up_user['path'])?'':',').$up_user['id'];
  126. $data['px_id'] = $up_user['id'];
  127. $data['pid'] = $up_user['id'];
  128. }else{
  129. // 当前有排线
  130. $max_user = $this->model->where(['px_id'=>$up_user['id']])->max('id');
  131. $max_user = $this->model->where(['id'=>$max_user])->field('id, path, px_id')->find();
  132. $data['path'] = $max_user['path'] .(empty($max_user['path'])?'':','). $max_user['id'];
  133. $data['px_id'] = $up_user['id'];
  134. $data['pid'] = $max_user['id'];
  135. }
  136. // 不在顶层注册
  137. }
  138. $data['px_pid'] = $up_user['id']; // 排线pid
  139. $data['reg_pid'] = $up_user['id'];
  140. $data['is_px'] = 1; // 标记排线
  141. }
  142. // $data['path'] = trim_string($data['path']);
  143. // $data['nickname'] = 'yju' . $uid;
  144. $data['total_number'] = 0;
  145. // $data['market_uid'] = $up_user['market_uid'];
  146. // $data['firstcode_uid'] = $up_user['firstcode_uid'];
  147. if ($is_px){
  148. if (!isset($data['path']) || empty($data['path'])){
  149. throw new Exception('注册失败');
  150. }
  151. }
  152. $this->model->where('id', $uid)->save($data);
  153. incTeamStatic($uid, 1, $data['path']);
  154. }
  155. public function userLogin(array $param)
  156. {
  157. $user_name = $param['user_name'];
  158. $f_user = Db::name('user')->where('mobile|user_name', $user_name)->find();
  159. if (!$f_user){
  160. sr_throw('请核对登录账号信息');
  161. }
  162. $user = $this->model->getUserDetailByUid($f_user['id']);
  163. if ($user['status'] == 0 ){
  164. $msg = '账号被禁用'.(empty($user['deny_reason'])?"":(',原因:'. $user['deny_reason']));
  165. throw new Exception($msg);
  166. }
  167. if ($user['status'] != 1){
  168. sr_throw('用户状态错误,登录失败');
  169. }
  170. $login_type = 'pass';
  171. if ($user){
  172. if ($login_type === 'pass'){
  173. $param['password'] = AESjiemi($param['password']);
  174. if (md5(AESjiami($param['password'])) == $user['password']){
  175. // 验证是否需要验证码登录
  176. if (checkUserNeedsSecuritLogin($f_user['id'])){
  177. throw new Exception('请使用验证码登录', 1148);
  178. }else{
  179. }
  180. }else{
  181. throw new Exception('账号密码错误,请检查', 500);
  182. }
  183. }elseif ($login_type === 'security'){
  184. // 验证短信验证码
  185. if (!UserServices::checkSmsCode($param['code'], 'loingcheck', $param['mobile'])) {
  186. throw new Exception('验证码错误或过期', 500);
  187. }
  188. // 验证滑动验证
  189. $ali_verify = json_decode(html_entity_decode($param['ali_verify']), true);
  190. /** @var Verify $verify */
  191. $verify = new Verify();
  192. $result = $verify->ali_verify($ali_verify['appkey'], $ali_verify['scene'], $ali_verify['csessionid'], $ali_verify['nc_token'], $ali_verify['sig']);
  193. if (!$result){
  194. throw new Exception('登录失败请重新滑动验证', 500);
  195. }
  196. }
  197. }else {
  198. throw new Exception('用户名或者密码错误,请检查');
  199. }
  200. $this->model->updateUserLoginInfo($user['id']);
  201. unset($user['password']);
  202. $user['login_count'] = $user['login_count']+1;
  203. $user['token'] = $this->createToken($user['id'], $user);
  204. unset($user['status']);
  205. unset($user['login_count']);
  206. unset($user['login_ip']);
  207. unset($user['login_time']);
  208. return ['msg'=>'登录成功', 'data'=>$user];
  209. }
  210. // // 老的排线
  211. // protected function bindRelation(string $code, int $uid)
  212. // {
  213. // $data['px_pid'] = $data['pid'] = $data['is_px'] = 0;
  214. // if (($up_user = $this->model->where('code', $code)->field('id')->lock(true)->find()) !== false && !empty($up_user)) { // 直推码
  215. // $data['pid'] = $up_user['id']; // pid
  216. // $relation = $this->model->where('id', $up_user['id'])->value('path');
  217. // $data['path'] = $relation . ',' . $up_user['id'];
  218. // } elseif (($up_user = $this->model->where('px_code', $code)->field('id')->lock(true)->find()) !== false && !empty($up_user)) { // 排线码
  219. // $last_user = $this->model->where('id', $up_user['id'])->field('id,px_pid,path')->order('id desc')->find(); // 排线最后一位
  220. // if (!empty($last_user)) { // 如果有则在该用户下继续排线
  221. // $data['pid'] = $last_user['id']; // 上级id
  222. // $data['path'] = $last_user['path'] . ',' . $last_user['id'];
  223. // } else { // 没有就开启新的排线
  224. // $relation = $this->model->where('id', $up_user['id'])->value('path');
  225. // $data['pid'] = $up_user['id']; // pid
  226. // $data['path'] = $relation . ',' . $up_user['id'];
  227. // }
  228. // $data['px_pid'] = $up_user['id']; // 排线pid
  229. // $data['is_px'] = 1; // 标记排线
  230. // }
  231. // $data['path'] = trim_string($data['path']);
  232. // $data['nickname'] = '会员_' . $uid;
  233. //// $data['market_uid'] = $up_user['market_uid'];
  234. //// $data['firstcode_uid'] = $up_user['firstcode_uid'];
  235. // if (!isset($data['path']) || empty($data['path']))
  236. // throw new Exception('注册失败');
  237. // $this->model->where('id', $uid)->save($data);
  238. // $data['path'] .= ',' . $uid;
  239. // $data['path'] = trim_string($data['path']);
  240. // $this->model->where('id', 'in', $data['path'])->save(['total_number' => ['inc', 1]]);
  241. // }
  242. public function checkSmsCode($code, $type, $phone = 0) : bool
  243. {
  244. // sr_log(env('APP.CUR_SYS_PARAMS'));
  245. // sr_log(intval($code));
  246. if (in_array(env('APP.CUR_SYS_PARAMS'), [1, 2])){
  247. if (intval($code) == 111111){
  248. return true;
  249. }
  250. }else{
  251. // if (intval($code) == 931256){
  252. // sr_log('统一验证码使用,手机号:'.$phone.',type:'.$type);
  253. // return true;
  254. // }
  255. }
  256. // // 测试手机号
  257. // if ($phone == '18280066008'){
  258. // return true;
  259. // }
  260. $m = new NumRecordModel();
  261. $data = [
  262. 'type'=>$type,
  263. 'content'=>$code,
  264. 'is_use'=>0,
  265. 'phone'=>$phone
  266. ];
  267. if ($info = $m->where($data)->find()){
  268. // $info = $m->getQuery($data, '*', 'create_time asc');
  269. //
  270. // $old_time = $info[0]['create_time'];
  271. // if ((time()-$old_time) > 900){
  272. // return false;
  273. // }else {
  274. $m->where(['id'=>$info['id']])->save(['is_use'=>1]);
  275. return true;
  276. // }
  277. }else {
  278. return false;
  279. }
  280. }
  281. public function userAuth($request)
  282. {
  283. $param = $request->param();
  284. if (Db::name('user')->where(['id'=>$request->uid, 'is_auth'=>1])->find()){
  285. throw new Exception('已经实名啦');
  286. }
  287. // if (Db::name('user_data')->where(['idcard'=>$param['id_card']])->find()){
  288. // throw new Exception('该身份证已经实名');
  289. // }
  290. Db::startTrans();
  291. try {
  292. $ser_request = new \app\services\RequestUtilService();
  293. $result = $ser_request->requestJUHEidCardAuth($param['id_name'], $param['id_card']);
  294. // {
  295. // "data": {
  296. // "reason": "成功",
  297. // "result": {
  298. // "res": 1,
  299. // "realname": "王红力",
  300. // "idcard": "513023199202066138",
  301. // "orderid": "J2022010412272517923w7rV9"
  302. // },
  303. // "error_code": 0
  304. // }
  305. //}
  306. // {
  307. // "reason": "参数错误:姓名或身份证格式错误",
  308. // "result": {
  309. // "orderid": "J202201041221481052DNYXe4"
  310. // },
  311. // "error_code": 210304
  312. //}
  313. if ($result->error_code == 0){
  314. if ($result->result->res == 1){
  315. // 更新用户表的数据
  316. $m = new UserDataModel();
  317. $m->where(['uid'=>$request->uid])->save([
  318. 'idcard'=>$param['id_card'],
  319. 'idname'=>$param['id_name'],
  320. 'real_name'=>$param['id_name'],
  321. ]);
  322. Db::name('user')->where(['id'=>$request->uid])->save(['is_auth'=>1]);
  323. // $m->save(['true_name'=>$true_name, 'id_card'=>$id_card], ['uid'=>$this->uid]);
  324. // return $this->outMessage($title, []);
  325. // 赠送用户100积分
  326. Db::name('user')->where(['id'=>$request->uid])->inc('score', 100)->update();
  327. UserScore::change(['before_score' => $request->user_info->score, 'score' => 100, 'type' => 6, 'uid' => $request->uid, 'state' => 1]);
  328. Queue::instance()->log('实名人数执行成功')->do('userRelationAuth')->job(UserJobs::class)->push([$request->user_info->code, $request->uid]); // 绑定用户关系和添加用户静态数据
  329. } else {
  330. throw new Exception('输入信息不匹配');
  331. }
  332. }else{
  333. throw new Exception($result->reason);
  334. }
  335. Db::commit();
  336. return api_succ_return('实名认证成功');
  337. }catch (\Exception $e){
  338. Db::rollback();
  339. throw new Exception($e->getMessage());
  340. }
  341. }
  342. /**
  343. * 设置用户信息
  344. * @param $uid
  345. * @param $param
  346. * @return string
  347. */
  348. public function setUserInfo ($uid, $param)
  349. {
  350. $user = $this->model->findOrEmpty($uid);
  351. if (empty($user))
  352. throw new Exception('用户信息设置失败');
  353. // if (array_key_exists('pay_pass', $param)) { // 设置安全密码
  354. // if ($user->pay_pass) {
  355. // throw new Exception('不允许设置');
  356. // } else {
  357. // if ($param['pay_pass'] != $param['c_paypass'])
  358. // throw new Exception('两次输入的密码不一致');
  359. // else
  360. // $param['pay_pass'] = md5(AES::encrypt($param['pay_pass']));
  361. // }
  362. // }
  363. $config = ['avatar' => 'user', 'pay_pass' => 'user', 'sex' => 'user_data', 'email' => 'user_data', 'intro' => 'user_data', 'birthday' => 'user_data', 'qq' => 'user_data', 'wx' => 'user_data', 'nickname' => 'user', 'area_code_ids' => 'user_data', 'area_text' => 'user_data']; // 允许修改的字段
  364. foreach ($param as $key => $value) {
  365. $map = [];
  366. if (isset($config[$key]) && ($model_name = $config[$key]) !== false) {
  367. if ($model_name == 'user')
  368. $map['id'] = $uid;
  369. else
  370. $map['uid'] = $uid;
  371. Db::name($model_name)->where($map)->save([$key => $value]);
  372. }
  373. }
  374. // 如果有设置昵称跟头像要刷新消息聊天记录的信息
  375. if (isset($param['nickname'])){
  376. Db::name('chat_message')->where('uid', $uid)->save(['nick_name'=>$param['nickname']]);
  377. }
  378. if (isset($param['avatar'])){
  379. Db::name('chat_message')->where('uid', $uid)->save(['avatar'=>(getWebUrl().'/'.$param['avatar'])]);
  380. }
  381. return '用户信息设置成功';
  382. }
  383. public function setSellerPayWay ($uid, $param)
  384. {
  385. $user = $this->model->findOrEmpty($uid);
  386. if (empty($user))
  387. throw new Exception('用户信息设置失败');
  388. // if (array_key_exists('pay_pass', $param)) { // 设置安全密码
  389. // if ($user->pay_pass) {
  390. // throw new Exception('不允许设置');
  391. // } else {
  392. // if ($param['pay_pass'] != $param['c_paypass'])
  393. // throw new Exception('两次输入的密码不一致');
  394. // else
  395. // $param['pay_pass'] = md5(AES::encrypt($param['pay_pass']));
  396. // }
  397. // }
  398. $config = [
  399. 'wx_img' => 'user_data',
  400. 'wx_pay' => 'user_data',
  401. 'wx_pay_name' => 'user_data',
  402. 'ali_img' => 'user_data',
  403. 'ali_pay' => 'user_data',
  404. 'ali_pay_name' => 'user_data',
  405. 'bank_no' => 'user_data',
  406. 'bank_subname' => 'user_data'
  407. ]; // 允许修改的字段
  408. foreach ($param as $key => $value) {
  409. $map = [];
  410. if (isset($config[$key]) && ($model_name = $config[$key]) !== false) {
  411. if ($model_name == 'user')
  412. $map['id'] = $uid;
  413. else
  414. $map['uid'] = $uid;
  415. Db::name($model_name)->where($map)->save([$key => $value]);
  416. }
  417. }
  418. return '配置成功';
  419. }
  420. // 用户升星
  421. public function upgradeStar($param)
  422. {
  423. $uid = $param->uid;
  424. $user = $this->model->getUserInfo(['id' => $uid], 'total_active,level,nickname,performance_me,total_performance');
  425. $max_level = Db::name('level_setting')->order('level desc')->value('level');
  426. if ($user['level'] >= $max_level){
  427. throw new Exception('当前已是最大星级,无需升级', 308);
  428. }
  429. $next_data = Db::name('level_setting')->where('level', $user['level']+1)->find();
  430. // $star_config = get_star_config($user['level'] + 1);
  431. if (empty($next_data))
  432. throw new Exception('配置错误');
  433. $result = $this->upgradeResult($uid, $next_data, $user);
  434. if ($param->data['type'] == 2) {
  435. if ($result){
  436. return api_succ_return(['code'=>206, 'msg'=>'当前可以升级']);
  437. }
  438. throw new Exception('未达到升级条件', 205);
  439. }
  440. if ($result) {
  441. Db::startTrans();
  442. try {
  443. Db::name('user')->where('id', $uid)->update(['level' => $user['level'] + 1]);
  444. Db::name('upgrade_log')->insert(['uid' => $uid, 'nickname' => $user['nickname'], 'original_level' => $user['level'], 'level' => $user['level'] + 1, 'condition' => json_encode($next_data), 'type' => 2, 'state' => 1]);
  445. Db::commit();
  446. return api_succ_return(['msg' => '升级成功,当前等级为:' . $next_data['name'], 'data'=>['level_name' => $next_data['name']]]);
  447. } catch (\Exception $e) {
  448. Db::rollback();
  449. throw new Exception('升级失败' . $e->getMessage());
  450. }
  451. } else {
  452. throw new Exception('未达到升级条件', 500);
  453. }
  454. }
  455. protected function upgradeResult(int $uid, array $next_data, array $user)
  456. {
  457. $m_user = new UserModel();
  458. $zt_num = $m_user->where('pid', $uid)->where('has_fd', '1')->count();
  459. // $xiaoqu_active = $m_user->xiaoquActive($uid);
  460. // $ye_ji = $user['total_performance'] + $user['performance_me'];
  461. // $ye_ji = $user['total_performance'];
  462. // 小区业绩
  463. // $xiaoqu_performance = $m_user->xiaoquPerformance($uid);
  464. if ($next_data['level'] == 4){
  465. return false;
  466. }
  467. if ($next_data['level'] == 1){
  468. if ($zt_num >= $next_data['zt_num']){
  469. return true;
  470. }
  471. }else{
  472. $team_count = Db::name('user')->whereFindInSet('path', $uid)->where('has_fd', 1)->count();
  473. if ($team_count >= $next_data['zt_num']){
  474. $count = Db::name('user')->where('pid', $uid)->where('level_type', 1)->where('level', $next_data['level']-1)->count();
  475. if ($count >= 2){
  476. return true;
  477. }
  478. }
  479. }
  480. return false;
  481. }
  482. // 用户星钻升星
  483. public function upgradeXzStar($param)
  484. {
  485. $uid = $param->uid;
  486. $user = $this->model->getUserInfo(['id' => $uid], 'total_active,active_me,xz_level,nickname,performance_me,total_performance');
  487. $max_level = Db::name('xzlevel_setting')->order('level desc')->value('level');
  488. if ($user['xz_level'] >= $max_level)
  489. throw new Exception('当前已是最大星级,无需升级', 308);
  490. $next_data = Db::name('xzlevel_setting')->where('level', $user['xz_level']+1)->find();
  491. // $star_config = get_star_config($user['level'] + 1);
  492. if (empty($next_data))
  493. throw new Exception('配置错误');
  494. $result = $this->upgradeXzResult($uid, $next_data, $user);
  495. if ($param->data['type'] == 2) {
  496. if ($result)
  497. return api_succ_return(['code'=>208, 'msg'=>'当前可以升级']);
  498. throw new Exception('未达到升级条件', 207);
  499. }
  500. if ($result) {
  501. Db::startTrans();
  502. try {
  503. Db::name('user')->where('id', $uid)->update(['xz_level' => $user['xz_level'] + 1]);
  504. Db::name('upgrade_log')->insert(['channel'=>2,'uid' => $uid, 'nickname' => $user['nickname'], 'original_level' => $user['xz_level'], 'level' => $user['xz_level'] + 1, 'condition' => json_encode($next_data), 'type' => 2, 'state' => 1]);
  505. $hasMore = false;
  506. if ($user['xz_level']>=3){
  507. $hasMore = true;
  508. }
  509. // 赠送矿机
  510. $machineinfo = Db::name('machine')->where('mlevel', $user['xz_level'] + ($hasMore?2:1))->find();
  511. $m_machinelist = new MachineListModel();
  512. $time = time();
  513. $m_machinelist->insert([
  514. 'mid'=>$machineinfo['id'],
  515. 'uid'=>$uid,
  516. 'create_time'=>sr_getcurtime($time),
  517. 'expire_time'=>sr_getcurtime(strtotime("+ ".$machineinfo['valid_day']." days")),
  518. 'back_count'=>$machineinfo['cycle'],
  519. 'once_back'=>number_format($machineinfo['total_number']/$machineinfo['cycle'], '4', '.', ''),
  520. 'over_count'=>$machineinfo['cycle'],
  521. 'get_type'=>3
  522. ]);
  523. Db::commit();
  524. return api_succ_return(['msg' => '升级成功,当前等级为:' . $next_data['name'], 'data'=>['level_name' => $next_data['name']]]);
  525. } catch (\Exception $e) {
  526. Db::rollback();
  527. throw new Exception('升级失败' . $e->getMessage());
  528. }
  529. } else {
  530. throw new Exception('未达到升级条件');
  531. }
  532. }
  533. protected function upgradeXzResult(int $uid, array $next_data, array $user)
  534. {
  535. $m_user = new UserModel();
  536. $zt_num = $m_user->where('pid', $uid)->where('is_auth', 1)->count();
  537. $xiaoqu_active = $m_user->xiaoquActive($uid);
  538. // $ye_ji = $user['total_performance'] + $user['performance_me'];
  539. if ($zt_num >= $next_data['zt_num'] && ($user['total_active']>=$next_data['total_active']) && ($xiaoqu_active >= $next_data['xiaoqu_active'])) {
  540. return true;
  541. }
  542. return false;
  543. }
  544. // 绑定提现信息
  545. public function bangWithdrawInfo($uid,$type, $number, $name)
  546. {
  547. $m_user = new UserModel();
  548. $m_userdata = new UserDataModel();
  549. $user = $m_user->where('id', $uid)->find();
  550. if (!$user) {
  551. throw new Exception('用户信息查询失败');
  552. }
  553. if ($user['status'] != 1) {
  554. throw new Exception('用户状态失败');
  555. }
  556. if ($user['is_auth'] != 1) {
  557. throw new Exception('还未实名绑定失败');
  558. }
  559. if ($type == 1) {
  560. $datainfo = $m_userdata->where('uid', $uid)->find();
  561. if ($name != $datainfo['idname']) {
  562. throw new Exception('填写真实名字与实名不符');
  563. }
  564. $m_userdata->where('uid', $uid)->save([
  565. 'alipay_name' => $name,
  566. 'alipay' => $number
  567. ]);
  568. }
  569. if ($type == 2) {
  570. $datainfo = $m_userdata->where('uid', $uid)->find();
  571. if ($name != $datainfo['idname']) {
  572. throw new Exception('填写真实名字与实名不符');
  573. }
  574. // 比对银行卡
  575. $res = _curlrequest('https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?cardNo=' . $number . '&cardBinCheck=true', []);
  576. // $res = json_decode($res);
  577. sr_log($res);
  578. $bankCard = json_decode($res, true);
  579. if ($bankCard['validated'] == false) {
  580. throw new Exception('银行卡信息错误,不要输入空格或者特殊符号');
  581. } else {
  582. $bank = array(
  583. "CDB" => "国家开发银行", "ICBC" => "中国工商银行", "ABC" => "中国农业银行", "BOC" => "中国银行", "CCB" => "中国建设银行", "PSBC" => "中国邮政储蓄银行", "COMM" => "交通银行", "CMB" => "招商银行", "SPDB" => "上海浦东发展银行", "CIB" => "兴业银行", "HXBANK" => "华夏银行", "GDB" => "广东发展银行", "CMBC" => "中国民生银行", "CITIC" => "中信银行", "CEB" => "中国光大银行", "EGBANK" => "恒丰银行", "CZBANK" => "浙商银行", "BOHAIB" => "渤海银行", "SPABANK" => "平安银行", "SHRCB" => "上海农村商业银行", "YXCCB" => "玉溪市商业银行", "YDRCB" => "尧都农商行", "BJBANK" => "北京银行", "SHBANK" => "上海银行", "JSBANK" => "江苏银行", "HZCB" => "杭州银行", "NJCB" => "南京银行", "NBBANK" => "宁波银行", "HSBANK" => "徽商银行", "CSCB" => "长沙银行", "CDCB" => "成都银行", "CQBANK" => "重庆银行", "DLB" => "大连银行", "NCB" => "南昌银行", "FJHXBC" => "福建海峡银行", "HKB" => "汉口银行",
  584. "WZCB" => "温州银行", "QDCCB" => "青岛银行", "TZCB" => "台州银行", "JXBANK" => "嘉兴银行", "CSRCB" => "常熟农村商业银行", "NHB" => "南海农村信用联社", "CZRCB" => "常州农村信用联社", "H3CB" => "内蒙古银行", "SXCB" => "绍兴银行", "SDEB" => "顺德农商银行", "WJRCB" => "吴江农商银行", "ZBCB" => "齐商银行", "GYCB" => "贵阳市商业银行", "ZYCBANK" => "遵义市商业银行", "HZCCB" => "湖州市商业银行", "DAQINGB" => "龙江银行", "JINCHB" => "晋城银行JCBANK", "ZJTLCB" => "浙江泰隆商业银行", "GDRCC" => "广东省农村信用社联合社", "DRCBCL" => "东莞农村商业银行", "MTBANK" => "浙江民泰商业银行", "GCB" => "广州银行", "LYCB" => "辽阳市商业银行", "JSRCU" => "江苏省农村信用联合社", "LANGFB" => "廊坊银行", "CZCB" => "浙江稠州商业银行", "DYCB" => "德阳商业银行", "JZBANK" => "晋中市商业银行", "BOSZ" => "苏州银行", "GLBANK" => "桂林银行", "URMQCCB" => "乌鲁木齐市商业银行", "CDRCB" => "成都农商银行",
  585. "ZRCBANK" => "张家港农村商业银行", "BOD" => "东莞银行", "LSBANK" => "莱商银行", "BJRCB" => "北京农村商业银行", "TRCB" => "天津农商银行", "SRBANK" => "上饶银行", "FDB" => "富滇银行", "CRCBANK" => "重庆农村商业银行", "ASCB" => "鞍山银行", "NXBANK" => "宁夏银行", "BHB" => "河北银行", "HRXJB" => "华融湘江银行", "ZGCCB" => "自贡市商业银行", "YNRCC" => "云南省农村信用社", "JLBANK" => "吉林银行", "DYCCB" => "东营市商业银行", "KLB" => "昆仑银行", "ORBANK" => "鄂尔多斯银行", "XTB" => "邢台银行", "JSB" => "晋商银行", "TCCB" => "天津银行", "BOYK" => "营口银行", "JLRCU" => "吉林农信", "SDRCU" => "山东农信", "XABANK" => "西安银行", "HBRCU" => "河北省农村信用社", "NXRCU" => "宁夏黄河农村商业银行", "GZRCU" => "贵州省农村信用社", "FXCB" => "阜新银行", "HBHSBANK" => "湖北银行黄石分行", "ZJNX" => "浙江省农村信用社联合社", "XXBANK" => "新乡银行", "HBYCBANK" => "湖北银行宜昌分行",
  586. "LSCCB" => "乐山市商业银行", "TCRCB" => "江苏太仓农村商业银行", "BZMD" => "驻马店银行", "GZB" => "赣州银行", "WRCB" => "无锡农村商业银行", "BGB" => "广西北部湾银行", "GRCB" => "广州农商银行", "JRCB" => "江苏江阴农村商业银行", "BOP" => "平顶山银行", "TACCB" => "泰安市商业银行", "CGNB" => "南充市商业银行", "CCQTGB" => "重庆三峡银行", "XLBANK" => "中山小榄村镇银行", "HDBANK" => "邯郸银行", "KORLABANK" => "库尔勒市商业银行", "BOJZ" => "锦州银行", "QLBANK" => "齐鲁银行", "BOQH" => "青海银行", "YQCCB" => "阳泉银行", "SJBANK" => "盛京银行", "FSCB" => "抚顺银行", "ZZBANK" => "郑州银行", "SRCB" => "深圳农村商业银行", "BANKWF" => "潍坊银行", "JJBANK" => "九江银行", "JXRCU" => "江西省农村信用", "HNRCU" => "河南省农村信用", "GSRCU" => "甘肃省农村信用", "SCRCU" => "四川省农村信用", "GXRCU" => "广西省农村信用", "SXRCCU" => "陕西信合", "WHRCB" => "武汉农村商业银行", "YBCCB" => "宜宾市商业银行",
  587. "KSRB" => "昆山农村商业银行", "SZSBK" => "石嘴山银行", "HSBK" => "衡水银行", "XYBANK" => "信阳银行", "NBYZ" => "鄞州银行", "ZJKCCB" => "张家口市商业银行", "XCYH" => "许昌银行", "JNBANK" => "济宁银行", "CBKF" => "开封市商业银行", "WHCCB" => "威海市商业银行", "HBC" => "湖北银行", "BOCD" => "承德银行", "BODD" => "丹东银行", "JHBANK" => "金华银行", "BOCY" => "朝阳银行", "LSBC" => "临商银行", "BSB" => "包商银行", "LZYH" => "兰州银行", "BOZK" => "周口银行", "DZBANK" => "德州银行", "SCCB" => "三门峡银行", "AYCB" => "安阳银行", "ARCU" => "安徽省农村信用社", "HURCB" => "湖北省农村信用社", "HNRCC" => "湖南省农村信用社", "NYNB" => "广东南粤银行", "LYBANK" => "洛阳银行", "NHQS" => "农信银清算中心", "CBBQS" => "城市商业银行资金清算中心",
  588. "FJNX"=>"福建农商银行");
  589. $bc = ['bank_name' => $bank[$bankCard['bank']], 'card_type' => $bankCard['cardType'], 'bank_code' => $bankCard['bank']];
  590. $m_userdata->where('uid', $uid)->save([
  591. 'bank_name' => $bc['bank_name'],
  592. 'bank_no' => $number
  593. ]);
  594. }
  595. }
  596. }
  597. // 绑定提现信息
  598. public function unbangWithdrawInfo($uid,$type)
  599. {
  600. $m_user = new UserModel();
  601. $m_userdata = new UserDataModel();
  602. $user = $m_user->where('id', $uid)->find();
  603. if (!$user){
  604. throw new Exception('用户信息查询失败');
  605. }
  606. if ($user['status'] != 1){
  607. throw new Exception('用户状态失败');
  608. }
  609. if ($type == 1){
  610. $m_userdata->where('uid', $uid)->save([
  611. 'alipay_name'=>'',
  612. 'alipay'=>''
  613. ]);
  614. }
  615. if ($type == 2){
  616. $m_userdata->where('uid', $uid)->save([
  617. 'bank_name'=>'',
  618. 'bank_no'=>''
  619. ]);
  620. }
  621. }
  622. // 提现
  623. public function withdraw ($uid, $money, $type, $params)
  624. {
  625. $today_count = Db::name('withdraw_log')->where('uid', $uid)->whereDay('create_at', 'today')->count();
  626. if ($today_count > 0){
  627. sr_throw('每天只能提现一次');
  628. }
  629. if ($type == 1 && (empty($params['name']) || empty($params['number']))){
  630. sr_throw('参数错误');
  631. }
  632. if ($type == 2 && (empty($params['name']) || empty($params['number']) || empty($params['bank_subname']))){
  633. sr_throw('参数错误');
  634. }
  635. if ($money<=0){
  636. throw new Exception('参数错误');
  637. }
  638. if (!preg_match("/^[1-9][0-9]*$/" ,$money)){
  639. throw new Exception('输入非法');
  640. }
  641. $user = Db::name('user')
  642. ->alias('a')
  643. ->join('user_data b', 'a.id=b.uid')
  644. ->field('money,status,path,alipay,real_name,is_auth,level,user_type,b.*')
  645. ->where('a.id', $uid)->find();
  646. // sr_log('user==='.json_encode($user));
  647. if ($user['status'] != 1)
  648. throw new Exception('账号已被禁用');
  649. // if ($user['user_type'] == 9){
  650. // if (!in_array($uid,explode('|', env('APP.WITHDRAW_FUCHI_OPEN')))){
  651. // sr_throw('不可提现');
  652. // }
  653. // }
  654. // if ($user['is_auth'] != 1)
  655. // throw new Exception('你还未实名认证');
  656. // if ($type == 1){
  657. // throw new Exception('暂不支持支付宝提现');
  658. // if (empty($user['alipay']) || empty($user['alipay_name'])){
  659. // throw new Exception('你还未绑定支付宝');
  660. // }
  661. // }
  662. // if ($type == 2){
  663. // if (empty($user['bank_name']) || empty($user['bank_no'])){
  664. // throw new Exception('你还未绑定银行卡');
  665. // }
  666. // }
  667. if ($money < env('WITHDRAW.ONE_COUNT_MONEY')){
  668. throw new Exception('单次提现金额要大于'.env('WITHDRAW.ONE_COUNT_MONEY'));
  669. }
  670. if ($money % 10 > 0)
  671. {
  672. throw new Exception('提现失败,金额必须是10的整数倍');
  673. }
  674. if ($money > $user['money'])
  675. {
  676. throw new Exception('余额不足');
  677. }
  678. $service_money = $money * env('WITHDRAW.APP_WITHDRAW_SCALE')/100;
  679. $practical_money = $money - $service_money; // 最终金额
  680. // 2 + 5 其中的 2快
  681. // $practical_money = $money - env('WITHDRAW.APP_WITHDRAW_BEGIN');
  682. // $tz_num = number_format($service_money / sysconfig('jhmoney', 'jh_money'), '4', '.','');
  683. // if ($money < $pay_configs['AGENT_REFLECT_MONEY'])
  684. // return api_error_return('单笔提现金额小于' . $pay_configs['AGENT_REFLECT_MONEY'] . '元');
  685. if ($money > 5000 && $type == 1)
  686. {
  687. throw new Exception('单笔提现金额大于5000只能选择银行卡');
  688. }
  689. if ($money > 30000)
  690. {
  691. throw new Exception('单笔提现金额不能大于30000元');
  692. }
  693. $insert_log = [ // 提现流水
  694. 'uid' => $uid,
  695. 'apply_money' => $money,
  696. 'status' => 0,
  697. 'zfb_number' => $type==1?$params['number']:'',
  698. 'zfb_name' => $type==1?$params['name']:'',
  699. 'bank_no'=>$type==2?$params['number']:'',
  700. 'bank_name'=>$type==2?$params['name']:'',
  701. 'bank_subname'=>$type==2?$params['bank_subname']:'',
  702. 'real_name'=>'',
  703. 'channel' => intval($type),
  704. 'service_money' => $service_money,
  705. // 'service_tz' => $tz_num,
  706. 'transfer_type' => 1,
  707. 'practical_money' => $practical_money,
  708. 'ip' => get_client_ip(),
  709. 'mchid' => '111', // 商户号
  710. 'w_ordersn'=>('sd'. date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999)))
  711. ];
  712. // sr_log(json_encode($insert_log));
  713. edit_user_money(9, $uid, $money);
  714. // edit_user_tz(3, $uid, $tz_num);
  715. $log_id = Db::name('withdraw_log')->insertGetId($insert_log);
  716. if (!Db::name('withdraw_accoiuntinfo')->where('uid', $uid)->where('type', $type)->where('number', $params['number'])->find()){
  717. Db::name('withdraw_accoiuntinfo')->insert([
  718. 'type'=>$type,
  719. 'name'=>$params['name'],
  720. 'number'=>$params['number'],
  721. 'bank_subname'=>$type==1?'':$params['bank_subname'],
  722. 'create_time'=>sr_getcurtime(time()),
  723. 'uid'=>$uid
  724. ]);
  725. }
  726. Db::name('user')->where('id', $uid)->inc('total_withdraw', $money)->update();
  727. }
  728. public function exchargeMoneyBefore($uid, $money, $mobile){
  729. if (!preg_match("/^[1-9][0-9]*$/" ,$money)){
  730. throw new Exception('输入非法');
  731. }
  732. $user_info = Db::name('user')->where('id', $uid)->find();
  733. if ($user_info['user_type'] == 9){
  734. // if (!in_array($uid,explode('|', env('APP.WITHDRAW_FUCHI_OPEN')))){
  735. sr_throw('不可转赠余额');
  736. // }
  737. }
  738. $recevice_user_info = Db::name('user')->where('mobile', $mobile)->field('*')->find();
  739. if (!$recevice_user_info){
  740. throw new Exception('查询好友失败,请核对好友手机号');
  741. }
  742. if ($user_info['status'] != 1){
  743. throw new Exception('用户状态错误,请联系客服');
  744. }
  745. $is_can = false;
  746. if (strpos($recevice_user_info['path'], strval($uid)) !== false){
  747. // 给下级转
  748. $is_can = true;
  749. }
  750. if (strpos($user_info['path'], strval($recevice_user_info['id'])) !== false){
  751. // 给上级转
  752. $is_can = true;
  753. }
  754. if (!$is_can){
  755. sr_throw('只能上下级转赠余额');
  756. }
  757. $need_jhmoney = $money*5/100;
  758. $need_jh = number_format($need_jhmoney / sysconfig('jhmoney', 'jh_money'), '4', '.','');
  759. // if ($user_info['tz_num'] < $need_jh){
  760. // sr_throw('当前通证不足'.$need_jh);
  761. // }
  762. return [$user_info, $recevice_user_info, $need_jh];
  763. }
  764. public function exchargeMoneySubmit($uid, $money, $mobile){
  765. [$user, $recevice_user_info, $need_jh] = $this->exchargeMoneyBefore($uid, $money, $mobile);
  766. return ['avatar'=>($recevice_user_info['avatar']=='static/img/user/default_avatar.jpg'?__HTTPLOCALIMG($recevice_user_info['avatar']) :__HTTPIMG($recevice_user_info['avatar'])), 'mobile'=>$mobile, 'nickname'=>$recevice_user_info['nickname']];
  767. // edit_user_xz(5, uid, $need_xz);
  768. // edit_user_xz(6, $recevice_user_info['id'], $xz_num);
  769. // Db::name('user')->where('id', $recevice_user_info['id'])->save(['is_excharge_lock'=>1]);
  770. }
  771. public function exchargeMoneyDone($uid, $money, $mobile, $sendmbile){
  772. [$user, $recevice_user_info, $need_jh] = $this->exchargeMoneyBefore($uid, $money, $mobile);
  773. if ($user['money'] < $money){
  774. sr_throw('余额不足');
  775. }
  776. if (strpos($user['path'], '5344240') || intval($uid) == 5344240){
  777. if ((strpos($user['path'], '5344327') || intval($uid) == 5344327) || (strpos($user['path'], '5344253') || intval($uid) == 5344253)){
  778. }else{
  779. sr_throw('失败,请联系上面团队长');
  780. }
  781. }
  782. edit_user_money(5,$uid, $money, '', $recevice_user_info['id']);
  783. edit_user_tz(4, $uid, $need_jh);
  784. // addUserMessage($uid, 2, '余额转赠成功', '您已经转给你好友'.$money.'.并扣除通证服务费用:'.$need_jh);
  785. addUserMessage($uid, 2, '余额转赠成功,', '您已经转给你好友:'.$mobile.'转赠'.$money.'.');
  786. edit_user_money(4, $recevice_user_info['id'], $money, '', $uid);
  787. addUserMessage($recevice_user_info['id'], 4, '收到好友转赠成功', '您已经收到好友'.$sendmbile.'的余额转赠:'.$money.'.');
  788. }
  789. public function exchargeJhBefore($uid, $money, $mobile){
  790. if (!preg_match("/^[1-9][0-9]*$/" ,$money)){
  791. throw new Exception('输入非法');
  792. }
  793. $user_info = Db::name('user')->where('id', $uid)->find();
  794. $recevice_user_info = Db::name('user')->where('mobile', $mobile)->field('*')->find();
  795. if (!$recevice_user_info){
  796. throw new Exception('查询好友失败,请核对好友手机号');
  797. }
  798. if ($user_info['status'] != 1){
  799. throw new Exception('用户状态错误,请联系客服');
  800. }
  801. // $need_jh = $money+ $money*5/100;
  802. $need_jh = $money;
  803. // if ($user_info['tz_num'] < $need_jh){
  804. // sr_throw('当前通证不足');
  805. // }
  806. return [$user_info, $recevice_user_info, $need_jh];
  807. }
  808. public function exchargeJhSubmit($uid, $money, $mobile){
  809. [$user, $recevice_user_info, $need_jh] = $this->exchargeJhBefore($uid, $money, $mobile);
  810. return ['avatar'=>($recevice_user_info['avatar']=='static/img/user/default_avatar.jpg'?__HTTPLOCALIMG($recevice_user_info['avatar']) :__HTTPIMG($recevice_user_info['avatar'])), 'mobile'=>$mobile, 'nickname'=>$recevice_user_info['nickname']];
  811. }
  812. public function exchargeJHDone($uid, $money, $mobile){
  813. [$user, $recevice_user_info, $need_jh] = $this->exchargeJhBefore($uid, $money, $mobile);
  814. // edit_user_tz(4, $uid, $need_jh-$money);
  815. edit_user_tz(5, $uid, $money, 0, $recevice_user_info['id']);
  816. edit_user_tz(6, $recevice_user_info['id'], $money, 0, $uid);
  817. }
  818. public function JHBuy($uid, $count){
  819. $info = Db::name('user')->where('id', $uid)->find();
  820. $need_money = sysconfig('jhmoney', 'jh_money')*$count;
  821. if ($info['money'] < $need_money){
  822. sr_throw('当前你的余额不足');
  823. }
  824. if ($count < 0){
  825. sr_throw('购买数量有问题');
  826. }
  827. // 找到最大一期
  828. $info = Db::name('jh_sellplan')->where('status', 1)->find();
  829. if ($count > $info['less_num']){
  830. sr_throw('最多可购买'.$info['less_num'].'枚');
  831. }
  832. if ($info['less_num'] > 100){
  833. if ($count <= 100){
  834. sr_throw('100枚起兑');
  835. }
  836. }
  837. Db::name('jh_sellplan')->where('id', $info['id'])->dec('less_num', $count)->update();
  838. edit_user_money(7, $uid, $need_money);
  839. edit_user_tz(1, $uid, $count);
  840. return $info['less_num']-$count;
  841. }
  842. }