Index.php 39 KB

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