Pay.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Http\Controllers\WechatPay;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Models\BuyInviteModel;
  6. use App\Models\ConfessModel;
  7. use App\Models\User\UserModel;
  8. use App\Models\NoticeModel;
  9. use App\Models\PartnerModel;
  10. use App\Models\RecodeModel;
  11. use App\Models\TicketModel;
  12. use Illuminate\Http\Request;
  13. class Pay extends Controller
  14. {
  15. public const PRICE = 5;
  16. /**
  17. * 通过解锁卡解锁卡片
  18. * @param int $partner_id
  19. * @param Request $request
  20. * @return array
  21. * @throws \ApiException
  22. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  23. */
  24. public function buyByTicket(int $partner_id, Request $request)
  25. {
  26. $this->validate($request, [
  27. 'ticket_code' => 'required'
  28. ]);
  29. $partner = $this->look($partner_id);
  30. if ($partner) {
  31. return array(
  32. 'code' => 200,
  33. 'message' => 'success',
  34. 'data' => $partner
  35. );
  36. } else {
  37. $partner = PartnerModel::find($partner_id);
  38. $uid = Auth::auth();
  39. $ticketObj = new Ticket();
  40. if ($ticketObj->checkTicket($request->input('ticket_code'))) {
  41. \DB::beginTransaction();
  42. try {
  43. // 核销解锁卡
  44. TicketModel::where('code', $request->input('ticket_code'))->update(['state' => 1]);
  45. // 添加解锁记录
  46. \DB::table('kdgx_partner_charge_recode')->insert([
  47. 'uid' => $uid,
  48. 'partner_id' => $partner->id,
  49. 'create_at' => time()
  50. ]);
  51. // 记录购买日志
  52. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  53. 'uid' => $uid,
  54. 'create_at' => time(),
  55. 'type' => 1,
  56. 'gold_flower' => 0,
  57. 'red_flower' => 0,
  58. 'jsk' => 1,
  59. 'type_id' => $partner->id,
  60. 'remark' => "解锁卡「{$request->input('ticket_code')}」购买对象卡片联系方式"
  61. ]);
  62. // 气泡双向通知
  63. NoticeModel::create([
  64. 'uid' => $uid,
  65. 'title' => '解锁联系方式成功',
  66. 'content' => "您刚刚进行的解锁联系方式操作成功。卡片id: {$partner->id}",
  67. 'type' => 1,
  68. 'type_id' => $partner->id,
  69. ]);
  70. NoticeModel::create([
  71. 'uid' => $partner->uid,
  72. 'title' => '卡片被解锁通知',
  73. 'content' => "您上架的卡片被id: {$uid} 解锁",
  74. 'type' => 4,
  75. 'type_id' => $uid,
  76. ]);
  77. \DB::commit();
  78. return array(
  79. 'code' => 200,
  80. 'message' => 'success',
  81. 'data' => $partner
  82. );
  83. } catch (\Exception $e) {
  84. \DB::rollBack();
  85. throw new \ApiException('数据库异常', 501);
  86. }
  87. } else {
  88. throw new \ApiException('卡劵错误', 101);
  89. }
  90. }
  91. }
  92. /**
  93. * 通过小fa解锁卡片
  94. * @param int $partner_id
  95. * @param Request $request
  96. * @return array
  97. * @throws \ApiException
  98. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  99. */
  100. public function buyByflower(int $partner_id, Request $request)
  101. {
  102. $partner = $this->look($partner_id);
  103. if ($partner) {
  104. return array(
  105. 'code' => 200,
  106. 'message' => 'success',
  107. 'data' => $partner
  108. );
  109. } else {
  110. $partner = PartnerModel::find($partner_id);
  111. $uid = Auth::auth();
  112. // 检验账户余额
  113. if ($request->input('gold_flower') < 0 || $request->input('red_flower') < 0) {
  114. return array(
  115. 'code' => 101,
  116. 'message' => '参数错误'
  117. );
  118. }
  119. $user = UserModel::find($uid);
  120. if (
  121. $user->gold_flower < $request->input('gold_flower')
  122. || $user->red_flower < $request->input('red_flower')
  123. ) {
  124. return array(
  125. 'code' => 102,
  126. 'message' => '账户小fa不足'
  127. );
  128. }
  129. $buyInviteModel = new BuyInviteModel();
  130. $dis = $buyInviteModel->effectiveInvite($uid, $partner_id);
  131. $price = (float)self::PRICE * (100 - $dis) / 100;
  132. if ($request->input('gold_flower') + $request->input('red_flower') < $price) {
  133. return array(
  134. 'code' => 103,
  135. 'message' => "需要{$price}朵小fa"
  136. );
  137. }
  138. \DB::beginTransaction();
  139. try {
  140. // 核销fa
  141. $user->decrement('gold_flower', $request->input('gold_flower'));
  142. $user->decrement('red_flower', $request->input('red_flower'));
  143. // 添加解锁记录
  144. \DB::table('kdgx_partner_charge_recode')->insert([
  145. 'uid' => $uid,
  146. 'partner_id' => $partner->id,
  147. 'create_at' => time()
  148. ]);
  149. // 记录购买日志
  150. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  151. 'uid' => $uid,
  152. 'create_at' => time(),
  153. 'type' => 1,
  154. 'gold_flower' => 0,
  155. 'red_flower' => 0,
  156. 'jsk' => 1,
  157. 'type_id' => $partner->id,
  158. 'remark' => "小fa购买对象卡片联系方式"
  159. ]);
  160. // 气泡双向通知
  161. NoticeModel::create([
  162. 'uid' => $uid,
  163. 'title' => '解锁联系方式成功',
  164. 'content' => "您刚刚进行的解锁联系方式操作成功。卡片id: {$partner->id}",
  165. 'type' => 1,
  166. 'type_id' => $partner->id,
  167. ]);
  168. NoticeModel::create([
  169. 'uid' => $partner->uid,
  170. 'title' => '卡片被解锁通知',
  171. 'content' => "您上架的卡片被id: {$uid} 解锁",
  172. 'type' => 4,
  173. 'type_id' => $uid,
  174. ]);
  175. \DB::commit();
  176. return array(
  177. 'code' => 200,
  178. 'message' => 'success',
  179. 'data' => $partner
  180. );
  181. } catch (\Exception $e) {
  182. \DB::rollBack();
  183. throw new \ApiException('数据库异常', 501);
  184. }
  185. }
  186. }
  187. /**
  188. * 查看卡片详情
  189. * @param int $partner_id
  190. * @return PartnerModel|bool|mixed
  191. * @throws \ApiException
  192. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  193. */
  194. public function look(int $partner_id)
  195. {
  196. $uid = Auth::auth();
  197. $partner = PartnerModel::find($partner_id);
  198. if (collect($partner)->isEmpty()) {
  199. throw new \ApiException('卡片不存在', 404);
  200. }
  201. if ($partner->is_sell != 1 || $partner->is_hide != 0) {
  202. throw new \ApiException('应用户要求,该卡片已经下架', 401);
  203. }
  204. // 自己上传的卡片
  205. if ($partner->uid == $uid) {
  206. return $partner;
  207. }
  208. // 已经购买过的卡片
  209. if (RecodeModel::where(array(['uid', $uid], ['partner_id', $partner->id]))->exists()) {
  210. return $partner;
  211. }
  212. // 被接收表白的卡片
  213. $where = array(
  214. ['uid', $uid],
  215. ['partner_id', $partner->id],
  216. ['receive_state', 1],
  217. ['state', 1]
  218. );
  219. if (ConfessModel::where($where)->exists()) {
  220. return $partner;
  221. } else {
  222. return false;
  223. }
  224. }
  225. /**
  226. * 兑换解锁卡
  227. * @param Request $request
  228. * @return array
  229. * @throws \ApiException
  230. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  231. */
  232. public function exchangeTicket(Request $request)
  233. {
  234. $uid = Auth::auth();
  235. if ($request->input('gold_flower') < 0 || $request->input('red_flower') < 0) {
  236. return array(
  237. 'code' => 101,
  238. 'message' => '参数错误'
  239. );
  240. }
  241. $user = UserModel::find($uid);
  242. if (
  243. $user->gold_flower < $request->input('gold_flower')
  244. || $user->red_flower < $request->input('red_flower')
  245. ) {
  246. return array(
  247. 'code' => 102,
  248. 'message' => '账户小fa不足'
  249. );
  250. }
  251. if ($request->input('gold_flower') + $request->input('red_flower') < self::PRICE) {
  252. return array(
  253. 'code' => 103,
  254. 'message' => "需要{self::PRICE}朵小fa"
  255. );
  256. }
  257. \DB::beginTransaction();
  258. try {
  259. $ticketObj = new Ticket();
  260. $ticket_id = $ticketObj->create(5, $uid);
  261. $user->decrement('gold_flower', $request->input('gold_flower'));
  262. $user->decrement('red_flower', $request->input('red_flower'));
  263. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  264. 'uid' => $uid,
  265. 'create_at' => time(),
  266. 'type' => 1,
  267. 'gold_flower' => $request->input('gold_flower'),
  268. 'red_flower' => $request->input('red_flower'),
  269. 'jsk' => 0,
  270. 'type_id' => 0,
  271. 'remark' => "兑换一张解锁卡"
  272. ]);
  273. \DB::commit();
  274. return array(
  275. 'code' => 200,
  276. 'message' => 'success',
  277. 'data' => [
  278. 'ticket_id' => $ticket_id
  279. ],
  280. );
  281. } catch (\Exception $e) {
  282. \DB::rollBack();
  283. throw new \ApiException('数据库异常', 501);
  284. }
  285. }
  286. }