User.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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')&&!input('img')) {
  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. 'img' => input('img'),
  514. 'money' => $money,
  515. 'moneydz' => $money - $sxf,
  516. 'sxf' => $sxf,
  517. 'iscl' => 1,
  518. 'memo' => '用户提现'
  519. ];
  520. //写入提现记录
  521. Txjl::create($up);
  522. Db::commit();
  523. $this->success('提交成功');
  524. } catch (Exception $e) {
  525. Db::rollback();
  526. $this->error($e->getMessage());
  527. }
  528. } else {
  529. $this->error(__('金额不对'));
  530. }
  531. }
  532. public function txinfo()
  533. {
  534. $user = $this->auth->getUser();
  535. $this->relationSearch = true;
  536. //如果发送的来源是Selectpage,则转发到Selectpage
  537. if ($this->request->request('keyField')) {
  538. return $this->selectpage();
  539. }
  540. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  541. $map['uid'] = $user['id'];
  542. $total = Txjl::where($where)->where($map)->order($sort, $order)->count();
  543. $list = Txjl::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  544. if ($list) {
  545. foreach ($list as $k => $v) {
  546. $list[$k]['createtime'] = date('Y-m-d', $v['createtime']);
  547. }
  548. }
  549. $result = array("total" => $total, "rows" => $list);
  550. return json($result);
  551. }
  552. public function userscoreinfo()
  553. {
  554. $user = $this->auth->getUser();
  555. $this->relationSearch = true;
  556. //如果发送的来源是Selectpage,则转发到Selectpage
  557. if ($this->request->request('keyField')) {
  558. return $this->selectpage();
  559. }
  560. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  561. $map['user_id'] = $user['id'];
  562. $total = ScoreLog::where($where)->where($map)->order($sort, $order)->count();
  563. $list = ScoreLog::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  564. if ($list) {
  565. foreach ($list as $k => $v) {
  566. $list[$k]['createtime'] = date('Y-m-d H:i', $v['createtime']);
  567. if ($v['after'] > $v['before']) {
  568. $list[$k]['jzt'] = '+';
  569. } else if ($v['after'] == $v['before']) {
  570. $list[$k]['jzt'] = '';
  571. } else {
  572. $list[$k]['jzt'] = '-';
  573. }
  574. }
  575. }
  576. $result = array("total" => $total, "rows" => $list);
  577. return json($result);
  578. }
  579. public function usermoneyinfo()
  580. {
  581. $user = $this->auth->getUser();
  582. $this->relationSearch = true;
  583. //如果发送的来源是Selectpage,则转发到Selectpage
  584. if ($this->request->request('keyField')) {
  585. return $this->selectpage();
  586. }
  587. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  588. $map['user_id'] = $user['id'];
  589. $total = MoneyLog::where($where)->where($map)->order($sort, $order)->count();
  590. $list = MoneyLog::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  591. if ($list) {
  592. foreach ($list as $k => $v) {
  593. $list[$k]['createtime'] = date('Y-m-d H:i', $v['createtime']);
  594. if ($v['after'] > $v['before']) {
  595. $list[$k]['jzt'] = '+';
  596. } else if ($v['after'] == $v['before']) {
  597. $list[$k]['jzt'] = '';
  598. } else {
  599. $list[$k]['jzt'] = '-';
  600. }
  601. }
  602. }
  603. $result = array("total" => $total, "rows" => $list);
  604. return json($result);
  605. }
  606. public function userfxinfo()
  607. {
  608. $user = $this->auth->getUser();
  609. $this->relationSearch = true;
  610. //如果发送的来源是Selectpage,则转发到Selectpage
  611. if ($this->request->request('keyField')) {
  612. return $this->selectpage();
  613. }
  614. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  615. $map['user_id'] = $user['id'];
  616. $map['oid'] = ['>', 0];
  617. $total = MoneyLog::where($where)->where($map)->order($sort, $order)->count();
  618. $list = MoneyLog::where($where)->where($map)->order($sort, $order)->limit($offset, $limit)->select();
  619. if ($list) {
  620. foreach ($list as $k => $v) {
  621. $list[$k]['createtime'] = date('Y-m-d H:i', $v['createtime']);
  622. if ($v['after'] > $v['before']) {
  623. $list[$k]['jzt'] = '+';
  624. } else if ($v['after'] == $v['before']) {
  625. $list[$k]['jzt'] = '';
  626. } else {
  627. $list[$k]['jzt'] = '-';
  628. }
  629. }
  630. }
  631. $result = array("total" => $total, "rows" => $list);
  632. return json($result);
  633. }
  634. /**
  635. * 上传文件
  636. */
  637. public function upload()
  638. {
  639. return action('api/common/upload');
  640. }
  641. /**
  642. * 上传头像
  643. */
  644. public function avatar()
  645. {
  646. if (input('base64')) {
  647. return action('api/common/base64_image_content');
  648. }
  649. return action('api/common/uploadavatar');
  650. }
  651. public function paysj()
  652. {
  653. $site = config('site');
  654. $arr = [];
  655. if (isset($site['vipsj'])) {
  656. foreach ($site['vipsj'] as $k => $v) {
  657. //一年(¥80) num:'10',coupon:0
  658. $arr[] = ['num' => $k . '个月(¥' . $v . ')', 'coupon' => $k];
  659. }
  660. }
  661. $this->success('success', $arr);
  662. }
  663. public function taplove()
  664. {
  665. $up = [
  666. 'uid' => input('uid'),
  667. 'vid' => input('vid'),
  668. 'mid' => input('mid'),
  669. 'vtime' => 0,
  670. 'createtime' => time(),
  671. 'updatetime' => time(),
  672. ];
  673. $videolove = Db::name('videolove')->where(['uid' => input('uid'), 'vid' => input('vid'), 'mid' => input('mid')])->find();
  674. if ($videolove) {
  675. Db::name('videolove')->where(['id' => $videolove['id']])->delete();
  676. Db::name('videolist')->where(['id' => input('vid')])->setDec('give');
  677. $this->success('取消成功');
  678. } else {
  679. $id = Db::name('videolove')->insertGetId($up);
  680. Db::name('videolist')->where(['id' => input('vid')])->setInc('give');
  681. $this->success('点赞成功');
  682. }
  683. }
  684. public function videorecord()
  685. {
  686. $up = [
  687. 'uid' => input('uid'),
  688. 'vid' => input('vid'),
  689. 'mid' => input('mid'),
  690. 'vtime' => 0,
  691. 'createtime' => time(),
  692. 'updatetime' => time(),
  693. ];
  694. $videorecord = Db::name('videorecord')->where(['uid' => input('uid'), 'vid' => input('vid'), 'mid' => input('mid')])->find();
  695. if ($videorecord) {
  696. Db::name('videorecord')->where(['id' => $videorecord['id']])->delete();
  697. $this->success('取消成功');
  698. } else {
  699. $id = Db::name('videorecord')->insertGetId($up);
  700. $this->success('追剧成功');
  701. }
  702. }
  703. public function qingdao()
  704. {
  705. $times = date('Y-m-d', time());
  706. $qdjl = Db::name('qdjl')->where(['uid' => input('uid'), 'time' => $times])->find();
  707. $yesterday = date("Y-m-d", strtotime("-1 day"));
  708. $yesterdaydata = Db::name('qdjl')->where(['uid' => input('uid'), 'time' => $yesterday])->find();
  709. if ($yesterdaydata) {
  710. $sum = $yesterdaydata['sum'] + 1;
  711. $tdsy = $yesterdaydata['sy'] + 50;
  712. if ($sum >= 7) {//连续7天
  713. $sum = 1;
  714. }
  715. } else {
  716. $sum = 1;
  717. $tdsy = 100;
  718. }
  719. if ($qdjl) {
  720. $this->error('今日已经签到过了');
  721. }
  722. $up = [
  723. 'uid' => input('uid'),
  724. 'sy' => $tdsy,
  725. 'sum' => $sum,
  726. 'time' => $times,
  727. ];
  728. $this->qdmoney($sum);
  729. $id = Db::name('qdjl')->insertGetId($up);
  730. $this->success('签到成功');
  731. }
  732. public function vipsj()
  733. {
  734. $site = config('site');
  735. $num = input('num');
  736. if (!input('num')) {
  737. $this->error('请填选择会员升级期限');
  738. }
  739. if (isset($site['vipsj'])) {
  740. //var_dump();
  741. $money = $site['vipsj'][$num];
  742. $user = $this->auth->getUser();
  743. if ($user->money < $money) {
  744. $this->error('余额不足');
  745. }
  746. $before = $user->money;
  747. $after = $user->money - $money;
  748. Db::startTrans();
  749. try {
  750. //更新会员信息
  751. if ($user->dtime - time() <= 0 or $user->group_id == 1) {
  752. $timea = strtotime("+" . $num . " months", time());
  753. } else {
  754. $timea = strtotime("+" . $num . " months", $user->dtime);
  755. }
  756. $user->save(['money' => $after, 'group_id' => 2, 'stime' => time(), 'dtime' => $timea]);
  757. //写入日志
  758. MoneyLog::create(['user_id' => $user['id'], 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '会员升级']);
  759. $this->yongjin(1, $user['id'], $money, '级升级佣金结算');//佣金结算
  760. Db::commit();
  761. $this->success('升级成功', ['config' => $this->config(), 'userinfo' => $this->auth->getUserinfo()]);
  762. } catch (Exception $e) {
  763. Db::rollback();
  764. $this->error($e->getMessage());
  765. }
  766. }
  767. }
  768. public function kami()
  769. {
  770. $crd = input('crd');
  771. if (!$crd) {
  772. $this->error('请填写卡密');
  773. }
  774. $Kami = Kami::where(['crd' => $crd])->find();
  775. if (!$Kami) {
  776. $this->error('卡号不正确');
  777. }
  778. if ($Kami['type'] == 2) {
  779. $this->error('已经使用');
  780. }
  781. $money = $Kami['price'];
  782. $user = $this->auth->getUser();
  783. $before = $user->money;
  784. $after = $user->money + $money;
  785. Db::startTrans();
  786. try {
  787. //更新会员信息
  788. $user->save(['money' => $after]);
  789. //写入日志
  790. MoneyLog::create(['user_id' => $user['id'], 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '卡密充值']);
  791. Kami::where(['crd' => $crd])->update(['uid' => $user['id'], 'type' => 2, 'stime' => time()]);
  792. Db::commit();
  793. $data = ['config' => $this->config(), 'userinfo' => $this->auth->getUserinfo()];
  794. $this->success('充值成功', $data);
  795. } catch (Exception $e) {
  796. Db::rollback();
  797. $this->error($e->getMessage());
  798. }
  799. }
  800. public function getOpenid()
  801. {
  802. $site = config('site');
  803. $site['dltel'] = isset($site['dltel']) ? $site['dltel'] : -1;
  804. $code = input('code');//小程序传来的code值
  805. $appid = $site['weixinxcx']['appid'];//小程序的appid
  806. $appSecret = $site['weixinxcx']['appSecret'];// 小程序的$appSecret
  807. $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
  808. $getUrl = sprintf($wxUrl, $appid, $appSecret, $code);//把appid,appsecret,code拼接到url里
  809. $result = $this->curl_get($getUrl);//请求拼接好的url
  810. $wxResult = json_decode($result, true);
  811. if (empty($wxResult)) {
  812. $this->error('获取openid时异常,微信内部错误');
  813. } else {
  814. $loginFail = array_key_exists('errcode', $wxResult);
  815. if ($loginFail) {//请求失败
  816. $this->error($wxResult);
  817. } else {//请求成功
  818. $openid = $wxResult['openid'];
  819. if ($site['dltel'] == 0) {//等于0的时候直接登录
  820. $users = \app\common\model\User::getByWxxopenid($openid);
  821. if ($users) {
  822. $this->auth->direct($users->id);
  823. $data = ['openid' => $openid, 'userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  824. $this->success(__('登录成功'), $data);
  825. }
  826. }
  827. $wxResult['dltel'] = $site['dltel'];
  828. $this->success('获取openid成功', $wxResult);
  829. }
  830. }
  831. }
  832. public function logingzh()
  833. {
  834. $openid = input('openid');
  835. $unionid = input('unionid');
  836. $users = \app\common\model\User::getByUnionid($unionid);
  837. //$users = \app\common\model\User::getByGzhopenid($openid);//仅仅公众号
  838. if ($users) {
  839. if ($users->status != 'normal') {
  840. $this->error(__('Account is locked'));
  841. }
  842. //如果已经有账号则直接登录
  843. $ret = $this->auth->direct($users->id);
  844. if ($ret) {
  845. \app\common\model\User::where(['id' => $users->id])->update(['gzhopenid' => $openid]);
  846. $data = ['userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  847. $this->success(__('Logged in successful'), $data);
  848. }
  849. } else {
  850. $pid = $this->request->request('pid');
  851. if (!$pid) {
  852. $pid = 1;
  853. }
  854. $usera = \app\common\model\User::getById($pid);
  855. if (!$usera) {
  856. $this->error(__('邀请码不正确'));
  857. }
  858. $mobile = $openid;
  859. $email = $mobile . '@100.com';
  860. $avatar = '/uploads/face/A' . rand(1, 96) . '.jpg';;
  861. $nickName = input('nickName', '公众号用户');
  862. $ret = $this->auth->register($mobile, '123456', $email, $mobile, ['wxxopenid' => $openid, 'unionid' => $unionid, 'nickname' => $nickName, 'avatar' => $avatar, 'pid' => $pid]);
  863. if ($ret) {
  864. $data = ['userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  865. $this->success(__('Sign up successful'), $data);
  866. } else {
  867. $this->error($this->auth->getError());
  868. }
  869. }
  870. }
  871. public function getphone()
  872. {
  873. $site = config('site');
  874. $dltel = isset($site['dltel']) ? $site['dltel'] : -1;
  875. if ($dltel == 0) {
  876. $openid = input('openid');
  877. $users = \app\common\model\User::getByWxxopenid($openid);
  878. if ($users) {
  879. $this->auth->direct($users->id);
  880. $data = ['userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  881. $this->success(__('登录成功'), $data);
  882. } else {
  883. $pid = $this->request->request('pid');
  884. if (!$pid) {
  885. $pid = 1;
  886. }
  887. $usera = \app\common\model\User::getById($pid);
  888. if (!$usera) {
  889. $this->error(__('邀请码不正确'));
  890. }
  891. //$number = date('ymdh', time()) . rand(10000, 99999);
  892. $mobile = $openid;
  893. $email = $mobile . '@100.com';
  894. $avatar = input('avatarUrl');
  895. $nickName = input('nickName');
  896. $ret = $this->auth->register($mobile, '123456', $email, $mobile, ['wxxopenid' => $openid, 'nickname' => $nickName, 'avatar' => $avatar, 'pid' => $pid]);
  897. if ($ret) {
  898. $users = $this->auth->getUserinfo();
  899. //var_dump($users);
  900. $idsa = $users['id'];
  901. \app\common\model\User::where(['id' => $idsa])->update(['mobile' => 'user_' . $idsa, 'email' => 'user_' . $idsa . '@100.com', 'username' => 'user_' . $idsa]);
  902. $data = ['userinfo' => $users, 'config' => $this->config()];
  903. $this->success(__('Sign up successful'), $data);
  904. } else {
  905. $this->error($this->auth->getError());
  906. }
  907. }
  908. }
  909. $APPID = $site['weixinxcx']['appid'];//小程序的appid
  910. $AppSecret = $site['weixinxcx']['appSecret'];// 小程序的$appSecret
  911. if (!input('code')) {
  912. $this->error('code出错');
  913. exit;
  914. }
  915. $code = input('code');
  916. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $APPID . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
  917. $arr = $this->vget($url); // 一个使用curl实现的get方法请求
  918. $arr = json_decode($arr, true);
  919. $session_key = $arr['session_key'];
  920. $openid = $arr['openid'];
  921. $unionid = isset($arr['unionid']) ? $arr['unionid'] : null;
  922. Vendor("wxBizData.wxBizDataCrypt"); //加载解密文件,在官方有下载
  923. $pc = new \WXBizDataCrypt($APPID, $session_key);
  924. $iv = input('iv');
  925. $encryptedData = input('encryptedData');
  926. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  927. if ($errCode == 0) {
  928. //print($data . "\n");
  929. //dump($arr);
  930. $phone = json_decode($data, true);
  931. if ($phone['purePhoneNumber']) {
  932. //$users = \app\common\model\User::getByMobile($phone['purePhoneNumber']);
  933. $user = \app\common\model\User::getByUnionid($unionid);
  934. if ($user) {
  935. if ($user->status != 'normal') {
  936. $this->error(__('Account is locked'));
  937. }
  938. //如果已经有账号则直接登录
  939. $ret = $this->auth->direct($user->id);
  940. if ($ret) {
  941. $data = ['userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  942. \app\common\model\User::where(['id' => $user->id])->update(['mobile' => $phone['purePhoneNumber'], 'wxxopenid' => $openid, 'unionid' => $unionid]);
  943. $this->success(__('Logged in successful'), $data);
  944. }
  945. } else {
  946. $users = \app\common\model\User::getByMobile($phone['purePhoneNumber']);
  947. if ($users) {
  948. if ($users->status != 'normal') {
  949. $this->error(__('Account is locked'));
  950. }
  951. //如果已经有账号则直接登录
  952. $ret = $this->auth->direct($users->id);
  953. if ($ret) {
  954. $data = ['userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  955. \app\common\model\User::where(['id' => $users->id])->update(['mobile' => $phone['purePhoneNumber'], 'wxxopenid' => $openid, 'unionid' => $unionid]);
  956. $this->success(__('Logged in successful'), $data);
  957. }
  958. } else {
  959. $pid = $this->request->request('pid');
  960. if (!$pid) {
  961. $pid = 1;
  962. }
  963. $usera = \app\common\model\User::getById($pid);
  964. if (!$usera) {
  965. $this->error(__('邀请码不正确'));
  966. }
  967. $mobile = $phone['purePhoneNumber'];
  968. $email = $mobile . '@100.com';
  969. $avatar = input('avatarUrl');
  970. $nickName = input('nickName');
  971. $ret = $this->auth->register($mobile, '123456', $email, $mobile, ['wxxopenid' => $openid, 'unionid' => $unionid, 'nickname' => $nickName, 'avatar' => $avatar, 'pid' => $pid]);
  972. if ($ret) {
  973. $data = ['userinfo' => $this->auth->getUserinfo(), 'config' => $this->config()];
  974. $this->success(__('Sign up successful'), $data);
  975. } else {
  976. $this->error($this->auth->getError());
  977. }
  978. }
  979. }
  980. }
  981. } else {
  982. $this->error($errCode);
  983. }
  984. }
  985. public function qdmoney($money)
  986. {
  987. $site = config('site');
  988. $user = $this->auth->getUser();
  989. if ($user && $money != 0) {
  990. $before = $user->money;
  991. $after = $user->money + $money;
  992. //更新会员信息
  993. $user->save(['score' => $after]);
  994. //写入日志
  995. ScoreLog::create(['user_id' => $user->id, 'score' => $money, 'before' => $before, 'after' => $after, 'memo' => '签到赠送']);
  996. } else {
  997. $this->error(__('金额不对'));
  998. }
  999. }
  1000. public function admoney()
  1001. {
  1002. $site = config('site');
  1003. $money = isset($site['admoney']) ? $site['admoney'] : 0.01;
  1004. $user = $this->auth->getUser();
  1005. if ($user && $money != 0) {
  1006. $before = $user->money;
  1007. $after = $user->money + $money;
  1008. //更新会员信息
  1009. $user->save(['money' => $after]);
  1010. //写入日志
  1011. MoneyLog::create(['user_id' => $user->id, 'money' => $money, 'before' => $before, 'after' => $after, 'fid' => 0, 'sxf' => 0, 'memo' => '看广告赠送']);
  1012. $this->success('获取成功', $this->config());
  1013. } else {
  1014. $this->error(__('金额不对'));
  1015. }
  1016. }
  1017. /**
  1018. * @Author : kiven
  1019. * @Description:curl模拟请求方法
  1020. * @LastModify : kiven
  1021. * @param $url
  1022. * @return bool|string
  1023. */
  1024. public function vget($url)
  1025. {
  1026. $info = curl_init();
  1027. curl_setopt($info, CURLOPT_RETURNTRANSFER, true);
  1028. curl_setopt($info, CURLOPT_HEADER, 0);
  1029. curl_setopt($info, CURLOPT_NOBODY, 0);
  1030. curl_setopt($info, CURLOPT_SSL_VERIFYPEER, false);
  1031. curl_setopt($info, CURLOPT_SSL_VERIFYHOST, false);
  1032. curl_setopt($info, CURLOPT_URL, $url);
  1033. $output = curl_exec($info);
  1034. curl_close($info);
  1035. return $output;
  1036. }
  1037. //php请求网络的方法
  1038. public function curl_get($url, &$httpCode = 0)
  1039. {
  1040. $ch = curl_init();
  1041. curl_setopt($ch, CURLOPT_URL, $url);
  1042. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1043. //不做证书校验,部署在linux环境下请改为true
  1044. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1045. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  1046. $file_contents = curl_exec($ch);
  1047. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  1048. curl_close($ch);
  1049. return $file_contents;
  1050. }
  1051. }