User.php 40 KB

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