Index.php 38 KB

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