| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 | <?phpnamespace app\api\controller\user;use app\api\controller\Base;use app\common\model\user\Account as AccountModel;use app\api\validate\user\Account as AccountValidate;class Account extends Base{    protected $noNeedLogin = [];    protected $noNeedRight = ['*'];    public function index()    {        $params = $this->request->only([            'type'        ]);                // 验证参数        $validate = new \app\api\validate\user\Account();        if (!$validate->scene('index')->check($params)) {            $this->error($validate->getError());        }                $user = auth_user();        $where = [            'user_id' => $user->id        ];        if (!empty($params['type'])) {            $where['type'] = $params['type'];        }        $data = AccountModel::where($where)->order('updatetime desc')->find();        if (!$data) {            $this->error(__('No Results were found'));        }        $this->success('获取成功', $data);    }    public function save()    {        $user = auth_user();            $params = $this->request->only([            'type', 'account_name', 'account_header', 'account_no'        ]);                // 使用验证器验证基础参数        $validate = new \app\api\validate\user\Account();        if (!$validate->scene($params['type'])->check($params)) {            $this->error($validate->getError());        }                // 根据账户类型设置默认值        if ($params['type'] === 'alipay') {            $params['account_header'] = '支付宝账户';        }        if ($params['type'] === 'wechat') {            $params['account_header'] = '微信账户';            $params['account_no'] = '-';        }        $data = AccountModel::where(['user_id' => $user->id, 'type' => $params['type']])->find();        if (!$data) {            $data = AccountModel::create([                'user_id' => $user->id,                'type' => $params['type'],                'account_name' => $params['account_name'],                'account_header' => $params['account_header'],                'account_no' => $params['account_no'],            ]);        } else {            $data->save($params);        }        $this->success('保存成功', $data);    }}
 |