AccountController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\controller\Curd;
  4. use app\model\Card;
  5. use app\model\Member;
  6. use app\model\MemberAccount;
  7. use app\model\MemberAccountList;
  8. use app\model\MemberBenefit;
  9. use support\Db;
  10. use support\exception\BusinessException;
  11. use support\Log;
  12. use support\Redis;
  13. use support\Request;
  14. use Tinywan\Jwt\JwtToken;
  15. class AccountController extends Curd
  16. {
  17. public function __construct()
  18. {
  19. $this->model = new MemberAccount();
  20. }
  21. public function my($id)
  22. {
  23. $account = MemberAccount::where('join_account_member_id', $id)->get()->toArray();
  24. if (!$account) {
  25. // 账户表
  26. $accountData = [
  27. 'join_account_member_id' => $id,
  28. 'member_account_classify' => 'POINTS',
  29. 'member_account_status' => 'ACTIVED',
  30. 'member_account_category' => 'NORMAL',
  31. 'member_account_nbr' => $id . '-POINTS',
  32. 'member_account_name' => '积分账户',
  33. 'member_account_addtimes' => time()
  34. ];
  35. // 积分账户
  36. MemberAccount::insert($accountData);
  37. $accountData['member_account_classify'] = 'CASH';
  38. $accountData['member_account_nbr'] = $id . '-CASH';
  39. $accountData['member_account_name'] = '余额账户';
  40. // 现金/余额账户
  41. MemberAccount::insert($accountData);
  42. $account = MemberAccount::where('join_account_member_id', $id)->get()->toArray();
  43. }
  44. if ($account[0]['member_account_classify'] == 'POINTS') {
  45. $account = array_reverse($account);
  46. }
  47. foreach ($account as &$item) {
  48. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s', $item['member_account_duedate']) : '长期有效';
  49. $item['member_account_surplus'] = round($item['member_account_surplus'] + $item['member_account_added'], 2);
  50. }
  51. return json_success('', $account);
  52. }
  53. public function incomeExpend(Request $request)
  54. {
  55. $params = $request->post();
  56. $account = MemberAccount::where('member_account_id', $params['member_account_id'])->first();
  57. if (!$account) {
  58. return json_fail('账户不存在');
  59. }
  60. try {
  61. if (!empty($params['type']) && $params['type'] == 'income') {
  62. $account->member_account_income = $account->member_account_income + $params['income'];
  63. $account->member_account_surplus = $account->member_account_surplus + $params['income'];
  64. $account->member_account_update_user_id = JwtToken::getCurrentId();
  65. $account->member_account_updatetimes = time();
  66. $account->save();
  67. } elseif (!empty($params['type']) && $params['type'] == 'expend') {
  68. $account->member_account_expend = $account->member_account_expend + $params['expend'];
  69. $account->member_account_surplus = $account->member_account_surplus - $params['expend'];
  70. $account->member_account_update_user_id = JwtToken::getCurrentId();
  71. $account->member_account_updatetimes = time();
  72. $account->save();
  73. }
  74. _syslog("账户出入账", $params['type'] == 'income' ? '入账' : '出账');
  75. return json_success("操作成功");
  76. } catch (\Exception $e) {
  77. return json_fail('操作失败');
  78. }
  79. }
  80. /**
  81. * 用户的账户
  82. */
  83. public function selectMemberAccount(Request $request)
  84. {
  85. $memberId = $request->get('member_id', '');
  86. if (!$memberId) {
  87. return json_fail('参数异常');
  88. }
  89. $account = MemberAccount::where('join_account_member_id', $memberId)->get()->toArray();
  90. $data = [];
  91. foreach ($account as $item) {
  92. $item['member_account_duedate'] = !empty($item['member_account_duedate']) ? date('Y-m-d H:i:s', $item['member_account_duedate']) : '长期有效';
  93. if ($item['member_account_classify'] == 'POINTS') {
  94. $data['points'] = $item;
  95. } elseif ($item['member_account_classify'] == 'CASH') {
  96. $data['cash'] = $item;
  97. } elseif ($item['member_account_classify'] == 'CARD') {
  98. if (intval($item['member_account_surplus']) == 0 && intval($item['member_account_added']) == 0){
  99. continue;
  100. }
  101. $data['card'][] = $item;
  102. } elseif ($item['member_account_classify'] == 'WELFARE') {
  103. $data['welfare'] = $item;
  104. } elseif ($item['member_account_classify'] == 'VIP') {
  105. $data['vip'] = $item;
  106. }
  107. }
  108. return json_success('', $data);
  109. }
  110. /**
  111. * @Desc 修改账户状态
  112. * @Author Gorden
  113. * @Date 2024/9/12 16:28
  114. *
  115. * @param Request $request
  116. * @return \support\Response
  117. */
  118. public function updateStatus(Request $request)
  119. {
  120. $accountId = $request->post('account_id');
  121. $status = $request->post('status');
  122. if (!$accountId || !$status || !in_array($status, ['DISABLED', 'ACTIVED', 'EXPIRED'])) {
  123. return json_fail('参数异常');
  124. }
  125. try {
  126. $account = MemberAccount::where('member_account_id', $accountId)->first();
  127. $account->member_account_status = $status;
  128. $account->save();
  129. _syslog("修改账户状态", '修改账户状态成功');
  130. return json_success('账户状态修改成功');
  131. } catch (\Exception $e) {
  132. _syslog("修改账户状态", '账户状态修改失败');
  133. return json_fail('账户状态修改失败');
  134. }
  135. }
  136. /**
  137. * @Desc 会员绑定储值卡
  138. * @Author Gorden
  139. * @Date 2024/11/19 11:12
  140. *
  141. * @param Request $request
  142. * @return \support\Response
  143. */
  144. public function bindCard(Request $request)
  145. {
  146. $cardId = $request->post('card_id');
  147. $cardKey = $request->post('card_cdkey');
  148. $memberId = $request->post('member_id');
  149. if (!Member::where('member_id', $memberId)->where('member_status', 'ACTIVED')->exists()) {
  150. return json_fail('会员不存在');
  151. }
  152. // 防刷
  153. $redisKey = $memberId . '_' . $cardId;
  154. $num = 0;
  155. if (Redis::exists($redisKey)) {
  156. $num = Redis::get($redisKey);
  157. }
  158. if ($num >= 3) {
  159. return json_fail("请30分钟后重试");
  160. }
  161. Redis::set($redisKey, $num + 1, 'EX', 1800);
  162. $card = Card::with([
  163. 'main' => function ($query) {
  164. $query->select('card_main_id', 'card_main_name','card_main_amount','card_main_added');
  165. }
  166. ])->where('card_id', $cardId)
  167. ->first();
  168. if (!$card || empty($card->main)) {
  169. return json_fail("卡号不存在");
  170. }
  171. if ($card->card_cdkey != $cardKey){
  172. return json_fail("秘钥错误,请重新输入");
  173. }
  174. if ($card->is_issue == 'N') {
  175. return json_fail("卡号未发行");
  176. }
  177. if (!in_array($card->card_status, ['INIT', 'WAITING', 'PENDING'])) {
  178. return json_fail("卡号不是可绑定状态");
  179. }
  180. // 清除防刷
  181. Redis::del($redisKey);
  182. Db::beginTransaction();
  183. try {
  184. $duedate = 0;
  185. if ($card->main->card_main_valid_mode == 'DAYS') {
  186. $duedate = date('Y-m-d H:i:s', strtotime("+ " . $card->main->card_main_valid_days . "days") - 1);
  187. } elseif ($card->main->card_main_valid_mode == 'DATE') {
  188. $duedate = $card->main->card_main_valid_end;
  189. }
  190. // 创建储值卡账户
  191. $accountId = MemberAccount::insertGetId([
  192. 'join_account_member_id' => $memberId,
  193. 'member_account_classify' => 'CARD',
  194. 'member_account_status' => 'ACTIVED',
  195. 'member_account_category' => 'NORMAL',
  196. 'member_account_nbr' => $card->card_id,
  197. 'member_account_name' => $card->main->card_main_name,
  198. 'member_account_income' => $card->main->card_main_amount,
  199. 'member_account_surplus' => $card->main->card_main_amount,
  200. 'member_account_added' => $card->main->card_main_added,
  201. 'member_account_duedate' => $duedate,
  202. 'member_account_addtimes' => time(),
  203. 'member_account_update_user_id' => JwtToken::getCurrentId(),
  204. 'member_account_updatetimes' => time()
  205. ]);
  206. // 会员绑定卡
  207. $card->join_card_member_id = $memberId;
  208. $card->join_card_member_account_id = $accountId;
  209. $card->card_assign_datetime = date('Y-m-d H:i:s');
  210. $card->card_deadline_datetime = $duedate;
  211. $card->card_status = 'USED';
  212. $card->save();
  213. // member_account_list 加卡充值记录
  214. MemberAccountList::insert([
  215. 'join_list_member_account_nbr' => $cardId,
  216. 'join_member_account_list_member_id' => $memberId,
  217. 'member_account_list_status' => 'ACTIVED',
  218. 'member_account_list_attr' => 'IN',
  219. 'member_account_list_classify' => 'CARD',
  220. 'member_account_list_category' => '绑定储值卡',
  221. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  222. 'member_account_list_amount' => $card->main->card_main_amount,
  223. 'member_account_list_addtimes' => time()
  224. ]);
  225. Db::commit();
  226. return json_success('success');
  227. } catch (\Exception $e) {
  228. Db::rollBack();
  229. Log::error("储值卡绑定失败",['msg'=>$e->getMessage()]);
  230. return json_fail("绑定失败");
  231. }
  232. }
  233. }