Usercenter.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 会员中心
  7. */
  8. class Usercenter extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //申请实名认证
  13. //阿里云数链云三要素
  14. //https://market.aliyun.com/products/57000002/cmapi00053444.html?spm=5176.2020520132.101.7.3dc17218ETmLVi#sku=yuncode4744400005
  15. public function idcard_apply_new(){
  16. $truename = input('truename','');
  17. $idcard = input('idcard','');
  18. $idcard_images = input('idcard_images','');
  19. if(empty($truename) || empty($idcard) || empty($idcard_images)){
  20. $this->error('实名认证信息必填');
  21. }
  22. if($this->auth->idcard_status == 1){
  23. $this->error('您已经完成实名认证');
  24. }
  25. if($this->auth->idcard_status == 0){
  26. $this->error('您已经提交实名认证,请等待审核');
  27. }
  28. Db::startTrans();
  29. $check = Db::name('user_idcard')->where('user_id',$this->auth->id)->lock(true)->find();
  30. if(!empty($check)){
  31. if($check['status'] == 0){
  32. Db::rollback();
  33. $this->error('您已经提交实名认证,请等待审核');
  34. }
  35. if($check['status'] == 1){
  36. Db::rollback();
  37. $this->error('您已经完成实名认证');
  38. }
  39. }
  40. $count = Db::name('user_idcard')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
  41. if ($count) {
  42. $this->error('该身份证号已被他人使用');
  43. }
  44. //限制每日请求次数
  45. $time = time();
  46. $today_end = strtotime(date('Y-m-d 23:59:59', $time));
  47. $cache_time = $today_end - $time; //缓存时间
  48. $time_count = Cache::get('fourauth' . $this->auth->id);
  49. if (!$time_count) {
  50. Cache::set('fourauth' . $this->auth->id, 1, $cache_time);
  51. } else {
  52. Cache::set('fourauth' . $this->auth->id, $time_count + 1, $cache_time);
  53. if ($time_count > 3) {
  54. $this->error('今日实名次数已到上限,明天再来吧');
  55. }
  56. }
  57. //阿里云身份证三要素认证
  58. $auth_restult = $this->usercertify_aliyun_three($idcard, $truename,$this->auth->mobile);
  59. if($auth_restult == false){
  60. $this->error('身份证信息与姓名或注册手机号不符');
  61. }
  62. //
  63. $data = [
  64. 'user_id' => $this->auth->id,
  65. 'truename' => $truename,
  66. 'idcard' => $idcard,
  67. 'idcard_images' => $idcard_images,
  68. 'status' => 1, //不需要人工刚审核了,直接过审
  69. 'createtime' => time(),
  70. 'updatetime' => time(),
  71. ];
  72. //更新
  73. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>1]);//不需要人工刚审核了,直接过审
  74. if(!empty($check)){
  75. $rs = Db::name('user_idcard')->where('id',$check['id'])->update($data);
  76. }else{
  77. $rs = Db::name('user_idcard')->insertGetId($data);
  78. }
  79. if(!$rs || !$update_rs){
  80. Db::rollback();
  81. $this->error('认证失败');
  82. }
  83. Db::commit();
  84. $this->success('认证通过');
  85. }
  86. //实名认证三要素
  87. private function usercertify_aliyun_three($cardNo = '',$realname = '',$mobile = ''){
  88. if(!$cardNo || !$realname || !$mobile){
  89. return false;
  90. }
  91. $host = 'https://slysjsysgz.market.alicloudapi.com';
  92. $path = '/mobile_three/check';
  93. $method = 'GET';
  94. $appcode = config('ali_three_code');
  95. $headers = array();
  96. array_push($headers, 'Authorization:APPCODE ' . $appcode);
  97. $querys = 'idcard='.$cardNo.'&mobile='.$mobile.'&name='.urlencode($realname);
  98. $bodys = '';
  99. $url = $host . $path . '?' . $querys;
  100. $curl = curl_init();
  101. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  102. curl_setopt($curl, CURLOPT_URL, $url);
  103. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  104. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  105. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  106. curl_setopt($curl, CURLOPT_HEADER, 0);
  107. if (1 == strpos('$'.$host, 'https://'))
  108. {
  109. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  110. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  111. }
  112. $result = (curl_exec($curl));
  113. curl_close($curl);
  114. $result = json_decode($result,true);
  115. if(is_array($result) && isset($result['success']) && $result['success'] == true){
  116. if(isset($result['data']) && isset($result['data']['result'])){
  117. if($result['data']['result'] == 0){
  118. //实名过了
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. //申请实名认证
  126. public function idcard_apply(){
  127. $truename = input('truename','');
  128. $idcard = input('idcard','');
  129. $idcard_images = input('idcard_images','');
  130. if(empty($truename) || empty($idcard) || empty($idcard_images)){
  131. $this->error('实名认证信息必填');
  132. }
  133. if($this->auth->idcard_status == 1){
  134. $this->error('您已经完成实名认证');
  135. }
  136. if($this->auth->idcard_status == 0){
  137. $this->error('您已经提交实名认证,请等待审核');
  138. }
  139. Db::startTrans();
  140. $check = Db::name('user_idcard')->where('user_id',$this->auth->id)->lock(true)->find();
  141. if(!empty($check)){
  142. if($check['status'] == 0){
  143. Db::rollback();
  144. $this->error('您已经提交实名认证,请等待审核');
  145. }
  146. if($check['status'] == 1){
  147. Db::rollback();
  148. $this->error('您已经完成实名认证');
  149. }
  150. }
  151. $count = Db::name('user_idcard')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
  152. if ($count) {
  153. $this->error('该身份证号已被他人使用');
  154. }
  155. $data = [
  156. 'user_id' => $this->auth->id,
  157. 'truename' => $truename,
  158. 'idcard' => $idcard,
  159. 'idcard_images' => $idcard_images,
  160. 'status' => 0,
  161. 'createtime' => time(),
  162. 'updatetime' => time(),
  163. ];
  164. //更新
  165. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  166. if(!empty($check)){
  167. $rs = Db::name('user_idcard')->where('id',$check['id'])->update($data);
  168. }else{
  169. $rs = Db::name('user_idcard')->insertGetId($data);
  170. }
  171. if(!$rs || !$update_rs){
  172. Db::rollback();
  173. $this->error('提交失败');
  174. }
  175. Db::commit();
  176. $this->success('提交成功,请等待审核');
  177. }
  178. //实名认证信息
  179. public function idcard_info(){
  180. $check = Db::name('user_idcard')->where('user_id',$this->auth->id)->order('id desc')->find();
  181. $this->success('success',$check);
  182. }
  183. //驾驶证认证
  184. public function driver_apply(){
  185. $driver_images = input('driver_images','');
  186. if(empty($driver_images)){
  187. $this->error('驾驶证图片必填');
  188. }
  189. if($this->auth->driver_status == 1){
  190. $this->error('您已经完成驾驶证认证');
  191. }
  192. if($this->auth->driver_status == 0){
  193. $this->error('您已经提交驾驶证认证,请等待审核');
  194. }
  195. Db::startTrans();
  196. $check = Db::name('user_driver')->where('user_id',$this->auth->id)->lock(true)->find();
  197. if(!empty($check)){
  198. if($check['status'] == 0){
  199. Db::rollback();
  200. $this->error('您已经提交驾驶证认证,请等待审核');
  201. }
  202. if($check['status'] == 1){
  203. Db::rollback();
  204. $this->error('您已经完成驾驶证认证');
  205. }
  206. }
  207. $data = [
  208. 'user_id' => $this->auth->id,
  209. 'driver_images' => $driver_images,
  210. 'status' => 0,
  211. 'createtime' => time(),
  212. 'updatetime' => time(),
  213. ];
  214. //更新
  215. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['driver_status'=>0]);
  216. if(!empty($check)){
  217. $rs = Db::name('user_driver')->where('id',$check['id'])->update($data);
  218. }else{
  219. $rs = Db::name('user_driver')->insertGetId($data);
  220. }
  221. if(!$rs || !$update_rs){
  222. Db::rollback();
  223. $this->error('提交失败');
  224. }
  225. Db::commit();
  226. $this->success('提交成功,请等待审核');
  227. }
  228. //驾驶证认证信息
  229. public function driver_info(){
  230. $check = Db::name('user_driver')->where('user_id',$this->auth->id)->order('id desc')->find();
  231. $this->success('success',$check);
  232. }
  233. //我的消息通知
  234. public function msg_list(){
  235. $list = Db::name('user_msg')->where('user_id',$this->auth->id)->autopage()->select();
  236. if(!empty($list)){
  237. $ids = array_column($list,'id');
  238. Db::name('user_msg')->where('id','IN',$ids)->update(['status'=>1]);
  239. }
  240. $this->success('success',$list);
  241. }
  242. //我的消息通知
  243. public function msg_info(){
  244. $id = input('id');
  245. $info = Db::name('user_msg')->where('id',$id)->find();
  246. $this->success('success',$info);
  247. }
  248. //用户钱包流水
  249. public function moneylog(){
  250. $list = Db::name('user_money_log')->where('user_id',$this->auth->id)->order('id desc')->autopage()->select();
  251. $this->success(1,$list);
  252. }
  253. //我的积分页面
  254. public function myscore(){
  255. $score = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('score');
  256. $where = [
  257. 'type' => 2,
  258. 'stock' => ['gt',1],
  259. 'limit' => ['gt',0],
  260. 'usetimeend' => ['gt',time()],
  261. ];
  262. $coupons = Db::name('coupons')->where($where)->order('id desc')->select();
  263. //去掉不能领的
  264. $new_coupons = [];
  265. foreach($coupons as $key => $val){
  266. $user_conpon = Db::name('user_coupons')->where('user_id',$this->auth->id)->where('coupons_id',$val['id'])->count();
  267. if($user_conpon >= $val['limit']){
  268. continue;
  269. }
  270. $new_coupons[] = $val;
  271. }
  272. $rs['score'] = $score;
  273. $rs['coupons'] = $new_coupons;
  274. $this->success(1,$rs);
  275. }
  276. //积分日志
  277. public function scorelog(){
  278. $score_log = Db::name('user_score_log')->where('user_id',$this->auth->id)->order('id desc')->autopage()->select();
  279. $this->success(1,$score_log);
  280. }
  281. //积分兑换优惠券
  282. public function score_to_coupons(){
  283. $coupon_id = input('coupon_id',0);
  284. //检查
  285. Db::startTrans();
  286. $coupon_info = Db::name('coupons')->where('type',2)->where('id',$coupon_id)->lock(true)->find();
  287. if(!$coupon_info){
  288. Db::rollback();
  289. $this->error('不存在的优惠券');
  290. }
  291. if($coupon_info['stock'] < 1){
  292. Db::rollback();
  293. $this->error('优惠券已经领光了');
  294. }
  295. //检查
  296. $where = [
  297. 'user_id' => $this->auth->id,
  298. 'type' => 2,
  299. 'coupons_id' => $coupon_id,
  300. ];
  301. $check = Db::name('user_coupons')->where($where)->count();
  302. if($check >= $coupon_info['limit']){
  303. Db::rollback();
  304. $this->error('此券每人最多领取'.$coupon_info['limit'].'次');
  305. }
  306. //插入数据
  307. $data = [
  308. 'user_id' => $this->auth->id,
  309. 'company_id' => $coupon_info['company_id'],
  310. 'enough' => $coupon_info['enough'],
  311. 'amount' => $coupon_info['amount'],
  312. 'type' => $coupon_info['type'],
  313. 'coupons_id' => $coupon_id,
  314. 'createtime' => time(),
  315. 'usetimestart' => $coupon_info['usetimestart'],
  316. 'usetimeend' => $coupon_info['usetimeend'],
  317. 'status' => 0
  318. ];
  319. $log_id = Db::name('user_coupons')->insertGetId($data);
  320. //扣积分
  321. $wallet = new \app\common\model\Wallet;
  322. $wallet_rs = $wallet->lockChangeAccountRemain($this->auth->id,'score',-$coupon_info['score'],21,'兑换优惠券','user_coupons',$log_id);
  323. if($wallet_rs['status'] === false){
  324. Db::rollback();
  325. $this->error($wallet_rs['msg']);
  326. }
  327. //扣库存
  328. $coupon_rs = Db::name('coupons')->where('id',$coupon_id)->update(['stock' => $coupon_info['stock'] - 1]);
  329. if($coupon_rs === false){
  330. Db::rollback();
  331. $this->error('领取失败');
  332. }
  333. //结束
  334. Db::commit();
  335. $this->success('兑换成功');
  336. }
  337. //领券中心
  338. public function coupons_hub(){
  339. $where = [
  340. 'coupons.type' => 1,
  341. 'coupons.stock' => ['gt',1],
  342. 'coupons.limit' => ['gt',0],
  343. 'coupons.usetimeend' => ['gt',time()],
  344. ];
  345. $list = Db::name('coupons')
  346. ->field('coupons.*,company.name as company_name')
  347. ->join('company','coupons.company_id = company.id','LEFT')
  348. ->where($where)
  349. ->order('coupons.id desc')->select();
  350. //去掉不能领的
  351. $new_list = [];
  352. foreach($list as $key => $val){
  353. $user_conpon = Db::name('user_coupons')->where('user_id',$this->auth->id)->where('coupons_id',$val['id'])->count();
  354. if($user_conpon >= $val['limit']){
  355. continue;
  356. }
  357. $new_list[] = $val;
  358. }
  359. $this->success(1,$new_list);
  360. }
  361. //领取一个优惠券
  362. public function coupons_get(){
  363. $coupon_id = input('coupon_id',0);
  364. //检查
  365. Db::startTrans();
  366. $coupon_info = Db::name('coupons')->where('type',1)->where('id',$coupon_id)->lock(true)->find();
  367. if(!$coupon_info){
  368. Db::rollback();
  369. $this->error('不存在的优惠券');
  370. }
  371. if($coupon_info['stock'] < 1){
  372. Db::rollback();
  373. $this->error('优惠券已经领光了');
  374. }
  375. //检查
  376. $where = [
  377. 'user_id' => $this->auth->id,
  378. 'type' => 1,
  379. 'coupons_id' => $coupon_id,
  380. ];
  381. $check = Db::name('user_coupons')->where($where)->count();
  382. if($check >= $coupon_info['limit']){
  383. Db::rollback();
  384. $this->error('此券每人最多领取'.$coupon_info['limit'].'次');
  385. }
  386. //插入数据
  387. $data = [
  388. 'user_id' => $this->auth->id,
  389. 'company_id' => $coupon_info['company_id'],
  390. 'enough' => $coupon_info['enough'],
  391. 'amount' => $coupon_info['amount'],
  392. 'type' => $coupon_info['type'],
  393. 'coupons_id' => $coupon_id,
  394. 'createtime' => time(),
  395. 'usetimestart' => $coupon_info['usetimestart'],
  396. 'usetimeend' => $coupon_info['usetimeend'],
  397. 'status' => 0
  398. ];
  399. $log_id = Db::name('user_coupons')->insertGetId($data);
  400. //扣库存
  401. $coupon_rs = Db::name('coupons')->where('id',$coupon_id)->update(['stock' => $coupon_info['stock'] - 1]);
  402. if($coupon_rs === false){
  403. Db::rollback();
  404. $this->error('领取失败');
  405. }
  406. //结束
  407. Db::commit();
  408. $this->success('领取成功');
  409. }
  410. //我的优惠券
  411. public function mycoupons(){
  412. $status = input('status',0);
  413. $where = [
  414. 'c.user_id' => $this->auth->id,
  415. 'c.status' => $status,
  416. ];
  417. //未使用的要过滤掉过期的
  418. if($status == 0){
  419. $where['c.usetimeend'] = ['gt',time()];
  420. }
  421. $list = Db::name('user_coupons')->alias('c')
  422. ->field('c.*,company.name as company_name')
  423. ->join('company','c.company_id = company.id','LEFT')
  424. ->where($where)
  425. ->autopage()
  426. ->select();
  427. $this->success(1,$list);
  428. }
  429. //推广小程序码
  430. public function usershareposter()
  431. {
  432. $scene = $this->auth->introcode;
  433. // $qrcode = Db::name('shopro_store')->where('id', $scene)->value('qrcode');
  434. // if (!$qrcode ) {
  435. $qrcode = $this->circle_code($scene, '');
  436. // Db::name('shopro_store')->where('id', $scene)->update(['qrcode' => $qrcode]);
  437. // }
  438. $this->success(1, $qrcode);
  439. }
  440. //二维码海报
  441. public function circle_code($scene, $page_url)
  442. {
  443. $value = config('wxMiniProgram');
  444. $appid = $value['appid'];
  445. $secret = $value['secret'];
  446. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
  447. $res = file_get_contents($url);
  448. $token = json_decode($res, true)['access_token'];
  449. $URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $token;
  450. $data = [
  451. 'scene' => $scene, //二维码传入参数
  452. 'page' => $page_url, //扫码后进入页面
  453. 'env_version' => 'trial', //要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
  454. 'width' => 280, //二维码的宽度,单位 px,最小 280px,最大 1280px
  455. 'auto_color' => false, //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
  456. 'is_hyaline' => false, //是否需要透明底色,为 true 时,生成透明底色的小程序
  457. ];
  458. $json = json_encode($data); //数组加密
  459. //$result = $this->api_notice_increment($URL, $json); //用CURL 进行POST请求
  460. $result = curl_post($URL,$json);
  461. $path = ROOT_PATH . 'public/upload/extend/qrcode/original'; //ROOT_PATH 我使用的是TP5框架
  462. $path2 = '/upload/extend/qrcode/original';
  463. if (!file_exists($path)) { //判断目录是否存在
  464. mkdir($path, 0777, true);
  465. }
  466. $path = $path . '/' . $scene . '.png'; //最后要写入的目录及文件名
  467. $path2 = $path2 . '/' . $scene . '.png'; //最后要写入的目录及文件名
  468. //小程序码
  469. //if (!file_exists($path)) {
  470. // 创建将数据流文件写入我们创建的文件内容中
  471. file_put_contents($path, $result);
  472. //}
  473. //加上背景地图
  474. $path = $this->shareposter($path2);
  475. // $path = ROOT_PATH . 'public/'. $path;
  476. $path = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $path;
  477. return $path;
  478. //上传到oss
  479. /*$fileName = sha1(date('YmdHis', time()) . uniqid()) . '.png';
  480. $oss = new Oss();
  481. $oss_path = $oss->uploadFile($fileName, $path);
  482. return $oss_path;*/
  483. }
  484. //生成海报
  485. public function shareposter($path) {
  486. $info['id'] = $this->auth->id;
  487. $data = [
  488. [
  489. "left"=> "90px",
  490. "top"=> "445px",
  491. "type"=> "img",
  492. "width"=> "195px",
  493. "height"=> "185px",
  494. "src"=> $path
  495. ],
  496. ];
  497. $data = json_encode($data, 320);
  498. $poster = [
  499. 'id' => 1,
  500. 'title' => '测试',
  501. 'waittext' => '海报正在拼命生成中,请等待片刻...',
  502. 'bg_image' => '/assets/img/sharebak.png',
  503. 'data' => $data,
  504. 'status' => 'normal',
  505. 'weigh' => 0,
  506. 'createtime' => 1653993709,
  507. 'updatetime' => 1653994259,
  508. ];
  509. $image = new \addons\poster\library\Image();
  510. $imgurl = $image->createPosterImage($poster, $info);
  511. if (!$imgurl) {
  512. $this->error('生成海报出错');
  513. }
  514. // $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  515. return $imgurl;
  516. }
  517. //我邀请的金钱日志,给邀请页面
  518. public function myinvite_moneylog(){
  519. $list = Db::name('user_money_log')->field('log.change_value,log.createtime,user.avatar,user.mobile')->alias('log')
  520. ->join('order','order.id = log.table_id')
  521. ->join('user','order.user_id = user.id')
  522. ->where('log.user_id',$this->auth->id)->where('log.log_type',3)->order('log.id desc')->autopage()->select();
  523. $list = list_domain_image($list,['avatar']);
  524. foreach($list as $key => &$val){
  525. $val['mobile'] = str_replace(substr($val['mobile'],3,5),'****',$val['mobile']);
  526. }
  527. $this->success(1,$list);
  528. }
  529. }