Pay.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Core\Auth;
  4. use App\Models\BuyInviteModel;
  5. use App\Models\ConfessModel;
  6. use App\Models\User\UserModel;
  7. use App\Models\NoticeModel;
  8. use App\Models\PartnerModel;
  9. use App\Models\RecodeModel;
  10. use App\Models\TicketModel;
  11. use Illuminate\Http\Request;
  12. use phpDocumentor\Reflection\Types\Integer;
  13. /**
  14. * 系统内部小花交易转换
  15. */
  16. class Pay extends Controller
  17. {
  18. public const PRICE = 5;
  19. /**
  20. * @param int $partner_id
  21. * @param Request $request
  22. * @return array
  23. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  24. */
  25. public function buyByTicket(int $partner_id, Request $request)
  26. {
  27. $this->validate($request, [
  28. 'ticket_code' => 'required'
  29. ]);
  30. $partner = $this->look($partner_id);
  31. if ($partner) {
  32. return array(
  33. 'code' => 200,
  34. 'message' => 'success',
  35. 'data' => $partner
  36. );
  37. } else {
  38. $partner = PartnerModel::find($partner_id);
  39. $uid = Auth::auth();
  40. $ticketObj = new Ticket();
  41. if ($ticketObj->checkTicket($request->input('ticket_code'))) {
  42. \DB::beginTransaction();
  43. try {
  44. // 核销解锁卡
  45. TicketModel::where('code', $request->input('ticket_code'))->update(['state' => 1]);
  46. // 添加解锁记录
  47. \DB::table('kdgx_partner_charge_recode')->insert([
  48. 'uid' => $uid,
  49. 'partner_id' => $partner->id,
  50. 'create_at' => time()
  51. ]);
  52. // 记录购买日志
  53. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  54. 'uid' => $uid,
  55. 'create_at' => time(),
  56. 'type' => 1,
  57. 'gold_flower' => 0,
  58. 'red_flower' => 0,
  59. 'jsk' => 1,
  60. 'type_id' => $partner->id,
  61. 'remark' => "解锁卡「{$request->input('ticket_code')}」购买对象卡片联系方式"
  62. ]);
  63. // 气泡双向通知
  64. NoticeModel::create([
  65. 'uid' => $uid,
  66. 'title' => '解锁联系方式成功',
  67. 'content' => "你刚刚进行的解锁联系方式操作成功。卡片id: {$partner->id}",
  68. 'type' => 1,
  69. 'type_id' => $partner->id,
  70. ]);
  71. NoticeModel::create([
  72. 'uid' => $partner->uid,
  73. 'title' => '卡片被解锁通知',
  74. 'content' => "你上架的卡片被id: {$uid} 解锁",
  75. 'type' => 4,
  76. 'type_id' => $uid
  77. ]);
  78. \DB::commit();
  79. return array(
  80. 'code' => 200,
  81. 'message' => 'success',
  82. 'data' => $partner
  83. );
  84. } catch (\Exception $e) {
  85. \DB::rollBack();
  86. throw new \ApiException('数据库异常', 501);
  87. }
  88. } else {
  89. throw new \ApiException('卡劵错误', 101);
  90. }
  91. }
  92. }
  93. /**
  94. * 通过小fa解锁卡片
  95. * @param int $partner_id
  96. * @param Request $request
  97. * @return array
  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 response([
  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. */
  192. public function look(int $partner_id)
  193. {
  194. $uid = Auth::auth();
  195. $partner = PartnerModel::find($partner_id);
  196. if (collect($partner)->isEmpty()) {
  197. throw new \ApiException('卡片不存在', 404);
  198. }
  199. if ($partner->is_sell != 1 || $partner->is_hide != 0) {
  200. throw new \ApiException('应用户要求,该卡片已经下架', 401);
  201. }
  202. // 自己上传的卡片
  203. if ($partner->uid == $uid) {
  204. return $partner;
  205. }
  206. // 已经购买过的卡片
  207. if (RecodeModel::where(array(['uid', $uid], ['partner_id', $partner->id]))->exists()) {
  208. return $partner;
  209. }
  210. // 被接收表白的卡片
  211. $where = array(
  212. ['uid', $uid],
  213. ['partner_id', $partner->id],
  214. ['receive_state', 1],
  215. ['state', 1]
  216. );
  217. if (ConfessModel::where($where)->exists()) {
  218. return $partner;
  219. } else {
  220. return false;
  221. }
  222. }
  223. /**
  224. * 兑换解锁卡
  225. * @param Request $request
  226. * @return array
  227. */
  228. public function exchangeTicket(Request $request)
  229. {
  230. $uid = Auth::auth();
  231. if ($request->input('gold_flower') < 0 || $request->input('red_flower') < 0) {
  232. return array(
  233. 'code' => 101,
  234. 'message' => '参数错误'
  235. );
  236. }
  237. $user = UserModel::find($uid);
  238. if (
  239. $user->gold_flower < $request->input('gold_flower')
  240. || $user->red_flower < $request->input('red_flower')
  241. ) {
  242. return array(
  243. 'code' => 102,
  244. 'message' => '账户小fa不足'
  245. );
  246. }
  247. if ($request->input('gold_flower') + $request->input('red_flower') < self::PRICE) {
  248. return array(
  249. 'code' => 103,
  250. 'message' => "需要{self::PRICE}朵小fa"
  251. );
  252. }
  253. \DB::beginTransaction();
  254. try {
  255. $ticketObj = new Ticket();
  256. $ticket_id = $ticketObj->create(5, $uid);
  257. $user->decrement('gold_flower', $request->input('gold_flower'));
  258. $user->decrement('red_flower', $request->input('red_flower'));
  259. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  260. 'uid' => $uid,
  261. 'create_at' => time(),
  262. 'type' => 1,
  263. 'gold_flower' => $request->input('gold_flower'),
  264. 'red_flower' => $request->input('red_flower'),
  265. 'jsk' => 0,
  266. 'type_id' => 0,
  267. 'remark' => "兑换一张解锁卡"
  268. ]);
  269. \DB::commit();
  270. return array(
  271. 'code' => 200,
  272. 'message' => 'success',
  273. 'data' => [
  274. 'ticket_id' => $ticket_id
  275. ],
  276. );
  277. } catch (\Exception $e) {
  278. \DB::rollBack();
  279. throw new \ApiException('数据库异常', 501);
  280. }
  281. }
  282. }