Account.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace addons\shopro\controller\user;
  3. use addons\shopro\controller\Common;
  4. use app\admin\model\shopro\user\Account as AccountModel;
  5. class Account extends Common
  6. {
  7. protected $noNeedLogin = [];
  8. protected $noNeedRight = ['*'];
  9. public function index()
  10. {
  11. $params = $this->request->only([
  12. 'type'
  13. ]);
  14. $user = auth_user();
  15. $where = [
  16. 'user_id' => $user->id
  17. ];
  18. if (!empty($params['type'])) {
  19. $where['type'] = $params['type'];
  20. }
  21. $data = AccountModel::where($where)->order('updatetime desc')->find();
  22. if (!$data) {
  23. $this->error(__('No Results were found'));
  24. }
  25. $this->success('获取成功', $data);
  26. }
  27. public function save()
  28. {
  29. $user = auth_user();
  30. $params = $this->request->only([
  31. 'type', 'account_name', 'account_header', 'account_no'
  32. ]);
  33. if (!in_array($params['type'], ['wechat', 'alipay', 'bank'])) {
  34. $this->error('请选择正确的账户类型');
  35. }
  36. if ($params['type'] === 'alipay') {
  37. $params['account_header'] = '支付宝账户';
  38. }
  39. if ($params['type'] === 'wechat') {
  40. $params['account_header'] = '微信账户';
  41. $params['account_no'] = '-';
  42. }
  43. $this->svalidate($params, ".{$params['type']}");
  44. $data = AccountModel::where(['user_id' => $user->id, 'type' => $params['type']])->find();
  45. if (!$data) {
  46. $data = AccountModel::create([
  47. 'user_id' => $user->id,
  48. 'type' => $params['type'],
  49. 'account_name' => $params['account_name'],
  50. 'account_header' => $params['account_header'],
  51. 'account_no' => $params['account_no'],
  52. ]);
  53. } else {
  54. $data->save($params);
  55. }
  56. $this->success('保存成功', $data);
  57. }
  58. }