CardController.php 8.1 KB

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