Account.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\api\controller\Base;
  4. use app\common\model\user\Account as AccountModel;
  5. use app\api\validate\user\Account as AccountValidate;
  6. class Account extends Base
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = ['*'];
  10. public function index()
  11. {
  12. $params = $this->request->only([
  13. 'type'
  14. ]);
  15. // 验证参数
  16. $validate = new \app\api\validate\user\Account();
  17. if (!$validate->scene('index')->check($params)) {
  18. $this->error($validate->getError());
  19. }
  20. $user = auth_user();
  21. $where = [
  22. 'user_id' => $user->id
  23. ];
  24. if (!empty($params['type'])) {
  25. $where['type'] = $params['type'];
  26. }
  27. $data = AccountModel::where($where)->order('updatetime desc')->find();
  28. if (!$data) {
  29. $this->error(__('No Results were found'));
  30. }
  31. $this->success('获取成功', $data);
  32. }
  33. public function save()
  34. {
  35. $user = auth_user();
  36. $params = $this->request->only([
  37. 'type', 'account_name', 'account_header', 'account_no'
  38. ]);
  39. // 使用验证器验证基础参数
  40. $validate = new \app\api\validate\user\Account();
  41. if (!$validate->scene($params['type'])->check($params)) {
  42. $this->error($validate->getError());
  43. }
  44. // 根据账户类型设置默认值
  45. if ($params['type'] === 'alipay') {
  46. $params['account_header'] = '支付宝账户';
  47. }
  48. if ($params['type'] === 'wechat') {
  49. $params['account_header'] = '微信账户';
  50. $params['account_no'] = '-';
  51. }
  52. $data = AccountModel::where(['user_id' => $user->id, 'type' => $params['type']])->find();
  53. if (!$data) {
  54. $data = AccountModel::create([
  55. 'user_id' => $user->id,
  56. 'type' => $params['type'],
  57. 'account_name' => $params['account_name'],
  58. 'account_header' => $params['account_header'],
  59. 'account_no' => $params['account_no'],
  60. ]);
  61. } else {
  62. $data->save($params);
  63. }
  64. $this->success('保存成功', $data);
  65. }
  66. }