Index.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use wxpay;
  6. /**
  7. * 首页接口
  8. */
  9. class Index extends Api
  10. {
  11. protected $noNeedLogin = ['banner', 'activelist', 'personaltype', 'personalactivetype', 'car', 'leader', 'personaldirection', 'activeinfo'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 首页
  15. *
  16. */
  17. public function index()
  18. {
  19. $this->success('请求成功');
  20. }
  21. //轮播图
  22. public function banner()
  23. {
  24. $list = Db::name('banner')->field('id, title, image, url')->order('weigh', 'desc')->select();
  25. $list = list_domain_image($list, ['image']);
  26. $this->success('轮播图', $list);
  27. }
  28. //活动列表
  29. public function activelist() {
  30. $type = input('type', 0, 'intval'); //分类:1=休闲,2=中度
  31. $keyword = input('keyword', '', 'trim'); //关键字
  32. $where['signupendtime'] = ['gt', time()];
  33. $where['status'] = 0;
  34. if ($type) {
  35. $where['type'] = $type;
  36. }
  37. if ($keyword !== '') {
  38. $where['title|desc|remark|collectionplace|leader'] = ['like', '%'.$keyword.'%'];
  39. }
  40. $list = Db::name('active')->field('id, type, title, desc, remark, image, price, maxperson, currentperson, status')
  41. ->where($where)->page($this->page, $this->pagenum)->order('createtime', 'desc')->select();
  42. $list = list_domain_image($list, ['image']);
  43. foreach ($list as &$v) {
  44. if ($v['maxperson'] <= $v['currentperson']) {
  45. $v['is_full'] = 1;
  46. } else {
  47. $v['is_full'] = 0;
  48. }
  49. }
  50. $this->success('活动列表', $list);
  51. }
  52. //活动详情
  53. public function activeinfo() {
  54. $id = input('id', 0, 'intval');
  55. if (!$id) {
  56. $this->error('参数缺失');
  57. }
  58. $info = Db::name('active')->find($id);
  59. if (!$info) {
  60. $this->error('数据不存在');
  61. }
  62. $info = info_domain_image($info, ['image']);
  63. $info['starttime'] = date('Y-m-d H:i', $info['starttime']);
  64. $info['endtime'] = date('Y-m-d H:i', $info['endtime']);
  65. $info['collectiontime'] = date('Y-m-d H:i', $info['collectiontime']);
  66. $info['signupendtime'] = date('Y-m-d H:i', $info['signupendtime']);
  67. $info['refundendtime'] = date('Y-m-d H:i', $info['refundendtime']);
  68. //查询已报名列表
  69. $active_people = Db::name('active_people')->where(['active_id' => $id, 'status' => ['neq', 3]])->field('user_id, name, createtime')->select();
  70. $user = Db::name('user');
  71. foreach ($active_people as &$v) {
  72. $v['avatar'] = $user->where(['id' => $v['user_id']])->value('avatar');
  73. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  74. }
  75. $info['active_people'] = $active_people;
  76. $info['customer_service'] = config('site.customer_service') ? config('site.customer_service') : ''; //客服电话
  77. $this->success('活动详情', $info);
  78. }
  79. //报名活动之前检查
  80. public function beforesignup() {
  81. $id = input('id', 0, 'intval'); //活动id
  82. if (!$id) {
  83. $this->error('请选择要报名的活动');
  84. }
  85. $info = Db::name('active')->find($id);
  86. if (!$info) {
  87. $this->error('活动不存在');
  88. }
  89. if ($info['status'] == 2) {
  90. $this->error('活动已经结束');
  91. }
  92. if ($info['status'] == 3) {
  93. $this->error('活动已经取消');
  94. }
  95. if ($info['signupendtime'] < time()) {
  96. $this->error('活动报名已经截止');
  97. }
  98. if ($info['currentperson'] >= $info['maxperson']) {
  99. $this->error('活动名额已满');
  100. }
  101. //检查自己信息是否完善
  102. if (!$this->auth->realname) {
  103. $this->success('请在个人资料中完善真实姓名', ['code' => 3]);
  104. }
  105. if (!$this->auth->idcard) {
  106. $this->success('请在个人资料中完善身份证号', ['code' => 3]);
  107. }
  108. if (!$this->auth->emergencycontact) {
  109. $this->success('请在个人资料中完善紧急联系人', ['code' => 3]);
  110. }
  111. if (!$this->auth->contactmobile) {
  112. $this->success('请在个人资料中完善紧急联系方式', ['code' => 3]);
  113. }
  114. $this->success('检查通过');
  115. }
  116. //查询可用优惠券
  117. public function canusercoupon() {
  118. $price = input('price', '', 'trim'); //价格
  119. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $price) || $price < 0) {
  120. $this->error('价格错误');
  121. }
  122. $where['user_id'] = $this->auth->id;
  123. $where['endtime'] = ['egt', time()];
  124. $where['status'] = 0;
  125. $map = 'type = 1 or (type = 2 and minmoney <= '.$price.')';
  126. $list = Db::name('user_coupon')->field('id, title, desc, type, money, minmoney')->where($where)->where($map)->select();
  127. // p(\db()->getLastSql());
  128. $this->success('查询可用优惠券', $list);
  129. }
  130. //报名活动
  131. public function signupactive() {
  132. //检查自己信息是否完善
  133. if (!$this->auth->realname || !$this->auth->idcard || !$this->auth->emergencycontact || !$this->auth->contactmobile) {
  134. $this->success('请在个人资料中完善资料', ['code' => 3]);
  135. }
  136. $id = input('id', 0, 'intval'); //活动id
  137. //人员信息json串:
  138. // name姓名 credtype证件类型 idcard身份证号 mobile手机号 emergencycontact紧急联系人 contactmobile紧急联系方式
  139. // insurance保险 originalprice原价 vipprice会员价 coupon_id用户优惠券ID is_free是否使用免费次数:0=否,1=是
  140. // price小计 is_self是否本人:0=否,1=是
  141. $active_people = input('active_people', '', 'trim');
  142. $paytype = input('paytype', 0, 'intval'); //支付方式:0=余额,1=微信
  143. $total_price = input('total_price', 0, 'trim'); //总价格
  144. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $total_price) || $total_price < 0) {
  145. $this->error('合计价格错误');
  146. }
  147. if (!in_array($paytype, [0, 1])) {
  148. $this->error('支付错误');
  149. }
  150. if ($total_price == 0 && $paytype != 0) {
  151. $this->error('请选择余额支付');
  152. }
  153. //检查活动信息
  154. if (!$id) {
  155. $this->error('请选择要报名的活动');
  156. }
  157. $info = Db::name('active')->find($id);
  158. if (!$info) {
  159. $this->error('活动不存在');
  160. }
  161. if ($info['status'] == 2) {
  162. $this->error('活动已经结束');
  163. }
  164. if ($info['status'] == 3) {
  165. $this->error('活动已经取消');
  166. }
  167. if ($info['signupendtime'] < time()) {
  168. $this->error('活动报名已经截止');
  169. }
  170. if ($info['currentperson'] >= $info['maxperson']) {
  171. $this->error('活动名额已满');
  172. }
  173. //检查人员信息
  174. if (!$active_people) {
  175. $this->error('请添加报名人员信息');
  176. }
  177. $active_people_arr = json_decode($active_people, true);
  178. if (!$active_people_arr) {
  179. $this->error('请添加报名人员信息');
  180. }
  181. if ($info['currentperson'] + count($active_people_arr) > $info['maxperson']) {
  182. $this->error('活动名额不足');
  183. }
  184. //会员信息
  185. $vip_info = Db::name('vip')->find($this->auth->maxlevel);
  186. if (!$vip_info) {
  187. $this->error('会员信息缺失,请联系管理员');
  188. }
  189. $_data = [];
  190. $total_amount = 0; //总价格验证
  191. $active_people = Db::name('active_people'); //报名人员表
  192. $active_people_modify = Db::name('active_people_modify'); //报名修改信息表
  193. $user_coupon = Db::name('user_coupon');
  194. foreach ($active_people_arr as $k => &$v) {
  195. $data = [];
  196. //检查信息
  197. if (!$v['name'] || iconv_strlen($v['name'], 'utf-8') > 50) {
  198. $this->error('请输入正确姓名');
  199. break;
  200. }
  201. if (iconv_strlen($v['idcard'], 'utf-8') != 18) {
  202. $this->error('请输入正确身份证号');
  203. break;
  204. }
  205. if (!is_mobile($v['mobile'])) {
  206. $this->error('请输入正确手机号');
  207. break;
  208. }
  209. if (!$v['emergencycontact'] || iconv_strlen($v['emergencycontact'], 'utf-8') > 50) {
  210. $this->error('请输入紧急联系人');
  211. break;
  212. }
  213. if (!is_mobile($v['contactmobile'])) {
  214. $this->error('请输入正确紧急联系人方式');
  215. break;
  216. }
  217. //判断是否报名过
  218. $count = $active_people->where(['active_id' => $id, 'idcard' => $v['idcard'], 'status' => ['neq', 3]])->count('id');
  219. if ($count) {
  220. $this->error($v['name'] . $v['idcard'] . '已报名过该活动');
  221. break;
  222. }
  223. $count2 = $active_people_modify->where(['active_id' => $id, 'idcard' => $v['idcard'], 'status' => 0])->count('id');
  224. if ($count2) {
  225. $this->error($v['name'] . $v['idcard'] . '已提交过修改,请等待审核');
  226. break;
  227. }
  228. //判断证件类型和保险
  229. if (!$v['credtype'] || iconv_strlen($v['credtype'], 'utf-8') > 50) {
  230. $this->error('证件类型错误');
  231. break;
  232. }
  233. if (!$v['insurance'] || iconv_strlen($v['insurance'], 'utf-8') > 50) {
  234. $this->error('保险信息错误');
  235. break;
  236. }
  237. //判断用户信息
  238. if ($v['is_self'] && $k == 0) {
  239. //判断用户信息
  240. if ($v['name'] != $this->auth->realname || $v['idcard'] != $this->auth->idcard || $v['mobile'] != $this->auth->mobile || $v['emergencycontact'] != $this->auth->emergencycontact || $v['contactmobile'] != $this->auth->contactmobile) {
  241. $this->error('本人信息错误');
  242. break;
  243. }
  244. //本人判断年龄 价格
  245. if ($info['maxage'] > 0) {
  246. $age = $this->idcardage($this->auth->idcard);
  247. if ($age < $info['minage'] || $age > $info['maxage']) {
  248. $this->error('活动年龄限制为' . $info['minage'] . '-' . $info['maxage']);
  249. break;
  250. }
  251. }
  252. if ($v['is_free'] == 1) { //使用免费次数
  253. if ($info['is_free'] != 1) {
  254. $this->error('活动暂不支持免费体验');
  255. break;
  256. }
  257. if ($this->auth->freenumber <= 0) {
  258. $this->error('您的免费次数不足');
  259. break;
  260. }
  261. //检查当月是否用过免费次数
  262. $month_time = strtotime(date('Y-m-1', time()));
  263. $count = $active_people->where(['user_id' => $this->auth->id, 'is_free' => 1, 'createtime' => ['egt', $month_time]])->count('id');
  264. if ($count) {
  265. $this->error('您当月已使用过免费次数');
  266. break;
  267. }
  268. if ($v['vipprice'] != 0 || $v['price'] != 0) {
  269. $this->error($this->auth->realname . '价格错误');
  270. break;
  271. }
  272. if ($v['coupon_id']) {
  273. $this->error('使用免费体验,无法使用优惠券');
  274. break;
  275. }
  276. } else { //不使用免费次数
  277. //会员价和优惠券是否可以叠加使用:0=否,1=是
  278. if ($info['is_overlying'] == 0 && $v['vipprice'] != $info['price'] && $v['coupon_id']) {
  279. $this->error('该活动不支持会员价和优惠券同时使用');
  280. break;
  281. }
  282. //计算会员价, 会员价优先顺序: 生日 > 女生特权日 > 会员价格
  283. $birthday = date('md', strtotime($this->auth->birthday));
  284. $now_day = date('md', time());
  285. if ($birthday == $now_day) {
  286. //生日折扣
  287. if ($vip_info['birthdiscount'] > 100 || $vip_info['birthdiscount'] < 0) {
  288. $this->error('会员生日折扣错误,请联系管理员');
  289. break;
  290. }
  291. $discount = $vip_info['birthdiscount'];
  292. } elseif ($info['girldiscount'] < 100 && $info['girldiscount'] > 0 && $this->auth->gender == 2) {
  293. //女生特权日折扣
  294. $discount = $info['girldiscount'];
  295. } else {
  296. //会员折扣
  297. if ($vip_info['vipdiscount'] > 100 || $vip_info['vipdiscount'] < 0) {
  298. $this->error('会员折扣错误,请联系管理员');
  299. break;
  300. }
  301. $discount = $vip_info['vipdiscount'];
  302. }
  303. $vipprice = number_format($info['price'] * $discount / 100, 2, '.', ''); //会员价
  304. //查询优惠券
  305. if ($v['coupon_id']) {
  306. $user_coupon_info = $user_coupon->where(['id' => $v['coupon_id'], 'user_id' => $this->auth->id])->find();
  307. if (!$user_coupon_info) {
  308. $this->error('优惠券不存在');
  309. }
  310. if ($user_coupon_info['status'] != 0) {
  311. $this->error('优惠券已使用');
  312. }
  313. if ($user_coupon_info['endtime'] < time()) {
  314. $this->error('优惠券已过期');
  315. }
  316. if ($user_coupon_info['type'] == 1) {
  317. //打折券
  318. if ($user_coupon_info['money'] < 0 || $user_coupon_info['money'] > 100) {
  319. $this->error('优惠券折扣错误,请联系管理员');
  320. break;
  321. }
  322. } else {
  323. //抵扣券
  324. //会员价和优惠券是否可以叠加使用:0=否,1=是
  325. if ($info['is_overlying'] == 0) {
  326. if ($info['price'] < $user_coupon_info['minmoney']) {
  327. $this->error('优惠券使用条件不满足');
  328. break;
  329. }
  330. } else {
  331. if ($vipprice < $user_coupon_info['minmoney']) {
  332. $this->error('优惠券使用条件不满足');
  333. break;
  334. }
  335. }
  336. }
  337. }
  338. //判断价格
  339. //会员价和优惠券是否可以叠加使用:0=否,1=是
  340. if ($info['is_overlying'] == 0) {
  341. if ($v['coupon_id']) { //使用优惠券
  342. if ($v['vipprice'] != $info['price']) {
  343. $this->error('会员价显示错误');
  344. break;
  345. }
  346. if ($user_coupon_info['type'] == 1) {
  347. //打折券
  348. $coupon_price = number_format($info['price'] * (100 - $user_coupon_info['money']) / 100, 2, '.', '');
  349. $price = number_format($info['price'] - $coupon_price, 2, '.', '');
  350. } else {
  351. //抵扣券
  352. $price = number_format($info['price'] - $user_coupon_info['money'], 2, '.', '');
  353. }
  354. } else {
  355. $price = $vipprice;
  356. }
  357. } else {
  358. if ($vipprice != $v['vipprice']) {
  359. $this->error('会员价显示错误');
  360. break;
  361. }
  362. if ($v['coupon_id']) { //使用优惠券
  363. if ($user_coupon_info['type'] == 1) {
  364. //打折券
  365. $coupon_price = number_format($vipprice * (100 - $user_coupon_info['money']) / 100, 2, '.', '');
  366. $price = number_format($vipprice - $coupon_price, 2, '.', '');
  367. } else {
  368. //抵扣券
  369. $price = number_format($vipprice - $user_coupon_info['money'], 2, '.', '');
  370. }
  371. } else {
  372. $price = $vipprice;
  373. }
  374. }
  375. //判断小计
  376. if ($price != $v['price']) {
  377. $this->error('小计显示错误');
  378. break;
  379. }
  380. $data['vipprice'] = $v['vipprice']; //会员价
  381. $data['coupon_id'] = $v['coupon_id']; //优惠券id
  382. if ($v['coupon_id']) {
  383. $data['coupontype'] = $user_coupon_info['type'];
  384. $data['couponprice'] = $user_coupon_info['money'];
  385. }
  386. }
  387. $data['name'] = $this->auth->realname;
  388. $data['idcard'] = $this->auth->idcard;
  389. $data['mobile'] = $this->auth->mobile;
  390. $data['emergencycontact'] = $this->auth->emergencycontact;
  391. $data['contactmobile'] = $this->auth->contactmobile;
  392. } else {
  393. //帮人报名判断年龄 报名信息
  394. if ($info['maxage'] > 0) {
  395. $age = $this->idcardage($v['idcard']);
  396. if ($age < $info['minage'] || $age > $info['maxage']) {
  397. $this->error('活动年龄限制为' . $info['minage'] . '-' . $info['maxage']);
  398. break;
  399. }
  400. }
  401. //判断是否符合满几人减免一人费用
  402. //会员权限
  403. if ($vip_info['manypeople'] > 1 && $k == $vip_info['manypeople'] - 1) {
  404. if ($v['price'] != 0) {
  405. $this->error('小计显示错误');
  406. break;
  407. }
  408. } else {
  409. if ($v['price'] != $info['price']) {
  410. $this->error('小计显示错误');
  411. break;
  412. }
  413. }
  414. $data['name'] = $v['name'];
  415. $data['idcard'] = $v['idcard'];
  416. $data['mobile'] = $v['mobile'];
  417. $data['emergencycontact'] = $v['emergencycontact'];
  418. $data['contactmobile'] = $v['contactmobile'];
  419. $data['vipprice'] = $v['price']; //会员价
  420. }
  421. $data['active_id'] = $id;
  422. $data['user_id'] = $this->auth->id;
  423. $data['credtype'] = $v['credtype'];
  424. $data['insurance'] = $v['insurance'];
  425. $data['originalprice'] = $info['price'];
  426. $data['is_free'] = $v['is_free'];
  427. $data['price'] = $v['price']; //小计
  428. $data['is_self'] = $v['is_self'];
  429. $data['createtime'] = time();
  430. array_push($_data, $data);
  431. $total_amount += $v['price'];
  432. }
  433. if ($total_amount != $total_price) {
  434. $this->error('合计价格错误');
  435. }
  436. if ($paytype == 0) { //余额支付
  437. if ($this->auth->money < $total_amount) {
  438. $this->success('您的余额不足,请先去充值', ['code' => 2]);
  439. }
  440. }
  441. //构建订单信息
  442. $order_data['order_sn'] = date('YmdHis', time()) . rand(10000000, 99999999);
  443. $order_data['active_id'] = $id;
  444. $order_data['user_id'] = $this->auth->id;
  445. $order_data['paytype'] = $paytype;
  446. $order_data['price'] = $total_amount;
  447. $order_data['number'] = count($active_people_arr);
  448. $order_data['status'] = $paytype == 1 ? 0 : 1;
  449. $order_data['createtime'] = time();
  450. //构建活动信息
  451. $active_data['currentperson'] = $info['currentperson'] + count($active_people_arr);
  452. if ($info['currentperson'] + count($active_people_arr) >= $info['minperson'] && $info['status'] == 0) {
  453. $active_data['status'] = 1;
  454. }
  455. //开始事务
  456. Db::startTrans();
  457. //修改活动表数据
  458. $active_rs = Db::name('active')->where(['id' => $id, 'status' => $info['status'],'currentperson' => $info['currentperson']])->setField($active_data);
  459. if (!$active_rs) {
  460. Db::rollback();
  461. $this->error('网络延迟,请稍后再试');
  462. }
  463. //添加订单
  464. $rs = Db::name('active_order')->insertGetId($order_data);
  465. if (!$rs) {
  466. Db::rollback();
  467. $this->error('网络延迟,请稍后再试');
  468. }
  469. //添加人员信息
  470. foreach ($_data as &$v) {
  471. $v['order_id'] = $rs;
  472. $v['status'] = $order_data['status'];
  473. $rt = $active_people->insertGetId($v);
  474. if (!$rt) {
  475. Db::rollback();
  476. $this->error('网络延迟,请稍后再试');
  477. }
  478. }
  479. //扣除免费次数
  480. if ($info['is_free'] == 1 && $active_people_arr[0]['is_self'] == 1 && $active_people_arr[0]['is_free'] == 1) {
  481. $freenumber = $this->auth->freenumber - 1;
  482. $user_rs = Db::name('user')->where(['id' => $this->auth->id, 'freenumber' => $this->auth->freenumber])->setField('freenumber', $freenumber);
  483. if (!$user_rs) {
  484. Db::rollback();
  485. $this->error('网络延迟,请稍后再试');
  486. }
  487. }
  488. //扣除优惠券
  489. if ($active_people_arr[0]['is_self'] == 1 && $active_people_arr[0]['coupon_id']) {
  490. $user_coupon_rs = Db::name('user_coupon')->where(['id' => $active_people_arr[0]['coupon_id'], 'user_id' => $this->auth->id, 'status' => 0])->setField(['active_id' => $id, 'order_id' => $rs, 'status' => 1]);
  491. if (!$user_coupon_rs) {
  492. Db::rollback();
  493. $this->error('网络延迟,请稍后再试');
  494. }
  495. }
  496. //扣款 支付方式:0=余额,1=微信
  497. if ($paytype == 0) {
  498. $res = create_log(-$total_amount, '支付活动订单', $this->auth->id, 2, $rs);
  499. if ($res != 1) {
  500. Db::rollback();
  501. $this->error('余额资金异常,请联系管理员');
  502. }
  503. //给上级发送优惠券
  504. if ($this->auth->pre_user_id) {
  505. //查询报名活动优惠券
  506. $invite_coupon = Db::name('coupon')->where(['purpose' => 5, 'status' => 1])->order('weigh desc, id desc')->find();
  507. if ($invite_coupon) {
  508. $invite_coupon_data = [
  509. 'user_id' => $this->auth->pre_user_id,
  510. 'coupon_id' => $invite_coupon['id'],
  511. 'title' => $invite_coupon['title'],
  512. 'desc' => $invite_coupon['desc'],
  513. 'type' => $invite_coupon['type'],
  514. 'money' => $invite_coupon['money'],
  515. 'minmoney' => $invite_coupon['minmoney'],
  516. 'purpose' => $invite_coupon['purpose'],
  517. 'starttime' => time(),
  518. 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
  519. 'active_id' => $id,
  520. 'order_id' => $rs,
  521. 'createtime' => time()
  522. ];
  523. $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
  524. if (!$invite_coupon_rs) {
  525. Db::rollback();
  526. $this->error('发放优惠券失败');
  527. }
  528. }
  529. }
  530. //发送消息
  531. $data = [
  532. 'user_id' => $this->auth->id,
  533. 'type' => 1,
  534. 'title' => '活动通知',
  535. 'content' => '您已成功报名' . $info['title'] . '活动',
  536. 'createtime' => time()
  537. ];
  538. $sys_rs = Db::name('sys_msg')->insertGetId($data);
  539. if (!$sys_rs) {
  540. Db::rollback();
  541. $this->error('网络延迟,请稍后再试');
  542. }
  543. Db::commit();
  544. $this->success('报名成功');
  545. } else {
  546. //生成支付订单记录
  547. $rechar_order['user_id'] = $this->auth->id;
  548. $rechar_order['order_no'] = date('YmdHis', time()) . rand(10000000, 99999999); //微信订单编号
  549. $rechar_order['money'] = $total_amount;
  550. $rechar_order['purpose'] = 1; //充值用途:1=支付订单,2=充值,3=开通会员
  551. $rechar_order['pay_type'] = 'wechat';
  552. $rechar_order['relation_id'] = $rs;
  553. $rechar_order['createtime'] = time();
  554. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  555. if (!$result) {
  556. Db::rollback();
  557. $this->error('网络延迟,请稍后再试');
  558. }
  559. Db::commit();
  560. //构建支付链接数据
  561. $wxData['body'] = '报名活动支付';
  562. $wxData['out_trade_no'] = $rechar_order['order_no'];
  563. $wxData['total_fee'] = $total_amount;
  564. // $wxData['total_fee'] = 0.01;
  565. $wxData['openid'] = $this->auth->openid;
  566. // require_once($_SERVER['DOCUMENT_ROOT'] . '/Plugins/Weixin/WxPay/WxPay.php');
  567. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  568. $doResult = $wxPay->WxPayJs($wxData);
  569. $this->success('微信支付参数返回成功', $doResult);
  570. }
  571. }
  572. //根据身份证号获取年龄
  573. public function idcardage($idcard = '') {
  574. //截取身份证里的出生日期
  575. $year = substr($idcard, 6, 4);
  576. $month = substr($idcard, 10, 2);
  577. $day = substr($idcard, 12, 2);
  578. //获取当前日期
  579. $current_year = date('Y');
  580. $current_month = date('m');
  581. $current_day = date('d');
  582. //计算年龄
  583. $age = $current_year - $year;//今年减去生日年
  584. if ($month > $current_month || ($month == $current_month && $day > $current_day)) {
  585. //如果出生月大于当前月或出生月等于当前月但出生日大于当前日则减一岁
  586. $age--;
  587. }
  588. return $age;
  589. }
  590. //私人订制类型
  591. public function personaltype() {
  592. $list = Db::name('personal_type')->field('id, name')->order('weigh', 'desc')->select();
  593. $this->success('私人订制类型', $list);
  594. }
  595. //私人订制活动类型
  596. public function personalactivetype() {
  597. $list = Db::name('personal_active_type')->field('id, name')->where(['pid' => 0])->order('weigh', 'desc')->select();
  598. $list = $this->getchildtype($list);
  599. $this->success('私人订制活动类型', $list);
  600. }
  601. //无限级私人订制活动类型
  602. public function getchildtype($list = [])
  603. {
  604. $complaint_type = Db::name('personal_active_type');
  605. foreach ($list as &$v) {
  606. $child = $complaint_type->field('id, name')->where(['pid' => $v['id']])->order('weigh', 'desc')->select();
  607. if ($child) {
  608. $child = $this->getchildtype($child);
  609. }
  610. $v['child'] = $child;
  611. }
  612. return $list;
  613. }
  614. //车辆类型
  615. public function car() {
  616. $list = Db::name('car')->field('id, name, desc')->order('weigh', 'desc')->select();
  617. $this->success('车辆类型', $list);
  618. }
  619. //领队
  620. public function leader() {
  621. $list = Db::name('leader')->field('id, name')->order('weigh', 'desc')->select();
  622. $this->success('领队', $list);
  623. }
  624. //私人订制出行方向
  625. public function personaldirection() {
  626. $list = Db::name('personal_direction')->field('id, name')->order('weigh', 'desc')->select();
  627. $this->success('私人订制出行方向', $list);
  628. }
  629. //私人订制
  630. public function personalactive() {
  631. $type = input('type', '', 'trim'); //类型
  632. $number = input('number', 0, 'intval'); //出行人数
  633. $traveltime = input('traveltime', '', 'strtotime'); //出行时间
  634. $traveday = input('traveday', 0, 'intval'); //出行天数
  635. $activetype = input('activetype', '', 'trim'); //活动类型
  636. $car = input('car', '', 'trim'); //车辆
  637. $leader = input('leader', '', 'trim'); //领队
  638. $travedirection = input('travedirection', '', 'trim'); //出行方向
  639. $freerange = input('freerange', '', 'trim'); //空闲时段
  640. $remark = input('remark', '', 'trim'); //留言
  641. if (!$type || iconv_strlen($type, 'utf-8') > 50) {
  642. $this->error('请选择类型');
  643. }
  644. if ($number <= 0) {
  645. $this->error('请填写出行人数');
  646. }
  647. if ($traveltime < time()) {
  648. $this->error('请选择正确出行时间');
  649. }
  650. if ($traveday <= 0) {
  651. $this->error('请填写正确出行天数');
  652. }
  653. if (!$activetype || iconv_strlen($activetype, 'utf-8') > 50) {
  654. $this->error('请选择活动类型');
  655. }
  656. if ($car && iconv_strlen($car, 'utf-8') > 50) {
  657. $this->error('请选择车辆');
  658. }
  659. if ($leader && iconv_strlen($leader, 'utf-8') > 50) {
  660. $this->error('请选择领队');
  661. }
  662. if (!$travedirection || iconv_strlen($travedirection, 'utf-8') > 50) {
  663. $this->error('请选择出行方向');
  664. }
  665. if (!$freerange || iconv_strlen($freerange, 'utf-8') > 50) {
  666. $this->error('请选择空闲时段');
  667. }
  668. if (iconv_strlen($remark, 'utf-8') > 500) {
  669. $this->error('留言最多500字');
  670. }
  671. $data['user_id'] = $this->auth->id;
  672. $data['type'] = $type;
  673. $data['number'] = $number;
  674. $data['traveltime'] = $traveltime;
  675. $data['traveday'] = $traveday;
  676. $data['activetype'] = $activetype;
  677. $data['car'] = $car;
  678. $data['leader'] = $leader;
  679. $data['travedirection'] = $travedirection;
  680. $data['freerange'] = $freerange;
  681. $data['remark'] = $remark;
  682. $data['createtime'] = time();
  683. $rs = Db::name('personal_order')->insertGetId($data);
  684. if (!$rs) {
  685. $this->error('提交失败');
  686. }
  687. $this->success('提交成功');
  688. }
  689. //查询轮播图优惠券列表
  690. public function bannercoupon() {
  691. $list = Db::name('coupon')->field('id, title, desc, type, money')
  692. ->where(['purpose' => 4, 'status' => 1])->page($this->page, $this->pagenum)->order('weigh desc, id desc')->select();
  693. $user_coupon = Db::name('user_coupon');
  694. foreach ($list as &$v) {
  695. $v['is_receive'] = $user_coupon->where(['user_id' => $this->auth->id, 'coupon_id' => $v['id']])->count('id');
  696. }
  697. $this->success('查询轮播图优惠券列表', $list);
  698. }
  699. //领取轮播图优惠券
  700. public function receivebannercoupon() {
  701. $id = input('id', 0, 'intval'); //优惠券id
  702. if (!$id) {
  703. $this->error('参数缺失');
  704. }
  705. $info = Db::name('coupon')->where(['id' => $id, 'purpose' => 4])->find();
  706. if (!$info) {
  707. $this->error('优惠券不存在');
  708. }
  709. if ($info['status'] != 1) {
  710. $this->error('优惠券已下架');
  711. }
  712. //查询是否已领取
  713. $count = Db::name('user_coupon')->where(['user_id' => $this->auth->id, 'coupon_id' => $id])->count('id');
  714. if ($count) {
  715. $this->error('您已经领取过了');
  716. }
  717. $data['user_id'] = $this->auth->id;
  718. $data['coupon_id'] = $id;
  719. $data['title'] = $info['title'];
  720. $data['desc'] = $info['desc'];
  721. $data['type'] = $info['type'];
  722. $data['money'] = $info['money'];
  723. $data['minmoney'] = $info['minmoney'];
  724. $data['purpose'] = $info['purpose'];
  725. $data['starttime'] = time();
  726. $data['endtime'] = time() + $info['effectiveday'] * 86400;
  727. $data['createtime'] = time();
  728. //开启事务
  729. Db::startTrans();
  730. //添加领取记录
  731. $rs = Db::name('user_coupon')->insertGetId($data);
  732. if (!$rs) {
  733. Db::rollback();
  734. $this->error('领取失败');
  735. }
  736. $rt = Db::name('user_coupon')->where(['user_id' => $this->auth->id, 'coupon_id' => $id])->count('id');
  737. if ($rt != 1) {
  738. Db::rollback();
  739. $this->error('领取失败');
  740. }
  741. Db::commit();
  742. $this->success('领取成功');
  743. }
  744. //体验会员列表
  745. public function experiencevip() {
  746. $list = Db::name('vip')->where(['id' => ['gt', $this->auth->maxlevel], 'status' => 1])->select();
  747. $vip_privilege = Db::name('vip_privilege');
  748. foreach ($list as &$v) {
  749. $v['vip_privilege'] = $vip_privilege->field('id, title, desc')
  750. ->where(['vip_id' => $v['id']])->order('weigh desc')->select();
  751. }
  752. $this->success('体验会员列表', $list);
  753. }
  754. //购买体验会员
  755. public function buyexperiencevip() {
  756. //检查是否已经开通体验会员
  757. if ($this->auth->experiencetime >= time()) {
  758. $this->error('您已开通体验会员,不能重复开通');
  759. }
  760. $id = input('id', 0, 'intval');
  761. if (!$id) {
  762. $this->error('参数缺失');
  763. }
  764. $info = Db::name('vip')->where(['id' => $id])->find();
  765. if (!$info) {
  766. $this->error('会员不存在');
  767. }
  768. if ($info['status'] != 1) {
  769. $this->error('体验会员已下架');
  770. }
  771. if ($info['price'] <= 0) {
  772. $this->error('会员价格异常');
  773. }
  774. //体验会员等级必须大于成长值会员
  775. if ($id <= $this->auth->maxlevel) {
  776. $this->error('请开通更高等级体验会员');
  777. }
  778. //判断余额
  779. if ($this->auth->money < $info['price']) {
  780. $this->success('您的余额不足,请先去充值', ['code' => 2]);
  781. }
  782. $data['user_id'] = $this->auth->id;
  783. $data['vip_id'] = $id;
  784. $data['title'] = $info['title'];
  785. $data['level'] = $info['level'];
  786. $data['growthvalue'] = $info['growthvalue'];
  787. $data['free'] = $info['free'];
  788. $data['price'] = $info['price'];
  789. $data['endtime'] = time() + $info['day'] * 86400;
  790. $data['vipdiscount'] = $info['vipdiscount'];
  791. $data['birthdiscount'] = $info['birthdiscount'];
  792. $data['manypeople'] = $info['manypeople'];
  793. $data['createtime'] = time();
  794. //开启事务
  795. Db::startTrans();
  796. //添加开通记录
  797. $rs = Db::name('vip_log')->insertGetId($data);
  798. if (!$rs) {
  799. Db::rollback();
  800. $this->error('开通失败');
  801. }
  802. //扣除余额
  803. $result = create_log(-$info['price'], '开通会员', $this->auth->id, 4, $rs);
  804. if ($result != 1) {
  805. Db::rollback();
  806. $this->error('资金异常,请联系管理员');
  807. }
  808. //成长值会员信息
  809. $growth_vip_info = Db::name('vip')->find($this->auth->growthlevel);
  810. //修改用户表信息
  811. $user_data['experiencelevel'] = $id;
  812. $user_data['experiencetime'] = $data['endtime'];
  813. $user_data['maxlevel'] = $id;
  814. $freenumber = $this->auth->freenumber + $info['free'] - $growth_vip_info['free'];
  815. $user_data['freenumber'] = $freenumber > 0 ? $freenumber : 0;
  816. $rt = Db::name('user')->where([
  817. 'id' => $this->auth->id,
  818. 'experiencelevel' => $this->auth->experiencelevel,
  819. 'maxlevel' => $this->auth->maxlevel,
  820. 'freenumber' => $this->auth->freenumber])->setField($user_data);
  821. if (!$rt) {
  822. Db::rollback();
  823. $this->error('开通失败');
  824. }
  825. Db::commit();
  826. $this->success('开通成功');
  827. }
  828. }