User.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\MoneyLog;
  5. use app\common\model\ScoreLog;
  6. use app\common\model\Txjl;
  7. use app\common\model\Kami;
  8. use app\common\library\Ems;
  9. use app\common\library\Sms;
  10. use fast\Random;
  11. use think\Validate;
  12. use think\Db;
  13. /**
  14. * 会员接口
  15. */
  16. class User extends Api
  17. {
  18. protected $noNeedLogin = ['login', 'logingzh','getphone','getOpenid','mobilelogin', 'register','reg', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  19. protected $noNeedRight = '*';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->Usermodel = model('User');
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $site=config('site');
  31. $data=$this->auth->getUserinfo();
  32. $UserGroup=model('UserGroup')->where(['id'=>$data['group_id']])->find();
  33. if(isset($UserGroup['name'])){
  34. $data['Groupname']=$UserGroup['name'];
  35. }else{
  36. $data['Groupname']='';
  37. }
  38. if($site){
  39. $data['config']=$site;
  40. }else{
  41. $data['config']=[];
  42. }
  43. $data['zstx']=$site['zstx']*1;
  44. if($data['dtime']<time() and $data['group_id']>1){
  45. $timediff = $data['dtime']-time();
  46. $data['dqts']=intval($timediff/86400);
  47. }else{
  48. $timediff = $data['dtime']-time();
  49. $data['dqts']=intval($timediff/86400);
  50. }
  51. $data['dtime']=date('Y-m-d',$data['dtime']);
  52. $this->success('',$data);
  53. }
  54. public function duihuan(){
  55. $site=config('site');
  56. $sum=input('sum');
  57. if(!input('sum')){
  58. $this->error('请填写提兑换数量');
  59. }
  60. if($sum<$site['jfdhzs']){
  61. $this->error($site['jfdhsm']);
  62. }
  63. Db::startTrans();
  64. try {
  65. $user = $this->auth->getUser();
  66. if ($user && $sum != 0) {
  67. $before = $user->money;
  68. if($user->money<$sum){
  69. $this->error('积分不够');
  70. }
  71. $after = $user->score - $sum;
  72. //更新会员信息
  73. $user->save(['score' => $after]);
  74. //写入日志
  75. ScoreLog::create(['user_id' => $user->id, 'score' => $sum, 'before' => $before, 'after' => $after,'memo' => '积分兑换']);
  76. }else{
  77. $this->error(__('金额不对'));
  78. }
  79. $usersa = $this->auth->getUser();
  80. $money=$sum*$site['jfdh'];
  81. if ($usersa && $money != 0) {
  82. $before = $usersa->money;
  83. $after = $usersa->money + $money;
  84. //更新会员信息
  85. $usersa->save(['money' => $after]);
  86. //写入日志
  87. MoneyLog::create(['user_id' => $usersa->id, 'money' => $money, 'before' => $before, 'after' => $after,'fid' => 0, 'sxf' =>0,'memo' => '积分兑换']);
  88. }else{
  89. $this->error(__('兑换错误'));
  90. }
  91. Db::commit();
  92. $this->success(__('兑换成功'));
  93. }catch (Exception $e){
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. }
  98. public function mytuandui()
  99. {
  100. $configs=config('site');
  101. $user = $this->auth->getUser();
  102. $usera = \app\common\model\User::where(['pid'=>$this->auth->id])->select();
  103. foreach ($usera as $k=>$v){
  104. if(isset($v['avatar'])){
  105. if(strpos($v['avatar'],'http') !== false){
  106. $usera[$k]['avatar']=$v['avatar'];
  107. }else{
  108. if($v['avatar']){
  109. $usera[$k]['avatar']= $configs['imgurl'].$v['avatar'];
  110. }else{
  111. $usera[$k]['avatar']=$configs['imgurl'].'/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
  112. }
  113. }
  114. }else{
  115. $usera[$k]['avatar']=$configs['imgurl'].'/uploads/20200523/250b3f89b40ff3714b07cc51b4c2f63d.png';
  116. }
  117. }
  118. $this->success('success',$usera);
  119. }
  120. /**
  121. * 会员登录
  122. *
  123. * @param string $account 账号
  124. * @param string $password 密码
  125. */
  126. public function login()
  127. {
  128. $account = $this->request->request('account');
  129. $password = $this->request->request('password');
  130. if (!$account || !$password) {
  131. $this->error(__('Invalid parameters'));
  132. }
  133. $ret = $this->auth->login($account, $password);
  134. if ($ret) {
  135. $userinfo=$this->auth->getUserinfo();
  136. $data = ['config'=>$this->config(),'userinfo' =>$userinfo ];
  137. $this->success(__('Logged in successful'), $data);
  138. } else {
  139. $this->error($this->auth->getError());
  140. }
  141. }
  142. /**
  143. * 手机验证码登录
  144. *
  145. * @param string $mobile 手机号
  146. * @param string $captcha 验证码
  147. */
  148. public function mobilelogin()
  149. {
  150. $mobile = $this->request->request('mobile');
  151. $captcha = $this->request->request('captcha');
  152. if (!$mobile || !$captcha) {
  153. $this->error(__('Invalid parameters'));
  154. }
  155. if (!Validate::regex($mobile, "^1\d{10}$")) {
  156. $this->error(__('Mobile is incorrect'));
  157. }
  158. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  159. $this->error(__('Captcha is incorrect'));
  160. }
  161. $user = \app\common\model\User::getByMobile($mobile);
  162. if ($user) {
  163. if ($user->status != 'normal') {
  164. $this->error(__('Account is locked'));
  165. }
  166. //如果已经有账号则直接登录
  167. $ret = $this->auth->direct($user->id);
  168. } else {
  169. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  170. }
  171. if ($ret) {
  172. Sms::flush($mobile, 'mobilelogin');
  173. $data = ['userinfo' => $this->auth->getUserinfo()];
  174. $this->success(__('Logged in successful'), $data);
  175. } else {
  176. $this->error($this->auth->getError());
  177. }
  178. }
  179. /**
  180. * 注册会员
  181. *
  182. * @param string $username 用户名
  183. * @param string $password 密码
  184. * @param string $email 邮箱
  185. * @param string $mobile 手机号
  186. * @param string $code 验证码
  187. */
  188. public function register()
  189. {
  190. $username = $this->request->request('username');
  191. $password = $this->request->request('password');
  192. $email = $this->request->request('email');
  193. $mobile = $this->request->request('mobile');
  194. $code = $this->request->request('code');
  195. if (!$username || !$password) {
  196. $this->error(__('Invalid parameters'));
  197. }
  198. if ($email && !Validate::is($email, "email")) {
  199. $this->error(__('Email is incorrect'));
  200. }
  201. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  202. $this->error(__('Mobile is incorrect'));
  203. }
  204. $ret = Sms::check($mobile, $code, 'register');
  205. if (!$ret) {
  206. $this->error(__('Captcha is incorrect'));
  207. }
  208. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  209. if ($ret) {
  210. $data = ['userinfo' => $this->auth->getUserinfo()];
  211. $this->success(__('Sign up successful'), $data);
  212. } else {
  213. $this->error($this->auth->getError());
  214. }
  215. }
  216. public function reg()
  217. {
  218. $username = $this->request->request('username');
  219. $password = $this->request->request('password');
  220. $email = $this->request->request('email');
  221. $mobile = $this->request->request('mobile');
  222. $code = $this->request->request('code');
  223. $pid = $this->request->request('pid');
  224. if(!$pid){
  225. $pid=1;
  226. }
  227. $usera = \app\common\model\User::getById($pid);
  228. if(!$usera){
  229. $this->error(__('邀请码不正确'));
  230. }
  231. if (!$username || !$password) {
  232. $this->error(__('Invalid parameters'));
  233. }
  234. if(!$email){
  235. $email=$mobile.'@10068.com';
  236. }
  237. if ($email && !Validate::is($email, "email")) {
  238. $this->error(__('Email is incorrect'));
  239. }
  240. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  241. $this->error(__('Mobile is incorrect'));
  242. }
  243. // $ret = Db::name('sms')->where(['mobile' => $mobile, 'event' => 'register'])
  244. // ->order('id', 'DESC')
  245. // ->find();
  246. // if (!$ret) {
  247. // $this->error(__('Captcha is incorrect'));
  248. // }
  249. // if($ret['code']!=$code){
  250. // $this->error(__('Captcha is incorrect'));
  251. // }
  252. // $ret = Sms::check($mobile, $code, 'register');
  253. // if (!$ret) {
  254. // $this->error(__('Captcha is incorrect'));
  255. // }
  256. $avatar='/uploads/face/A'.rand(1, 96).'.jpg';
  257. $ret = $this->auth->register($username, $password, $email, $mobile, ['avatar'=>$avatar,'pid'=>$pid]);
  258. if ($ret) {
  259. $data = ['userinfo' => $this->auth->getUserinfo()];
  260. $this->success(__('Sign up successful'), $data);
  261. } else {
  262. $this->error($this->auth->getError());
  263. }
  264. }
  265. /**
  266. * 注销登录
  267. */
  268. public function logout()
  269. {
  270. $this->auth->logout();
  271. $this->success(__('Logout successful'));
  272. }
  273. /**
  274. * 修改会员个人信息
  275. *
  276. * @param string $avatar 头像地址
  277. * @param string $username 用户名
  278. * @param string $nickname 昵称
  279. * @param string $bio 个人简介
  280. */
  281. public function profile()
  282. {
  283. $user = $this->auth->getUser();
  284. $username = $this->request->request('username');
  285. $nickname = $this->request->request('nickname');
  286. $bio = $this->request->request('bio');
  287. $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
  288. if ($username) {
  289. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  290. if ($exists) {
  291. $this->error(__('Username already exists'));
  292. }
  293. $user->username = $username;
  294. }
  295. $user->nickname = $nickname;
  296. $user->bio = $bio;
  297. $user->avatar = $avatar;
  298. $user->save();
  299. $this->success();
  300. }
  301. public function edituser()
  302. {
  303. $user = $this->auth->getUser();
  304. $nickname = $this->request->request('nickname');
  305. $birthday = $this->request->request('org');
  306. $gender = $this->request->request('sex');
  307. if($nickname){
  308. $user->nickname = $nickname;
  309. $user->save();
  310. $this->success();
  311. }
  312. if($birthday){
  313. $user->birthday = $birthday;
  314. $user->save();
  315. $this->success();
  316. }
  317. if($gender>=0){
  318. $user->gender = $gender;
  319. $user->save();
  320. $this->success();
  321. }
  322. $this->error(__('修改失败'));
  323. }
  324. /**
  325. * 修改邮箱
  326. *
  327. * @param string $email 邮箱
  328. * @param string $captcha 验证码
  329. */
  330. public function changeemail()
  331. {
  332. $user = $this->auth->getUser();
  333. $email = $this->request->post('email');
  334. $captcha = $this->request->request('captcha');
  335. if (!$email || !$captcha) {
  336. $this->error(__('Invalid parameters'));
  337. }
  338. if (!Validate::is($email, "email")) {
  339. $this->error(__('Email is incorrect'));
  340. }
  341. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  342. $this->error(__('Email already exists'));
  343. }
  344. $result = Ems::check($email, $captcha, 'changeemail');
  345. if (!$result) {
  346. $this->error(__('Captcha is incorrect'));
  347. }
  348. $verification = $user->verification;
  349. $verification->email = 1;
  350. $user->verification = $verification;
  351. $user->email = $email;
  352. $user->save();
  353. Ems::flush($email, 'changeemail');
  354. $this->success();
  355. }
  356. /**
  357. * 修改手机号
  358. *
  359. * @param string $mobile 手机号
  360. * @param string $captcha 验证码
  361. */
  362. public function changemobile()
  363. {
  364. $user = $this->auth->getUser();
  365. $mobile = $this->request->request('mobile');
  366. $captcha = $this->request->request('captcha');
  367. if (!$mobile || !$captcha) {
  368. $this->error(__('Invalid parameters'));
  369. }
  370. if (!Validate::regex($mobile, "^1\d{10}$")) {
  371. $this->error(__('Mobile is incorrect'));
  372. }
  373. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  374. $this->error(__('Mobile already exists'));
  375. }
  376. $result = Sms::check($mobile, $captcha, 'changemobile');
  377. if (!$result) {
  378. $this->error(__('Captcha is incorrect'));
  379. }
  380. $verification = $user->verification;
  381. $verification->mobile = 1;
  382. $user->verification = $verification;
  383. $user->mobile = $mobile;
  384. $user->save();
  385. Sms::flush($mobile, 'changemobile');
  386. $this->success();
  387. }
  388. /**
  389. * 第三方登录
  390. *
  391. * @param string $platform 平台名称
  392. * @param string $code Code码
  393. */
  394. public function third()
  395. {
  396. $url = url('user/index');
  397. $platform = $this->request->request("platform");
  398. $code = $this->request->request("code");
  399. $config = get_addon_config('third');
  400. if (!$config || !isset($config[$platform])) {
  401. $this->error(__('Invalid parameters'));
  402. }
  403. $app = new \addons\third\library\Application($config);
  404. //通过code换access_token和绑定会员
  405. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  406. if ($result) {
  407. $loginret = \addons\third\library\Service::connect($platform, $result);
  408. if ($loginret) {
  409. $data = [
  410. 'userinfo' => $this->auth->getUserinfo(),
  411. 'thirdinfo' => $result
  412. ];
  413. $this->success(__('Logged in successful'), $data);
  414. }
  415. }
  416. $this->error(__('Operation failed'), $url);
  417. }
  418. /**
  419. * 重置密码
  420. *
  421. * @param string $mobile 手机号
  422. * @param string $newpassword 新密码
  423. * @param string $captcha 验证码
  424. */
  425. public function resetpwd()
  426. {
  427. $type = $this->request->request("type");
  428. $mobile = $this->request->request("mobile");
  429. $email = $this->request->request("email");
  430. $newpassword = $this->request->request("newpassword");
  431. $captcha = $this->request->request("captcha");
  432. if (!$newpassword || !$captcha) {
  433. $this->error(__('Invalid parameters'));
  434. }
  435. if ($type == 'mobile') {
  436. if (!Validate::regex($mobile, "^1\d{10}$")) {
  437. $this->error(__('Mobile is incorrect'));
  438. }
  439. $user = \app\common\model\User::getByMobile($mobile);
  440. if (!$user) {
  441. $this->error(__('User not found'));
  442. }
  443. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  444. if (!$ret) {
  445. $this->error(__('Captcha is incorrect'));
  446. }
  447. Sms::flush($mobile, 'resetpwd');
  448. } else {
  449. if (!Validate::is($email, "email")) {
  450. $this->error(__('Email is incorrect'));
  451. }
  452. $user = \app\common\model\User::getByEmail($email);
  453. if (!$user) {
  454. $this->error(__('User not found'));
  455. }
  456. $ret = Ems::check($email, $captcha, 'resetpwd');
  457. if (!$ret) {
  458. $this->error(__('Captcha is incorrect'));
  459. }
  460. Ems::flush($email, 'resetpwd');
  461. }
  462. //模拟一次登录
  463. $this->auth->direct($user->id);
  464. $ret = $this->auth->changepwd($newpassword, '', true);
  465. if ($ret) {
  466. $this->success(__('Reset password successful'));
  467. } else {
  468. $this->error($this->auth->getError());
  469. }
  470. }
  471. public function tx(){
  472. $user = $this->auth->getUser();
  473. $site=config('site');
  474. $money=input('money');
  475. if($money<$site['zstx']){
  476. $this->error($site['txbz']);
  477. }
  478. if(!input('type')){
  479. $this->error('请填写提现类型');
  480. }
  481. if(!input('name')){
  482. $this->error('请填写提现名字');
  483. }
  484. if(!input('cord')){
  485. $this->error('请填写提现帐号');
  486. }
  487. $user_id=$user['id'];
  488. if ($user && $money != 0) {
  489. if($user->money<$money){
  490. $this->error('余额不足');
  491. }
  492. $before = $user->money;
  493. $after = $user->money - $money;
  494. Db::startTrans();
  495. try {
  496. //更新会员信息
  497. $user->save(['money' => $after]);
  498. //写入日志
  499. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户提现']);
  500. $site['txsxf']=isset($site['txsxf'])?$site['txsxf']:0;
  501. $sxf=$money*$site['txsxf'];
  502. $up=[
  503. 'uid' => $user_id,
  504. 'type' => input('type'),
  505. 'name' => input('name'),
  506. 'cord' => input('cord'),
  507. 'money' => $money,
  508. 'moneydz' => $money-$sxf,
  509. 'sxf' => $sxf,
  510. 'iscl' => 1,
  511. 'memo' => '用户提现'
  512. ];
  513. //写入提现记录
  514. Txjl::create($up);
  515. Db::commit();
  516. $this->success('提交成功');
  517. }catch (Exception $e){
  518. Db::rollback();
  519. $this->error($e->getMessage());
  520. }
  521. }else{
  522. $this->error(__('金额不对'));
  523. }
  524. }
  525. public function txinfo()
  526. {
  527. $user = $this->auth->getUser();
  528. $this->relationSearch = true;
  529. //如果发送的来源是Selectpage,则转发到Selectpage
  530. if ($this->request->request('keyField')) {
  531. return $this->selectpage();
  532. }
  533. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  534. $map['uid']=$user['id'];
  535. $total = Txjl::where($where)->where($map)->order($sort, $order)->count();
  536. $list = Txjl::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  537. if($list){
  538. foreach ($list as $k=>$v){
  539. $list[$k]['createtime']=date('Y-m-d',$v['createtime']);
  540. }
  541. }
  542. $result = array("total" => $total, "rows" => $list);
  543. return json($result);
  544. }
  545. public function userscoreinfo()
  546. {
  547. $user = $this->auth->getUser();
  548. $this->relationSearch = true;
  549. //如果发送的来源是Selectpage,则转发到Selectpage
  550. if ($this->request->request('keyField')) {
  551. return $this->selectpage();
  552. }
  553. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  554. $map['user_id']=$user['id'];
  555. $total = ScoreLog::where($where)->where($map)->order($sort, $order)->count();
  556. $list = ScoreLog::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  557. if($list){
  558. foreach ($list as $k=>$v){
  559. $list[$k]['createtime']=date('Y-m-d H:i',$v['createtime']);
  560. if($v['after']>$v['before']){
  561. $list[$k]['jzt']='+';
  562. }else if($v['after']==$v['before']){
  563. $list[$k]['jzt']='';
  564. }else{
  565. $list[$k]['jzt']='-';
  566. }
  567. }
  568. }
  569. $result = array("total" => $total, "rows" => $list);
  570. return json($result);
  571. }
  572. public function usermoneyinfo()
  573. {
  574. $user = $this->auth->getUser();
  575. $this->relationSearch = true;
  576. //如果发送的来源是Selectpage,则转发到Selectpage
  577. if ($this->request->request('keyField')) {
  578. return $this->selectpage();
  579. }
  580. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  581. $map['user_id']=$user['id'];
  582. $total = MoneyLog::where($where)->where($map)->order($sort, $order)->count();
  583. $list = MoneyLog::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  584. if($list){
  585. foreach ($list as $k=>$v){
  586. $list[$k]['createtime']=date('Y-m-d H:i',$v['createtime']);
  587. if($v['after']>$v['before']){
  588. $list[$k]['jzt']='+';
  589. }else if($v['after']==$v['before']){
  590. $list[$k]['jzt']='';
  591. }else{
  592. $list[$k]['jzt']='-';
  593. }
  594. }
  595. }
  596. $result = array("total" => $total, "rows" => $list);
  597. return json($result);
  598. }
  599. public function userfxinfo()
  600. {
  601. $user = $this->auth->getUser();
  602. $this->relationSearch = true;
  603. //如果发送的来源是Selectpage,则转发到Selectpage
  604. if ($this->request->request('keyField')) {
  605. return $this->selectpage();
  606. }
  607. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  608. $map['user_id']=$user['id'];
  609. $map['oid']=['>',0];
  610. $total = MoneyLog::where($where)->where($map)->order($sort, $order)->count();
  611. $list = MoneyLog::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  612. if($list){
  613. foreach ($list as $k=>$v){
  614. $list[$k]['createtime']=date('Y-m-d H:i',$v['createtime']);
  615. if($v['after']>$v['before']){
  616. $list[$k]['jzt']='+';
  617. }else if($v['after']==$v['before']){
  618. $list[$k]['jzt']='';
  619. }else{
  620. $list[$k]['jzt']='-';
  621. }
  622. }
  623. }
  624. $result = array("total" => $total, "rows" => $list);
  625. return json($result);
  626. }
  627. /**
  628. * 上传文件
  629. */
  630. public function upload()
  631. {
  632. return action('api/common/upload');
  633. }
  634. /**
  635. * 上传头像
  636. */
  637. public function avatar()
  638. {
  639. if(input('base64')){
  640. return action('api/common/base64_image_content');
  641. }
  642. return action('api/common/uploadavatar');
  643. }
  644. public function paysj()
  645. {
  646. $site=config('site');
  647. $arr=[];
  648. if(isset($site['vipsj'])){
  649. foreach ($site['vipsj'] as $k=>$v){
  650. //一年(¥80) num:'10',coupon:0
  651. $arr[]=['num'=>$k.'个月(¥'.$v.')','coupon'=>$k];
  652. }
  653. }
  654. $this->success('success',$arr);
  655. }
  656. public function taplove(){
  657. $up=[
  658. 'uid'=>input('uid'),
  659. 'vid'=>input('vid'),
  660. 'mid'=>input('mid'),
  661. 'vtime'=>0,
  662. 'createtime'=>time(),
  663. 'updatetime'=>time(),
  664. ];
  665. $videolove=Db::name('videolove')->where(['uid'=>input('uid'),'vid'=>input('vid'),'mid'=>input('mid')])->find();
  666. if($videolove){
  667. Db::name('videolove')->where(['id'=>$videolove['id']])->delete();
  668. Db::name('videolist')->where(['id'=>input('vid')])->setDec('give');
  669. $this->success('取消成功');
  670. }else{
  671. $id=Db::name('videolove')->insertGetId($up);
  672. Db::name('videolist')->where(['id'=>input('vid')])->setInc('give');
  673. $this->success('点赞成功');
  674. }
  675. }
  676. public function videorecord(){
  677. $up=[
  678. 'uid'=>input('uid'),
  679. 'vid'=>input('vid'),
  680. 'mid'=>input('mid'),
  681. 'vtime'=>0,
  682. 'createtime'=>time(),
  683. 'updatetime'=>time(),
  684. ];
  685. $videorecord=Db::name('videorecord')->where(['uid'=>input('uid'),'vid'=>input('vid'),'mid'=>input('mid')])->find();
  686. if($videorecord){
  687. Db::name('videorecord')->where(['id'=>$videorecord['id']])->delete();
  688. $this->success('取消成功');
  689. }else{
  690. $id=Db::name('videorecord')->insertGetId($up);
  691. $this->success('追剧成功');
  692. }
  693. }
  694. public function qingdao(){
  695. $times=date('Y-m-d',time());
  696. $qdjl=Db::name('qdjl')->where(['uid'=>input('uid'),'time'=>$times])->find();
  697. $yesterday=date("Y-m-d",strtotime("-1 day"));
  698. $yesterdaydata=Db::name('qdjl')->where(['uid'=>input('uid'),'time'=>$yesterday])->find();
  699. if($yesterdaydata){
  700. $sum= $yesterdaydata['sum']+1;
  701. $tdsy=$yesterdaydata['sy']+50;
  702. if($sum>=7){//连续7天
  703. $sum=1;
  704. }
  705. }else{
  706. $sum=1;
  707. $tdsy=100;
  708. }
  709. if($qdjl){
  710. $this->error('今日已经签到过了');
  711. }
  712. $up=[
  713. 'uid'=>input('uid'),
  714. 'sy'=>$tdsy,
  715. 'sum'=>$sum,
  716. 'time'=>$times,
  717. ];
  718. $this->qdmoney($sum);
  719. $id=Db::name('qdjl')->insertGetId($up);
  720. $this->success('签到成功');
  721. }
  722. public function vipsj()
  723. {
  724. $site=config('site');
  725. $num=input('num');
  726. if(!input('num')){
  727. $this->error('请填选择会员升级期限');
  728. }
  729. if(isset($site['vipsj'])){
  730. //var_dump();
  731. $money=$site['vipsj'][$num];
  732. $user = $this->auth->getUser();
  733. if($user->money<$money){
  734. $this->error('余额不足');
  735. }
  736. $before = $user->money;
  737. $after = $user->money - $money;
  738. Db::startTrans();
  739. try {
  740. //更新会员信息
  741. if($user->dtime-time()<=0 or $user->group_id==1){
  742. $timea=strtotime("+".$num." months", time());
  743. }else{
  744. $timea=strtotime("+".$num." months", $user->dtime);
  745. }
  746. $user->save(['money' => $after,'group_id' =>2,'stime'=>time(),'dtime' =>$timea]);
  747. //写入日志
  748. MoneyLog::create(['user_id' => $user['id'], 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '会员升级']);
  749. $this->yongjin(1,$user['id'],$money,'级升级佣金结算');//佣金结算
  750. Db::commit();
  751. $this->success('升级成功',['config'=>$this->config(),'userinfo' => $this->auth->getUserinfo()]);
  752. }catch (Exception $e){
  753. Db::rollback();
  754. $this->error($e->getMessage());
  755. }
  756. }
  757. }
  758. public function kami()
  759. {
  760. $crd=input('crd');
  761. if(!$crd){
  762. $this->error('请填写卡密');
  763. }
  764. $Kami=Kami::where(['crd'=>$crd])->find();
  765. if(!$Kami){
  766. $this->error('卡号不正确');
  767. }
  768. if($Kami['type']==2){
  769. $this->error('已经使用');
  770. }
  771. $money=$Kami['price'];
  772. $user = $this->auth->getUser();
  773. $before = $user->money;
  774. $after = $user->money + $money;
  775. Db::startTrans();
  776. try {
  777. //更新会员信息
  778. $user->save(['money' => $after]);
  779. //写入日志
  780. MoneyLog::create(['user_id' => $user['id'], 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '卡密充值']);
  781. Kami::where(['crd'=>$crd])->update(['uid'=>$user['id'],'type'=>2,'stime'=>time()]);
  782. Db::commit();
  783. $data = ['config'=>$this->config(),'userinfo' => $this->auth->getUserinfo()];
  784. $this->success('充值成功',$data);
  785. }catch (Exception $e){
  786. Db::rollback();
  787. $this->error($e->getMessage());
  788. }
  789. }
  790. public function getOpenid() {
  791. $site=config('site');
  792. $site['dltel']=isset($site['dltel'])?$site['dltel']:-1;
  793. $code = input('code');//小程序传来的code值
  794. $appid =$site['weixinxcx']['appid'];//小程序的appid
  795. $appSecret = $site['weixinxcx']['appSecret'];// 小程序的$appSecret
  796. $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
  797. $getUrl = sprintf($wxUrl, $appid, $appSecret, $code);//把appid,appsecret,code拼接到url里
  798. $result = $this->curl_get($getUrl);//请求拼接好的url
  799. $wxResult = json_decode($result, true);
  800. if (empty($wxResult)) {
  801. $this->error('获取openid时异常,微信内部错误');
  802. } else {
  803. $loginFail = array_key_exists('errcode', $wxResult);
  804. if ($loginFail) {//请求失败
  805. $this->error($wxResult);
  806. } else {//请求成功
  807. $openid = $wxResult['openid'];
  808. if($site['dltel']==0){//等于0的时候直接登录
  809. $users = \app\common\model\User::getByWxxopenid($openid);
  810. if($users){
  811. $this->auth->direct($users->id);
  812. $data = ['openid'=>$openid,'userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  813. $this->success(__('登录成功'), $data);
  814. }
  815. }
  816. $wxResult['dltel']=$site['dltel'];
  817. $this->success('获取openid成功',$wxResult);
  818. }
  819. }
  820. }
  821. public function logingzh(){
  822. $openid = input('openid');
  823. $unionid = input('unionid');
  824. $users = \app\common\model\User::getByUnionid($unionid);
  825. //$users = \app\common\model\User::getByGzhopenid($openid);//仅仅公众号
  826. if($users){
  827. if ($users->status != 'normal') {
  828. $this->error(__('Account is locked'));
  829. }
  830. //如果已经有账号则直接登录
  831. $ret = $this->auth->direct($users->id);
  832. if ($ret) {
  833. \app\common\model\User::where(['id'=> $users->id])->update(['gzhopenid'=>$openid]);
  834. $data = ['userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  835. $this->success(__('Logged in successful'), $data);
  836. }
  837. }else{
  838. $pid = $this->request->request('pid');
  839. if(!$pid){
  840. $pid=1;
  841. }
  842. $usera = \app\common\model\User::getById($pid);
  843. if(!$usera){
  844. $this->error(__('邀请码不正确'));
  845. }
  846. $mobile=$openid;
  847. $email=$mobile.'@100.com';
  848. $avatar='/uploads/face/A'.rand(1, 96).'.jpg';;
  849. $nickName=input('nickName','公众号用户');
  850. $ret = $this->auth->register($mobile, '123456', $email, $mobile, ['wxxopenid'=>$openid,'unionid'=>$unionid,'nickname'=>$nickName,'avatar'=>$avatar,'pid'=>$pid]);
  851. if ($ret) {
  852. $data = ['userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  853. $this->success(__('Sign up successful'), $data);
  854. } else {
  855. $this->error($this->auth->getError());
  856. }
  857. }
  858. }
  859. public function getphone()
  860. {
  861. $site=config('site');
  862. $dltel=isset($site['dltel'])?$site['dltel']:-1;
  863. if($dltel==0){
  864. $openid=input('openid');
  865. $users = \app\common\model\User::getByWxxopenid($openid);
  866. if($users){
  867. $this->auth->direct($users->id);
  868. $data = ['userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  869. $this->success(__('登录成功'), $data);
  870. }else{
  871. $pid = $this->request->request('pid');
  872. if(!$pid){
  873. $pid=1;
  874. }
  875. $usera = \app\common\model\User::getById($pid);
  876. if(!$usera){
  877. $this->error(__('邀请码不正确'));
  878. }
  879. //$number = date('ymdh', time()) . rand(10000, 99999);
  880. $mobile=$openid;
  881. $email=$mobile.'@100.com';
  882. $avatar=input('avatarUrl');
  883. $nickName=input('nickName');
  884. $ret = $this->auth->register($mobile, '123456', $email, $mobile, ['wxxopenid'=>$openid,'nickname'=>$nickName,'avatar'=>$avatar,'pid'=>$pid]);
  885. if ($ret) {
  886. $users=$this->auth->getUserinfo();
  887. //var_dump($users);
  888. $idsa=$users['id'];
  889. \app\common\model\User::where(['id'=> $idsa])->update(['mobile'=>'user_'.$idsa,'email'=>'user_'.$idsa.'@100.com','username'=>'user_'.$idsa]);
  890. $data = ['userinfo' => $users,'config'=>$this->config()];
  891. $this->success(__('Sign up successful'), $data);
  892. } else {
  893. $this->error($this->auth->getError());
  894. }
  895. }
  896. }
  897. $APPID =$site['weixinxcx']['appid'];//小程序的appid
  898. $AppSecret = $site['weixinxcx']['appSecret'];// 小程序的$appSecret
  899. if(!input('code')){
  900. $this->error('code出错');
  901. exit;
  902. }
  903. $code=input('code');
  904. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $APPID . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
  905. $arr = $this->vget($url); // 一个使用curl实现的get方法请求
  906. $arr = json_decode($arr, true);
  907. $session_key = $arr['session_key'];
  908. $openid = $arr['openid'];
  909. $unionid = isset($arr['unionid'])?$arr['unionid']:null;
  910. Vendor("wxBizData.wxBizDataCrypt"); //加载解密文件,在官方有下载
  911. $pc = new \WXBizDataCrypt($APPID, $session_key);
  912. $iv = input('iv');
  913. $encryptedData = input('encryptedData');
  914. $errCode = $pc->decryptData($encryptedData, $iv, $data );
  915. if ($errCode == 0) {
  916. //print($data . "\n");
  917. //dump($arr);
  918. $phone = json_decode($data,true);
  919. if($phone['purePhoneNumber']){
  920. //$users = \app\common\model\User::getByMobile($phone['purePhoneNumber']);
  921. $user = \app\common\model\User::getByUnionid($unionid);
  922. if ($user) {
  923. if ($user->status != 'normal') {
  924. $this->error(__('Account is locked'));
  925. }
  926. //如果已经有账号则直接登录
  927. $ret = $this->auth->direct($user->id);
  928. if ($ret) {
  929. $data = ['userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  930. \app\common\model\User::where(['id'=> $user->id])->update(['mobile'=>$phone['purePhoneNumber'],'wxxopenid'=>$openid,'unionid'=>$unionid]);
  931. $this->success(__('Logged in successful'), $data);
  932. }
  933. }else{
  934. $users = \app\common\model\User::getByMobile($phone['purePhoneNumber']);
  935. if($users){
  936. if ($users->status != 'normal') {
  937. $this->error(__('Account is locked'));
  938. }
  939. //如果已经有账号则直接登录
  940. $ret = $this->auth->direct($users->id);
  941. if ($ret) {
  942. $data = ['userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  943. \app\common\model\User::where(['id'=> $users->id])->update(['mobile'=>$phone['purePhoneNumber'],'wxxopenid'=>$openid,'unionid'=>$unionid]);
  944. $this->success(__('Logged in successful'), $data);
  945. }
  946. }else{
  947. $pid = $this->request->request('pid');
  948. if(!$pid){
  949. $pid=1;
  950. }
  951. $usera = \app\common\model\User::getById($pid);
  952. if(!$usera){
  953. $this->error(__('邀请码不正确'));
  954. }
  955. $mobile=$phone['purePhoneNumber'];
  956. $email=$mobile.'@100.com';
  957. $avatar=input('avatarUrl');
  958. $nickName=input('nickName');
  959. $ret = $this->auth->register($mobile, '123456', $email, $mobile, ['wxxopenid'=>$openid,'unionid'=>$unionid,'nickname'=>$nickName,'avatar'=>$avatar,'pid'=>$pid]);
  960. if ($ret) {
  961. $data = ['userinfo' => $this->auth->getUserinfo(),'config'=>$this->config()];
  962. $this->success(__('Sign up successful'), $data);
  963. } else {
  964. $this->error($this->auth->getError());
  965. }
  966. }
  967. }
  968. }
  969. }else{
  970. $this->error($errCode);
  971. }
  972. }
  973. public function qdmoney($money){
  974. $site=config('site');
  975. $user = $this->auth->getUser();
  976. if ($user && $money != 0) {
  977. $before = $user->money;
  978. $after = $user->money + $money;
  979. //更新会员信息
  980. $user->save(['score' => $after]);
  981. //写入日志
  982. ScoreLog::create(['user_id' => $user->id, 'score' => $money, 'before' => $before, 'after' => $after,'memo' => '签到赠送']);
  983. }else{
  984. $this->error(__('金额不对'));
  985. }
  986. }
  987. public function admoney(){
  988. $site=config('site');
  989. $money=isset($site['admoney'])?$site['admoney']:0.01;
  990. $user = $this->auth->getUser();
  991. if ($user && $money != 0) {
  992. $before = $user->money;
  993. $after = $user->money + $money;
  994. //更新会员信息
  995. $user->save(['money' => $after]);
  996. //写入日志
  997. MoneyLog::create(['user_id' => $user->id, 'money' => $money, 'before' => $before, 'after' => $after,'fid' => 0, 'sxf' =>0,'memo' => '看广告赠送']);
  998. $this->success('获取成功',$this->config());
  999. }else{
  1000. $this->error(__('金额不对'));
  1001. }
  1002. }
  1003. /**
  1004. * @Author : kiven
  1005. * @Description:curl模拟请求方法
  1006. * @LastModify : kiven
  1007. * @param $url
  1008. * @return bool|string
  1009. */
  1010. public function vget($url){
  1011. $info=curl_init();
  1012. curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
  1013. curl_setopt($info,CURLOPT_HEADER,0);
  1014. curl_setopt($info,CURLOPT_NOBODY,0);
  1015. curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
  1016. curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
  1017. curl_setopt($info,CURLOPT_URL,$url);
  1018. $output= curl_exec($info);
  1019. curl_close($info);
  1020. return $output;
  1021. }
  1022. //php请求网络的方法
  1023. public function curl_get($url, &$httpCode = 0) {
  1024. $ch = curl_init();
  1025. curl_setopt($ch, CURLOPT_URL, $url);
  1026. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1027. //不做证书校验,部署在linux环境下请改为true
  1028. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1029. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  1030. $file_contents = curl_exec($ch);
  1031. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  1032. curl_close($ch);
  1033. return $file_contents;
  1034. }
  1035. }