CardController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\admin\model\card\Card;
  4. use app\admin\model\card\CardOrder;
  5. use app\admin\model\card\CardProject;
  6. use app\admin\model\diagnosis\DiagnosisCate;
  7. use app\admin\model\diagnosis\DiagnosisService;
  8. use app\admin\model\user\UserCard;
  9. use app\admin\model\user\UserEquity;
  10. use app\models\user\UserBill;
  11. use crmeb\basic\BaseModel;
  12. use app\admin\model\system\{
  13. SystemAttachment, ShippingTemplates
  14. };
  15. use app\admin\model\user\User;
  16. use app\Request;
  17. use crmeb\services\{
  18. CacheService,
  19. ExpressService,
  20. SystemConfigService,
  21. UtilService
  22. };
  23. /**
  24. * 优惠卡
  25. * Class StoreOrderController
  26. * @package app\api\controller\order
  27. */
  28. class CardController
  29. {
  30. /**
  31. * 优惠卡列表
  32. * @param Request $request
  33. * @return mixed
  34. */
  35. public function list(Request $request)
  36. {
  37. $data = UtilService::getMore([
  38. ['page', 1],
  39. ['limit', 10]
  40. ]);
  41. $list = Card::order('id DESC')->page($data['page'],$data['limit'])->select();
  42. $list = count($list) > 0 ? $list->toArray() : [];
  43. return app('json')->successful($list);
  44. }
  45. /**
  46. * 优惠卡详情
  47. * @param Request $request
  48. * @param $id
  49. * @return mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function details(Request $request,$id)
  55. {
  56. if (!$id) return app('json')->fail('传入id');
  57. $details = Card::order('id DESC')->where('id', $id)->find()->toArray();
  58. return app('json')->successful($details);
  59. }
  60. /**
  61. * 小程序购买
  62. *
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function routine(Request $request)
  67. {
  68. list($card_id, $type) = UtilService::postMore([['card_id', 0], ['type', 0]], $request, true);
  69. switch ((int)$type) {
  70. case 0: //支付购买会员卡
  71. $order = CardOrder::addCard($request->uid(), $card_id, 'routine');
  72. if (!$order) return app('json')->fail('订单生成失败!');
  73. try {
  74. return app('json')->successful(CardOrder::jsPay($order));
  75. } catch (\Exception $e) {
  76. return app('json')->fail($e->getMessage());
  77. }
  78. break;
  79. default:
  80. return app('json')->fail('缺少参数');
  81. break;
  82. }
  83. }
  84. /**
  85. * 公众号购买
  86. *
  87. * @param Request $request
  88. * @return mixed
  89. */
  90. public function wechat(Request $request)
  91. {
  92. list($card_id, $type, $from) = UtilService::postMore([['card_id', 0], ['type', 0], ['from', 0]], $request, true);
  93. switch ((int)$type) {
  94. case 0: //支付充值余额
  95. $order = CardOrder::addCard($request->uid(), $card_id, 'weixinh5');
  96. if (!$order) return app('json')->fail('订单生成失败!');
  97. try {
  98. if ($from == 'weixinh5') {
  99. $recharge = CardOrder::wxH5Pay($order);
  100. } else {
  101. $recharge = CardOrder::wxPay($order);
  102. }
  103. } catch (\Exception $e) {
  104. return app('json')->fail($e->getMessage());
  105. }
  106. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  107. break;
  108. default:
  109. return app('json')->fail('缺少参数');
  110. break;
  111. }
  112. }
  113. /**
  114. * 用户优惠券
  115. * @param Request $request
  116. * @return mixed
  117. */
  118. public function user_card(Request $request)
  119. {
  120. $data = UtilService::getMore([
  121. ['status'],
  122. ['page', 1],
  123. ['limit', 10]
  124. ]);
  125. $model = UserCard::alias('a')
  126. ->field('a.*,b.name,b.type,b.number')
  127. ->leftJoin('card b', 'a.card_id = b.id')
  128. ->where('a.uid', $request->uid())
  129. ->order('a.id DESC');
  130. if ($data['status'] == 1){
  131. $model = $model->where('a.status', 0);// 未使用
  132. }elseif ($data['status'] == 2){
  133. $model = $model->where('a.status', 1)->where('use', 1);//使用中
  134. }elseif ($data['status'] ==3){
  135. $model = $model->where('a.status', 1)->where('use', -1);//失效
  136. }
  137. $list = $model->page($data['page'], $data['limit'])->select();
  138. $list = count($list) > 0 ? $list->toArray() : [];
  139. return app('json')->successful($list);
  140. }
  141. /**
  142. * 使用优惠卡
  143. * @param Request $request
  144. * @param $id
  145. * @return mixed
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. public function use_card(Request $request)
  151. {
  152. $data = UtilService::postMore([
  153. ['card'],
  154. ['pwd']
  155. ]);
  156. BaseModel::beginTrans();
  157. $user_card = UserCard::where('card', $data['card'])->where('pwd', $data['pwd'])->find();
  158. if (!$user_card) return app('json')->fail('卡密账号错误');
  159. if ($user_card['status'] > 0) return app('json')->fail('优惠卡已使用');
  160. $card = Card::where('id', $user_card['card_id'])->find();
  161. $card_pro = CardProject::where('card_id', $card['id'])->select();
  162. if (!$user_card) return app('json')->fail('优惠卡不存在');
  163. $uid = $user_card['uid'];
  164. if ($user_card['uid'] != $request->uid()){
  165. $user_card['uid'] = $request->uid();
  166. }
  167. $user_card['status'] = 1;
  168. $user_card['start_time'] = time();
  169. $user_card['end_time'] = time() + 86400*$card['time'];
  170. try {
  171. $res = $user_card->save();
  172. foreach ($card_pro as $item)
  173. {
  174. UserEquity::create([
  175. 'uid' => $request->uid(),
  176. 'user_card_id' => $user_card['id'],
  177. 'c_id' => $item['c_id'],
  178. 'type' => $item['type'],
  179. 'number' => $item['number'],
  180. 'start_week' => $card['start_week'],
  181. 'end_week' => $card['end_week'],
  182. ]);
  183. }
  184. UserBill::expend('使用优惠卡', $request->uid(), 'now_money', 'card', 0, '', 0, '使用优惠卡'.$card['name'].',优惠卡原拥有人'.$uid);
  185. BaseModel::commitTrans();
  186. return app('json')->success('使用成功');
  187. } catch (\Exception $e) {
  188. BaseModel::rollbackTrans();
  189. return app('json')->fail($e->getMessage());
  190. }
  191. }
  192. /**
  193. * 使用优惠卡后,用户的拥有详情
  194. * @param Request $request
  195. * @param $id
  196. * @return mixed
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function user_card_details(Request $request,$id)
  202. {
  203. if (!$id) return app('json')->fail('传入id');
  204. $list = UserCard::where('id', $id)
  205. ->where('uid', $request->uid())
  206. ->find();
  207. if (!$list) return app('json')->fail('优惠卡不存在');
  208. $list = $list->toArray();
  209. $list['cards'] = [];
  210. if ($list['status'] == 1){
  211. $card = UserEquity::where('user_card_id', $id)->select();
  212. foreach ($card as &$item){
  213. $item['name'] = implode(',', DiagnosisCate::where('id', 'in', $item['c_id'])->column('name'));
  214. $list['cards'][] = $item;
  215. }
  216. }else{
  217. $card = CardProject::where('card_id', $list['card_id'])->select();
  218. foreach ($card as $item){
  219. $item['name'] = implode(',', DiagnosisCate::where('id', 'in', $item['c_id'])->column('name'));
  220. $list['cards'][] = $item;
  221. }
  222. }
  223. return app('json')->successful($list);
  224. }
  225. }